* Fixes of errors detected while integrating DynamORM to existing project
* New features (VirtualColumn, VirtualMode, BeginBlock, EndBlock)
This commit is contained in:
@@ -37,7 +37,7 @@
|
|||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core" />
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="System.Data.SQLite">
|
<Reference Include="System.Data.SQLite">
|
||||||
<HintPath>lib\System.Data.SQLite.dll</HintPath>
|
<HintPath>Libraries\System.Data.SQLite.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System.Xml.Linq" />
|
<Reference Include="System.Xml.Linq" />
|
||||||
<Reference Include="System.Data.DataSetExtensions" />
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
|
||||||
@@ -53,5 +54,20 @@ namespace DynamORM.Tests.Helpers
|
|||||||
|
|
||||||
Assert.AreEqual(a.GetType(), b.GetType());
|
Assert.AreEqual(a.GetType(), b.GetType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Test anonymous type value.</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestAnonTypeValue()
|
||||||
|
{
|
||||||
|
var a = new { x = 1, y = "bla bla" };
|
||||||
|
var b = new { x = 1, y = "bla bla" };
|
||||||
|
|
||||||
|
Assert.AreEqual(a, b);
|
||||||
|
Assert.IsTrue(a.Equals(b));
|
||||||
|
|
||||||
|
Dictionary<object, int> dict = new Dictionary<object, int>() { { a, 999 } };
|
||||||
|
|
||||||
|
Assert.IsTrue(dict.ContainsKey(b));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -51,6 +51,9 @@ namespace DynamORM.Builders
|
|||||||
/// <summary>Gets a value indicating whether database supports standard schema.</summary>
|
/// <summary>Gets a value indicating whether database supports standard schema.</summary>
|
||||||
public bool SupportSchema { get; private set; }
|
public bool SupportSchema { get; private set; }
|
||||||
|
|
||||||
|
/// <summary>Gets or sets a value indicating whether set parameters for null values.</summary>
|
||||||
|
public bool VirtualMode { get; set; }
|
||||||
|
|
||||||
/// <summary>Gets table name.</summary>
|
/// <summary>Gets table name.</summary>
|
||||||
public string TableName { get; private set; }
|
public string TableName { get; private set; }
|
||||||
|
|
||||||
@@ -60,6 +63,7 @@ namespace DynamORM.Builders
|
|||||||
{
|
{
|
||||||
DynamicTable = table;
|
DynamicTable = table;
|
||||||
TableName = table.TableName;
|
TableName = table.TableName;
|
||||||
|
VirtualMode = false;
|
||||||
|
|
||||||
WhereConditions = new List<DynamicColumn>();
|
WhereConditions = new List<DynamicColumn>();
|
||||||
|
|
||||||
@@ -245,7 +249,7 @@ namespace DynamORM.Builders
|
|||||||
|
|
||||||
foreach (var v in WhereConditions)
|
foreach (var v in WhereConditions)
|
||||||
{
|
{
|
||||||
var col = Schema.TryGetNullable(v.ColumnName);
|
var col = Schema.TryGetNullable(v.ColumnName.ToLower());
|
||||||
|
|
||||||
string column = col.HasValue ? col.Value.Name : v.ColumnName;
|
string column = col.HasValue ? col.Value.Name : v.ColumnName;
|
||||||
|
|
||||||
@@ -253,14 +257,17 @@ namespace DynamORM.Builders
|
|||||||
(column.IndexOf('(') == -1 || column.IndexOf(')') == -1))
|
(column.IndexOf('(') == -1 || column.IndexOf(')') == -1))
|
||||||
column = db.DecorateName(column);
|
column = db.DecorateName(column);
|
||||||
|
|
||||||
if (v.Value == null)
|
if ((v.Value == null || v.Value == DBNull.Value) && !VirtualMode && !v.VirtualColumn)
|
||||||
{
|
{
|
||||||
#region Null operators
|
#region Null operators
|
||||||
|
|
||||||
if (v.Operator == DynamicColumn.CompareOperator.Not || v.Operator == DynamicColumn.CompareOperator.Eq)
|
if (v.Operator == DynamicColumn.CompareOperator.Not || v.Operator == DynamicColumn.CompareOperator.Eq)
|
||||||
sb.AppendFormat(" {0} {1} IS{2} NULL",
|
sb.AppendFormat(" {0} {1}{2} IS{3} NULL{4}",
|
||||||
first ? "WHERE" : "AND", column,
|
first ? "WHERE" : v.Or ? "OR" : "AND",
|
||||||
v.Operator == DynamicColumn.CompareOperator.Not ? " NOT" : string.Empty);
|
v.BeginBlock ? "(" : string.Empty,
|
||||||
|
column,
|
||||||
|
v.Operator == DynamicColumn.CompareOperator.Not ? " NOT" : string.Empty,
|
||||||
|
v.EndBlock ? ")" : string.Empty);
|
||||||
else
|
else
|
||||||
throw new InvalidOperationException("NULL can only be compared by IS or IS NOT operator.");
|
throw new InvalidOperationException("NULL can only be compared by IS or IS NOT operator.");
|
||||||
|
|
||||||
@@ -273,12 +280,17 @@ namespace DynamORM.Builders
|
|||||||
|
|
||||||
int pos = command.Parameters.Count;
|
int pos = command.Parameters.Count;
|
||||||
|
|
||||||
sb.AppendFormat(" {0} {1} {2} ",
|
sb.AppendFormat(" {0} {1}{2} {3} ",
|
||||||
first ? "WHERE" : "AND", column,
|
first ? "WHERE" : v.Or ? "OR" : "AND",
|
||||||
|
v.BeginBlock ? "(" : string.Empty,
|
||||||
|
column,
|
||||||
ToOperator(v.Operator));
|
ToOperator(v.Operator));
|
||||||
|
|
||||||
db.GetParameterName(sb, pos);
|
db.GetParameterName(sb, pos);
|
||||||
|
|
||||||
|
if (v.EndBlock)
|
||||||
|
sb.Append(")");
|
||||||
|
|
||||||
command.AddParameter(this, v);
|
command.AddParameter(this, v);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -298,8 +310,10 @@ namespace DynamORM.Builders
|
|||||||
|
|
||||||
if (vals.Count == 2)
|
if (vals.Count == 2)
|
||||||
{
|
{
|
||||||
sb.AppendFormat(" {0} {1} BETWEEN ",
|
sb.AppendFormat(" {0} {1}{2} BETWEEN ",
|
||||||
first ? "WHERE" : "AND", column);
|
first ? "WHERE" : v.Or ? "OR" : "AND",
|
||||||
|
v.BeginBlock ? "(" : string.Empty,
|
||||||
|
column);
|
||||||
|
|
||||||
// From parameter
|
// From parameter
|
||||||
db.GetParameterName(sb, command.Parameters.Count);
|
db.GetParameterName(sb, command.Parameters.Count);
|
||||||
@@ -313,6 +327,9 @@ namespace DynamORM.Builders
|
|||||||
v.Value = vals[1];
|
v.Value = vals[1];
|
||||||
command.AddParameter(this, v);
|
command.AddParameter(this, v);
|
||||||
|
|
||||||
|
if (v.EndBlock)
|
||||||
|
sb.Append(")");
|
||||||
|
|
||||||
// Reset value
|
// Reset value
|
||||||
v.Value = vals;
|
v.Value = vals;
|
||||||
}
|
}
|
||||||
@@ -325,8 +342,10 @@ namespace DynamORM.Builders
|
|||||||
{
|
{
|
||||||
#region In operator
|
#region In operator
|
||||||
|
|
||||||
sb.AppendFormat(" {0} {1} IN(",
|
sb.AppendFormat(" {0} {1}{2} IN(",
|
||||||
first ? "WHERE" : "AND", column);
|
first ? "WHERE" : v.Or ? "OR" : "AND",
|
||||||
|
v.BeginBlock ? "(" : string.Empty,
|
||||||
|
column);
|
||||||
|
|
||||||
bool firstParam = true;
|
bool firstParam = true;
|
||||||
|
|
||||||
@@ -354,6 +373,9 @@ namespace DynamORM.Builders
|
|||||||
|
|
||||||
sb.Append(")");
|
sb.Append(")");
|
||||||
|
|
||||||
|
if (v.EndBlock)
|
||||||
|
sb.Append(")");
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ namespace DynamORM
|
|||||||
/// <remarks>Constructor provided for easier object creation in qeries.</remarks>
|
/// <remarks>Constructor provided for easier object creation in qeries.</remarks>
|
||||||
/// <param name="columnName">Name of column to set.</param>
|
/// <param name="columnName">Name of column to set.</param>
|
||||||
public DynamicColumn(string columnName)
|
public DynamicColumn(string columnName)
|
||||||
|
: this()
|
||||||
{
|
{
|
||||||
ColumnName = columnName;
|
ColumnName = columnName;
|
||||||
}
|
}
|
||||||
@@ -132,6 +133,18 @@ namespace DynamORM
|
|||||||
/// <summary>Gets or sets condition operator.</summary>
|
/// <summary>Gets or sets condition operator.</summary>
|
||||||
public CompareOperator Operator { get; set; }
|
public CompareOperator Operator { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Gets or sets a value indicating whether this condition will be treated as or condition.</summary>
|
||||||
|
public bool Or { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Gets or sets a value indicating whether start new block in where steatement.</summary>
|
||||||
|
public bool BeginBlock { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Gets or sets a value indicating whether end existing block in where steatement.</summary>
|
||||||
|
public bool EndBlock { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Gets or sets a value indicating whether set parameters for null values.</summary>
|
||||||
|
public bool VirtualColumn { get; set; }
|
||||||
|
|
||||||
#endregion Properties
|
#endregion Properties
|
||||||
|
|
||||||
#region Query creation helpers
|
#region Query creation helpers
|
||||||
@@ -382,7 +395,7 @@ namespace DynamORM
|
|||||||
string column = ColumnName == "*" ? "*" : ColumnName;
|
string column = ColumnName == "*" ? "*" : ColumnName;
|
||||||
|
|
||||||
if (column != "*" &&
|
if (column != "*" &&
|
||||||
(column.IndexOf(db.LeftDecorator) == -1 || column.IndexOf(db.LeftDecorator) == -1) &&
|
(column.IndexOf(db.LeftDecorator) == -1 || column.IndexOf(db.RightDecorator) == -1) &&
|
||||||
(column.IndexOf('(') == -1 || column.IndexOf(')') == -1))
|
(column.IndexOf('(') == -1 || column.IndexOf(')') == -1))
|
||||||
column = db.DecorateName(column);
|
column = db.DecorateName(column);
|
||||||
|
|
||||||
|
|||||||
@@ -196,7 +196,7 @@ namespace DynamORM
|
|||||||
Dictionary<string, DynamicSchemaColumn> schema = null;
|
Dictionary<string, DynamicSchemaColumn> schema = null;
|
||||||
|
|
||||||
lock (SyncLock)
|
lock (SyncLock)
|
||||||
schema = Schema.TryGetValue(table.GetType().FullName) ??
|
schema = Schema.TryGetValue(table.ToLower()) ??
|
||||||
BuildAndCacheSchema(table, null);
|
BuildAndCacheSchema(table, null);
|
||||||
|
|
||||||
return schema;
|
return schema;
|
||||||
@@ -417,6 +417,7 @@ namespace DynamORM
|
|||||||
{
|
{
|
||||||
IDbConnection conn = null;
|
IDbConnection conn = null;
|
||||||
DynamicConnection ret = null;
|
DynamicConnection ret = null;
|
||||||
|
bool opened = false;
|
||||||
|
|
||||||
lock (SyncLock)
|
lock (SyncLock)
|
||||||
{
|
{
|
||||||
@@ -427,6 +428,7 @@ namespace DynamORM
|
|||||||
conn = _provider.CreateConnection();
|
conn = _provider.CreateConnection();
|
||||||
conn.ConnectionString = _connectionString;
|
conn.ConnectionString = _connectionString;
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
opened = true;
|
||||||
|
|
||||||
TransactionPool.Add(conn, new Stack<IDbTransaction>());
|
TransactionPool.Add(conn, new Stack<IDbTransaction>());
|
||||||
CommandsPool.Add(conn, new List<IDbCommand>());
|
CommandsPool.Add(conn, new List<IDbCommand>());
|
||||||
@@ -436,7 +438,10 @@ namespace DynamORM
|
|||||||
conn = TransactionPool.Keys.First();
|
conn = TransactionPool.Keys.First();
|
||||||
|
|
||||||
if (conn.State != ConnectionState.Open)
|
if (conn.State != ConnectionState.Open)
|
||||||
|
{
|
||||||
conn.Open();
|
conn.Open();
|
||||||
|
opened = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = new DynamicConnection(this, conn, _singleTransaction);
|
ret = new DynamicConnection(this, conn, _singleTransaction);
|
||||||
@@ -445,6 +450,9 @@ namespace DynamORM
|
|||||||
ret = _tempConn;
|
ret = _tempConn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opened)
|
||||||
|
ExecuteInitCommands(ret);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -491,6 +499,19 @@ namespace DynamORM
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Gets or sets contains commands executed when connection is opened.</summary>
|
||||||
|
public List<string> InitCommands { get; set; }
|
||||||
|
|
||||||
|
private void ExecuteInitCommands(IDbConnection conn)
|
||||||
|
{
|
||||||
|
if (InitCommands != null)
|
||||||
|
using (IDbCommand command = conn.CreateCommand())
|
||||||
|
foreach (string commandText in InitCommands)
|
||||||
|
command
|
||||||
|
.SetCommand(commandText)
|
||||||
|
.ExecuteNonQuery();
|
||||||
|
}
|
||||||
|
|
||||||
#endregion Connection
|
#endregion Connection
|
||||||
|
|
||||||
#region Transaction
|
#region Transaction
|
||||||
|
|||||||
@@ -43,16 +43,16 @@ namespace DynamORM
|
|||||||
/// <summary>Only one transaction.</summary>
|
/// <summary>Only one transaction.</summary>
|
||||||
SingleTransaction,
|
SingleTransaction,
|
||||||
|
|
||||||
/// <summary>Database supports top syntax.</summary>
|
/// <summary>Database supports top syntax (SELECT TOP x ... FROM ...).</summary>
|
||||||
SupportTop,
|
SupportTop,
|
||||||
|
|
||||||
/// <summary>Database supports limit offset syntax.</summary>
|
/// <summary>Database supports limit offset syntax (SELECT ... FROM ... LIMIT x OFFSET y).</summary>
|
||||||
SupportLimitOffset,
|
SupportLimitOffset,
|
||||||
|
|
||||||
/// <summary>Database support standard schema.</summary>
|
/// <summary>Database support standard schema.</summary>
|
||||||
SupportSchema,
|
SupportSchema,
|
||||||
|
|
||||||
/// <summary>Database support stored procedures.</summary>
|
/// <summary>Database support stored procedures (EXEC proc ...).</summary>
|
||||||
SupportStoredProcedures
|
SupportStoredProcedures
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -35,6 +35,7 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Text;
|
||||||
using DynamORM.Builders;
|
using DynamORM.Builders;
|
||||||
using DynamORM.Mapper;
|
using DynamORM.Mapper;
|
||||||
|
|
||||||
@@ -645,6 +646,16 @@ namespace DynamORM
|
|||||||
|
|
||||||
#endregion Generic Execution
|
#endregion Generic Execution
|
||||||
|
|
||||||
|
/// <summary>Dump command into text writer.</summary>
|
||||||
|
/// <param name="command">Command to dump.</param>
|
||||||
|
/// <param name="buider">Builder to which write output.</param>
|
||||||
|
/// <returns>Returns dumped <see cref="System.Data.IDbCommand"/> instance.</returns>
|
||||||
|
public static IDbCommand Dump(this IDbCommand command, StringBuilder buider)
|
||||||
|
{
|
||||||
|
using (StringWriter sw = new StringWriter(buider))
|
||||||
|
return command.Dump(sw);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Dump command into text writer.</summary>
|
/// <summary>Dump command into text writer.</summary>
|
||||||
/// <param name="command">Command to dump.</param>
|
/// <param name="command">Command to dump.</param>
|
||||||
/// <param name="writer">Writer to which write output.</param>
|
/// <param name="writer">Writer to which write output.</param>
|
||||||
@@ -688,6 +699,20 @@ namespace DynamORM
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Turns the dictionary into an ExpandoObject.</summary>
|
||||||
|
/// <param name="d">Dictionary to convert.</param>
|
||||||
|
/// <returns>Converted dictionary.</returns>
|
||||||
|
public static dynamic ToDynamic(this IDictionary<string, object> d)
|
||||||
|
{
|
||||||
|
var result = new ExpandoObject();
|
||||||
|
var dict = result as IDictionary<string, object>;
|
||||||
|
|
||||||
|
foreach (var prop in d)
|
||||||
|
dict.Add(prop.Key, prop.Value);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Turns the object into an ExpandoObject.</summary>
|
/// <summary>Turns the object into an ExpandoObject.</summary>
|
||||||
/// <param name="o">Object to convert.</param>
|
/// <param name="o">Object to convert.</param>
|
||||||
/// <returns>Converted object.</returns>
|
/// <returns>Converted object.</returns>
|
||||||
|
|||||||
Reference in New Issue
Block a user