This commit is contained in:
grzegorz.russek
2012-08-14 13:35:47 +00:00
parent bfa078841f
commit d135ab1ba0
6 changed files with 94 additions and 32 deletions

View File

@@ -48,7 +48,8 @@ namespace DynamORM.Builders
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendFormat("DELETE FROM {0}", TableName); sb.Append("DELETE FROM ");
DynamicTable.Database.DecorateName(sb, TableName);
FillWhere(command, sb); FillWhere(command, sb);

View File

@@ -179,7 +179,7 @@ namespace DynamORM.Builders
first = false; first = false;
} }
return command.SetCommand("INSERT INTO {0} ({1}) VALUES ({2})", TableName, builderColumns, builderValues); return command.SetCommand("INSERT INTO {0} ({1}) VALUES ({2})", db.DecorateName(TableName), builderColumns, builderValues);
} }
/// <summary>Execute this builder.</summary> /// <summary>Execute this builder.</summary>

View File

@@ -10,7 +10,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DynamORM</RootNamespace> <RootNamespace>DynamORM</RootNamespace>
<AssemblyName>DynamORM</AssemblyName> <AssemblyName>DynamORM</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>

View File

@@ -51,7 +51,7 @@ namespace DynamORM
lock (_db.SyncLock) lock (_db.SyncLock)
{ {
if (!_db.CommandsPool.ContainsKey(_con.Connection)) if (!_db.CommandsPool.ContainsKey(_con.Connection))
throw new InvalidOperationException("Can't create transaction using disposed connection."); throw new InvalidOperationException("Can't create command using disposed connection.");
else else
{ {
_command = _con.Connection.CreateCommand(); _command = _con.Connection.CreateCommand();
@@ -60,16 +60,28 @@ namespace DynamORM
} }
} }
/// <summary>Initializes a new instance of the <see cref="DynamicCommand"/> class.</summary>
/// <param name="db">The databas manager.</param>
/// <remarks>Used intenaly to create command without context.</remarks>
internal DynamicCommand(DynamicDatabase db)
{
_db = db;
_command = db.Provider.CreateCommand();
}
/// <summary>Prepare command for execution.</summary> /// <summary>Prepare command for execution.</summary>
/// <returns>Returns edited <see cref="System.Data.IDbCommand"/> instance.</returns> /// <returns>Returns edited <see cref="System.Data.IDbCommand"/> instance.</returns>
private IDbCommand PrepareForExecution() internal IDbCommand PrepareForExecution()
{ {
if (_poolStamp < _db.PoolStamp) // TODO: Fix that
// if (_poolStamp < _db.PoolStamp)
{ {
_command.CommandTimeout = _commandTimeout ?? _db.CommandTimeout ?? _command.CommandTimeout; _command.CommandTimeout = _commandTimeout ?? _db.CommandTimeout ?? _command.CommandTimeout;
if (_db.TransactionPool[_command.Connection].Count > 0) if (_db.TransactionPool[_command.Connection].Count > 0)
_command.Transaction = _db.TransactionPool[_command.Connection].Peek(); _command.Transaction = _db.TransactionPool[_command.Connection].Peek();
else
_command.Transaction = null;
_poolStamp = _db.PoolStamp; _poolStamp = _db.PoolStamp;
} }
@@ -106,7 +118,26 @@ namespace DynamORM
/// <summary>Gets or sets the <see cref="T:System.Data.IDbConnection"/> /// <summary>Gets or sets the <see cref="T:System.Data.IDbConnection"/>
/// used by this instance of the <see cref="T:System.Data.IDbCommand"/>.</summary> /// used by this instance of the <see cref="T:System.Data.IDbCommand"/>.</summary>
/// <returns>The connection to the data source.</returns> /// <returns>The connection to the data source.</returns>
public IDbConnection Connection { get { return _con; } set { _con = (DynamicConnection)value; } } public IDbConnection Connection
{
get { return _con; }
set
{
_con = value as DynamicConnection;
if (_con != null)
{
_poolStamp = 0;
_command.Connection = _con.Connection;
}
else
{
_command.Transaction = null;
_command.Connection = null;
}
}
}
/// <summary>Creates a new instance of an /// <summary>Creates a new instance of an
/// <see cref="T:System.Data.IDbDataParameter"/> object.</summary> /// <see cref="T:System.Data.IDbDataParameter"/> object.</summary>
@@ -190,11 +221,14 @@ namespace DynamORM
public void Dispose() public void Dispose()
{ {
lock (_db.SyncLock) lock (_db.SyncLock)
{
if (_con != null)
{ {
var pool = _db.CommandsPool.TryGetValue(_con.Connection); var pool = _db.CommandsPool.TryGetValue(_con.Connection);
if (pool != null) if (pool != null && pool.Contains(_command))
pool.Remove(_command); pool.Remove(_command);
}
_command.Dispose(); _command.Dispose();
} }

View File

@@ -59,7 +59,24 @@ namespace DynamORM
/// <summary>Gets or sets timestamp of last transaction pool or configuration change.</summary> /// <summary>Gets or sets timestamp of last transaction pool or configuration change.</summary>
/// <remarks>This property is used to allow commands to determine if /// <remarks>This property is used to allow commands to determine if
/// they need to update transaction object or not.</remarks> /// they need to update transaction object or not.</remarks>
internal long PoolStamp { get { return _poolStamp; } set { lock (SyncLock) _poolStamp = value; } } internal long PoolStamp
{
get
{
long r = 0;
lock (SyncLock)
r = _poolStamp;
return r;
}
set
{
lock (SyncLock)
_poolStamp = value;
}
}
/// <summary>Gets pool of connections and transactions.</summary> /// <summary>Gets pool of connections and transactions.</summary>
internal Dictionary<IDbConnection, Stack<IDbTransaction>> TransactionPool { get; private set; } internal Dictionary<IDbConnection, Stack<IDbTransaction>> TransactionPool { get; private set; }
@@ -83,6 +100,9 @@ namespace DynamORM
/// <summary>Gets or sets command timeout.</summary> /// <summary>Gets or sets command timeout.</summary>
public int? CommandTimeout { get { return _commandTimeout; } set { _commandTimeout = value; _poolStamp = DateTime.Now.Ticks; } } public int? CommandTimeout { get { return _commandTimeout; } set { _commandTimeout = value; _poolStamp = DateTime.Now.Ticks; } }
/// <summary>Gets the database provider.</summary>
public DbProviderFactory Provider { get { return _provider; } }
/// <summary>Gets or sets a value indicating whether /// <summary>Gets or sets a value indicating whether
/// dump commands to console or not.</summary> /// dump commands to console or not.</summary>
public bool DumpCommands { get; set; } public bool DumpCommands { get; set; }

View File

@@ -255,10 +255,6 @@ namespace DynamORM
var p = cmd.CreateParameter(); var p = cmd.CreateParameter();
p.ParameterName = builder.DynamicTable.Database.GetParameterName(cmd.Parameters.Count); p.ParameterName = builder.DynamicTable.Database.GetParameterName(cmd.Parameters.Count);
if (item.Value == null)
p.Value = DBNull.Value;
else
{
var col = builder.Schema.TryGetNullable(item.ColumnName.ToLower()); var col = builder.Schema.TryGetNullable(item.ColumnName.ToLower());
if (col.HasValue) if (col.HasValue)
@@ -270,8 +266,16 @@ namespace DynamORM
p.Size = col.Value.Size; p.Size = col.Value.Size;
p.Precision = col.Value.Precision; p.Precision = col.Value.Precision;
p.Scale = col.Value.Scale; p.Scale = col.Value.Scale;
// Quick fix - review that
if (p.Scale > p.Precision)
p.Scale = 4;
} }
p.Value = item.Value;
} }
else if (item.Value == null || item.Value == DBNull.Value)
p.Value = DBNull.Value;
else else
{ {
p.DbType = TypeMap.TryGetNullable(item.Value.GetType()) ?? DbType.String; p.DbType = TypeMap.TryGetNullable(item.Value.GetType()) ?? DbType.String;
@@ -280,9 +284,6 @@ namespace DynamORM
p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000; p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000;
} }
p.Value = item.Value;
}
cmd.Parameters.Add(p); cmd.Parameters.Add(p);
return cmd; return cmd;
@@ -666,12 +667,16 @@ namespace DynamORM
if (command.Parameters.Count > 0) if (command.Parameters.Count > 0)
{ {
writer.Write("Parameters:"); writer.WriteLine("Parameters:");
foreach (IDbDataParameter param in command.Parameters) foreach (IDbDataParameter param in command.Parameters)
{ {
writer.Write(" '{0}'({1}) = '{2}'({3});", writer.WriteLine(" '{0}' ({1} (s:{2} p:{3} s:{4})) = '{5}' ({6});",
param.ParameterName, param.DbType, param.ParameterName,
param.DbType,
param.Scale,
param.Precision,
param.Scale,
param.Value ?? "NULL", param.Value ?? "NULL",
param.Value != null ? param.Value.GetType().Name : "DBNull"); param.Value != null ? param.Value.GetType().Name : "DBNull");
} }