This commit is contained in:
@@ -860,6 +860,31 @@ namespace DynamORM
|
||||
|
||||
#region Schema
|
||||
|
||||
/// <summary>Builds query cache if necessary and returns it.</summary>
|
||||
/// <param name="builder">The builder containing query to read schema from.</param>
|
||||
/// <returns>Query schema.</returns>
|
||||
public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(IDynamicSelectQueryBuilder builder)
|
||||
{
|
||||
using (var con = Open())
|
||||
using (var cmd = con.CreateCommand().SetCommand(builder))
|
||||
return ReadSchema(cmd)
|
||||
.Distinct()
|
||||
.ToDictionary(k => k.Name.ToLower(), k => k);
|
||||
}
|
||||
|
||||
/// <summary>Builds query cache if necessary and returns it.</summary>
|
||||
/// <param name="sql">Sql query from which read schema.</param>
|
||||
/// <param name="args">Sql query argumants.</param>
|
||||
/// <returns>Query schema.</returns>
|
||||
public Dictionary<string, DynamicSchemaColumn> GetQuerySchema(string sql, params object[] args)
|
||||
{
|
||||
using (var con = Open())
|
||||
using (var cmd = con.CreateCommand().SetCommand(sql, args))
|
||||
return ReadSchema(cmd)
|
||||
.Distinct()
|
||||
.ToDictionary(k => k.Name.ToLower(), k => k);
|
||||
}
|
||||
|
||||
/// <summary>Builds table cache if necessary and returns it.</summary>
|
||||
/// <param name="table">Name of table for which build schema.</param>
|
||||
/// <param name="owner">Owner of table for which build schema.</param>
|
||||
@@ -917,29 +942,35 @@ namespace DynamORM
|
||||
protected virtual IEnumerable<DynamicSchemaColumn> ReadSchema(string table, string owner)
|
||||
{
|
||||
using (var con = Open())
|
||||
using (var cmd = con.CreateCommand())
|
||||
{
|
||||
using (var rdr = cmd
|
||||
.SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0",
|
||||
!string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty,
|
||||
DecorateName(table)))
|
||||
.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo))
|
||||
foreach (DataRow col in rdr.GetSchemaTable().Rows)
|
||||
{
|
||||
var c = col.RowToDynamicUpper();
|
||||
using (var cmd = con.CreateCommand()
|
||||
.SetCommand(string.Format("SELECT * FROM {0}{1} WHERE 1 = 0",
|
||||
!string.IsNullOrEmpty(owner) ? string.Format("{0}.", DecorateName(owner)) : string.Empty,
|
||||
DecorateName(table))))
|
||||
return ReadSchema(cmd);
|
||||
}
|
||||
|
||||
yield return new DynamicSchemaColumn
|
||||
{
|
||||
Name = c.COLUMNNAME,
|
||||
Type = DynamicExtensions.TypeMap.TryGetNullable((Type)c.DATATYPE) ?? DbType.String,
|
||||
IsKey = c.ISKEY ?? false,
|
||||
IsUnique = c.ISUNIQUE ?? false,
|
||||
Size = (int)(c.COLUMNSIZE ?? 0),
|
||||
Precision = (byte)(c.NUMERICPRECISION ?? 0),
|
||||
Scale = (byte)(c.NUMERICSCALE ?? 0)
|
||||
};
|
||||
}
|
||||
}
|
||||
/// <summary>Get schema describing objects from reader.</summary>
|
||||
/// <param name="cmd">Command containing query to execute.</param>
|
||||
/// <returns>List of <see cref="DynamicSchemaColumn"/> objects .
|
||||
/// If your database doesn't get those values in upper case (like most of the databases) you should override this method.</returns>
|
||||
protected virtual IEnumerable<DynamicSchemaColumn> ReadSchema(IDbCommand cmd)
|
||||
{
|
||||
using (var rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo))
|
||||
foreach (DataRow col in rdr.GetSchemaTable().Rows)
|
||||
{
|
||||
var c = col.RowToDynamicUpper();
|
||||
|
||||
yield return new DynamicSchemaColumn
|
||||
{
|
||||
Name = c.COLUMNNAME,
|
||||
Type = DynamicExtensions.TypeMap.TryGetNullable((Type)c.DATATYPE) ?? DbType.String,
|
||||
IsKey = c.ISKEY ?? false,
|
||||
IsUnique = c.ISUNIQUE ?? false,
|
||||
Size = (int)(c.COLUMNSIZE ?? 0),
|
||||
Precision = (byte)(c.NUMERICPRECISION ?? 0),
|
||||
Scale = (byte)(c.NUMERICSCALE ?? 0)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private Dictionary<string, DynamicSchemaColumn> BuildAndCacheSchema(string tableName, DynamicTypeMap mapper, string owner = null)
|
||||
@@ -959,6 +990,7 @@ namespace DynamORM
|
||||
if (databaseSchemaSupport && !Schema.ContainsKey(tableName.ToLower()))
|
||||
{
|
||||
schema = ReadSchema(tableName, owner)
|
||||
.Distinct()
|
||||
.ToDictionary(k => k.Name.ToLower(), k => k);
|
||||
|
||||
Schema[tableName.ToLower()] = schema;
|
||||
@@ -1237,7 +1269,47 @@ namespace DynamORM
|
||||
{
|
||||
_tempConn = Open() as DynamicConnection;
|
||||
|
||||
return _tempConn.BeginTransaction(null, () =>
|
||||
return _tempConn.BeginTransaction(null, null, () =>
|
||||
{
|
||||
var t = TransactionPool.TryGetValue(_tempConn.Connection);
|
||||
|
||||
if (t == null | t.Count == 0)
|
||||
{
|
||||
_tempConn.Dispose();
|
||||
_tempConn = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Begins a database transaction with the specified
|
||||
/// <see cref="System.Data.IsolationLevel"/> value.</summary>
|
||||
/// <param name="il">One of the <see cref="System.Data.IsolationLevel"/> values.</param>
|
||||
/// <returns>Returns <see cref="DynamicTransaction"/> representation.</returns>
|
||||
public IDbTransaction BeginTransaction(IsolationLevel il)
|
||||
{
|
||||
_tempConn = Open() as DynamicConnection;
|
||||
|
||||
return _tempConn.BeginTransaction(il, null, () =>
|
||||
{
|
||||
var t = TransactionPool.TryGetValue(_tempConn.Connection);
|
||||
|
||||
if (t == null | t.Count == 0)
|
||||
{
|
||||
_tempConn.Dispose();
|
||||
_tempConn = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>Begins a database transaction with the specified
|
||||
/// <see cref="System.Data.IsolationLevel"/> value.</summary>
|
||||
/// <param name="custom">Custom parameter describing transaction options.</param>
|
||||
/// <returns>Returns <see cref="DynamicTransaction"/> representation.</returns>
|
||||
public IDbTransaction BeginTransaction(object custom)
|
||||
{
|
||||
_tempConn = Open() as DynamicConnection;
|
||||
|
||||
return _tempConn.BeginTransaction(null, custom, () =>
|
||||
{
|
||||
var t = TransactionPool.TryGetValue(_tempConn.Connection);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user