aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs
diff options
context:
space:
mode:
authorlbsa712008-01-15 20:07:02 +0000
committerlbsa712008-01-15 20:07:02 +0000
commitf5103b98be60cc8b1a69288328cdc20f78857835 (patch)
treeb50381385478f57a05ed9fef7c5176397d76d2f2 /ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs
parent* Added some comments to the linkset positioning code (diff)
downloadopensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.zip
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.gz
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.bz2
opensim-SC_OLD-f5103b98be60cc8b1a69288328cdc20f78857835.tar.xz
* Renamed a bunch of Data baseclasses for clarity and readability
(Slowly getting there)
Diffstat (limited to 'ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs245
1 files changed, 245 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs
new file mode 100644
index 0000000..e4fe0fd
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs
@@ -0,0 +1,245 @@
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.Data;
29using System.Data.Common;
30using TribalMedia.Framework.Data;
31
32namespace TribalMedia.Framework.Data
33{
34 public abstract class BaseTableMapper
35 {
36 private readonly BaseDatabaseConnector m_database;
37 private readonly object m_syncRoot = new object();
38
39 protected void WithConnection(Action<DbConnection> action)
40 {
41 lock (m_syncRoot)
42 {
43 DbConnection m_connection = m_database.GetNewConnection();
44
45 if (m_connection.State != ConnectionState.Open)
46 {
47 m_connection.Open();
48 }
49
50 action(m_connection);
51
52 if (m_connection.State == ConnectionState.Open)
53 {
54 m_connection.Close();
55 }
56 }
57 }
58
59 private readonly string m_tableName;
60 public string TableName
61 {
62 get { return m_tableName; }
63 }
64
65 protected Schema m_schema;
66 public Schema Schema
67 {
68 get { return m_schema; }
69 }
70
71 protected BaseFieldMapper m_keyFieldMapper;
72 public BaseFieldMapper KeyFieldMapper
73 {
74 get { return m_keyFieldMapper; }
75 }
76
77 public BaseTableMapper(BaseDatabaseConnector database, string tableName)
78 {
79 m_database = database;
80 m_tableName = tableName.ToLower(); // Stupid MySQL hack.
81 }
82
83 public string CreateParamName(string fieldName)
84 {
85 return m_database.CreateParamName(fieldName);
86 }
87
88 protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey)
89 {
90 return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey);
91 }
92
93 public string CreateCondition(DbCommand command, string fieldName, object key)
94 {
95 return m_database.CreateCondition(this, command, fieldName, key);
96 }
97
98 public DbCommand CreateInsertCommand(DbConnection connection, object obj)
99 {
100 return m_database.CreateInsertCommand(this, connection, obj);
101 }
102
103 public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey)
104 {
105 return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey);
106 }
107
108 public object ConvertToDbType(object value)
109 {
110 return m_database.ConvertToDbType(value);
111 }
112
113 protected virtual DataReader CreateReader(IDataReader reader)
114 {
115 return new DataReader(reader);
116 }
117 }
118
119 public abstract class BaseTableMapper<TRowMapper, TPrimaryKey> : BaseTableMapper
120 {
121 public BaseTableMapper(BaseDatabaseConnector database, string tableName)
122 : base(database, tableName)
123 {
124 }
125
126 public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value)
127 {
128 TRowMapper result = default(TRowMapper);
129 bool success = false;
130
131 WithConnection(delegate(DbConnection connection)
132 {
133 using (
134 DbCommand command =
135 CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey))
136 {
137 using (IDataReader reader = command.ExecuteReader())
138 {
139 if (reader.Read())
140 {
141 result = FromReader(CreateReader(reader));
142 success = true;
143 }
144 else
145 {
146 success = false;
147 }
148 }
149 }
150 });
151
152 value = result;
153
154 return success;
155 }
156
157 public virtual bool Remove(TPrimaryKey id)
158 {
159 int deleted = 0;
160
161 WithConnection(delegate(DbConnection connection)
162 {
163 using (
164 DbCommand command =
165 CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id))
166 {
167 deleted = command.ExecuteNonQuery();
168 }
169 });
170
171 if (deleted == 1)
172 {
173 return true;
174 }
175 else
176 {
177 return false;
178 }
179 }
180
181
182 public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey)
183 {
184 string table = TableName;
185
186 DbCommand command = connection.CreateCommand();
187
188 string conditionString = CreateCondition(command, fieldName, primaryKey);
189
190 string query =
191 String.Format("delete from {0} where {1}", table, conditionString);
192
193 command.CommandText = query;
194 command.CommandType = CommandType.Text;
195
196 return command;
197 }
198
199 public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value)
200 {
201 int updated = 0;
202
203 WithConnection(delegate(DbConnection connection)
204 {
205 using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey))
206 {
207 updated = command.ExecuteNonQuery();
208 }
209 });
210
211 if (updated == 1)
212 {
213 return true;
214 }
215 else
216 {
217 return false;
218 }
219 }
220
221 public virtual bool Add(TRowMapper value)
222 {
223 int added = 0;
224
225 WithConnection(delegate(DbConnection connection)
226 {
227 using (DbCommand command = CreateInsertCommand(connection, value))
228 {
229 added = command.ExecuteNonQuery();
230 }
231 });
232
233 if (added == 1)
234 {
235 return true;
236 }
237 else
238 {
239 return false;
240 }
241 }
242
243 public abstract TRowMapper FromReader(DataReader reader);
244 }
245} \ No newline at end of file