From f5103b98be60cc8b1a69288328cdc20f78857835 Mon Sep 17 00:00:00 2001 From: lbsa71 Date: Tue, 15 Jan 2008 20:07:02 +0000 Subject: * Renamed a bunch of Data baseclasses for clarity and readability (Slowly getting there) --- OpenSim/Framework/Data/OpenSimDatabaseMapper.cs | 2 +- OpenSim/Framework/Data/OpenSimObjectFieldMapper.cs | 4 +- OpenSim/Framework/Data/OpenSimTableMapper.cs | 4 +- .../Data/PrimitiveBaseShapeTableMapper.cs | 4 +- .../TribalMedia.Framework.Data/BaseDataReader.cs | 127 +++++++++++ .../BaseDatabaseConnector.cs | 139 ++++++++++++ .../TribalMedia.Framework.Data/BaseFieldMapper.cs | 203 +++++++++++++++++ .../TribalMedia.Framework.Data/BaseRowMapper.cs | 85 +++++++ .../TribalMedia.Framework.Data/BaseSchema.cs | 90 ++++++++ .../TribalMedia.Framework.Data/BaseTableMapper.cs | 245 +++++++++++++++++++++ .../TribalMedia.Framework.Data/DataReader.cs | 127 ----------- .../TribalMedia.Framework.Data/DatabaseMapper.cs | 139 ------------ .../TribalMedia.Framework.Data/FieldMapper.cs | 205 ----------------- .../ObjectTableMapper.cs | 159 ------------- .../TribalMedia.Framework.Data/RowMapper.cs | 85 ------- .../TribalMedia.Framework.Data/Schema.cs | 89 -------- .../TribalMedia.Framework.Data/TableMapper.cs | 118 ---------- .../TribalMedia.Framework.Data.snk | Bin 0 -> 596 bytes 18 files changed, 896 insertions(+), 929 deletions(-) create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDatabaseConnector.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseRowMapper.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseSchema.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseTableMapper.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs delete mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs create mode 100644 ThirdParty/TribalMedia/TribalMedia.Framework.Data/TribalMedia.Framework.Data.snk diff --git a/OpenSim/Framework/Data/OpenSimDatabaseMapper.cs b/OpenSim/Framework/Data/OpenSimDatabaseMapper.cs index bb82462..4f202bc 100644 --- a/OpenSim/Framework/Data/OpenSimDatabaseMapper.cs +++ b/OpenSim/Framework/Data/OpenSimDatabaseMapper.cs @@ -4,7 +4,7 @@ using TribalMedia.Framework.Data; namespace OpenSim.Framework.Data { - public abstract class OpenSimDatabaseMapper : DatabaseMapper + public abstract class OpenSimDatabaseMapper : BaseDatabaseConnector { public OpenSimDatabaseMapper(string connectionString) : base(connectionString) { diff --git a/OpenSim/Framework/Data/OpenSimObjectFieldMapper.cs b/OpenSim/Framework/Data/OpenSimObjectFieldMapper.cs index 851ebcd..d8c2ddf 100644 --- a/OpenSim/Framework/Data/OpenSimObjectFieldMapper.cs +++ b/OpenSim/Framework/Data/OpenSimObjectFieldMapper.cs @@ -9,7 +9,7 @@ namespace OpenSim.Framework.Data { public class OpenSimObjectFieldMapper : ObjectField { - public OpenSimObjectFieldMapper(TableMapper tableMapper, string fieldName, + public OpenSimObjectFieldMapper(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor rowMapperGetAccessor, ObjectSetAccessor rowMapperSetAccessor) : base(tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor) @@ -40,7 +40,7 @@ namespace OpenSim.Framework.Data } else { - base.ExpandField(obj, command, fieldNames); + base.ExpandField(obj, command, fieldNames); } } diff --git a/OpenSim/Framework/Data/OpenSimTableMapper.cs b/OpenSim/Framework/Data/OpenSimTableMapper.cs index c72f523..b6406b6 100644 --- a/OpenSim/Framework/Data/OpenSimTableMapper.cs +++ b/OpenSim/Framework/Data/OpenSimTableMapper.cs @@ -3,9 +3,9 @@ using TribalMedia.Framework.Data; namespace OpenSim.Framework.Data { - public abstract class OpenSimTableMapper : ObjectTableMapper + public abstract class OpenSimTableMapper : BaseTableMapper { - public OpenSimTableMapper(DatabaseMapper database, string tableName) : base(database, tableName) + public OpenSimTableMapper(BaseDatabaseConnector database, string tableName) : base(database, tableName) { } diff --git a/OpenSim/Framework/Data/PrimitiveBaseShapeTableMapper.cs b/OpenSim/Framework/Data/PrimitiveBaseShapeTableMapper.cs index c8098ab..af49376 100644 --- a/OpenSim/Framework/Data/PrimitiveBaseShapeTableMapper.cs +++ b/OpenSim/Framework/Data/PrimitiveBaseShapeTableMapper.cs @@ -16,10 +16,10 @@ namespace OpenSim.Framework.Data public class PrimitiveBaseShapeTableMapper : OpenSimTableMapper { - public PrimitiveBaseShapeTableMapper(DatabaseMapper connection, string tableName) + public PrimitiveBaseShapeTableMapper(BaseDatabaseConnector connection, string tableName) : base(connection, tableName) { - RowMapperSchema rowMapperSchema = new RowMapperSchema(this); + ObjectSchema rowMapperSchema = new ObjectSchema(this); m_schema = rowMapperSchema; m_keyFieldMapper = rowMapperSchema.AddMapping("SceneObjectPartId", diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs new file mode 100644 index 0000000..34f894c --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDataReader.cs @@ -0,0 +1,127 @@ +/* +* 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 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(); + } + + public 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/BaseDatabaseConnector.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDatabaseConnector.cs new file mode 100644 index 0000000..ae70fc1 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseDatabaseConnector.cs @@ -0,0 +1,139 @@ +/* +* 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 BaseDatabaseConnector + { + protected string m_connectionString; + + public BaseDatabaseConnector(string connectionString) + { + m_connectionString = connectionString; + } + + public abstract DbConnection GetNewConnection(); + public abstract string CreateParamName(string fieldName); + + public DbCommand CreateSelectCommand(BaseTableMapper 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(BaseTableMapper mapper, DbCommand command, string fieldName, object key) + { + string keyFieldParamName = mapper.CreateParamName(fieldName); + + DbParameter param = command.CreateParameter(); + param.ParameterName = keyFieldParamName; + param.Value = ConvertToDbType(key); + command.Parameters.Add(param); + + return String.Format("{0}={1}", fieldName, keyFieldParamName); + } + + public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) + { + string table = mapper.TableName; + + List fieldNames = new List(); + + DbCommand command = connection.CreateCommand(); + + foreach (BaseFieldMapper 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(BaseTableMapper mapper, DbConnection connection, object obj) + { + string table = mapper.TableName; + + List fieldNames = new List(); + + DbCommand command = connection.CreateCommand(); + + foreach (BaseFieldMapper 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; + } + + public virtual object ConvertToDbType(object value) + { + return value; + } + } +} \ No newline at end of file diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs new file mode 100644 index 0000000..20d8cf1 --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseFieldMapper.cs @@ -0,0 +1,203 @@ +/* +* 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 BaseFieldMapper + { + private readonly BaseTableMapper 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 BaseFieldMapper(BaseTableMapper 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 virtual void ExpandField(TObj obj, DbCommand command, List fieldNames) + { + string fieldName = FieldName; + object value = GetParamValue(obj); + + RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value)); + } + + protected virtual object GetValue(DataReader reader) + { + object value; + + 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 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 : BaseFieldMapper + { + 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(BaseTableMapper 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/BaseRowMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseRowMapper.cs new file mode 100644 index 0000000..2ab8daf --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseRowMapper.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 (BaseFieldMapper 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 (BaseFieldMapper fieldMapper in m_schema.Fields.Values) + { + fieldMapper.SetPropertyFromReader(this, reader); + } + } + } +} \ No newline at end of file diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseSchema.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseSchema.cs new file mode 100644 index 0000000..fc59f3b --- /dev/null +++ b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/BaseSchema.cs @@ -0,0 +1,90 @@ +/* +* 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 BaseTableMapper m_tableMapper; + protected Dictionary m_mappings; + + public Dictionary Fields + { + get { return m_mappings; } + } + + public Schema(BaseTableMapper tableMapper) + { + m_mappings = new Dictionary(); + m_tableMapper = tableMapper; + } + } + + public class ObjectSchema : Schema + { + public ObjectSchema(BaseTableMapper 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/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 @@ +/* +* 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 BaseTableMapper + { + private readonly BaseDatabaseConnector m_database; + private readonly object m_syncRoot = new object(); + + protected void WithConnection(Action action) + { + lock (m_syncRoot) + { + DbConnection m_connection = m_database.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; } + } + + protected Schema m_schema; + public Schema Schema + { + get { return m_schema; } + } + + protected BaseFieldMapper m_keyFieldMapper; + public BaseFieldMapper KeyFieldMapper + { + get { return m_keyFieldMapper; } + } + + public BaseTableMapper(BaseDatabaseConnector database, string tableName) + { + m_database = database; + m_tableName = tableName.ToLower(); // Stupid MySQL hack. + } + + public string CreateParamName(string fieldName) + { + return m_database.CreateParamName(fieldName); + } + + protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) + { + return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey); + } + + public string CreateCondition(DbCommand command, string fieldName, object key) + { + return m_database.CreateCondition(this, command, fieldName, key); + } + + public DbCommand CreateInsertCommand(DbConnection connection, object obj) + { + return m_database.CreateInsertCommand(this, connection, obj); + } + + public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) + { + return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey); + } + + public object ConvertToDbType(object value) + { + return m_database.ConvertToDbType(value); + } + + protected virtual DataReader CreateReader(IDataReader reader) + { + return new DataReader(reader); + } + } + + public abstract class BaseTableMapper : BaseTableMapper + { + public BaseTableMapper(BaseDatabaseConnector database, string tableName) + : base(database, 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(CreateReader(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/DataReader.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs deleted file mode 100644 index 4792e9e..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DataReader.cs +++ /dev/null @@ -1,127 +0,0 @@ -/* -* 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 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(); - } - - public 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 deleted file mode 100644 index 63e59aa..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/DatabaseMapper.cs +++ /dev/null @@ -1,139 +0,0 @@ -/* -* 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 = 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; - } - - public virtual object ConvertToDbType(object value) - { - return value; - } - } -} \ No newline at end of file diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs deleted file mode 100644 index 89bc0f0..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/FieldMapper.cs +++ /dev/null @@ -1,205 +0,0 @@ -/* -* 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 virtual void ExpandField(TObj obj, DbCommand command, List fieldNames) - { - string fieldName = FieldName; - object value = GetParamValue(obj); - - RawAddParam(command, fieldNames, fieldName, m_tableMapper.ConvertToDbType(value)); - } - - protected virtual object GetValue(DataReader reader) - { - object value; - - 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 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 deleted file mode 100644 index f2ee4f8..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/ObjectTableMapper.cs +++ /dev/null @@ -1,159 +0,0 @@ -/* -* 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; - -namespace TribalMedia.Framework.Data -{ - public abstract class ObjectTableMapper : TableMapper - { - public ObjectTableMapper(DatabaseMapper database, string tableName) - : base(database, 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( CreateReader(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/RowMapper.cs b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs deleted file mode 100644 index 2771ec9..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/RowMapper.cs +++ /dev/null @@ -1,85 +0,0 @@ -/* -* 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 deleted file mode 100644 index c6bf5d0..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/Schema.cs +++ /dev/null @@ -1,89 +0,0 @@ -/* -* 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 deleted file mode 100644 index a2b0aa1..0000000 --- a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TableMapper.cs +++ /dev/null @@ -1,118 +0,0 @@ -/* -* 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_database; - private readonly object m_syncRoot = new object(); - - protected void WithConnection(Action action) - { - lock (m_syncRoot) - { - DbConnection m_connection = m_database.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; } - } - - protected Schema m_schema; - public Schema Schema - { - get { return m_schema; } - } - - protected FieldMapper m_keyFieldMapper; - public FieldMapper KeyFieldMapper - { - get { return m_keyFieldMapper; } - } - - public TableMapper(DatabaseMapper database, string tableName) - { - m_database = database; - m_tableName = tableName.ToLower(); // Stupid MySQL hack. - } - - public string CreateParamName(string fieldName) - { - return m_database.CreateParamName(fieldName); - } - - protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) - { - return m_database.CreateSelectCommand(this, connection, fieldName, primaryKey); - } - - public string CreateCondition(DbCommand command, string fieldName, object key) - { - return m_database.CreateCondition(this, command, fieldName, key); - } - - public DbCommand CreateInsertCommand(DbConnection connection, object obj) - { - return m_database.CreateInsertCommand(this, connection, obj); - } - - public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) - { - return m_database.CreateUpdateCommand(this, connection, rowMapper, primaryKey); - } - - public object ConvertToDbType(object value) - { - return m_database.ConvertToDbType(value); - } - - protected virtual DataReader CreateReader(IDataReader reader) - { - return new DataReader(reader); - } - } -} \ No newline at end of file diff --git a/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TribalMedia.Framework.Data.snk b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TribalMedia.Framework.Data.snk new file mode 100644 index 0000000..fc71027 Binary files /dev/null and b/ThirdParty/TribalMedia/TribalMedia.Framework.Data/TribalMedia.Framework.Data.snk differ -- cgit v1.1