* Renamed a bunch of Data baseclasses for clarity and readability

(Slowly getting there)
ThreadPoolClientBranch
lbsa71 2008-01-15 20:07:02 +00:00
parent 0a783e4442
commit f5103b98be
12 changed files with 199 additions and 232 deletions

View File

@ -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)
{

View File

@ -9,7 +9,7 @@ namespace OpenSim.Framework.Data
{
public class OpenSimObjectFieldMapper<TObject, TField> : ObjectField<TObject, TField>
{
public OpenSimObjectFieldMapper(TableMapper tableMapper, string fieldName,
public OpenSimObjectFieldMapper(BaseTableMapper tableMapper, string fieldName,
ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
: base(tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor)
@ -40,7 +40,7 @@ namespace OpenSim.Framework.Data
}
else
{
base.ExpandField<TObj>(obj, command, fieldNames);
base.ExpandField(obj, command, fieldNames);
}
}

View File

@ -3,9 +3,9 @@ using TribalMedia.Framework.Data;
namespace OpenSim.Framework.Data
{
public abstract class OpenSimTableMapper<TRowMapper, TPrimaryKey> : ObjectTableMapper<TRowMapper, TPrimaryKey>
public abstract class OpenSimTableMapper<TRowMapper, TPrimaryKey> : BaseTableMapper<TRowMapper, TPrimaryKey>
{
public OpenSimTableMapper(DatabaseMapper database, string tableName) : base(database, tableName)
public OpenSimTableMapper(BaseDatabaseConnector database, string tableName) : base(database, tableName)
{
}

View File

@ -16,10 +16,10 @@ namespace OpenSim.Framework.Data
public class PrimitiveBaseShapeTableMapper : OpenSimTableMapper<PrimitiveBaseShapeRowMapper, Guid>
{
public PrimitiveBaseShapeTableMapper(DatabaseMapper connection, string tableName)
public PrimitiveBaseShapeTableMapper(BaseDatabaseConnector connection, string tableName)
: base(connection, tableName)
{
RowMapperSchema<PrimitiveBaseShapeRowMapper> rowMapperSchema = new RowMapperSchema<PrimitiveBaseShapeRowMapper>(this);
ObjectSchema<PrimitiveBaseShapeRowMapper> rowMapperSchema = new ObjectSchema<PrimitiveBaseShapeRowMapper>(this);
m_schema = rowMapperSchema;
m_keyFieldMapper = rowMapperSchema.AddMapping<Guid>("SceneObjectPartId",

View File

@ -46,19 +46,19 @@ namespace TribalMedia.Framework.Data
public ushort GetUShort(string name)
{
return (ushort) m_source.GetInt32(m_source.GetOrdinal(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);
byte value = (byte)m_source.GetInt16(ordinal);
return value;
}
public sbyte GetSByte(string name)
{
return (sbyte) m_source.GetInt16(m_source.GetOrdinal(name));
return (sbyte)m_source.GetInt16(m_source.GetOrdinal(name));
}
public float GetFloat(string name)
@ -84,7 +84,7 @@ namespace TribalMedia.Framework.Data
int bytesRead;
do
{
bytesRead = (int) m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
bytesRead = (int)m_source.GetBytes(ordinal, totalRead, buffer, 0, buffer.Length);
totalRead += bytesRead;
memStream.Write(buffer, 0, bytesRead);
@ -103,7 +103,7 @@ namespace TribalMedia.Framework.Data
return null;
}
return (string) value;
return (string)value;
}
public bool Read()

View File

@ -31,11 +31,11 @@ using System.Data.Common;
namespace TribalMedia.Framework.Data
{
public abstract class DatabaseMapper
public abstract class BaseDatabaseConnector
{
protected string m_connectionString;
public DatabaseMapper(string connectionString)
public BaseDatabaseConnector(string connectionString)
{
m_connectionString = connectionString;
}
@ -43,7 +43,7 @@ namespace TribalMedia.Framework.Data
public abstract DbConnection GetNewConnection();
public abstract string CreateParamName(string fieldName);
public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key)
public DbCommand CreateSelectCommand(BaseTableMapper mapper, DbConnection connection, string fieldName, object key)
{
string table = mapper.TableName;
@ -60,7 +60,7 @@ namespace TribalMedia.Framework.Data
return command;
}
public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key)
public string CreateCondition(BaseTableMapper mapper, DbCommand command, string fieldName, object key)
{
string keyFieldParamName = mapper.CreateParamName(fieldName);
@ -72,7 +72,7 @@ namespace TribalMedia.Framework.Data
return String.Format("{0}={1}", fieldName, keyFieldParamName);
}
public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
public DbCommand CreateUpdateCommand(BaseTableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
{
string table = mapper.TableName;
@ -80,7 +80,7 @@ namespace TribalMedia.Framework.Data
DbCommand command = connection.CreateCommand();
foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
{
if (fieldMapper != mapper.KeyFieldMapper)
{
@ -104,7 +104,7 @@ namespace TribalMedia.Framework.Data
return command;
}
public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj)
public DbCommand CreateInsertCommand(BaseTableMapper mapper, DbConnection connection, object obj)
{
string table = mapper.TableName;
@ -112,7 +112,7 @@ namespace TribalMedia.Framework.Data
DbCommand command = connection.CreateCommand();
foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
foreach (BaseFieldMapper fieldMapper in mapper.Schema.Fields.Values)
{
fieldMapper.ExpandField(obj, command, fieldNames);
}

View File

@ -30,17 +30,15 @@ using System.Data.Common;
namespace TribalMedia.Framework.Data
{
public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper);
public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value);
//public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper);
//public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value);
public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
public abstract class FieldMapper
public abstract class BaseFieldMapper
{
private readonly TableMapper m_tableMapper;
private readonly BaseTableMapper m_tableMapper;
private readonly string m_fieldName;
public string FieldName
@ -57,7 +55,7 @@ namespace TribalMedia.Framework.Data
public abstract object GetParamValue(object obj);
public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType)
public BaseFieldMapper(BaseTableMapper tableMapper, string fieldName, Type valueType)
{
m_fieldName = fieldName;
m_valueType = valueType;
@ -94,32 +92,32 @@ namespace TribalMedia.Framework.Data
{
value = reader.GetGuid(m_fieldName);
}
else if (ValueType == typeof (bool))
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 (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)
{
@ -130,51 +128,51 @@ namespace TribalMedia.Framework.Data
}
}
public class RowMapperField<TRowMapper, TField> : FieldMapper
where TRowMapper : RowMapper
{
private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
//public class RowMapperField<TRowMapper, TField> : FieldMapper
// where TRowMapper : RowMapper
//{
// private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
// private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
public override object GetParamValue(object obj)
{
return m_fieldGetAccessor((TRowMapper) obj);
}
// public override object GetParamValue(object obj)
// {
// return m_fieldGetAccessor((TRowMapper) obj);
// }
public override void SetPropertyFromReader(object mapper, DataReader reader)
{
object value;
// public override void SetPropertyFromReader(object mapper, DataReader reader)
// {
// object value;
value = GetValue(reader);
// value = GetValue(reader);
if (value == null)
{
m_fieldSetAccessor((TRowMapper) mapper, default(TField));
}
else
{
m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
}
}
// if (value == null)
// {
// m_fieldSetAccessor((TRowMapper) mapper, default(TField));
// }
// else
// {
// m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
// }
// }
public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
: base(tableMapper, fieldName, typeof(TField))
{
m_fieldGetAccessor = rowMapperGetAccessor;
m_fieldSetAccessor = rowMapperSetAccessor;
}
}
// public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
// RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
// : base(tableMapper, fieldName, typeof(TField))
// {
// m_fieldGetAccessor = rowMapperGetAccessor;
// m_fieldSetAccessor = rowMapperSetAccessor;
// }
//}
public class ObjectField<TObject, TField> : FieldMapper
public class ObjectField<TObject, TField> : BaseFieldMapper
{
private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
public override object GetParamValue(object obj)
{
return m_fieldGetAccessor((TObject) obj);
return m_fieldGetAccessor((TObject)obj);
}
public override void SetPropertyFromReader(object obj, DataReader reader)
@ -185,18 +183,18 @@ namespace TribalMedia.Framework.Data
if (value == null)
{
m_fieldSetAccessor((TObject) obj, default(TField));
m_fieldSetAccessor((TObject)obj, default(TField));
}
else
{
m_fieldSetAccessor((TObject) obj, (TField) value);
m_fieldSetAccessor((TObject)obj, (TField)value);
}
}
public ObjectField(TableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
public ObjectField(BaseTableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
: base(tableMapper, fieldName, typeof (TField))
: base(tableMapper, fieldName, typeof(TField))
{
m_fieldGetAccessor = rowMapperGetAccessor;
m_fieldSetAccessor = rowMapperSetAccessor;

View File

@ -51,7 +51,7 @@ namespace TribalMedia.Framework.Data
public override void FillObject(DataReader reader)
{
foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
foreach (BaseFieldMapper fieldMapper in m_schema.Fields.Values)
{
fieldMapper.SetPropertyFromReader(m_obj, reader);
}
@ -76,7 +76,7 @@ namespace TribalMedia.Framework.Data
public override void FillObject(DataReader reader)
{
foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
foreach (BaseFieldMapper fieldMapper in m_schema.Fields.Values)
{
fieldMapper.SetPropertyFromReader(this, reader);
}

View File

@ -31,24 +31,25 @@ namespace TribalMedia.Framework.Data
{
public class Schema
{
protected TableMapper m_tableMapper;
protected Dictionary<string, FieldMapper> m_mappings;
protected BaseTableMapper m_tableMapper;
protected Dictionary<string, BaseFieldMapper> m_mappings;
public Dictionary<string, FieldMapper> Fields
public Dictionary<string, BaseFieldMapper> Fields
{
get { return m_mappings; }
}
public Schema(TableMapper tableMapper)
public Schema(BaseTableMapper tableMapper)
{
m_mappings = new Dictionary<string, FieldMapper>();
m_mappings = new Dictionary<string, BaseFieldMapper>();
m_tableMapper = tableMapper;
}
}
public class ObjectSchema<TObj> : Schema
{
public ObjectSchema(TableMapper tableMapper) : base(tableMapper)
public ObjectSchema(BaseTableMapper tableMapper)
: base(tableMapper)
{
}
@ -65,25 +66,25 @@ namespace TribalMedia.Framework.Data
}
}
public class RowMapperSchema<TRowMapper> : Schema
where TRowMapper : RowMapper
{
public RowMapperSchema(TableMapper tableMapper) : base(tableMapper)
{
}
//public class RowMapperSchema<TRowMapper> : Schema
// where TRowMapper : RowMapper
//{
// public RowMapperSchema(TableMapper tableMapper) : base(tableMapper)
// {
// }
public RowMapperField<TRowMapper, TField> AddMapping<TField>(string fieldName,
RowMapperGetAccessor<TRowMapper, TField>
rowMapperGetAccessor,
RowMapperSetAccessor<TRowMapper, TField>
rowMapperSetAccessor)
{
RowMapperField<TRowMapper, TField> rowMapperField =
new RowMapperField<TRowMapper, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor);
// public RowMapperField<TRowMapper, TField> AddMapping<TField>(string fieldName,
// RowMapperGetAccessor<TRowMapper, TField>
// rowMapperGetAccessor,
// RowMapperSetAccessor<TRowMapper, TField>
// rowMapperSetAccessor)
// {
// RowMapperField<TRowMapper, TField> rowMapperField =
// new RowMapperField<TRowMapper, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor);
m_mappings.Add(fieldName, rowMapperField);
// m_mappings.Add(fieldName, rowMapperField);
return rowMapperField;
}
}
// return rowMapperField;
// }
//}
}

View File

@ -27,12 +27,98 @@
using System;
using System.Data;
using System.Data.Common;
using TribalMedia.Framework.Data;
namespace TribalMedia.Framework.Data
{
public abstract class ObjectTableMapper<TRowMapper, TPrimaryKey> : TableMapper
public abstract class BaseTableMapper
{
public ObjectTableMapper(DatabaseMapper database, string tableName)
private readonly BaseDatabaseConnector m_database;
private readonly object m_syncRoot = new object();
protected void WithConnection(Action<DbConnection> 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<TRowMapper, TPrimaryKey> : BaseTableMapper
{
public BaseTableMapper(BaseDatabaseConnector database, string tableName)
: base(database, tableName)
{
}
@ -52,7 +138,7 @@ namespace TribalMedia.Framework.Data
{
if (reader.Read())
{
result = FromReader( CreateReader(reader));
result = FromReader(CreateReader(reader));
success = true;
}
else

View File

@ -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<DbConnection> 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);
}
}
}