Set svn:eol-style.

ThreadPoolClientBranch
Jeff Ames 2008-01-14 16:47:36 +00:00
parent c9ad862aab
commit 6716668187
8 changed files with 1010 additions and 1010 deletions

View File

@ -1,150 +1,150 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System; using System;
using System.Data; using System.Data;
using System.IO; using System.IO;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public class DataReader public class DataReader
{ {
private readonly IDataReader m_source; private readonly IDataReader m_source;
public DataReader(IDataReader source) public DataReader(IDataReader source)
{ {
m_source = source; m_source = source;
} }
public object Get(string name) public object Get(string name)
{ {
return m_source[name]; return m_source[name];
} }
public ushort GetUShort(string name) 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) public byte GetByte(string name)
{ {
int ordinal = m_source.GetOrdinal(name); int ordinal = m_source.GetOrdinal(name);
byte value = (byte) m_source.GetInt16(ordinal); byte value = (byte) m_source.GetInt16(ordinal);
return value; return value;
} }
public sbyte GetSByte(string name) public sbyte GetSByte(string name)
{ {
return (sbyte) m_source.GetInt16(m_source.GetOrdinal(name)); return (sbyte) m_source.GetInt16(m_source.GetOrdinal(name));
} }
//public Vector3 GetVector(string s) //public Vector3 GetVector(string s)
//{ //{
// float x = GetFloat(s + "X"); // float x = GetFloat(s + "X");
// float y = GetFloat(s + "Y"); // float y = GetFloat(s + "Y");
// float z = GetFloat(s + "Z"); // float z = GetFloat(s + "Z");
// Vector3 vector = new Vector3(x, y, z); // Vector3 vector = new Vector3(x, y, z);
// return vector; // return vector;
//} //}
//public Quaternion GetQuaternion(string s) //public Quaternion GetQuaternion(string s)
//{ //{
// float x = GetFloat(s + "X"); // float x = GetFloat(s + "X");
// float y = GetFloat(s + "Y"); // float y = GetFloat(s + "Y");
// float z = GetFloat(s + "Z"); // float z = GetFloat(s + "Z");
// float w = GetFloat(s + "W"); // float w = GetFloat(s + "W");
// Quaternion quaternion = new Quaternion(x, y, z, w); // Quaternion quaternion = new Quaternion(x, y, z, w);
// return quaternion; // return quaternion;
//} //}
public float GetFloat(string name) public float GetFloat(string name)
{ {
return m_source.GetFloat(m_source.GetOrdinal(name)); return m_source.GetFloat(m_source.GetOrdinal(name));
} }
public byte[] GetBytes(string name) public byte[] GetBytes(string name)
{ {
int ordinal = m_source.GetOrdinal(name); int ordinal = m_source.GetOrdinal(name);
if (m_source.GetValue(ordinal) == DBNull.Value) if (m_source.GetValue(ordinal) == DBNull.Value)
{ {
return null; return null;
} }
byte[] buffer = new byte[16384]; byte[] buffer = new byte[16384];
MemoryStream memStream = new MemoryStream(); MemoryStream memStream = new MemoryStream();
long totalRead = 0; long totalRead = 0;
int bytesRead; int bytesRead;
do 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; totalRead += bytesRead;
memStream.Write(buffer, 0, bytesRead); memStream.Write(buffer, 0, bytesRead);
} while (bytesRead == buffer.Length); } while (bytesRead == buffer.Length);
return memStream.ToArray(); return memStream.ToArray();
} }
public string GetString(string name) public string GetString(string name)
{ {
int ordinal = m_source.GetOrdinal(name); int ordinal = m_source.GetOrdinal(name);
object value = m_source.GetValue(ordinal); object value = m_source.GetValue(ordinal);
if (value is DBNull) if (value is DBNull)
{ {
return null; return null;
} }
return (string) value; return (string) value;
} }
public bool Read() public bool Read()
{ {
return m_source.Read(); return m_source.Read();
} }
internal Guid GetGuid(string name) internal Guid GetGuid(string name)
{ {
string guidString = GetString(name); string guidString = GetString(name);
if (String.IsNullOrEmpty(guidString)) if (String.IsNullOrEmpty(guidString))
{ {
return Guid.Empty; return Guid.Empty;
} }
else else
{ {
return new Guid(guidString); return new Guid(guidString);
} }
} }
} }
} }

