diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs new file mode 100644 index 0000000000..f0b3e88760 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs @@ -0,0 +1,150 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Data; +using System.IO; + +namespace TribalMedia.Framework.Data +{ + public class DataReader + { + private readonly IDataReader m_source; + + public DataReader(IDataReader source) + { + m_source = source; + } + + public object Get(string name) + { + return m_source[name]; + } + + public ushort GetUShort(string name) + { + return (ushort) m_source.GetInt32(m_source.GetOrdinal(name)); + } + + public byte GetByte(string name) + { + int ordinal = m_source.GetOrdinal(name); + byte value = (byte) m_source.GetInt16(ordinal); + return value; + } + + public sbyte GetSByte(string name) + { + return (sbyte) m_source.GetInt16(m_source.GetOrdinal(name)); + } + + //public Vector3 GetVector(string s) + //{ + // float x = GetFloat(s + "X"); + // float y = GetFloat(s + "Y"); + // float z = GetFloat(s + "Z"); + + // Vector3 vector = new Vector3(x, y, z); + + // return vector; + //} + + //public Quaternion GetQuaternion(string s) + //{ + // float x = GetFloat(s + "X"); + // float y = GetFloat(s + "Y"); + // float z = GetFloat(s + "Z"); + // float w = GetFloat(s + "W"); + + // Quaternion quaternion = new Quaternion(x, y, z, w); + + // return quaternion; + //} + + public float GetFloat(string name) + { + return m_source.GetFloat(m_source.GetOrdinal(name)); + } + + public byte[] GetBytes(string name) + { + int ordinal = m_source.GetOrdinal(name); + + if (m_source.GetValue(ordinal) == DBNull.Value) + { + return null; + } + + byte[] buffer = new byte[16384]; + + MemoryStream memStream = new MemoryStream(); + + long totalRead = 0; + + int bytesRead; + do + { + bytesRead = (int) m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length); + totalRead += bytesRead; + + memStream.Write(buffer, 0, bytesRead); + } while (bytesRead == buffer.Length); + + return memStream.ToArray(); + } + + public string GetString(string name) + { + int ordinal = m_source.GetOrdinal(name); + object value = m_source.GetValue(ordinal); + + if (value is DBNull) + { + return null; + } + + return (string) value; + } + + public bool Read() + { + return m_source.Read(); + } + + internal Guid GetGuid(string name) + { + string guidString = GetString(name); + if (String.IsNullOrEmpty(guidString)) + { + return Guid.Empty; + } + else + { + return new Guid(guidString); + } + } + } +} \ 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 0000000000..fe31177d74 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs @@ -0,0 +1,135 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; + +namespace TribalMedia.Framework.Data +{ + public abstract class DatabaseMapper + { + protected string m_connectionString; + + public DatabaseMapper(string connectionString) + { + m_connectionString = connectionString; + } + + public abstract DbConnection GetNewConnection(); + + public abstract string CreateParamName(string fieldName); + + public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key) + { + string table = mapper.TableName; + + DbCommand command = connection.CreateCommand(); + + string conditionString = CreateCondition(mapper, command, fieldName, key); + + string query = + String.Format("select * from {0} where {1}", table, conditionString); + + command.CommandText = query; + command.CommandType = CommandType.Text; + + return command; + } + + public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key) + { + string keyFieldParamName = mapper.CreateParamName(fieldName); + + DbParameter param = command.CreateParameter(); + param.ParameterName = keyFieldParamName; + param.Value = FieldMapper.ConvertToDbType(key); + command.Parameters.Add(param); + + return String.Format("{0}={1}", fieldName, keyFieldParamName); + } + + public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) + { + string table = mapper.TableName; + + List fieldNames = new List(); + + DbCommand command = connection.CreateCommand(); + + foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) + { + if (fieldMapper != mapper.KeyFieldMapper) + { + fieldMapper.ExpandField(rowMapper, command, fieldNames); + } + } + + List assignments = new List(); + + foreach (string field in fieldNames) + { + assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); + } + + string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); + + command.CommandText = + String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), + conditionString); + + return command; + } + + public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj) + { + string table = mapper.TableName; + + List fieldNames = new List(); + + DbCommand command = connection.CreateCommand(); + + foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) + { + fieldMapper.ExpandField(obj, command, fieldNames); + } + + List paramNames = new List(); + + foreach (string field in fieldNames) + { + paramNames.Add(mapper.CreateParamName(field)); + } + + command.CommandText = + String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), + String.Join(", ", paramNames.ToArray())); + + return command; + } + } +} \ 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 0000000000..28a603acf4 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs @@ -0,0 +1,249 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Collections.Generic; +using System.Data.Common; + +namespace TribalMedia.Framework.Data +{ + public delegate TField RowMapperGetAccessor(TRowMapper rowMapper); + + public delegate void RowMapperSetAccessor(TRowMapper rowMapper, TField value); + + public delegate TField ObjectGetAccessor(TObj obj); + + public delegate void ObjectSetAccessor(TObj obj, TField value); + + public abstract class FieldMapper + { + private readonly TableMapper m_tableMapper; + private readonly string m_fieldName; + + public string FieldName + { + get { return m_fieldName; } + } + + protected Type m_valueType; + + public Type ValueType + { + get { return m_valueType; } + } + + public abstract object GetParamValue(object obj); + + public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType) + { + m_fieldName = fieldName; + m_valueType = valueType; + m_tableMapper = tableMapper; + } + + public abstract void SetPropertyFromReader(object mapper, DataReader reader); + + public void RawAddParam(DbCommand command, List fieldNames, string fieldName, object value) + { + string paramName = m_tableMapper.CreateParamName(fieldName); + fieldNames.Add(fieldName); + + DbParameter param = command.CreateParameter(); + param.ParameterName = paramName; + param.Value = value; + + command.Parameters.Add(param); + } + + public void ExpandField(TObj obj, DbCommand command, List fieldNames) + { + string fieldName = FieldName; + object value = GetParamValue(obj); + + //if (ValueType == typeof (Vector3)) + //{ + // Vector3 vector = (Vector3) value; + + // RawAddParam(command, fieldNames, fieldName + "X", vector.X); + // RawAddParam(command, fieldNames, fieldName + "Y", vector.Y); + // RawAddParam(command, fieldNames, fieldName + "Z", vector.Z); + //} + //else if (ValueType == typeof (Quaternion)) + //{ + // Quaternion quaternion = (Quaternion) value; + + // RawAddParam(command, fieldNames, fieldName + "X", quaternion.X); + // RawAddParam(command, fieldNames, fieldName + "Y", quaternion.Y); + // RawAddParam(command, fieldNames, fieldName + "Z", quaternion.Z); + // RawAddParam(command, fieldNames, fieldName + "W", quaternion.W); + //} + //else + //{ + RawAddParam(command, fieldNames, fieldName, ConvertToDbType(value)); + //} + } + + protected object GetValue(DataReader reader) + { + object value; + //if (ValueType == typeof (Vector3)) + //{ + // value = reader.GetVector(m_fieldName); + //} + //else if (ValueType == typeof (Quaternion)) + //{ + // value = reader.GetQuaternion(m_fieldName); + //} + //else + //if (ValueType == typeof(UID)) + //{ + // Guid guid = reader.GetGuid(m_fieldName); + // value = new UID(guid); + //} + //else + if (ValueType == typeof(Guid)) + { + value = reader.GetGuid(m_fieldName); + } + else if (ValueType == typeof (bool)) + { + uint boolVal = reader.GetUShort(m_fieldName); + value = (boolVal == 1); + } + else + if (ValueType == typeof (byte)) + { + value = reader.GetByte(m_fieldName); + } + else if (ValueType == typeof (sbyte)) + { + value = reader.GetSByte(m_fieldName); + } + else if (ValueType == typeof (ushort)) + { + value = reader.GetUShort(m_fieldName); + } + else if (ValueType == typeof (byte[])) + { + value = reader.GetBytes(m_fieldName); + } + else + { + value = reader.Get(m_fieldName); + } + + if (value is DBNull) + { + value = default(ValueType); + } + + return value; + } + + public static object ConvertToDbType(object value) + { + //if (value is UID) + //{ + // return (value as UID).UUID.ToString(); + //} + + return value; + } + } + + public class RowMapperField : FieldMapper + where TRowMapper : RowMapper + { + private readonly RowMapperGetAccessor m_fieldGetAccessor; + private readonly RowMapperSetAccessor m_fieldSetAccessor; + + public override object GetParamValue(object obj) + { + return m_fieldGetAccessor((TRowMapper) obj); + } + + public override void SetPropertyFromReader(object mapper, DataReader reader) + { + object value; + + value = GetValue(reader); + + if (value == null) + { + m_fieldSetAccessor((TRowMapper) mapper, default(TField)); + } + else + { + m_fieldSetAccessor((TRowMapper) mapper, (TField) value); + } + } + + + public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor rowMapperGetAccessor, + RowMapperSetAccessor rowMapperSetAccessor) + : base(tableMapper, fieldName, typeof(TField)) + { + m_fieldGetAccessor = rowMapperGetAccessor; + m_fieldSetAccessor = rowMapperSetAccessor; + } + } + + public class ObjectField : FieldMapper + { + private readonly ObjectGetAccessor m_fieldGetAccessor; + private readonly ObjectSetAccessor m_fieldSetAccessor; + + public override object GetParamValue(object obj) + { + return m_fieldGetAccessor((TObject) obj); + } + + public override void SetPropertyFromReader(object obj, DataReader reader) + { + object value; + + value = GetValue(reader); + + if (value == null) + { + m_fieldSetAccessor((TObject) obj, default(TField)); + } + else + { + m_fieldSetAccessor((TObject) obj, (TField) value); + } + } + + + public ObjectField(TableMapper tableMapper, string fieldName, ObjectGetAccessor rowMapperGetAccessor, + ObjectSetAccessor rowMapperSetAccessor) + : base(tableMapper, fieldName, typeof (TField)) + { + m_fieldGetAccessor = rowMapperGetAccessor; + m_fieldSetAccessor = rowMapperSetAccessor; + } + } +} \ 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 0000000000..631a2caee1 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs @@ -0,0 +1,162 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using TribalMedia.Framework.Data; + +namespace TribalMedia.Framework.Data +{ + public abstract class ObjectTableMapper : TableMapper + { + public ObjectTableMapper(DatabaseMapper connectionPool, string tableName) + : base(connectionPool, tableName) + { + } + + public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) + { + TRowMapper result = default(TRowMapper); + bool success = false; + + WithConnection(delegate(DbConnection connection) + { + using ( + DbCommand command = + CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey)) + { + using (IDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + result = FromReader(new DataReader(reader)); + success = true; + } + else + { + success = false; + } + } + } + }); + + value = result; + + return success; + } + + + public virtual bool Remove(TPrimaryKey id) + { + int deleted = 0; + + WithConnection(delegate(DbConnection connection) + { + using ( + DbCommand command = + CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) + { + deleted = command.ExecuteNonQuery(); + } + }); + + if (deleted == 1) + { + return true; + } + else + { + return false; + } + } + + + public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey) + { + string table = TableName; + + DbCommand command = connection.CreateCommand(); + + string conditionString = CreateCondition(command, fieldName, primaryKey); + + string query = + String.Format("delete from {0} where {1}", table, conditionString); + + command.CommandText = query; + command.CommandType = CommandType.Text; + + return command; + } + + public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) + { + int updated = 0; + + WithConnection(delegate(DbConnection connection) + { + using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) + { + updated = command.ExecuteNonQuery(); + } + }); + + if (updated == 1) + { + return true; + } + else + { + return false; + } + } + + public virtual bool Add(TRowMapper value) + { + int added = 0; + + WithConnection(delegate(DbConnection connection) + { + using (DbCommand command = CreateInsertCommand(connection, value)) + { + added = command.ExecuteNonQuery(); + } + }); + + if (added == 1) + { + return true; + } + else + { + return false; + } + } + + public abstract TRowMapper FromReader(DataReader reader); + } +} \ 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 0000000000..4a073e9717 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Properties/AssemblyInfo.cs @@ -0,0 +1,40 @@ +using System.Reflection; +using System.Runtime.InteropServices; +using System.Security; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. + +[assembly : AssemblyTitle("TribalMedia.Framework.Data")] +[assembly : AssemblyDescription("Generic Database Abstraction Layer")] +[assembly : AssemblyConfiguration("")] +[assembly : AssemblyCompany("TribalMedia")] +[assembly : AssemblyProduct("TribalMedia.Framework.Data")] +[assembly: AssemblyCopyright("Copyright © 2007 Tribal Media")] +[assembly : AssemblyTrademark("")] +[assembly : AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. + +[assembly : ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM + +[assembly : Guid("9269f421-19d9-4eea-bfe3-c0ffe426fada")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: + +[assembly : AssemblyVersion("1.0.0.0")] +[assembly : AssemblyFileVersion("1.0.0.0")] +[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 0000000000..aa22c8b777 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs @@ -0,0 +1,85 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using TribalMedia.Framework.Data; + +namespace TribalMedia.Framework.Data +{ + public abstract class RowMapper + { + public abstract void FillObject(DataReader reader); + } + + public class ObjectMapper : RowMapper + { + private readonly Schema m_schema; + private readonly TObj m_obj; + + public TObj Object + { + get { return m_obj; } + } + + public ObjectMapper(Schema schema, TObj obj) + { + m_schema = schema; + m_obj = obj; + } + + public override void FillObject(DataReader reader) + { + foreach (FieldMapper fieldMapper in m_schema.Fields.Values) + { + fieldMapper.SetPropertyFromReader(m_obj, reader); + } + } + } + + public class RowMapper : RowMapper + { + private readonly Schema m_schema; + private readonly TObj m_obj; + + public TObj Object + { + get { return m_obj; } + } + + public RowMapper(Schema schema, TObj obj) + { + m_schema = schema; + m_obj = obj; + } + + public override void FillObject(DataReader reader) + { + foreach (FieldMapper fieldMapper in m_schema.Fields.Values) + { + fieldMapper.SetPropertyFromReader(this, reader); + } + } + } +} \ 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 0000000000..b7b89397ba --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs @@ -0,0 +1,89 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System.Collections.Generic; +using TribalMedia.Framework.Data; + +namespace TribalMedia.Framework.Data +{ + public class Schema + { + protected TableMapper m_tableMapper; + protected Dictionary m_mappings; + + public Dictionary Fields + { + get { return m_mappings; } + } + + public Schema(TableMapper tableMapper) + { + m_mappings = new Dictionary(); + m_tableMapper = tableMapper; + } + } + + public class ObjectSchema : Schema + { + public ObjectSchema(TableMapper tableMapper) : base(tableMapper) + { + } + + public ObjectField AddMapping(string fieldName, + ObjectGetAccessor rowMapperGetAccessor, + ObjectSetAccessor rowMapperSetAccessor) + { + ObjectField rowMapperField = + new ObjectField(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor); + + m_mappings.Add(fieldName, rowMapperField); + + return rowMapperField; + } + } + + public class RowMapperSchema : Schema + where TRowMapper : RowMapper + { + public RowMapperSchema(TableMapper tableMapper) : base(tableMapper) + { + } + + public RowMapperField AddMapping(string fieldName, + RowMapperGetAccessor + rowMapperGetAccessor, + RowMapperSetAccessor + rowMapperSetAccessor) + { + RowMapperField rowMapperField = + new RowMapperField(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor); + + m_mappings.Add(fieldName, rowMapperField); + + return rowMapperField; + } + } +} \ 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 0000000000..f041e79898 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs @@ -0,0 +1,108 @@ +/* +* Copyright (c) Tribal Media AB, http://tribalmedia.se/ +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above copyright +* notice, this list of conditions and the following disclaimer in the +* documentation and/or other materials provided with the distribution. +* * The name of Tribal Media AB may not be used to endorse or promote products +* derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY +* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY +* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +* +*/ + +using System; +using System.Data; +using System.Data.Common; +using TribalMedia.Framework.Data; + +namespace TribalMedia.Framework.Data +{ + public abstract class TableMapper + { + private readonly DatabaseMapper m_connectionPool; + private readonly object m_syncRoot = new object(); + + protected void WithConnection(Action action) + { + lock (m_syncRoot) + { + DbConnection m_connection = m_connectionPool.GetNewConnection(); + + if (m_connection.State != ConnectionState.Open) + { + m_connection.Open(); + } + + action(m_connection); + + if (m_connection.State == ConnectionState.Open) + { + m_connection.Close(); + } + } + } + + private readonly string m_tableName; + public string TableName + { + get { return m_tableName; } + } + + private Schema m_schema; + public Schema Schema + { + get { return m_schema; } + } + + private FieldMapper m_keyFieldMapper; + public FieldMapper KeyFieldMapper + { + get { return m_keyFieldMapper; } + } + + public TableMapper(DatabaseMapper connectionPool, string tableName) + { + m_connectionPool = connectionPool; + m_tableName = tableName.ToLower(); // Stupid MySQL hack. + } + + public string CreateParamName(string fieldName) + { + return m_connectionPool.CreateParamName(fieldName); + } + + protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) + { + return m_connectionPool.CreateSelectCommand(this, connection, fieldName, primaryKey); + } + + public string CreateCondition(DbCommand command, string fieldName, object key) + { + return m_connectionPool.CreateCondition(this, command, fieldName, key); + } + + public DbCommand CreateInsertCommand(DbConnection connection, object obj) + { + return m_connectionPool.CreateInsertCommand(this, connection, obj); + } + + public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) + { + return m_connectionPool.CreateUpdateCommand(this, connection, rowMapper, primaryKey); + } + } +} \ No newline at end of file diff --git a/prebuild.xml b/prebuild.xml index ab95a966d5..86a279168e 100644 --- a/prebuild.xml +++ b/prebuild.xml @@ -79,6 +79,26 @@ + + + + ../../../bin/ + + + + + ../../../bin/ + + + + ../../../bin/ + + + + + + +