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