View File

@ -1,135 +1,135 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public abstract class DatabaseMapper public abstract class DatabaseMapper
{ {
protected string m_connectionString; protected string m_connectionString;
public DatabaseMapper(string connectionString) public DatabaseMapper(string connectionString)
{ {
m_connectionString = connectionString; m_connectionString = connectionString;
} }
public abstract DbConnection GetNewConnection(); public abstract DbConnection GetNewConnection();
public abstract string CreateParamName(string fieldName); public abstract string CreateParamName(string fieldName);
public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key) public DbCommand CreateSelectCommand(TableMapper mapper, DbConnection connection, string fieldName, object key)
{ {
string table = mapper.TableName; string table = mapper.TableName;
DbCommand command = connection.CreateCommand(); DbCommand command = connection.CreateCommand();
string conditionString = CreateCondition(mapper, command, fieldName, key); string conditionString = CreateCondition(mapper, command, fieldName, key);
string query = string query =
String.Format("select * from {0} where {1}", table, conditionString); String.Format("select * from {0} where {1}", table, conditionString);
command.CommandText = query; command.CommandText = query;
command.CommandType = CommandType.Text; command.CommandType = CommandType.Text;
return command; return command;
} }
public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key) public string CreateCondition(TableMapper mapper, DbCommand command, string fieldName, object key)
{ {
string keyFieldParamName = mapper.CreateParamName(fieldName); string keyFieldParamName = mapper.CreateParamName(fieldName);
DbParameter param = command.CreateParameter(); DbParameter param = command.CreateParameter();
param.ParameterName = keyFieldParamName; param.ParameterName = keyFieldParamName;
param.Value = FieldMapper.ConvertToDbType(key); param.Value = FieldMapper.ConvertToDbType(key);
command.Parameters.Add(param); command.Parameters.Add(param);
return String.Format("{0}={1}", fieldName, keyFieldParamName); return String.Format("{0}={1}", fieldName, keyFieldParamName);
} }
public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey) public DbCommand CreateUpdateCommand(TableMapper mapper, DbConnection connection, object rowMapper, object primaryKey)
{ {
string table = mapper.TableName; string table = mapper.TableName;
List<string> fieldNames = new List<string>(); List<string> fieldNames = new List<string>();
DbCommand command = connection.CreateCommand(); DbCommand command = connection.CreateCommand();
foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
{ {
if (fieldMapper != mapper.KeyFieldMapper) if (fieldMapper != mapper.KeyFieldMapper)
{ {
fieldMapper.ExpandField(rowMapper, command, fieldNames); fieldMapper.ExpandField(rowMapper, command, fieldNames);
} }
} }
List<string> assignments = new List<string>(); List<string> assignments = new List<string>();
foreach (string field in fieldNames) foreach (string field in fieldNames)
{ {
assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field))); assignments.Add(String.Format("{0}={1}", field, mapper.CreateParamName(field)));
} }
string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey); string conditionString = mapper.CreateCondition(command, mapper.KeyFieldMapper.FieldName, primaryKey);
command.CommandText = command.CommandText =
String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()), String.Format("update {0} set {1} where {2}", table, String.Join(", ", assignments.ToArray()),
conditionString); conditionString);
return command; return command;
} }
public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj) public DbCommand CreateInsertCommand(TableMapper mapper, DbConnection connection, object obj)
{ {
string table = mapper.TableName; string table = mapper.TableName;
List<string> fieldNames = new List<string>(); List<string> fieldNames = new List<string>();
DbCommand command = connection.CreateCommand(); DbCommand command = connection.CreateCommand();
foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values) foreach (FieldMapper fieldMapper in mapper.Schema.Fields.Values)
{ {
fieldMapper.ExpandField(obj, command, fieldNames); fieldMapper.ExpandField(obj, command, fieldNames);
} }
List<string> paramNames = new List<string>(); List<string> paramNames = new List<string>();
foreach (string field in fieldNames) foreach (string field in fieldNames)
{ {
paramNames.Add(mapper.CreateParamName(field)); paramNames.Add(mapper.CreateParamName(field));
} }
command.CommandText = command.CommandText =
String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()), String.Format("insert into {0} ({1}) values ({2})", table, String.Join(", ", fieldNames.ToArray()),
String.Join(", ", paramNames.ToArray())); String.Join(", ", paramNames.ToArray()));
return command; return command;
} }
} }
} }

