public SqlServerBulkCopy(ITransactionalDbClient dbClient) { Ensure.That(dbClient, "dbClient").IsNotNull(); _dbClient = dbClient; Initialize(dbClient.Connection, dbClient.Transaction); }
public static IStructureBuilder GetDefaultBuilderForInserts(IStructureBuilders builders, IStructureSchema schema, IDbClient dbClient) { Ensure.That(builders, "builders").IsNotNull(); Ensure.That(schema, "schema").IsNotNull(); Ensure.That(dbClient, "dbClient").IsNotNull(); var idType = schema.IdAccessor.IdType; if (idType.IsGuid()) return new StructureBuilder { StructureIdGenerator = builders.GuidStructureIdGeneratorFn(schema), StructureSerializer = builders.StructureSerializerFn() }; if (idType.IsIdentity()) return new StructureBuilder { StructureIdGenerator = builders.IdentityStructureIdGeneratorFn(schema, dbClient), StructureSerializer = builders.StructureSerializerFn() }; if (idType.IsString()) return new StructureBuilderPreservingId { StructureIdGenerator = new EmptyStructureIdGenerator(), StructureSerializer = builders.StructureSerializerFn() }; throw new SisoDbException(ExceptionMessages.StructureBuilders_CreateForInsert.Inject(idType, schema.Name)); }
private string[] GetDbKeyNamesToDrop(IStructureSchema structureSchema, IDbClient dbClient) { var structureFields = new HashSet<string>(structureSchema.IndexAccessors.Select(iac => iac.Path)); var keyNames = GetExistingDbKeyNames(structureSchema, dbClient); return keyNames.Where(kn => !structureFields.Contains(kn)).ToArray(); }
private bool SchemaIsAllreadyUpserted(IStructureSchema structureSchema, IDbClient dbClient) { if (UpsertedSchemas.Contains(structureSchema.Name) || TransientSchemas.Contains(structureSchema.Name)) return true; return (UpsertedSchemasByDbClient.ContainsKey(dbClient.Id) && UpsertedSchemasByDbClient[dbClient.Id].Contains(structureSchema.Name)); }
public void Synchronize(IStructureSchema structureSchema, IDbClient dbClient) { var keyNamesToDrop = GetDbKeyNamesToDrop(structureSchema, dbClient); if (keyNamesToDrop.Any()) DeleteRecordsMatchingKeyNames(structureSchema, keyNamesToDrop, dbClient); }
private void DeleteRecordsMatchingKeyNames(string indexesTableName, IEnumerable<string> names, IDbClient dbClient) { var inString = string.Join(",", names.Select(n => "'" + n + "'")); var sql = _sqlStatements.GetSql("IndexesSchemaSynchronizer_DeleteRecordsMatchingKeyNames") .Inject(indexesTableName, IndexStorageSchema.Fields.MemberPath.Name, inString); dbClient.ExecuteNonQuery(sql); }
public virtual void DropStructureSet(IStructureSchema structureSchema, IDbClient dbClient) { lock (_upsertedSchemas) { _upsertedSchemas.Remove(structureSchema.Name); dbClient.Drop(structureSchema); } }
public virtual void Drop(IStructureSchema structureSchema, IDbClient dbClient) { lock (Lock) { UpsertedSchemas.Remove(structureSchema.Name); TransientSchemas.Remove(structureSchema.Name); dbClient.Drop(structureSchema); } }
public void Dispose() { GC.SuppressFinalize(this); if (_innerBulkCopy != null) { _innerBulkCopy.Close(); _innerBulkCopy = null; } _dbClient = null; }
private IEnumerable<string> GetExistingDbMemberPaths(string indexesTableName, IDbClient dbClient) { var dbColumns = new List<string>(); dbClient.SingleResultSequentialReader( _sqlStatements.GetSql("IndexesSchemaSynchronizer_GetKeyNames").Inject( IndexStorageSchema.Fields.MemberPath.Name, indexesTableName), dr => dbColumns.Add(dr.GetString(0))); return dbColumns; }
private IEnumerable<string> GetExistingDbKeyNames(IStructureSchema structureSchema, IDbClient dbClient) { var dbColumns = new List<string>(); dbClient.Read( _sqlStatements.GetSql("UniquesSchemaSynchronizer_GetKeyNames").Inject( UniqueStorageSchema.Fields.UqMemberPath.Name, structureSchema.GetUniquesTableName()), dr => dbColumns.Add(dr.GetString(0))); return dbColumns; }
public void Synchronize(IStructureSchema structureSchema, IDbClient dbClient, string[] indexesTableNames) { if(!indexesTableNames.Any()) return; var structureFields = new HashSet<string>(structureSchema.IndexAccessors.Select(iac => iac.Path)); foreach (var indexesTableName in indexesTableNames) { var keyNamesToDrop = GetMemberPathsToDrop(indexesTableName, structureFields, dbClient); if (keyNamesToDrop.Length > 0) DeleteRecordsMatchingKeyNames(indexesTableName, keyNamesToDrop, dbClient); } }
public virtual void Upsert(IStructureSchema structureSchema, IDbClient dbClient) { if (!Db.Settings.AllowsAnyDynamicSchemaChanges()) return; if (SchemaIsAllreadyUpserted(structureSchema, dbClient)) return; lock (Lock) { RegisterDbClient(dbClient); DbSchemaUpserter.Upsert(structureSchema, dbClient, Db.Settings.AllowDynamicSchemaCreation, Db.Settings.AllowDynamicSchemaUpdates); UpsertedSchemasByDbClient[dbClient.Id].Add(structureSchema.Name); TransientSchemas.Add(structureSchema.Name); } }
public virtual void Upsert(IStructureSchema structureSchema, IDbClient dbClient, bool allowDynamicSchemaCreation, bool allowDynamicSchemaUpdates) { if (!allowDynamicSchemaCreation && !allowDynamicSchemaUpdates) return; var modelInfo = dbClient.GetModelTablesInfo(structureSchema); if (allowDynamicSchemaUpdates) { IndexesDbSchemaSynchronizer.Synchronize(structureSchema, dbClient, modelInfo.GetIndexesTableNamesForExisting()); if (modelInfo.Statuses.UniquesTableExists) UniquesDbSchemaSynchronizer.Synchronize(structureSchema, dbClient); } if (!allowDynamicSchemaCreation || modelInfo.Statuses.AllExists) return; foreach (var sql in GenerateSql(structureSchema, modelInfo)) dbClient.ExecuteNonQuery(sql, new DacParameter(DbSchemaInfo.Parameters.EntityNameParamPrefix, structureSchema.Name)); }
public static IStructureBuilder GetBuilderForInsertsAssigningIfMissingId(IStructureBuilders builders, IStructureSchema schema, IDbClient dbClient) { Ensure.That(builders, "builders").IsNotNull(); Ensure.That(schema, "schema").IsNotNull(); Ensure.That(dbClient, "dbClient").IsNotNull(); IStructureIdGenerator idGenerator; var idType = schema.IdAccessor.IdType; if (idType.IsGuid()) idGenerator = builders.GuidStructureIdGeneratorFn(schema); else if (idType.IsIdentity()) idGenerator = builders.IdentityStructureIdGeneratorFn(schema, dbClient); else if (idType.IsString()) idGenerator = new EmptyStructureIdGenerator(); else throw new SisoDbException(ExceptionMessages.StructureBuilders_CreateForInsert.Inject(idType, schema.Name)); return new StructureBuilderAutoId { StructureIdGenerator = idGenerator, StructureSerializer = builders.StructureSerializerFn() }; }
/// <summary> /// 获取查询结果得行序列。 /// </summary> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">参数表。</param> /// <returns>查询结果得行序列。</returns> public static IEnumerable <IDataRecord> IxRows(this IDbClient client, string sql, params object[] param) { return(client.Rows(sql, GenerateParameters(client, param))); }
/// <summary> /// 使用<see cref="IMapper{T}"/>查询指定对象。 /// SQL命中的记录必须为1行,否则抛出异常。 /// </summary> /// <typeparam name="T">查询的目标类型。</typeparam> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="mapper"><see cref="IMapper{T}"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">参数表。</param> /// <returns>目标类型的实例。</returns> /// <exception cref="IncorrectResultSizeException">当SQL命中的记录行数不为 1。</exception> public static T IxForceGet <T>(this IDbClient client, IMapper <T> mapper, string sql, params object[] param) { return(client.ForceGet(mapper, sql, GenerateParameters(client, param))); }
/// <summary> /// 获取查询结果的第一行记录。 /// 若查询命中的行数为0,返回null。 /// </summary> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">参数表。</param> /// <returns><see cref="IDataRecord"/>的实现,包含查询的第一行记录。</returns> public static IDataRecord IxGetRow(this IDbClient client, string sql, params object[] param) { return(client.GetRow(sql, GenerateParameters(client, param))); }
public RouletteServices(IDbClient dbClient) { contextRoulette = dbClient.GetRouletteCollection(); }
protected virtual bool OnMigrationOfStructureSet <TFrom, TFromTemplate, TTo>(bool isMigratingSameStructureSet, IStructureSchema structureSchemaFrom, IStructureSchema structureSchemaFromTemplate, IStructureSchema structureSchemaTo, IDbClient dbClientTransactional, Func <TFromTemplate, TTo, MigrationStatuses> modifier) where TFrom : class where TFromTemplate : class where TTo : class { var maxKeepQueueSize = Db.Settings.MaxInsertManyBatchSize; var serializer = Db.Serializer; var keepQueue = new List <TTo>(maxKeepQueueSize); var trashQueue = new List <IStructureId>(maxKeepQueueSize); var structureBuilder = Db.StructureBuilders.ResolveBuilderForUpdate(structureSchemaTo); Db.DbSchemas.Upsert(structureSchemaTo, dbClientTransactional); using (var dbClientNonTransactional = Db.ProviderFactory.GetNonTransactionalDbClient(Db.ConnectionInfo)) { foreach (var json in dbClientNonTransactional.GetJsonOrderedByStructureId(structureSchemaFrom)) { var oldItem = serializer.Deserialize <TFromTemplate>(json); var oldId = GetOldStructureId(structureSchemaFromTemplate, oldItem); var newItem = serializer.Deserialize <TTo>(json); var modifierStatus = modifier.Invoke(oldItem, newItem); if (modifierStatus == MigrationStatuses.Abort) { return(false); } var newId = GetNewStructureId(structureSchemaTo, newItem); EnsureThatNewIdEqualsOldId(oldId, newId); if (modifierStatus == MigrationStatuses.Keep) { keepQueue.Add(newItem); } if (isMigratingSameStructureSet) { trashQueue.Add(newId); } if (keepQueue.Count == maxKeepQueueSize) { if (isMigratingSameStructureSet) { ProcessTrashQueue(trashQueue, structureSchemaTo, dbClientTransactional); } ProcessKeepQueue(keepQueue, structureSchemaTo, dbClientTransactional, structureBuilder); } } } if (isMigratingSameStructureSet) { ProcessTrashQueue(trashQueue, structureSchemaTo, dbClientTransactional); } ProcessKeepQueue(keepQueue, structureSchemaTo, dbClientTransactional, structureBuilder); return(true); }
public LoaderService(IConfiguration configuration, ApexService apexService, CookieDelegateHandler cookieDelegateHandler, ApexConfigs configs, ILogger <LoaderService> logger, IDbClient dbClient) { _configuration = configuration; _apexService = apexService; _cookieDelegateHandler = cookieDelegateHandler; _apexConfigs = configs; _logger = logger; _dbClient = dbClient; }
public AuthService(IDbClient dbClient, IResource resource) { db = dbClient; this.resource = resource; }
public TestPartsBuilder(IExtract extract, IDbClient client, IWpDbSchema schema) : base(extract, client, schema) { Table = Substitute.For <ITable>(); IdColumn = Substitute.For <IColumn>(); }
protected virtual IStructureBuilder GetDefaultBuilderForInserts(IStructureSchema structureSchema, IDbClient dbClient) { return(StructureBuildersFn.GetDefaultBuilderForInserts(this, structureSchema, dbClient)); }
// 事务传递 static void TransactionPost(IDbClient db) { Console.WriteLine(db.Get <string>("select 'IDbClient'")); }
//query = 'insert into tabelka () values (fdsg);' public void SaveData(string query, IDbClient dbClient) { dbClient.Connect("dfsafgsdg"); dbClient.SaveData(query); }
public DbStructureInserter(IDbClient mainDbClient) { MainDbClient = mainDbClient; }
public JiraClient(IDbClient dbClient) { this.dbClient = dbClient; }
public LoginRepo(IDbClient db) { _db = db; }
public virtual void UpsertStructureSet(IStructureSchema structureSchema, IDbClient dbClient) { UpsertStructureSet(structureSchema, () => dbClient); }
protected virtual void ProcessKeepQueue <T>(IList <T> keepQueue, IStructureSchema structureSchema, IDbClient dbClient, IStructureBuilder structureBuilder) where T : class { if (keepQueue.Count < 1) { return; } var structures = structureBuilder.CreateStructures(keepQueue.ToArray(), structureSchema); keepQueue.Clear(); if (structures.Length == 0) { return; } var bulkInserter = Db.ProviderFactory.GetStructureInserter(dbClient); bulkInserter.Insert(structureSchema, structures); }
public virtual IStructureBuilder ResolveBuilderForInsert(IStructureSchema structureSchema, IDbClient dbClient) { return(ResolveBuilderForInsertsBy(structureSchema, dbClient)); }
public BookService(IDbClient dbClient) { _books = dbClient.GetBooksCollection(); }
public AccountCollection(IDbClient client) : base(client) { }
/// <summary> /// 初始化类型的新实例,并指定被封装的<see cref="IDbClient"/>实例。 /// </summary> /// <param name="client">被封装的<see cref="IDbClient"/>实例。</param> public DbClientWrapper(IDbClient client) { Client = client; }
public DbRepository(IDbClient dbClient) { _dbClient = dbClient; }
public Radius(Parameters parameters) { _db = new DbClient(parameters); _db.AddDebugDelegate(PushDebug); }
protected virtual void ProcessTrashQueue(IList <IStructureId> structureIds, IStructureSchema structureSchema, IDbClient dbClient) { if (!structureIds.Any()) { return; } dbClient.DeleteByIds(structureIds.ToArray(), structureSchema); structureIds.Clear(); }
public SqlCe4DbBulkCopy(IDbClient dbClient) { Ensure.That(dbClient, "dbClient").IsNotNull(); Initialize(dbClient.Connection); }
public DbIdentityStructureIdGenerator(IDbClient dbClient) { Ensure.That(dbClient, "dbClient").IsNotNull(); DbClient = dbClient; }
/// <summary> /// 查询并根据结果创建目标类型的实例,由一个模板对象指定目标类型。 /// SQL命中的记录必须为1行,否则抛出异常。 /// 这是一个异步操作。 /// </summary> /// <typeparam name="T">查询的目标类型。</typeparam> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="template">用于指定目标类型的模板对象。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">记录SQL参数的对象。</param> /// <param name="commandType">命令的类型。</param> /// <param name="timeout">命令的超时时间,单位毫秒。0为不指定。</param> /// <returns>目标类型的实例。</returns> public static Task <T> TemplateForceGetAsync <T>(this IDbClient client, T template, string sql, object param = null, CommandType commandType = CommandType.Text, int timeout = 0) { return(ForceGetAsync <T>(client, sql, param, commandType, timeout)); }
public BookServices(IDbClient dbClient) { this.books = dbClient.GetBooksCollection(); }
/// <summary> /// 查询并根据结果返回目标类型的实例集合。 /// 这是一个异步操作。 /// </summary> /// <typeparam name="T">查询的目标类型。</typeparam> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">记录SQL参数的对象。</param> /// <param name="commandType">命令的类型。</param> /// <param name="timeout">命令的超时时间,单位毫秒。0为不指定。</param> /// <returns>目标类型的实例集合。</returns> public static async Task <IList <T> > ListAsync <T>(this IDbClient client, string sql, object param = null, CommandType commandType = CommandType.Text, int timeout = 0) { return((await QueryAsync <T>(client, sql, param, commandType, timeout)).ToList()); }
/// <summary> /// 获取查询结果的第一行记录,以数组形式返回记录内各列的值。 /// 数组元素顺序与列顺序一致。若查询命中的行数为0,返回null。 /// </summary> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">记录SQL参数的对象。</param> /// <returns>包含了各列的值的数组。</returns> public static object[] IxItemArray(this IDbClient client, string sql, params object[] param) { return(client.ItemArray(sql, GenerateParameters(client, param))); }
private string[] GetMemberPathsToDrop(string indexesTableName, HashSet<string> structureFields, IDbClient dbClient) { return GetExistingDbMemberPaths(indexesTableName, dbClient).Where(kn => !structureFields.Contains(kn)).ToArray(); }
/// <summary> /// 使用<see cref="IMapper{T}"/>查询指定对象的集合。 /// </summary> /// <typeparam name="T">查询的目标类型。</typeparam> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="mapper"><see cref="IMapper{T}"/>的实例。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">参数表。</param> /// <returns>目标类型的实例的集合。若查询命中的行数为0,返回空集合。</returns> public static IList <T> IxList <T>(this IDbClient client, IMapper <T> mapper, string sql, params object[] param) { return(client.List(mapper, sql, GenerateParameters(client, param))); }
public static IStructureBuilder GetBuilderForManualIdAssignment(IStructureBuilders builders, IStructureSchema schema, IDbClient dbClient) { return new StructureBuilderPreservingId { StructureIdGenerator = new EmptyStructureIdGenerator(), StructureSerializer = builders.StructureSerializerFn() }; }
protected virtual void CleanupAfterDbClient(IDbClient dbClient, Action<string> schemaCallback) { lock (Lock) { if (!UpsertedSchemasByDbClient.ContainsKey(dbClient.Id)) return; var schemasForClient = UpsertedSchemasByDbClient[dbClient.Id]; foreach (var schema in schemasForClient) schemaCallback(schema); UpsertedSchemasByDbClient.Remove(dbClient.Id); } }
public virtual IStructureBuilder ResolveBuilderForInsert(IStructureSchema structureSchema, IDbClient dbClient) { return ResolveBuilderForInsertsBy(structureSchema, dbClient); }
public virtual IStructureInserter GetStructureInserter(IDbClient dbClient) { return(new DbStructureInserter(dbClient)); }
protected virtual void RegisterDbClient(IDbClient dbClient) { if (UpsertedSchemasByDbClient.ContainsKey(dbClient.Id)) return; dbClient.AfterCommit = client => CleanupAfterDbClient(client, s => { UpsertedSchemas.Add(s); TransientSchemas.Remove(s); }); dbClient.OnCompleted = client => CleanupAfterDbClient(client, s => TransientSchemas.Remove(s)); UpsertedSchemasByDbClient.Add(dbClient.Id, new HashSet<string>()); }
public virtual IIdentityStructureIdGenerator GetIdentityStructureIdGenerator(IDbClient dbClient) { return(new DbIdentityStructureIdGenerator(dbClient)); }
/// <summary> /// 查询并根据结果返回目标类型的实例序列,由一个模板对象指定目标类型。 /// </summary> /// <typeparam name="T">查询的目标类型。</typeparam> /// <param name="client"><see cref="IDbClient"/>的实例。</param> /// <param name="template">用于指定目标类型的模板对象。</param> /// <param name="sql">SQL语句。</param> /// <param name="param">记录SQL参数的对象。</param> /// <param name="commandType">命令的类型。</param> /// <param name="timeout">命令的超时时间,单位毫秒。0为不指定。</param> /// <returns>目标类型的实例序列。</returns> public static Task <IEnumerable <T> > TemplateQueryAsync <T>(this IDbClient client, T template, string sql, object param = null, CommandType commandType = CommandType.Text, int timeout = 0) { return(QueryAsync <T>(client, sql, param, commandType, timeout)); }
public DbHandle(IDbClient dbClient) { this.DbClient = dbClient; }
protected virtual IStructureBuilder GetDefaultBuilderForInserts(IStructureSchema structureSchema, IDbClient dbClient) { return StructureBuildersFn.GetDefaultBuilderForInserts(this, structureSchema, dbClient); }