Corrected Time and Date types handling in SQL Server.
This commit is contained in:
@@ -44,7 +44,6 @@ using System.Collections.Concurrent;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.ComponentModel;
|
|
||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Dynamic;
|
using System.Dynamic;
|
||||||
@@ -3876,7 +3875,7 @@ namespace DynamORM
|
|||||||
{ typeof(Guid?), DbType.Guid },
|
{ typeof(Guid?), DbType.Guid },
|
||||||
{ typeof(DateTime?), DbType.DateTime },
|
{ typeof(DateTime?), DbType.DateTime },
|
||||||
{ typeof(TimeSpan?), DbType.Time },
|
{ typeof(TimeSpan?), DbType.Time },
|
||||||
{ typeof(DateTimeOffset?), DbType.DateTimeOffset }
|
{ typeof(DateTimeOffset?), DbType.DateTimeOffset },
|
||||||
};
|
};
|
||||||
|
|
||||||
#endregion Type column map
|
#endregion Type column map
|
||||||
@@ -4072,9 +4071,9 @@ namespace DynamORM
|
|||||||
p.DbType = TypeMap.TryGetNullable(type) ?? DbType.String;
|
p.DbType = TypeMap.TryGetNullable(type) ?? DbType.String;
|
||||||
|
|
||||||
if (type == typeof(DynamicExpando) || type == typeof(ExpandoObject))
|
if (type == typeof(DynamicExpando) || type == typeof(ExpandoObject))
|
||||||
p.Value = ((IDictionary<string, object>)item).Values.FirstOrDefault();
|
p.Value = CorrectValue(p.DbType, ((IDictionary<string, object>)item).Values.FirstOrDefault());
|
||||||
else
|
else
|
||||||
p.Value = item;
|
p.Value = CorrectValue(p.DbType, item);
|
||||||
|
|
||||||
if (p.DbType == DbType.String)
|
if (p.DbType == DbType.String)
|
||||||
p.Size = item.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = item.ToString().Length > 4000 ? -1 : 4000;
|
||||||
@@ -4114,7 +4113,7 @@ namespace DynamORM
|
|||||||
p.Scale = 4;
|
p.Scale = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Value = value == null ? DBNull.Value : value;
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
else if (value == null || value == DBNull.Value)
|
else if (value == null || value == DBNull.Value)
|
||||||
p.Value = DBNull.Value;
|
p.Value = DBNull.Value;
|
||||||
@@ -4127,7 +4126,7 @@ namespace DynamORM
|
|||||||
else if (p.DbType == DbType.String)
|
else if (p.DbType == DbType.String)
|
||||||
p.Size = value.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = value.ToString().Length > 4000 ? -1 : 4000;
|
||||||
|
|
||||||
p.Value = value;
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Parameters.Add(p);
|
cmd.Parameters.Add(p);
|
||||||
@@ -4135,6 +4134,24 @@ namespace DynamORM
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static object CorrectValue(DbType type, object value)
|
||||||
|
{
|
||||||
|
if (value == null || value == DBNull.Value)
|
||||||
|
return DBNull.Value;
|
||||||
|
|
||||||
|
if ((type == DbType.String || type == DbType.AnsiString || type == DbType.StringFixedLength || type == DbType.AnsiStringFixedLength) &&
|
||||||
|
!(value is string))
|
||||||
|
return value.ToString();
|
||||||
|
else if (type == DbType.Guid && value is string)
|
||||||
|
return Guid.Parse(value.ToString());
|
||||||
|
else if (type == DbType.Guid && value is byte[] && ((byte[])value).Length == 16)
|
||||||
|
return new Guid((byte[])value);
|
||||||
|
else if (type == DbType.DateTime && value is TimeSpan) // HACK: This is specific for SQL Server, to be verified with other databases
|
||||||
|
return DateTime.Today.Add((TimeSpan)value);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Extension for adding single parameter determining only type of object.</summary>
|
/// <summary>Extension for adding single parameter determining only type of object.</summary>
|
||||||
/// <param name="cmd">Command to handle.</param>
|
/// <param name="cmd">Command to handle.</param>
|
||||||
/// <param name="builder">Query builder containing schema.</param>
|
/// <param name="builder">Query builder containing schema.</param>
|
||||||
@@ -4167,7 +4184,7 @@ namespace DynamORM
|
|||||||
p.Scale = 4;
|
p.Scale = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Value = item.Value == null ? DBNull.Value : item.Value;
|
p.Value = item.Value == null ? DBNull.Value : CorrectValue(p.DbType, item.Value);
|
||||||
}
|
}
|
||||||
else if (item.Value == null || item.Value == DBNull.Value)
|
else if (item.Value == null || item.Value == DBNull.Value)
|
||||||
p.Value = DBNull.Value;
|
p.Value = DBNull.Value;
|
||||||
@@ -4180,7 +4197,7 @@ namespace DynamORM
|
|||||||
else if (p.DbType == DbType.String)
|
else if (p.DbType == DbType.String)
|
||||||
p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000;
|
||||||
|
|
||||||
p.Value = item.Value;
|
p.Value = CorrectValue(p.DbType, item.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Parameters.Add(p);
|
cmd.Parameters.Add(p);
|
||||||
@@ -4207,7 +4224,7 @@ namespace DynamORM
|
|||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4253,7 +4270,7 @@ namespace DynamORM
|
|||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4274,7 +4291,7 @@ namespace DynamORM
|
|||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4314,7 +4331,7 @@ namespace DynamORM
|
|||||||
param.Direction = parameterDirection;
|
param.Direction = parameterDirection;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4333,7 +4350,7 @@ namespace DynamORM
|
|||||||
param.ParameterName = parameterName;
|
param.ParameterName = parameterName;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4350,7 +4367,7 @@ namespace DynamORM
|
|||||||
IDbDataParameter param = command.CreateParameter();
|
IDbDataParameter param = command.CreateParameter();
|
||||||
param.ParameterName = parameterName;
|
param.ParameterName = parameterName;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -4401,7 +4418,8 @@ namespace DynamORM
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
((IDbDataParameter)command.Parameters[parameterName]).Value = value;
|
var p = ((IDbDataParameter)command.Parameters[parameterName]);
|
||||||
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -4420,7 +4438,8 @@ namespace DynamORM
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
((IDbDataParameter)command.Parameters[index]).Value = value;
|
var p = ((IDbDataParameter)command.Parameters[index]);
|
||||||
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -8425,7 +8444,7 @@ namespace DynamORM
|
|||||||
DynamicPropertyInvoker propMap = mapper.ColumnsMap.TryGetValue(colName.ToLower());
|
DynamicPropertyInvoker propMap = mapper.ColumnsMap.TryGetValue(colName.ToLower());
|
||||||
|
|
||||||
if (propMap == null || propMap.Column == null || !propMap.Column.IsNoInsert)
|
if (propMap == null || propMap.Column == null || !propMap.Column.IsNoInsert)
|
||||||
Insert(colName, con.Value);
|
Insert(colName, con.Value); // TODO: This probably should get value from mapper
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -15092,4 +15111,3 @@ namespace DynamORM
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,11 +71,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = GetTestTable().Single(code: "201");
|
var o = GetTestTable().Single(code: "201");
|
||||||
Assert.AreNotEqual(200, o.id);
|
Assert.AreNotEqual(200, o.id);
|
||||||
Assert.AreEqual("201", o.code.ToString());
|
Assert.AreEqual("201", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row insertion by dynamic object.</summary>
|
/// <summary>Test row insertion by dynamic object.</summary>
|
||||||
@@ -88,11 +88,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = GetTestTable().Single(code: "202");
|
var o = GetTestTable().Single(code: "202");
|
||||||
Assert.AreNotEqual(200, o.id);
|
Assert.AreNotEqual(200, o.id);
|
||||||
Assert.AreEqual("202", o.code.ToString());
|
Assert.AreEqual("202", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row insertion by mapped object.</summary>
|
/// <summary>Test row insertion by mapped object.</summary>
|
||||||
@@ -115,11 +115,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "203");
|
var o = u.Single(code: "203");
|
||||||
Assert.AreNotEqual(200, o.id);
|
Assert.AreNotEqual(200, o.id);
|
||||||
Assert.AreEqual("203", o.code.ToString());
|
Assert.AreEqual("203", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row insertion by basic object.</summary>
|
/// <summary>Test row insertion by basic object.</summary>
|
||||||
@@ -142,11 +142,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "204");
|
var o = u.Single(code: "204");
|
||||||
Assert.AreNotEqual(200, o.id);
|
Assert.AreNotEqual(200, o.id);
|
||||||
Assert.AreEqual("204", o.code.ToString());
|
Assert.AreEqual("204", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Insert
|
#endregion Insert
|
||||||
@@ -163,11 +163,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = GetTestTable().Single(code: "201");
|
var o = GetTestTable().Single(code: "201");
|
||||||
Assert.AreEqual(1, o.id);
|
Assert.AreEqual(1, o.id);
|
||||||
Assert.AreEqual("201", o.code.ToString());
|
Assert.AreEqual("201", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by dynamic objects.</summary>
|
/// <summary>Test row updating by dynamic objects.</summary>
|
||||||
@@ -180,11 +180,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = GetTestTable().Single(code: "202");
|
var o = GetTestTable().Single(code: "202");
|
||||||
Assert.AreEqual(2, o.id);
|
Assert.AreEqual(2, o.id);
|
||||||
Assert.AreEqual("202", o.code.ToString());
|
Assert.AreEqual("202", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by mapped object.</summary>
|
/// <summary>Test row updating by mapped object.</summary>
|
||||||
@@ -207,11 +207,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "203");
|
var o = u.Single(code: "203");
|
||||||
Assert.AreEqual(3, o.id);
|
Assert.AreEqual(3, o.id);
|
||||||
Assert.AreEqual("203", o.code.ToString());
|
Assert.AreEqual("203", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by basic object.</summary>
|
/// <summary>Test row updating by basic object.</summary>
|
||||||
@@ -234,11 +234,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "204");
|
var o = u.Single(code: "204");
|
||||||
Assert.AreEqual(4, o.id);
|
Assert.AreEqual(4, o.id);
|
||||||
Assert.AreEqual("204", o.code.ToString());
|
Assert.AreEqual("204", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by dynamic objects.</summary>
|
/// <summary>Test row updating by dynamic objects.</summary>
|
||||||
@@ -251,11 +251,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = GetTestTable().Single(code: "205");
|
var o = GetTestTable().Single(code: "205");
|
||||||
Assert.AreEqual(5, o.id);
|
Assert.AreEqual(5, o.id);
|
||||||
Assert.AreEqual("205", o.code.ToString());
|
Assert.AreEqual("205", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by mapped objects.</summary>
|
/// <summary>Test row updating by mapped objects.</summary>
|
||||||
@@ -278,11 +278,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "206");
|
var o = u.Single(code: "206");
|
||||||
Assert.AreEqual(6, o.id);
|
Assert.AreEqual(6, o.id);
|
||||||
Assert.AreEqual("206", o.code.ToString());
|
Assert.AreEqual("206", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Test row updating by basic objects.</summary>
|
/// <summary>Test row updating by basic objects.</summary>
|
||||||
@@ -305,11 +305,11 @@ namespace DynamORM.Tests.Modify
|
|||||||
var o = u.Single(code: "207");
|
var o = u.Single(code: "207");
|
||||||
Assert.AreEqual(7, o.id);
|
Assert.AreEqual(7, o.id);
|
||||||
Assert.AreEqual("207", o.code.ToString());
|
Assert.AreEqual("207", o.code.ToString());
|
||||||
Assert.AreEqual(null, o.first);
|
Assert.IsNull(o.first);
|
||||||
Assert.AreEqual("Gagarin", o.last);
|
Assert.AreEqual("Gagarin", o.last);
|
||||||
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
Assert.AreEqual("juri.gagarin@megacorp.com", o.email);
|
||||||
Assert.AreEqual("bla, bla, bla", o.quote);
|
Assert.AreEqual("bla, bla, bla", o.quote);
|
||||||
Assert.AreEqual(null, o.password);
|
Assert.IsNull(o.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Update
|
#endregion Update
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
<Copyright>Copyright © RUSSEK Software 2012-2023</Copyright>
|
<Copyright>Copyright © RUSSEK Software 2012-2023</Copyright>
|
||||||
<Company>RUSSEK Software</Company>
|
<Company>RUSSEK Software</Company>
|
||||||
<Authors>Grzegorz Russek</Authors>
|
<Authors>Grzegorz Russek</Authors>
|
||||||
<VersionPrefix>1.6</VersionPrefix>
|
<VersionPrefix>1.7</VersionPrefix>
|
||||||
<RepositoryUrl>https://git.dr4cul4.pl/RUSSEK-Software/DynamORM</RepositoryUrl>
|
<RepositoryUrl>https://git.dr4cul4.pl/RUSSEK-Software/DynamORM</RepositoryUrl>
|
||||||
<PackageProjectUrl>https://dr4cul4.pl</PackageProjectUrl>
|
<PackageProjectUrl>https://dr4cul4.pl</PackageProjectUrl>
|
||||||
<Product>DynamORM</Product>
|
<Product>DynamORM</Product>
|
||||||
|
|||||||
@@ -282,9 +282,9 @@ namespace DynamORM
|
|||||||
p.DbType = TypeMap.TryGetNullable(type) ?? DbType.String;
|
p.DbType = TypeMap.TryGetNullable(type) ?? DbType.String;
|
||||||
|
|
||||||
if (type == typeof(DynamicExpando) || type == typeof(ExpandoObject))
|
if (type == typeof(DynamicExpando) || type == typeof(ExpandoObject))
|
||||||
p.Value = ((IDictionary<string, object>)item).Values.FirstOrDefault();
|
p.Value = CorrectValue(p.DbType, ((IDictionary<string, object>)item).Values.FirstOrDefault());
|
||||||
else
|
else
|
||||||
p.Value = item;
|
p.Value = CorrectValue(p.DbType, item);
|
||||||
|
|
||||||
if (p.DbType == DbType.String)
|
if (p.DbType == DbType.String)
|
||||||
p.Size = item.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = item.ToString().Length > 4000 ? -1 : 4000;
|
||||||
@@ -324,7 +324,7 @@ namespace DynamORM
|
|||||||
p.Scale = 4;
|
p.Scale = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Value = value == null ? DBNull.Value : value;
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
else if (value == null || value == DBNull.Value)
|
else if (value == null || value == DBNull.Value)
|
||||||
p.Value = DBNull.Value;
|
p.Value = DBNull.Value;
|
||||||
@@ -337,7 +337,7 @@ namespace DynamORM
|
|||||||
else if (p.DbType == DbType.String)
|
else if (p.DbType == DbType.String)
|
||||||
p.Size = value.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = value.ToString().Length > 4000 ? -1 : 4000;
|
||||||
|
|
||||||
p.Value = value;
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Parameters.Add(p);
|
cmd.Parameters.Add(p);
|
||||||
@@ -345,6 +345,24 @@ namespace DynamORM
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static object CorrectValue(DbType type, object value)
|
||||||
|
{
|
||||||
|
if (value == null || value == DBNull.Value)
|
||||||
|
return DBNull.Value;
|
||||||
|
|
||||||
|
if ((type == DbType.String || type == DbType.AnsiString || type == DbType.StringFixedLength || type == DbType.AnsiStringFixedLength) &&
|
||||||
|
!(value is string))
|
||||||
|
return value.ToString();
|
||||||
|
else if (type == DbType.Guid && value is string)
|
||||||
|
return Guid.Parse(value.ToString());
|
||||||
|
else if (type == DbType.Guid && value is byte[] && ((byte[])value).Length == 16)
|
||||||
|
return new Guid((byte[])value);
|
||||||
|
else if (type == DbType.DateTime && value is TimeSpan) // HACK: This is specific for SQL Server, to be verified with other databases
|
||||||
|
return DateTime.Today.Add((TimeSpan)value);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Extension for adding single parameter determining only type of object.</summary>
|
/// <summary>Extension for adding single parameter determining only type of object.</summary>
|
||||||
/// <param name="cmd">Command to handle.</param>
|
/// <param name="cmd">Command to handle.</param>
|
||||||
/// <param name="builder">Query builder containing schema.</param>
|
/// <param name="builder">Query builder containing schema.</param>
|
||||||
@@ -377,7 +395,7 @@ namespace DynamORM
|
|||||||
p.Scale = 4;
|
p.Scale = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Value = item.Value == null ? DBNull.Value : item.Value;
|
p.Value = item.Value == null ? DBNull.Value : CorrectValue(p.DbType, item.Value);
|
||||||
}
|
}
|
||||||
else if (item.Value == null || item.Value == DBNull.Value)
|
else if (item.Value == null || item.Value == DBNull.Value)
|
||||||
p.Value = DBNull.Value;
|
p.Value = DBNull.Value;
|
||||||
@@ -390,7 +408,7 @@ namespace DynamORM
|
|||||||
else if (p.DbType == DbType.String)
|
else if (p.DbType == DbType.String)
|
||||||
p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000;
|
p.Size = item.Value.ToString().Length > 4000 ? -1 : 4000;
|
||||||
|
|
||||||
p.Value = item.Value;
|
p.Value = CorrectValue(p.DbType, item.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Parameters.Add(p);
|
cmd.Parameters.Add(p);
|
||||||
@@ -417,7 +435,7 @@ namespace DynamORM
|
|||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -463,7 +481,7 @@ namespace DynamORM
|
|||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -484,7 +502,7 @@ namespace DynamORM
|
|||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Precision = precision;
|
param.Precision = precision;
|
||||||
param.Scale = scale;
|
param.Scale = scale;
|
||||||
param.Value = value;
|
param.Value = CorrectValue(param.DbType, value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -524,7 +542,7 @@ namespace DynamORM
|
|||||||
param.Direction = parameterDirection;
|
param.Direction = parameterDirection;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -543,7 +561,7 @@ namespace DynamORM
|
|||||||
param.ParameterName = parameterName;
|
param.ParameterName = parameterName;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Size = size;
|
param.Size = size;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -560,7 +578,7 @@ namespace DynamORM
|
|||||||
IDbDataParameter param = command.CreateParameter();
|
IDbDataParameter param = command.CreateParameter();
|
||||||
param.ParameterName = parameterName;
|
param.ParameterName = parameterName;
|
||||||
param.DbType = databaseType;
|
param.DbType = databaseType;
|
||||||
param.Value = value ?? DBNull.Value;
|
param.Value = CorrectValue(param.DbType, value ?? DBNull.Value);
|
||||||
command.Parameters.Add(param);
|
command.Parameters.Add(param);
|
||||||
|
|
||||||
return command;
|
return command;
|
||||||
@@ -611,7 +629,8 @@ namespace DynamORM
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
((IDbDataParameter)command.Parameters[parameterName]).Value = value;
|
var p = ((IDbDataParameter)command.Parameters[parameterName]);
|
||||||
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -630,7 +649,8 @@ namespace DynamORM
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
((IDbDataParameter)command.Parameters[index]).Value = value;
|
var p = ((IDbDataParameter)command.Parameters[index]);
|
||||||
|
p.Value = CorrectValue(p.DbType, value);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Data;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using DynamORM;
|
using DynamORM;
|
||||||
using DynamORM.Helpers;
|
using DynamORM.Helpers;
|
||||||
|
using DynamORM.Mapper;
|
||||||
|
|
||||||
namespace Tester
|
namespace Tester
|
||||||
{
|
{
|
||||||
@@ -12,7 +13,7 @@ namespace Tester
|
|||||||
private static DynamicDatabase GetORM()
|
private static DynamicDatabase GetORM()
|
||||||
{
|
{
|
||||||
return new DynamicDatabase(System.Data.SqlClient.SqlClientFactory.Instance,
|
return new DynamicDatabase(System.Data.SqlClient.SqlClientFactory.Instance,
|
||||||
"packet size=4096;User Id=sa;Password=;data source=192.168.22.;initial catalog=PLAYGROUND;",
|
"packet size=4096;User Id=sa;Password=sWe7PepR;data source=192.168.22.10;initial catalog=PLAYGROUND;",
|
||||||
DynamicDatabaseOptions.SingleConnection | DynamicDatabaseOptions.SingleTransaction | DynamicDatabaseOptions.SupportSchema |
|
DynamicDatabaseOptions.SingleConnection | DynamicDatabaseOptions.SingleTransaction | DynamicDatabaseOptions.SupportSchema |
|
||||||
DynamicDatabaseOptions.SupportStoredProcedures | DynamicDatabaseOptions.SupportTop | DynamicDatabaseOptions.DumpCommands);
|
DynamicDatabaseOptions.SupportStoredProcedures | DynamicDatabaseOptions.SupportTop | DynamicDatabaseOptions.DumpCommands);
|
||||||
|
|
||||||
@@ -25,9 +26,57 @@ namespace Tester
|
|||||||
|
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
//var c = new System.Data.SqlClient.SqlConnection("packet size=4096;User Id=sa;Password=sa123;data source=192.168.0.6;initial catalog=DynamORM;");
|
|
||||||
|
|
||||||
using (var db = GetORM())
|
using (var db = GetORM())
|
||||||
|
{
|
||||||
|
//ProcedureHell(db);
|
||||||
|
|
||||||
|
TableFun(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void TableFun(DynamicDatabase db)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
db.Execute("DROP TABLE Experiments ");
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
db.Execute(@"CREATE TABLE Experiments (
|
||||||
|
id int NOT NULL PRIMARY KEY,
|
||||||
|
t1 nvarchar(50) NOT NULL DEFAULT N'',
|
||||||
|
t2 varchar(50) NOT NULL DEFAULT '',
|
||||||
|
dd date,
|
||||||
|
tt time);");
|
||||||
|
|
||||||
|
db.Insert<Ex>().Insert(new Ex
|
||||||
|
{
|
||||||
|
id = 1,
|
||||||
|
t1 = "T1",
|
||||||
|
t2 = "T1",
|
||||||
|
dd = DateTime.Now,
|
||||||
|
tt = TimeSpan.FromDays(2) + TimeSpan.FromHours(10),
|
||||||
|
}).Execute();
|
||||||
|
|
||||||
|
var tt = db.From<Ex>().Where(x => x.id == 1).ToList<Ex>().FirstOrDefault();
|
||||||
|
|
||||||
|
db.Update<Ex>().Where(x => x.id == 1).Set(x => x.tt = TimeSpan.FromMinutes(10), x => x.dd = DateTime.Now.AddDays(2)).Execute();
|
||||||
|
|
||||||
|
db.Execute("DROP TABLE Experiments ");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Table(Name = "Experiments")]
|
||||||
|
private class Ex
|
||||||
|
{
|
||||||
|
public int id { get; set; }
|
||||||
|
public string t1 { get; set; }
|
||||||
|
public string t2 { get; set; }
|
||||||
|
public DateTime dd { get; set; }
|
||||||
|
public TimeSpan tt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ProcedureHell(DynamicDatabase db)
|
||||||
{
|
{
|
||||||
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_Scalar AS SELECT 42;");
|
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_Scalar AS SELECT 42;");
|
||||||
var res0 = db.Procedures.sp_Exp_Scalar();
|
var res0 = db.Procedures.sp_Exp_Scalar();
|
||||||
@@ -80,41 +129,6 @@ RETURN 42;");
|
|||||||
{
|
{
|
||||||
Size = 256,
|
Size = 256,
|
||||||
}, ret_Return: 0);
|
}, ret_Return: 0);
|
||||||
|
|
||||||
//try
|
|
||||||
//{
|
|
||||||
// db.Execute("DROP TABLE Experiments ");
|
|
||||||
//}
|
|
||||||
//catch { }
|
|
||||||
|
|
||||||
//db.Execute("CREATE TABLE Experiments (t1 nvarchar(50) NOT NULL DEFAULT N'', t2 varchar(50) NOT NULL DEFAULT '');");
|
|
||||||
|
|
||||||
//var q = db.From(x => x.Experiments.As(x.e1));
|
|
||||||
//q
|
|
||||||
// .Where(x => x.t2 = "Dupa")
|
|
||||||
// .Where(x => x.Exists(
|
|
||||||
// q.SubQuery()
|
|
||||||
// .From(y => y.Experiments.As(x.e2))
|
|
||||||
// .Where(y => y.e2.t1 == y.e1.t1)))
|
|
||||||
// .Execute().ToList();
|
|
||||||
|
|
||||||
//db.Execute("DROP TABLE Experiments ");
|
|
||||||
|
|
||||||
//IDataReader rdr = db.Procedures.sp_getdate<IDataReader>();
|
|
||||||
//var dt = rdr.ToDataTable();
|
|
||||||
//var dt2 = db.Procedures.sp_getdate<DataTable>();
|
|
||||||
|
|
||||||
//db.Procedures.usp_API_Generate_Doc_Number<string>(key: Guid.NewGuid(), mdn_id: "ZZ");
|
|
||||||
|
|
||||||
//var resL = (db.Procedures.GetProductDesc<IList<GetProductDesc_Result>>() as IEnumerable<dynamic>)
|
|
||||||
// .Cast<GetProductDesc_Result>()
|
|
||||||
// .ToArray();
|
|
||||||
//var res = db.Procedures.GetProductDesc_withparameters<GetProductDesc_Result>(PID: 707);
|
|
||||||
//res = db.Procedures.GetProductDesc_withDefaultparameters<GetProductDesc_Result>();
|
|
||||||
|
|
||||||
//int id = -1;
|
|
||||||
//var resD = db.Procedures.ins_NewEmp_with_outputparamaters(Ename: "Test2", out_EId: id);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class sp_Exp_SomeData_Result
|
private class sp_Exp_SomeData_Result
|
||||||
|
|||||||
Reference in New Issue
Block a user