View File

@ -1,249 +1,249 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.Common; using System.Data.Common;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper); public delegate TField RowMapperGetAccessor<TRowMapper, TField>(TRowMapper rowMapper);
public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value); public delegate void RowMapperSetAccessor<TRowMapper, TField>(TRowMapper rowMapper, TField value);
public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj); public delegate TField ObjectGetAccessor<TObj, TField>(TObj obj);
public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value); public delegate void ObjectSetAccessor<TObj, TField>(TObj obj, TField value);
public abstract class FieldMapper public abstract class FieldMapper
{ {
private readonly TableMapper m_tableMapper; private readonly TableMapper m_tableMapper;
private readonly string m_fieldName; private readonly string m_fieldName;
public string FieldName public string FieldName
{ {
get { return m_fieldName; } get { return m_fieldName; }
} }
protected Type m_valueType; protected Type m_valueType;
public Type ValueType public Type ValueType
{ {
get { return m_valueType; } get { return m_valueType; }
} }
public abstract object GetParamValue(object obj); public abstract object GetParamValue(object obj);
public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType) public FieldMapper( TableMapper tableMapper, string fieldName, Type valueType)
{ {
m_fieldName = fieldName; m_fieldName = fieldName;
m_valueType = valueType; m_valueType = valueType;
m_tableMapper = tableMapper; m_tableMapper = tableMapper;
} }
public abstract void SetPropertyFromReader(object mapper, DataReader reader); public abstract void SetPropertyFromReader(object mapper, DataReader reader);
public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value) public void RawAddParam(DbCommand command, List<string> fieldNames, string fieldName, object value)
{ {
string paramName = m_tableMapper.CreateParamName(fieldName); string paramName = m_tableMapper.CreateParamName(fieldName);
fieldNames.Add(fieldName); fieldNames.Add(fieldName);
DbParameter param = command.CreateParameter(); DbParameter param = command.CreateParameter();
param.ParameterName = paramName; param.ParameterName = paramName;
param.Value = value; param.Value = value;
command.Parameters.Add(param); command.Parameters.Add(param);
} }
public void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames) public void ExpandField<TObj>(TObj obj, DbCommand command, List<string> fieldNames)
{ {
string fieldName = FieldName; string fieldName = FieldName;
object value = GetParamValue(obj); object value = GetParamValue(obj);
//if (ValueType == typeof (Vector3)) //if (ValueType == typeof (Vector3))
//{ //{
// Vector3 vector = (Vector3) value; // Vector3 vector = (Vector3) value;
// RawAddParam(command, fieldNames, fieldName + "X", vector.X); // RawAddParam(command, fieldNames, fieldName + "X", vector.X);
// RawAddParam(command, fieldNames, fieldName + "Y", vector.Y); // RawAddParam(command, fieldNames, fieldName + "Y", vector.Y);
// RawAddParam(command, fieldNames, fieldName + "Z", vector.Z); // RawAddParam(command, fieldNames, fieldName + "Z", vector.Z);
//} //}
//else if (ValueType == typeof (Quaternion)) //else if (ValueType == typeof (Quaternion))
//{ //{
// Quaternion quaternion = (Quaternion) value; // Quaternion quaternion = (Quaternion) value;
// RawAddParam(command, fieldNames, fieldName + "X", quaternion.X); // RawAddParam(command, fieldNames, fieldName + "X", quaternion.X);
// RawAddParam(command, fieldNames, fieldName + "Y", quaternion.Y); // RawAddParam(command, fieldNames, fieldName + "Y", quaternion.Y);
// RawAddParam(command, fieldNames, fieldName + "Z", quaternion.Z); // RawAddParam(command, fieldNames, fieldName + "Z", quaternion.Z);
// RawAddParam(command, fieldNames, fieldName + "W", quaternion.W); // RawAddParam(command, fieldNames, fieldName + "W", quaternion.W);
//} //}
//else //else
//{ //{
RawAddParam(command, fieldNames, fieldName, ConvertToDbType(value)); RawAddParam(command, fieldNames, fieldName, ConvertToDbType(value));
//} //}
} }
protected object GetValue(DataReader reader) protected object GetValue(DataReader reader)
{ {
object value; object value;
//if (ValueType == typeof (Vector3)) //if (ValueType == typeof (Vector3))
//{ //{
// value = reader.GetVector(m_fieldName); // value = reader.GetVector(m_fieldName);
//} //}
//else if (ValueType == typeof (Quaternion)) //else if (ValueType == typeof (Quaternion))
//{ //{
// value = reader.GetQuaternion(m_fieldName); // value = reader.GetQuaternion(m_fieldName);
//} //}
//else //else
//if (ValueType == typeof(UID)) //if (ValueType == typeof(UID))
//{ //{
// Guid guid = reader.GetGuid(m_fieldName); // Guid guid = reader.GetGuid(m_fieldName);
// value = new UID(guid); // value = new UID(guid);
//} //}
//else //else
if (ValueType == typeof(Guid)) if (ValueType == typeof(Guid))
{ {
value = reader.GetGuid(m_fieldName); value = reader.GetGuid(m_fieldName);
} }
else if (ValueType == typeof (bool)) else if (ValueType == typeof (bool))
{ {
uint boolVal = reader.GetUShort(m_fieldName); uint boolVal = reader.GetUShort(m_fieldName);
value = (boolVal == 1); value = (boolVal == 1);
} }
else else
if (ValueType == typeof (byte)) if (ValueType == typeof (byte))
{ {
value = reader.GetByte(m_fieldName); value = reader.GetByte(m_fieldName);
} }
else if (ValueType == typeof (sbyte)) else if (ValueType == typeof (sbyte))
{ {
value = reader.GetSByte(m_fieldName); value = reader.GetSByte(m_fieldName);
} }
else if (ValueType == typeof (ushort)) else if (ValueType == typeof (ushort))
{ {
value = reader.GetUShort(m_fieldName); value = reader.GetUShort(m_fieldName);
} }
else if (ValueType == typeof (byte[])) else if (ValueType == typeof (byte[]))
{ {
value = reader.GetBytes(m_fieldName); value = reader.GetBytes(m_fieldName);
} }
else else
{ {
value = reader.Get(m_fieldName); value = reader.Get(m_fieldName);
} }
if (value is DBNull) if (value is DBNull)
{ {
value = default(ValueType); value = default(ValueType);
} }
return value; return value;
} }
public static object ConvertToDbType(object value) public static object ConvertToDbType(object value)
{ {
//if (value is UID) //if (value is UID)
//{ //{
// return (value as UID).UUID.ToString(); // return (value as UID).UUID.ToString();
//} //}
return value; return value;
} }
} }
public class RowMapperField<TRowMapper, TField> : FieldMapper public class RowMapperField<TRowMapper, TField> : FieldMapper
where TRowMapper : RowMapper where TRowMapper : RowMapper
{ {
private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor; private readonly RowMapperGetAccessor<TRowMapper, TField> m_fieldGetAccessor;
private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor; private readonly RowMapperSetAccessor<TRowMapper, TField> m_fieldSetAccessor;
public override object GetParamValue(object obj) public override object GetParamValue(object obj)
{ {
return m_fieldGetAccessor((TRowMapper) obj); return m_fieldGetAccessor((TRowMapper) obj);
} }
public override void SetPropertyFromReader(object mapper, DataReader reader) public override void SetPropertyFromReader(object mapper, DataReader reader)
{ {
object value; object value;
value = GetValue(reader); value = GetValue(reader);
if (value == null) if (value == null)
{ {
m_fieldSetAccessor((TRowMapper) mapper, default(TField)); m_fieldSetAccessor((TRowMapper) mapper, default(TField));
} }
else else
{ {
m_fieldSetAccessor((TRowMapper) mapper, (TField) value); m_fieldSetAccessor((TRowMapper) mapper, (TField) value);
} }
} }
public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor, public RowMapperField(TableMapper tableMapper, string fieldName, RowMapperGetAccessor<TRowMapper, TField> rowMapperGetAccessor,
RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor) RowMapperSetAccessor<TRowMapper, TField> rowMapperSetAccessor)
: base(tableMapper, fieldName, typeof(TField)) : base(tableMapper, fieldName, typeof(TField))
{ {
m_fieldGetAccessor = rowMapperGetAccessor; m_fieldGetAccessor = rowMapperGetAccessor;
m_fieldSetAccessor = rowMapperSetAccessor; m_fieldSetAccessor = rowMapperSetAccessor;
} }
} }
public class ObjectField<TObject, TField> : FieldMapper public class ObjectField<TObject, TField> : FieldMapper
{ {
private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor; private readonly ObjectGetAccessor<TObject, TField> m_fieldGetAccessor;
private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor; private readonly ObjectSetAccessor<TObject, TField> m_fieldSetAccessor;
public override object GetParamValue(object obj) public override object GetParamValue(object obj)
{ {
return m_fieldGetAccessor((TObject) obj); return m_fieldGetAccessor((TObject) obj);
} }
public override void SetPropertyFromReader(object obj, DataReader reader) public override void SetPropertyFromReader(object obj, DataReader reader)
{ {
object value; object value;
value = GetValue(reader); value = GetValue(reader);
if (value == null) if (value == null)
{ {
m_fieldSetAccessor((TObject) obj, default(TField)); m_fieldSetAccessor((TObject) obj, default(TField));
} }
else 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(TableMapper tableMapper, string fieldName, ObjectGetAccessor<TObject, TField> rowMapperGetAccessor,
ObjectSetAccessor<TObject, TField> rowMapperSetAccessor) ObjectSetAccessor<TObject, TField> rowMapperSetAccessor)
: base(tableMapper, fieldName, typeof (TField)) : base(tableMapper, fieldName, typeof (TField))
{ {
m_fieldGetAccessor = rowMapperGetAccessor; m_fieldGetAccessor = rowMapperGetAccessor;
m_fieldSetAccessor = rowMapperSetAccessor; m_fieldSetAccessor = rowMapperSetAccessor;
} }
} }
} }

