/// <summary> /// Get the ColumnSchema /// </summary> /// <param name="datatype"></param> /// <returns></returns> public SafeValueType <string, ColumnSchema> GetColumnSchema(Type datatype) { try { var key = datatype.FullName + _transaction.DataBaseTypes.ToString(); if (CachedColumnSchema.ContainsKey(key)) { return(CachedColumnSchema[key]); } datatype = datatype.GetActualType(); var tableName = datatype.TableName(); var sql = $"SELECT COLUMN_NAME as columnname, data_type as datatype ,TABLE_CATALOG as db,TABLE_NAME as tb , IS_NULLABLE as isnullable FROM INFORMATION_SCHEMA.COLUMNS WHERE LOWER(TABLE_NAME) = LOWER(String[{tableName.Name}])"; if (_transaction.DataBaseTypes == DataBaseTypes.Sqllight) { sql = $"SELECT name as columnname, type as datatype FROM pragma_table_info(String[{tableName.Name}])"; } var columns = (List <ColumnSchema>)_transaction.DataReaderConverter(_transaction.GetSqlCommand(sql), typeof(ColumnSchema)); var dic = new SafeValueType <string, ColumnSchema>(); if (columns != null) { columns.ForEach(x => dic.Add(x.ColumnName, x)); } _transaction.CounterException = 0; return(CachedColumnSchema.GetOrAdd(key, dic)); } catch (Exception e) { if (_transaction.DataBaseTypes == DataBaseTypes.PostgreSql && _transaction.CounterException <= 3) { _transaction.CounterException++; _transaction.Renew(); return(GetColumnSchema(datatype)); } else { throw new EntityException(e.Message); } } }
internal static ISqlCommand ProcessSql(this IRepository repository, TransactionSqlConnection connection, IDbTransaction tran, string sql) { var i = 1; var dicCols = new SafeValueType <string, Tuple <object, SqlDbType> >(); MatchCollection matches = null; while ((matches = stringExp.Matches(sql)).Count > 0) { var exp = matches[0]; var col = "@CO" + i + "L"; object str = exp.Value.TrimEnd(']').Substring(@"String\[".Length - 1); sql = sql.Remove(exp.Index, exp.Value.Length); sql = sql.Insert(exp.Index, col); var v = str.ConvertValue <string>(); if (string.Equals(v, "null", StringComparison.OrdinalIgnoreCase)) { v = null; } dicCols.TryAdd(col, new Tuple <object, SqlDbType>(v, SqlDbType.NVarChar)); i++; } while ((matches = dateExp.Matches(sql)).Count > 0) { var exp = matches[0]; var col = "@CO" + i + "L"; object str = exp.Value.TrimEnd(']').Substring(@"Date\[".Length - 1); sql = sql.Remove(exp.Index, exp.Value.Length); sql = sql.Insert(exp.Index, col); dicCols.TryAdd(col, new Tuple <object, SqlDbType>(str.ConvertValue <DateTime?>(), SqlDbType.DateTime)); i++; } while ((matches = guidExp.Matches(sql)).Count > 0) { var exp = matches[0]; var col = "@CO" + i + "L"; object str = exp.Value.TrimEnd(']').Substring(@"Guid\[".Length - 1); sql = sql.Remove(exp.Index, exp.Value.Length); sql = sql.Insert(exp.Index, col); dicCols.TryAdd(col, new Tuple <object, SqlDbType>(str.ConvertValue <Guid?>(), SqlDbType.UniqueIdentifier)); i++; } sql = sql.CleanValidSqlName(repository.DataBaseTypes); DbCommand cmd = null; try { switch (repository.DataBaseTypes) { case DataBaseTypes.Mssql: cmd = tran != null ? "System.Data.SqlClient.SqlCommand".GetObjectType("System.Data.SqlClient").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand : "System.Data.SqlClient.SqlCommand".GetObjectType("System.Data.SqlClient").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand; break; case DataBaseTypes.PostgreSql: cmd = tran != null ? "Npgsql.NpgsqlCommand".GetObjectType("Npgsql").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand : "Npgsql.NpgsqlCommand".GetObjectType("Npgsql").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand; break; case DataBaseTypes.Sqllight: cmd = tran != null ? "System.Data.SQLite.SQLiteCommand".GetObjectType("System.Data.SQLite").CreateInstance(new object[] { sql, connection.DBConnection, tran }) as DbCommand : "System.Data.SQLite.SQLiteCommand".GetObjectType("System.Data.SQLite").CreateInstance(new object[] { sql, connection.DBConnection }) as DbCommand; break; } } catch (Exception e) { switch (repository.DataBaseTypes) { case DataBaseTypes.Mssql: throw new EntityException($"Please make sure that nuget 'System.Data.SqlClient' is installed \n orginal exception: \n {e.Message}"); case DataBaseTypes.PostgreSql: throw new EntityException($"Please make sure that nuget 'Npgsql' is installed \n orginal exception: \n {e.Message}"); case DataBaseTypes.Sqllight: throw new EntityException($"Please make sure that nuget 'System.Data.SQLite' is installed \n orginal exception: \n {e.Message}"); } } var dbCommandExtended = new DbCommandExtended(cmd, repository); foreach (var dic in dicCols) { dbCommandExtended.AddInnerParameter(dic.Key, dic.Value.Item1); } return(dbCommandExtended); }
internal static IList DataReaderConverter(Transaction.Transaction repository, IDataReader reader, ISqlCommand command, Type type) { var tType = type.GetActualType(); var attachable = tType.GetPrimaryKey() != null; var baseListType = typeof(List <>); var listType = baseListType.MakeGenericType(tType); var iList = DeepCloner.CreateInstance(listType) as IList; var props = DeepCloner.GetFastDeepClonerProperties(tType); try { var colNames = new SafeValueType <int, string>(); var pp = new SafeValueType <int, IFastDeepClonerProperty>(); while (reader.Read()) { object item = null; object clItem = null; item = DeepCloner.CreateInstance(tType); clItem = attachable ? DeepCloner.CreateInstance(tType) : null; var col = 0; while (col < reader.FieldCount) { string columnName; if (colNames.ContainsKey(col)) { columnName = colNames[col]; } else { columnName = reader.GetName(col); colNames.TryAdd(col, columnName); } var value = reader[columnName]; IFastDeepClonerProperty prop; if (!pp.ContainsKey(col)) { prop = DeepCloner.GetProperty(tType, columnName); if (prop == null) { prop = props.FirstOrDefault(x => string.Equals(x.GetPropertyName(), columnName, StringComparison.CurrentCultureIgnoreCase) || x.GetPropertyName().ToLower() == columnName); } pp.TryAdd(col, prop); } else { prop = pp[col]; } if (prop != null && value != DBNull.Value && value != null && prop.CanRead) { if (value as byte[] != null && prop.PropertyType.FullName.Contains("Guid")) { value = new Guid(value as byte[]); } var dataEncode = prop.GetCustomAttribute <DataEncode>(); if (prop.ContainAttribute <ToBase64String>()) { if (value.ConvertValue <string>().IsBase64String()) { value = MethodHelper.DecodeStringFromBase64(value.ConvertValue <string>()); } else { value = MethodHelper.ConvertValue(value, prop.PropertyType); } } else if (dataEncode != null) { value = new DataCipher(dataEncode.Key, dataEncode.KeySize).Decrypt(value.ConvertValue <string>()); } else if (prop.ContainAttribute <JsonDocument>()) { value = value?.ToString().FromJson(prop.PropertyType); } else if (prop.ContainAttribute <XmlDocument>()) { value = value?.ToString().FromXml(); } else { value = MethodHelper.ConvertValue(value, prop.PropertyType); } prop.SetValue(item, value); if (attachable) { prop.SetValue(clItem, value); } } col++; } if (clItem != null && !(repository?.IsAttached(clItem) ?? true)) { repository?.AttachNew(clItem); } iList.Add(item); } } catch (Exception e) { throw new EntityException(e.Message); } finally { reader.Close(); reader.Dispose(); if (repository.OpenedDataReaders.ContainsKey(reader)) { repository.OpenedDataReaders.Remove(reader); } } return(iList); }
public InternalDictionary(Dictionary <T, P> dic = null) { keyValuePairs = new SafeValueType <T, P>(dic); }