aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ThirdParty
diff options
context:
space:
mode:
authorlbsa712008-01-13 19:31:56 +0000
committerlbsa712008-01-13 19:31:56 +0000
commit6946050ada5fd1fb1daff58233ad4dad7bec875a (patch)
tree54b399dec776643e6303194d4c2a3aecedc69914 /ThirdParty
parentAnti-crash fix: Allow int (numbers without dot) when changing Estate terrain ... (diff)
downloadopensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.zip
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.gz
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.bz2
opensim-SC_OLD-6946050ada5fd1fb1daff58233ad4dad7bec875a.tar.xz
* Added ThirdParty node for external library sources that are shipped with the solution.
* Added conceptual TribalMedia.Framework.Data library; this is meant as a generic database layer abstraction library, that should be specialized into OpenSim.Framework.Data * OpenSim.Framework.Data should subclass FieldMappers to extend LLVector3 and LLQuaternions
Diffstat (limited to 'ThirdParty')
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs150
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs135
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs249
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs162
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/Properties/AssemblyInfo.cs40
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs85
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs89
-rw-r--r--ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs108
8 files changed, 1018 insertions, 0 deletions
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs
new file mode 100644
index 0000000..f0b3e88
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs
@@ -0,0 +1,150 @@
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.IO;
30
31namespace TribalMedia.Framework.Data
32{
33 public class DataReader
34 {
35 private readonly IDataReader m_source;
36
37 public DataReader(IDataReader source)
38 {
39 m_source = source;
40 }
41
42 public object Get(string name)
43 {
44 return m_source[name];
45 }
46
47 public ushort GetUShort(string name)
48 {
49 return (ushort) m_source.GetInt32(m_source.GetOrdinal(name));
50 }
51
52 public byte GetByte(string name)
53 {
54 int ordinal = m_source.GetOrdinal(name);
55 byte value = (byte) m_source.GetInt16(ordinal);
56 return value;
57 }
58
59 public sbyte GetSByte(string name)
60 {
61 return (sbyte) m_source.GetInt16(m_source.GetOrdinal(name));
62 }
63
64 //public Vector3 GetVector(string s)
65 //{
66 // float x = GetFloat(s + "X");
67 // float y = GetFloat(s + "Y");
68 // float z = GetFloat(s + "Z");
69
70 // Vector3 vector = new Vector3(x, y, z);
71
72 // return vector;
73 //}
74
75 //public Quaternion GetQuaternion(string s)
76 //{
77 // float x = GetFloat(s + "X");
78 // float y = GetFloat(s + "Y");
79 // float z = GetFloat(s + "Z");
80 // float w = GetFloat(s + "W");
81
82 // Quaternion quaternion = new Quaternion(x, y, z, w);
83
84 // return quaternion;
85 //}
86
87 public float GetFloat(string name)
88 {
89 return m_source.GetFloat(m_source.GetOrdinal(name));
90 }
91
92 public byte[] GetBytes(string name)
93 {
94 int ordinal = m_source.GetOrdinal(name);
95
96 if (m_source.GetValue(ordinal) == DBNull.Value)
97 {
98 return null;
99 }
100
101 byte[] buffer = new byte[16384];
102
103 MemoryStream memStream = new MemoryStream();
104
105 long totalRead = 0;
106
107 int bytesRead;
108 do
109 {
110 bytesRead = (int) m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
111 totalRead += bytesRead;
112
113 memStream.Write(buffer, 0, bytesRead);
114 } while (bytesRead == buffer.Length);
115
116 return memStream.ToArray();
117 }
118
119 public string GetString(string name)
120 {
121 int ordinal = m_source.GetOrdinal(name);
122 object value = m_source.GetValue(ordinal);
123
124 if (value is DBNull)
125 {
126 return null;
127 }
128
129 return (string) value;
130 }
131
132 public bool Read()
133 {
134 return m_source.Read();
135 }
136
137 internal Guid GetGuid(string name)
138 {
139 string guidString = GetString(name);
140 if (String.IsNullOrEmpty(guidString))
141 {
142 return Guid.Empty;
143 }
144 else
145 {
146 return new Guid(guidString);
147 }
148 }
149 }
150} \ No newline at end of file
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
new file mode 100644
index 0000000..fe31177
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs
@@ -0,0 +1,135 @@
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;
31
32namespace TribalMedia.Framework.Data
33{
34 public abstract class DatabaseMapper
35 {
36 protected string m_connectionString;
37
38 public DatabaseMapper(string connectionString)
39 {
40 m_connectionString = connectionString;
41 }
42
43 public abstract DbConnection GetNewConnection();
44
45 public abstract string CreateParamName(string fieldName);
46
47 public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key)
48 {
49 string table = mapper.TableName;
50
51 DbCommand command = connection.CreateCommand();
52
53 string conditionString = CreateCondition(mapper, command, fieldName, key);
54
55 string query =
56 String.Format("select * from {0} where {1}", table, conditionString);
57
58 command.CommandText = query;
59 command.CommandType = CommandType.Text;
60
61 return command;
62 }
63
64 public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key)
65 {
66 string keyFieldParamName = mapper.CreateParamName(fieldName);
67
68 DbParameter param = command.CreateParameter();
69 param.ParameterName = keyFieldParamName;
70 param.Value = FieldMapper.ConvertToDbType(key);
71 command.Parameters.Add(param);
72
73 return String.Format("{0}={1}", fieldName, keyFieldParamName);
74 }
75
76 public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
77 {
78 string table = mapper.TableName;
79
80 List<string> fieldNames = new List<string>();
81
82 DbCommand command = connection.CreateCommand();
83
84 foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
85 {
86 if (fieldMapper != mapper.KeyFieldMapper)
87 {
88 fieldMapper.ExpandField(rowMapper, command, fieldNames);
89 }
90 }
91
92 List<string> assignments = new List<string>();
93
94 foreach (string field in fieldNames)
95 {
96 assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
97 }
98
99 string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
100
101 command.CommandText =
102 String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
103 conditionString);
104
105 return command;
106 }
107
108 public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj)
109 {
110 string table = mapper.TableName;
111
112 List<string> fieldNames = new List<string>();
113
114 DbCommand command = connection.CreateCommand();
115
116 foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
117 {
118 fieldMapper.ExpandField(obj, command, fieldNames);
119 }
120
121 List<string> paramNames = new List<string>();
122
123 foreach (string field in fieldNames)
124 {
125 paramNames.Add(mapper.CreateParamName(field));
126 }
127
128 command.CommandText =
129 String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
130 String.Join(", ", paramNames.ToArray()));
131
132 return command;
133 }
134 }
135} \ No newline at end of file
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs
new file mode 100644
index 0000000..28a603a
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs
@@ -0,0 +1,249 @@
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.Common;
30
31namespace TribalMedia.Framework.Data
32{
33 public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper);
34
35 public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value);
36
37 public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
38
39 public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
40
41 public abstract class FieldMapper
42 {
43 private readonly TableMapper m_tableMapper;
44 private readonly string m_fieldName;
45
46 public string FieldName
47 {
48 get { return m_fieldName; }
49 }
50
51 protected Type m_valueType;
52
53 public Type ValueType
54 {
55 get { return m_valueType; }
56 }
57
58 public abstract object GetParamValue(object obj);
59
60 public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType)
61 {
62 m_fieldName = fieldName;
63 m_valueType = valueType;
64 m_tableMapper = tableMapper;
65 }
66
67 public abstract void SetPropertyFromReader(object mapper, DataReader reader);
68
69 public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
70 {
71 string paramName = m_tableMapper.CreateParamName(fieldName);
72 fieldNames.Add(fieldName);
73
74 DbParameter param = command.CreateParameter();
75 param.ParameterName = paramName;
76 param.Value = value;
77
78 command.Parameters.Add(param);
79 }
80
81 public void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
82 {
83 string fieldName = FieldName;
84 object value = GetParamValue(obj);
85
86 //if (ValueType == typeof (Vector3))
87 //{
88 // Vector3 vector = (Vector3) value;
89
90 // RawAddParam(command, fieldNames, fieldName + "X", vector.X);
91 // RawAddParam(command, fieldNames, fieldName + "Y", vector.Y);
92 // RawAddParam(command, fieldNames, fieldName + "Z", vector.Z);
93 //}
94 //else if (ValueType == typeof (Quaternion))
95 //{
96 // Quaternion quaternion = (Quaternion) value;
97
98 // RawAddParam(command, fieldNames, fieldName + "X", quaternion.X);
99 // RawAddParam(command, fieldNames, fieldName + "Y", quaternion.Y);
100 // RawAddParam(command, fieldNames, fieldName + "Z", quaternion.Z);
101 // RawAddParam(command, fieldNames, fieldName + "W", quaternion.W);
102 //}
103 //else
104 //{
105 RawAddParam(command, fieldNames, fieldName, ConvertToDbType(value));
106 //}
107 }
108
109 protected object GetValue(DataReader reader)
110 {
111 object value;
112 //if (ValueType == typeof (Vector3))
113 //{
114 // value = reader.GetVector(m_fieldName);
115 //}
116 //else if (ValueType == typeof (Quaternion))
117 //{
118 // value = reader.GetQuaternion(m_fieldName);
119 //}
120 //else
121 //if (ValueType == typeof(UID))
122 //{
123 // Guid guid = reader.GetGuid(m_fieldName);
124 // value = new UID(guid);
125 //}
126 //else
127 if (ValueType == typeof(Guid))
128 {
129 value = reader.GetGuid(m_fieldName);
130 }
131 else if (ValueType == typeof (bool))
132 {
133 uint boolVal = reader.GetUShort(m_fieldName);
134 value = (boolVal == 1);
135 }
136 else
137 if (ValueType == typeof (byte))
138 {
139 value = reader.GetByte(m_fieldName);
140 }
141 else if (ValueType == typeof (sbyte))
142 {
143 value = reader.GetSByte(m_fieldName);
144 }
145 else if (ValueType == typeof (ushort))
146 {
147 value = reader.GetUShort(m_fieldName);
148 }
149 else if (ValueType == typeof (byte[]))
150 {
151 value = reader.GetBytes(m_fieldName);
152 }
153 else
154 {
155 value = reader.Get(m_fieldName);
156 }
157
158 if (value is DBNull)
159 {
160 value = default(ValueType);
161 }
162
163 return value;
164 }
165
166 public static object ConvertToDbType(object value)
167 {
168 //if (value is UID)
169 //{
170 // return (value as UID).UUID.ToString();
171 //}
172
173 return value;
174 }
175 }
176
177 public class RowMapperField<TRowMapper, TField> : FieldMapper
178 where TRowMapper : RowMapper
179 {
180 private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
181 private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
182
183 public override object GetParamValue(object obj)
184 {
185 return m_fieldGetAccessor((TRowMapper) obj);
186 }
187
188 public override void SetPropertyFromReader(object mapper, DataReader reader)
189 {
190 object value;
191
192 value = GetValue(reader);
193
194 if (value == null)
195 {
196 m_fieldSetAccessor((TRowMapper) mapper, default(TField));
197 }
198 else
199 {
200 m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
201 }
202 }
203
204
205 public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
206 RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
207 : base(tableMapper, fieldName, typeof(TField))
208 {
209 m_fieldGetAccessor = rowMapperGetAccessor;
210 m_fieldSetAccessor = rowMapperSetAccessor;
211 }
212 }
213
214 public class ObjectField<TObject, TField> : FieldMapper
215 {
216 private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
217 private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
218
219 public override object GetParamValue(object obj)
220 {
221 return m_fieldGetAccessor((TObject) obj);
222 }
223
224 public override void SetPropertyFromReader(object obj, DataReader reader)
225 {
226 object value;
227
228 value = GetValue(reader);
229
230 if (value == null)
231 {
232 m_fieldSetAccessor((TObject) obj, default(TField));
233 }
234 else
235 {
236 m_fieldSetAccessor((TObject) obj, (TField) value);
237 }
238 }
239
240
241 public ObjectField(TableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
242 ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
243 : base(tableMapper, fieldName, typeof (TField))
244 {
245 m_fieldGetAccessor = rowMapperGetAccessor;
246 m_fieldSetAccessor = rowMapperSetAccessor;
247 }
248 }
249} \ No newline at end of file
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
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Properties/AssemblyInfo.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..4a073e9
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Properties/AssemblyInfo.cs
@@ -0,0 +1,40 @@
1using System.Reflection;
2using System.Runtime.InteropServices;
3using System.Security;
4
5// General Information about an assembly is controlled through the following
6// set of attributes. Change these attribute values to modify the information
7// associated with an assembly.
8
9[assembly : AssemblyTitle("TribalMedia.Framework.Data")]
10[assembly : AssemblyDescription("Generic Database Abstraction Layer")]
11[assembly : AssemblyConfiguration("")]
12[assembly : AssemblyCompany("TribalMedia")]
13[assembly : AssemblyProduct("TribalMedia.Framework.Data")]
14[assembly: AssemblyCopyright("Copyright © 2007 Tribal Media")]
15[assembly : AssemblyTrademark("")]
16[assembly : AssemblyCulture("")]
17
18// Setting ComVisible to false makes the types in this assembly not visible
19// to COM components. If you need to access a type in this assembly from
20// COM, set the ComVisible attribute to true on that type.
21
22[assembly : ComVisible(false)]
23
24// The following GUID is for the ID of the typelib if this project is exposed to COM
25
26[assembly : Guid("9269f421-19d9-4eea-bfe3-c0ffe426fada")]
27
28// Version information for an assembly consists of the following four values:
29//
30// Major Version
31// Minor Version
32// Build Number
33// Revision
34//
35// You can specify all the values or you can default the Revision and Build Numbers
36// by using the '*' as shown below:
37
38[assembly : AssemblyVersion("1.0.0.0")]
39[assembly : AssemblyFileVersion("1.0.0.0")]
40[assembly : AllowPartiallyTrustedCallers] \ No newline at end of file
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs
new file mode 100644
index 0000000..aa22c8b
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs
@@ -0,0 +1,85 @@
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 TribalMedia.Framework.Data;
28
29namespace TribalMedia.Framework.Data
30{
31 public abstract class RowMapper
32 {
33 public abstract void FillObject(DataReader reader);
34 }
35
36 public class ObjectMapper<TObj> : RowMapper
37 {
38 private readonly Schema m_schema;
39 private readonly TObj m_obj;
40
41 public TObj Object
42 {
43 get { return m_obj; }
44 }
45
46 public ObjectMapper(Schema schema, TObj obj)
47 {
48 m_schema = schema;
49 m_obj = obj;
50 }
51
52 public override void FillObject(DataReader reader)
53 {
54 foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
55 {
56 fieldMapper.SetPropertyFromReader(m_obj, reader);
57 }
58 }
59 }
60
61 public class RowMapper<TObj> : RowMapper
62 {
63 private readonly Schema m_schema;
64 private readonly TObj m_obj;
65
66 public TObj Object
67 {
68 get { return m_obj; }
69 }
70
71 public RowMapper(Schema schema, TObj obj)
72 {
73 m_schema = schema;
74 m_obj = obj;
75 }
76
77 public override void FillObject(DataReader reader)
78 {
79 foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
80 {
81 fieldMapper.SetPropertyFromReader(this, reader);
82 }
83 }
84 }
85} \ No newline at end of file
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs
new file mode 100644
index 0000000..b7b8939
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs
@@ -0,0 +1,89 @@
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.Collections.Generic;
28using TribalMedia.Framework.Data;
29
30namespace TribalMedia.Framework.Data
31{
32 public class Schema
33 {
34 protected TableMapper m_tableMapper;
35 protected Dictionary<string, FieldMapper> m_mappings;
36
37 public Dictionary<string, FieldMapper> Fields
38 {
39 get { return m_mappings; }
40 }
41
42 public Schema(TableMapper tableMapper)
43 {
44 m_mappings = new Dictionary<string, FieldMapper>();
45 m_tableMapper = tableMapper;
46 }
47 }
48
49 public class ObjectSchema<TObj> : Schema
50 {
51 public ObjectSchema(TableMapper tableMapper) : base(tableMapper)
52 {
53 }
54
55 public ObjectField<TObj, TField> AddMapping<TField>(string fieldName,
56 ObjectGetAccessor<TObj, TField> rowMapperGetAccessor,
57 ObjectSetAccessor<TObj, TField> rowMapperSetAccessor)
58 {
59 ObjectField<TObj, TField> rowMapperField =
60 new ObjectField<TObj, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor);
61
62 m_mappings.Add(fieldName, rowMapperField);
63
64 return rowMapperField;
65 }
66 }
67
68 public class RowMapperSchema<TRowMapper> : Schema
69 where TRowMapper : RowMapper
70 {
71 public RowMapperSchema(TableMapper tableMapper) : base(tableMapper)
72 {
73 }
74
75 public RowMapperField<TRowMapper, TField> AddMapping<TField>(string fieldName,
76 RowMapperGetAccessor<TRowMapper, TField>
77 rowMapperGetAccessor,
78 RowMapperSetAccessor<TRowMapper, TField>
79 rowMapperSetAccessor)
80 {
81 RowMapperField<TRowMapper, TField> rowMapperField =
82 new RowMapperField<TRowMapper, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor);
83
84 m_mappings.Add(fieldName, rowMapperField);
85
86 return rowMapperField;
87 }
88 }
89} \ No newline at end of file
diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs
new file mode 100644
index 0000000..f041e79
--- /dev/null
+++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs
@@ -0,0 +1,108 @@
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 TableMapper
35 {
36 private readonly DatabaseMapper m_connectionPool;
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_connectionPool.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 private Schema m_schema;
66 public Schema Schema
67 {
68 get { return m_schema; }
69 }
70
71 private FieldMapper m_keyFieldMapper;
72 public FieldMapper KeyFieldMapper
73 {
74 get { return m_keyFieldMapper; }
75 }
76
77 public TableMapper(DatabaseMapper connectionPool, string tableName)
78 {
79 m_connectionPool = connectionPool;
80 m_tableName = tableName.ToLower(); // Stupid MySQL hack.
81 }
82
83 public string CreateParamName(string fieldName)
84 {
85 return m_connectionPool.CreateParamName(fieldName);
86 }
87
88 protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey)
89 {
90 return m_connectionPool.CreateSelectCommand(this, connection, fieldName, primaryKey);
91 }
92
93 public string CreateCondition(DbCommand command, string fieldName, object key)
94 {
95 return m_connectionPool.CreateCondition(this, command, fieldName, key);
96 }
97
98 public DbCommand CreateInsertCommand(DbConnection connection, object obj)
99 {
100 return m_connectionPool.CreateInsertCommand(this, connection, obj);
101 }
102
103 public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey)
104 {
105 return m_connectionPool.CreateUpdateCommand(this, connection, rowMapper, primaryKey);
106 }
107 }
108} \ No newline at end of file