From c52c68f314c67c76c7181a6d0828f476290fbd66 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Wed, 2 Apr 2008 15:24:31 +0000 Subject: whole lot more moving --- OpenSim/Data/Base/BaseDataReader.cs | 139 +++++++++++ OpenSim/Data/Base/BaseDatabaseConnector.cs | 142 +++++++++++ OpenSim/Data/Base/BaseFieldMapper.cs | 168 +++++++++++++ OpenSim/Data/Base/BaseRowMapper.cs | 61 +++++ OpenSim/Data/Base/BaseSchema.cs | 69 ++++++ OpenSim/Data/Base/BaseTableMapper.cs | 281 ++++++++++++++++++++++ OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk | Bin 0 -> 596 bytes OpenSim/Data/Base/Properties/AssemblyInfo.cs | 67 ++++++ 8 files changed, 927 insertions(+) create mode 100644 OpenSim/Data/Base/BaseDataReader.cs create mode 100644 OpenSim/Data/Base/BaseDatabaseConnector.cs create mode 100644 OpenSim/Data/Base/BaseFieldMapper.cs create mode 100644 OpenSim/Data/Base/BaseRowMapper.cs create mode 100644 OpenSim/Data/Base/BaseSchema.cs create mode 100644 OpenSim/Data/Base/BaseTableMapper.cs create mode 100644 OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk create mode 100644 OpenSim/Data/Base/Properties/AssemblyInfo.cs (limited to 'OpenSim/Data/Base') diff --git a/OpenSim/Data/Base/BaseDataReader.cs b/OpenSim/Data/Base/BaseDataReader.cs new file mode 100644 index 0000000..3baefcd --- /dev/null +++ b/OpenSim/Data/Base/BaseDataReader.cs @@ -0,0 +1,139 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base +{ + public abstract class BaseDataReader + { + private readonly IDataReader m_source; + + public BaseDataReader(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 virtual Guid GetGuid(string name) + { + return m_source.GetGuid(m_source.GetOrdinal(name)); + } + + public UInt32 GetUInt32(string name ) + { + return (UInt32)GetInt32(name); + } + + private Int32 GetInt32(string name) + { + int ordinal = m_source.GetOrdinal(name); + int int32 = m_source.GetInt32(ordinal); + return int32; + } + + public Int64 GetInt64(string name) + { + int ordinal = m_source.GetOrdinal( name ); + long int64 = m_source.GetInt64(ordinal); + return int64; + } + } +} diff --git a/OpenSim/Data/Base/BaseDatabaseConnector.cs b/OpenSim/Data/Base/BaseDatabaseConnector.cs new file mode 100644 index 0000000..fa3a6c3 --- /dev/null +++ b/OpenSim/Data/Base/BaseDatabaseConnector.cs @@ -0,0 +1,142 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base +{ + 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; + } + + public abstract BaseDataReader CreateReader(IDataReader reader); + } +} diff --git a/OpenSim/Data/Base/BaseFieldMapper.cs b/OpenSim/Data/Base/BaseFieldMapper.cs new file mode 100644 index 0000000..03c7bfb --- /dev/null +++ b/OpenSim/Data/Base/BaseFieldMapper.cs @@ -0,0 +1,168 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base +{ + 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, BaseDataReader 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(BaseDataReader 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(uint)) + { + value = reader.GetUInt32(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 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, BaseDataReader 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; + } + } +} diff --git a/OpenSim/Data/Base/BaseRowMapper.cs b/OpenSim/Data/Base/BaseRowMapper.cs new file mode 100644 index 0000000..b008b86 --- /dev/null +++ b/OpenSim/Data/Base/BaseRowMapper.cs @@ -0,0 +1,61 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base; + +namespace OpenSim.Framework.Data.Base +{ + public abstract class BaseRowMapper + { + public abstract void FillObject(BaseDataReader reader); + } + + public class BaseRowMapper : BaseRowMapper + { + private readonly BaseSchema m_schema; + private readonly TObj m_obj; + + public TObj Object + { + get { return m_obj; } + } + + public BaseRowMapper(BaseSchema schema, TObj obj) + { + m_schema = schema; + m_obj = obj; + } + + public override void FillObject(BaseDataReader reader) + { + foreach (BaseFieldMapper fieldMapper in m_schema.Fields.Values) + { + fieldMapper.SetPropertyFromReader(this, reader); + } + } + } +} diff --git a/OpenSim/Data/Base/BaseSchema.cs b/OpenSim/Data/Base/BaseSchema.cs new file mode 100644 index 0000000..8a7ee71 --- /dev/null +++ b/OpenSim/Data/Base/BaseSchema.cs @@ -0,0 +1,69 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base; + +namespace OpenSim.Framework.Data.Base +{ + public class BaseSchema + { + protected BaseTableMapper m_tableMapper; + protected Dictionary m_mappings; + + public Dictionary Fields + { + get { return m_mappings; } + } + + public BaseSchema(BaseTableMapper tableMapper) + { + m_mappings = new Dictionary(); + m_tableMapper = tableMapper; + } + } + + public class BaseSchema : BaseSchema + { + public BaseSchema(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; + } + } +} diff --git a/OpenSim/Data/Base/BaseTableMapper.cs b/OpenSim/Data/Base/BaseTableMapper.cs new file mode 100644 index 0000000..cad4823 --- /dev/null +++ b/OpenSim/Data/Base/BaseTableMapper.cs @@ -0,0 +1,281 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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 OpenSim.Framework.Data.Base; + +namespace OpenSim.Framework.Data.Base +{ + 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 BaseSchema m_schema; + public BaseSchema 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 BaseDataReader CreateReader(IDataReader reader) + { + return m_database.CreateReader(reader); + } + } + + public abstract class BaseTableMapper : BaseTableMapper + { + public BaseTableMapper(BaseDatabaseConnector database, string tableName) + : base(database, tableName) + { + } + + // HACK: This is a temporary function used by TryGetValue(). + // Due to a bug in mono 1.2.6, delegate blocks cannot contain + // a using() block. This has been fixed in SVN, so the next + // mono release should work. + private void TryGetConnectionValue(DbConnection connection, TPrimaryKey primaryKey, ref TRowMapper result, ref bool success) + { + 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; + } + } + } + } + + public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) + { + TRowMapper result = default(TRowMapper); + bool success = false; + + WithConnection(delegate(DbConnection connection) + { + TryGetConnectionValue(connection, primaryKey, ref result, ref success); + }); + + value = result; + + return success; + } + + // HACK: This is a temporary function used by Remove(). + // Due to a bug in mono 1.2.6, delegate blocks cannot contain + // a using() block. This has been fixed in SVN, so the next + // mono release should work. + protected virtual void TryDelete(DbConnection connection, TPrimaryKey id, ref int deleted) + { + using ( + DbCommand command = + CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) + { + deleted = command.ExecuteNonQuery(); + } + } + + public virtual bool Remove(TPrimaryKey id) + { + int deleted = 0; + + WithConnection(delegate(DbConnection connection) + { + TryDelete(connection, id, ref deleted); + }); + + 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; + } + + // HACK: This is a temporary function used by Update(). + // Due to a bug in mono 1.2.6, delegate blocks cannot contain + // a using() block. This has been fixed in SVN, so the next + // mono release should work. + protected void TryUpdate(DbConnection connection, TPrimaryKey primaryKey, TRowMapper value, ref int updated) + { + using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) + { + updated = command.ExecuteNonQuery(); + } + } + + public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) + { + int updated = 0; + + WithConnection(delegate(DbConnection connection) + { + TryUpdate(connection, primaryKey, value, ref updated); + }); + + if (updated == 1) + { + return true; + } + else + { + return false; + } + } + + // HACK: This is a temporary function used by Add(). + // Due to a bug in mono 1.2.6, delegate blocks cannot contain + // a using() block. This has been fixed in SVN, so the next + // mono release should work. + protected void TryAdd(DbConnection connection, TRowMapper value, ref int added) + { + using (DbCommand command = CreateInsertCommand(connection, value)) + { + added = command.ExecuteNonQuery(); + } + } + + public virtual bool Add(TRowMapper value) + { + int added = 0; + + WithConnection(delegate(DbConnection connection) + { + TryAdd(connection, value, ref added); + }); + + if (added == 1) + { + return true; + } + else + { + return false; + } + } + + public abstract TRowMapper FromReader(BaseDataReader reader); + } +} diff --git a/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk b/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk new file mode 100644 index 0000000..fc71027 Binary files /dev/null and b/OpenSim/Data/Base/OpenSim.Framework.Data.Base.snk differ diff --git a/OpenSim/Data/Base/Properties/AssemblyInfo.cs b/OpenSim/Data/Base/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..ab97490 --- /dev/null +++ b/OpenSim/Data/Base/Properties/AssemblyInfo.cs @@ -0,0 +1,67 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * 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. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may 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.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("OpenSim.Framework.Data.Base")] +[assembly : AssemblyDescription("Generic Database Abstraction Layer")] +[assembly : AssemblyConfiguration("")] +[assembly : AssemblyCompany("OpenSim Project (www.opensimulator.org)")] +[assembly: AssemblyProduct("OpenSim.Framework.Data.Base")] +[assembly: AssemblyCopyright("Copyright (c) 2007 OpenSim Project (www.opensimulator.org)")] +[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] -- cgit v1.1