aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs162
1 files changed, 162 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs
new file mode 100644
index 0000000..631a2ca
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs
@@ -0,0 +1,162 @@
1/*
2* Copyright (c) Tribal Media AB, http://tribalmedia.se/
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are met:
6* * Redistributions of source code must retain the above copyright
7* notice, this list of conditions and the following disclaimer.
8* * Redistributions in binary form must reproduce the above copyright
9* notice, this list of conditions and the following disclaimer in the
10* documentation and/or other materials provided with the distribution.
11* * The name of Tribal Media AB may not be used to endorse or promote products
12* derived from this software without specific prior written permission.
13*
14* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
15* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
18* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*
25*/
26
27using System;
28using System.Collections.Generic;
29using System.Data;
30using System.Data.Common;
31using TribalMedia.Framework.Data;
32
33namespace TribalMedia.Framework.Data
34{
35 public abstract class ObjectTableMapper<TRowMapper, TPrimaryKey> : TableMapper
36 {
37 public ObjectTableMapper(DatabaseMapper connectionPool, string tableName)
38 : base(connectionPool, tableName)
39 {
40 }
41
42 public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value)
43 {
44 TRowMapper result = default(TRowMapper);
45 bool success = false;
46
47 WithConnection(delegate(DbConnection connection)
48 {
49 using (
50 DbCommand command =
51 CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey))
52 {
53 using (IDataReader reader = command.ExecuteReader())
54 {
55 if (reader.Read())
56 {
57 result = FromReader(new DataReader(reader));
58 success = true;
59 }
60 else
61 {
62 success = false;
63 }
64 }
65 }
66 });
67
68 value = result;
69
70 return success;
71 }
72
73
74 public virtual bool Remove(TPrimaryKey id)
75 {
76 int deleted = 0;
77
78 WithConnection(delegate(DbConnection connection)
79 {
80 using (
81 DbCommand command =
82 CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id))
83 {
84 deleted = command.ExecuteNonQuery();
85 }
86 });
87
88 if (deleted == 1)
89 {
90 return true;
91 }
92 else
93 {
94 return false;
95 }
96 }
97
98
99 public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey)
100 {
101 string table = TableName;
102
103 DbCommand command = connection.CreateCommand();
104
105 string conditionString = CreateCondition(command, fieldName, primaryKey);
106
107 string query =
108 String.Format("delete from {0} where {1}", table, conditionString);
109
110 command.CommandText = query;
111 command.CommandType = CommandType.Text;
112
113 return command;
114 }
115
116 public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value)
117 {
118 int updated = 0;
119
120 WithConnection(delegate(DbConnection connection)
121 {
122 using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey))
123 {
124 updated = command.ExecuteNonQuery();
125 }
126 });
127
128 if (updated == 1)
129 {
130 return true;
131 }
132 else
133 {
134 return false;
135 }
136 }
137
138 public virtual bool Add(TRowMapper value)
139 {
140 int added = 0;
141
142 WithConnection(delegate(DbConnection connection)
143 {
144 using (DbCommand command = CreateInsertCommand(connection, value))
145 {
146 added = command.ExecuteNonQuery();
147 }
148 });
149
150 if (added == 1)
151 {
152 return true;
153 }
154 else
155 {
156 return false;
157 }
158 }
159
160 public abstract TRowMapper FromReader(DataReader reader);
161 }
162} \ No newline at end of file