View File

@ -1,162 +1,162 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using TribalMedia.Framework.Data; using TribalMedia.Framework.Data;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public abstract class ObjectTableMapper<TRowMapper, TPrimaryKey> : TableMapper public abstract class ObjectTableMapper<TRowMapper, TPrimaryKey> : TableMapper
{ {
public ObjectTableMapper(DatabaseMapper connectionPool, string tableName) public ObjectTableMapper(DatabaseMapper connectionPool, string tableName)
: base(connectionPool, tableName) : base(connectionPool, tableName)
{ {
} }
public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value) public bool TryGetValue(TPrimaryKey primaryKey, out TRowMapper value)
{ {
TRowMapper result = default(TRowMapper); TRowMapper result = default(TRowMapper);
bool success = false; bool success = false;
WithConnection(delegate(DbConnection connection) WithConnection(delegate(DbConnection connection)
{ {
using ( using (
DbCommand command = DbCommand command =
CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey)) CreateSelectCommand(connection, KeyFieldMapper.FieldName, primaryKey))
{ {
using (IDataReader reader = command.ExecuteReader()) using (IDataReader reader = command.ExecuteReader())
{ {
if (reader.Read()) if (reader.Read())
{ {
result = FromReader(new DataReader(reader)); result = FromReader(new DataReader(reader));
success = true; success = true;
} }
else else
{ {
success = false; success = false;
} }
} }
} }
}); });
value = result; value = result;
return success; return success;
} }
public virtual bool Remove(TPrimaryKey id) public virtual bool Remove(TPrimaryKey id)
{ {
int deleted = 0; int deleted = 0;
WithConnection(delegate(DbConnection connection) WithConnection(delegate(DbConnection connection)
{ {
using ( using (
DbCommand command = DbCommand command =
CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id)) CreateDeleteCommand(connection, KeyFieldMapper.FieldName, id))
{ {
deleted = command.ExecuteNonQuery(); deleted = command.ExecuteNonQuery();
} }
}); });
if (deleted == 1) if (deleted == 1)
{ {
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey) public DbCommand CreateDeleteCommand(DbConnection connection, string fieldName, TPrimaryKey primaryKey)
{ {
string table = TableName; string table = TableName;
DbCommand command = connection.CreateCommand(); DbCommand command = connection.CreateCommand();
string conditionString = CreateCondition(command, fieldName, primaryKey); string conditionString = CreateCondition(command, fieldName, primaryKey);
string query = string query =
String.Format("delete from {0} where {1}", table, conditionString); String.Format("delete from {0} where {1}", table, conditionString);
command.CommandText = query; command.CommandText = query;
command.CommandType = CommandType.Text; command.CommandType = CommandType.Text;
return command; return command;
} }
public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value) public virtual bool Update(TPrimaryKey primaryKey, TRowMapper value)
{ {
int updated = 0; int updated = 0;
WithConnection(delegate(DbConnection connection) WithConnection(delegate(DbConnection connection)
{ {
using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey)) using (DbCommand command = CreateUpdateCommand(connection, value, primaryKey))
{ {
updated = command.ExecuteNonQuery(); updated = command.ExecuteNonQuery();
} }
}); });
if (updated == 1) if (updated == 1)
{ {
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
public virtual bool Add(TRowMapper value) public virtual bool Add(TRowMapper value)
{ {
int added = 0; int added = 0;
WithConnection(delegate(DbConnection connection) WithConnection(delegate(DbConnection connection)
{ {
using (DbCommand command = CreateInsertCommand(connection, value)) using (DbCommand command = CreateInsertCommand(connection, value))
{ {
added = command.ExecuteNonQuery(); added = command.ExecuteNonQuery();
} }
}); });
if (added == 1) if (added == 1)
{ {
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
public abstract TRowMapper FromReader(DataReader reader); public abstract TRowMapper FromReader(DataReader reader);
} }
} }

View File

@ -1,40 +1,40 @@
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security; using System.Security;
// General Information about an assembly is controlled through the following // General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information // set of attributes. Change these attribute values to modify the information
// associated with an assembly. // associated with an assembly.
[assembly : AssemblyTitle("TribalMedia.Framework.Data")] [assembly : AssemblyTitle("TribalMedia.Framework.Data")]
[assembly : AssemblyDescription("Generic Database Abstraction Layer")] [assembly : AssemblyDescription("Generic Database Abstraction Layer")]
[assembly : AssemblyConfiguration("")] [assembly : AssemblyConfiguration("")]
[assembly : AssemblyCompany("TribalMedia")] [assembly : AssemblyCompany("TribalMedia")]
[assembly : AssemblyProduct("TribalMedia.Framework.Data")] [assembly : AssemblyProduct("TribalMedia.Framework.Data")]
[assembly: AssemblyCopyright("Copyright © 2007 Tribal Media")] [assembly: AssemblyCopyright("Copyright © 2007 Tribal Media")]
[assembly : AssemblyTrademark("")] [assembly : AssemblyTrademark("")]
[assembly : AssemblyCulture("")] [assembly : AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible // 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 // to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. // COM, set the ComVisible attribute to true on that type.
[assembly : ComVisible(false)] [assembly : ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM // The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly : Guid("9269f421-19d9-4eea-bfe3-c0ffe426fada")] [assembly : Guid("9269f421-19d9-4eea-bfe3-c0ffe426fada")]
// Version information for an assembly consists of the following four values: // Version information for an assembly consists of the following four values:
// //
// Major Version // Major Version
// Minor Version // Minor Version
// Build Number // Build Number
// Revision // Revision
// //
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly : AssemblyVersion("1.0.0.0")] [assembly : AssemblyVersion("1.0.0.0")]
[assembly : AssemblyFileVersion("1.0.0.0")] [assembly : AssemblyFileVersion("1.0.0.0")]
[assembly : AllowPartiallyTrustedCallers] [assembly : AllowPartiallyTrustedCallers]

View File

@ -1,85 +1,85 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using TribalMedia.Framework.Data; using TribalMedia.Framework.Data;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public abstract class RowMapper public abstract class RowMapper
{ {
public abstract void FillObject(DataReader reader); public abstract void FillObject(DataReader reader);
} }
public class ObjectMapper<TObj> : RowMapper public class ObjectMapper<TObj> : RowMapper
{ {
private readonly Schema m_schema; private readonly Schema m_schema;
private readonly TObj m_obj; private readonly TObj m_obj;
public TObj Object public TObj Object
{ {
get { return m_obj; } get { return m_obj; }
} }
public ObjectMapper(Schema schema, TObj obj) public ObjectMapper(Schema schema, TObj obj)
{ {
m_schema = schema; m_schema = schema;
m_obj = obj; m_obj = obj;
} }
public override void FillObject(DataReader reader) public override void FillObject(DataReader reader)
{ {
foreach (FieldMapper fieldMapper in m_schema.Fields.Values) foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
{ {
fieldMapper.SetPropertyFromReader(m_obj, reader); fieldMapper.SetPropertyFromReader(m_obj, reader);
} }
} }
} }
public class RowMapper<TObj> : RowMapper public class RowMapper<TObj> : RowMapper
{ {
private readonly Schema m_schema; private readonly Schema m_schema;
private readonly TObj m_obj; private readonly TObj m_obj;
public TObj Object public TObj Object
{ {
get { return m_obj; } get { return m_obj; }
} }
public RowMapper(Schema schema, TObj obj) public RowMapper(Schema schema, TObj obj)
{ {
m_schema = schema; m_schema = schema;
m_obj = obj; m_obj = obj;
} }
public override void FillObject(DataReader reader) public override void FillObject(DataReader reader)
{ {
foreach (FieldMapper fieldMapper in m_schema.Fields.Values) foreach (FieldMapper fieldMapper in m_schema.Fields.Values)
{ {
fieldMapper.SetPropertyFromReader(this, reader); fieldMapper.SetPropertyFromReader(this, reader);
} }
} }
} }
} }

View File

@ -1,89 +1,89 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System.Collections.Generic; using System.Collections.Generic;
using TribalMedia.Framework.Data; using TribalMedia.Framework.Data;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public class Schema public class Schema
{ {
protected TableMapper m_tableMapper; protected TableMapper m_tableMapper;
protected Dictionary<string, FieldMapper> m_mappings; protected Dictionary<string, FieldMapper> m_mappings;
public Dictionary<string, FieldMapper> Fields public Dictionary<string, FieldMapper> Fields
{ {
get { return m_mappings; } get { return m_mappings; }
} }
public Schema(TableMapper tableMapper) public Schema(TableMapper tableMapper)
{ {
m_mappings = new Dictionary<string, FieldMapper>(); m_mappings = new Dictionary<string, FieldMapper>();
m_tableMapper = tableMapper; m_tableMapper = tableMapper;
} }
} }
public class ObjectSchema<TObj> : Schema public class ObjectSchema<TObj> : Schema
{ {
public ObjectSchema(TableMapper tableMapper) : base(tableMapper) public ObjectSchema(TableMapper tableMapper) : base(tableMapper)
{ {
} }
public ObjectField<TObj, TField> AddMapping<TField>(string fieldName, public ObjectField<TObj, TField> AddMapping<TField>(string fieldName,
ObjectGetAccessor<TObj, TField> rowMapperGetAccessor, ObjectGetAccessor<TObj, TField> rowMapperGetAccessor,
ObjectSetAccessor<TObj, TField> rowMapperSetAccessor) ObjectSetAccessor<TObj, TField> rowMapperSetAccessor)
{ {
ObjectField<TObj, TField> rowMapperField = ObjectField<TObj, TField> rowMapperField =
new ObjectField<TObj, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor); new ObjectField<TObj, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor);
m_mappings.Add(fieldName, rowMapperField); m_mappings.Add(fieldName, rowMapperField);
return rowMapperField; return rowMapperField;
} }
} }
public class RowMapperSchema<TRowMapper> : Schema public class RowMapperSchema<TRowMapper> : Schema
where TRowMapper : RowMapper where TRowMapper : RowMapper
{ {
public RowMapperSchema(TableMapper tableMapper) : base(tableMapper) public RowMapperSchema(TableMapper tableMapper) : base(tableMapper)
{ {
} }
public RowMapperField<TRowMapper, TField> AddMapping<TField>(string fieldName, public RowMapperField<TRowMapper, TField> AddMapping<TField>(string fieldName,
RowMapperGetAccessor<TRowMapper, TField> RowMapperGetAccessor<TRowMapper, TField>
rowMapperGetAccessor, rowMapperGetAccessor,
RowMapperSetAccessor<TRowMapper, TField> RowMapperSetAccessor<TRowMapper, TField>
rowMapperSetAccessor) rowMapperSetAccessor)
{ {
RowMapperField<TRowMapper, TField> rowMapperField = RowMapperField<TRowMapper, TField> rowMapperField =
new RowMapperField<TRowMapper, TField>(m_tableMapper, fieldName, rowMapperGetAccessor, rowMapperSetAccessor); 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

@ -1,108 +1,108 @@
/* /*
* Copyright (c) Tribal Media AB, http://tribalmedia.se/ * Copyright (c) Tribal Media AB, http://tribalmedia.se/
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright * * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the * notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution. * documentation and/or other materials provided with the distribution.
* * The name of Tribal Media AB may not be used to endorse or promote products * * The name of Tribal Media AB may not be used to endorse or promote products
* derived from this software without specific prior written permission. * derived from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
*/ */
using System; using System;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using TribalMedia.Framework.Data; using TribalMedia.Framework.Data;
namespace TribalMedia.Framework.Data namespace TribalMedia.Framework.Data
{ {
public abstract class TableMapper public abstract class TableMapper
{ {
private readonly DatabaseMapper m_connectionPool; private readonly DatabaseMapper m_connectionPool;
private readonly object m_syncRoot = new object(); private readonly object m_syncRoot = new object();
protected void WithConnection(Action<DbConnection> action) protected void WithConnection(Action<DbConnection> action)
{ {
lock (m_syncRoot) lock (m_syncRoot)
{ {
DbConnection m_connection = m_connectionPool.GetNewConnection(); DbConnection m_connection = m_connectionPool.GetNewConnection();
if (m_connection.State != ConnectionState.Open) if (m_connection.State != ConnectionState.Open)
{ {
m_connection.Open(); m_connection.Open();
} }
action(m_connection); action(m_connection);
if (m_connection.State == ConnectionState.Open) if (m_connection.State == ConnectionState.Open)
{ {
m_connection.Close(); m_connection.Close();
} }
} }
} }
private readonly string m_tableName; private readonly string m_tableName;
public string TableName public string TableName
{ {
get { return m_tableName; } get { return m_tableName; }
} }
private Schema m_schema; private Schema m_schema;
public Schema Schema public Schema Schema
{ {
get { return m_schema; } get { return m_schema; }
} }
private FieldMapper m_keyFieldMapper; private FieldMapper m_keyFieldMapper;
public FieldMapper KeyFieldMapper public FieldMapper KeyFieldMapper
{ {
get { return m_keyFieldMapper; } get { return m_keyFieldMapper; }
} }
public TableMapper(DatabaseMapper connectionPool, string tableName) public TableMapper(DatabaseMapper connectionPool, string tableName)
{ {
m_connectionPool = connectionPool; m_connectionPool = connectionPool;
m_tableName = tableName.ToLower(); // Stupid MySQL hack. m_tableName = tableName.ToLower(); // Stupid MySQL hack.
} }
public string CreateParamName(string fieldName) public string CreateParamName(string fieldName)
{ {
return m_connectionPool.CreateParamName(fieldName); return m_connectionPool.CreateParamName(fieldName);
} }
protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey) protected DbCommand CreateSelectCommand(DbConnection connection, string fieldName, object primaryKey)
{ {
return m_connectionPool.CreateSelectCommand(this, connection, fieldName, primaryKey); return m_connectionPool.CreateSelectCommand(this, connection, fieldName, primaryKey);
} }
public string CreateCondition(DbCommand command, string fieldName, object key) public string CreateCondition(DbCommand command, string fieldName, object key)
{ {
return m_connectionPool.CreateCondition(this, command, fieldName, key); return m_connectionPool.CreateCondition(this, command, fieldName, key);
} }
public DbCommand CreateInsertCommand(DbConnection connection, object obj) public DbCommand CreateInsertCommand(DbConnection connection, object obj)
{ {
return m_connectionPool.CreateInsertCommand(this, connection, obj); return m_connectionPool.CreateInsertCommand(this, connection, obj);
} }
public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey) public DbCommand CreateUpdateCommand(DbConnection connection, object rowMapper, object primaryKey)
{ {
return m_connectionPool.CreateUpdateCommand(this, connection, rowMapper, primaryKey); return m_connectionPool.CreateUpdateCommand(this, connection, rowMapper, primaryKey);
} }
} }
} }