/// <summary> /// DeleteAsync /// </summary> /// <param name="keys"></param> /// <param name="versions"></param> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task DeleteAsync <T>(IEnumerable <string> keys, IEnumerable <int> versions) where T : KVStoreEntity, new() { ThrowIf.NullOrEmpty(versions, nameof(versions)); if (keys.Count() != versions.Count()) { throw Exceptions.VersionsKeysNotEqualError(); } KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { await _engine.EntityDeleteAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !, keys, versions ).ConfigureAwait(false); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, keys: keys, values: versions, innerException: ex); } }
public void EntityMapper_ToParameter_Performance_Test(EngineType engineType) { var entities = Mocker.GetPublishers(1000000); var def = EntityDefFactory.GetDef <PublisherEntity>(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Restart(); foreach (var entity in entities) { _ = entity.ToParameters(def !, engineType); } stopwatch.Stop(); _output.WriteLine($"Emit: {stopwatch.ElapsedMilliseconds}"); stopwatch.Restart(); foreach (var entity in entities) { _ = entity.ToParametersUsingReflection(def !, engineType); } stopwatch.Stop(); _output.WriteLine($"Reflection: {stopwatch.ElapsedMilliseconds}"); }
public void EntityMapper_ToParameter_Test(EngineType engineType) { PublisherEntity publisherEntity = Mocker.MockOnePublisherEntity(); var emit_results = publisherEntity.ToParameters(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1); var reflect_results = publisherEntity.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity>() !, engineType, 1); AssertEqual(emit_results, reflect_results, engineType); //PublisherEntity2 PublisherEntity2 publisherEntity2 = new PublisherEntity2(); var emit_results2 = publisherEntity2.ToParameters(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1); var reflect_results2 = publisherEntity2.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity2>() !, engineType, 1); AssertEqual(emit_results2, reflect_results2, engineType); //PublisherEntity3 PublisherEntity3 publisherEntity3 = new PublisherEntity3(); var emit_results3 = publisherEntity3.ToParameters(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1); var reflect_results3 = publisherEntity3.ToParametersUsingReflection(EntityDefFactory.GetDef <PublisherEntity3>() !, engineType, 1); AssertEqual(emit_results3, reflect_results3, engineType); }
private FromExpression <T> InternalJoin <Target>(string joinType, Expression joinExpr) where Target : DatabaseEntity { EntityDef targetDef = EntityDefFactory.GetDef <Target>() !; _statementBuilder.Append(' '); _statementBuilder.Append(joinType); _statementBuilder.Append(' '); _statementBuilder.Append(targetDef.DbTableReservedName); _statementBuilder.Append(" ON "); _statementBuilder.Append(joinExpr.ToStatement(_expressionContext)); _statementBuilder.Append(' '); return(this); }
/// <summary> /// DeleteAllAsync /// </summary> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task DeleteAllAsync <T>() where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { await _engine.EntityDeleteAllAsync( entityDef.KVStoreName, entityDef.EntityType.FullName ! ).ConfigureAwait(false); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, null, ex); } }
/// <summary> /// 反应Version变化 /// </summary> /// <exception cref="KVStoreException"></exception> public async Task <IEnumerable <T?> > GetAllAsync <T>() where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { IEnumerable <Tuple <string?, int> > tuples = await _engine.EntityGetAllAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !).ConfigureAwait(false); return(MapTupleToEntity <T>(tuples)); } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(type: typeof(T).FullName, storeName: entityDef.KVStoreName, key: null, innerException: ex); } }
/// <summary> /// 反应Version变化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <param name="lastUser"></param> /// <returns></returns> /// <exception cref="KVStoreException"></exception> public async Task UpdateAsync <T>(IEnumerable <T> items, string lastUser) where T : KVStoreEntity, new() { if (!items.Any()) { return; } ThrowIf.NotValid(items, nameof(items)); KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); try { IEnumerable <int> originalVersions = items.Select(t => t.Version).ToArray(); foreach (var t in items) { t.LastUser = lastUser; t.LastTime = TimeUtil.UtcNow; } await _engine.EntityUpdateAsync( entityDef.KVStoreName, entityDef.EntityType.FullName !, items.Select(t => GetEntityKey(t, entityDef)), items.Select(t => SerializeUtil.ToJson(t)), originalVersions).ConfigureAwait(false); //反应Version变化 foreach (var t in items) { t.Version++; } } catch (Exception ex) when(!(ex is KVStoreException)) { throw Exceptions.Unkown(entityDef.EntityType.FullName, entityDef.KVStoreName, items, ex); } }
/// <summary> /// DeleteAsync /// </summary> /// <param name="database"></param> /// <param name="whereExpr"></param> /// <param name="transactionContext"></param> /// <returns></returns> /// <exception cref="DatabaseException"></exception> public static async Task DeleteAsync <T>(this IDatabase database, Expression <Func <T, bool> > whereExpr, TransactionContext?transactionContext = null) where T : DatabaseEntity, new() { EntityDef entityDef = EntityDefFactory.GetDef <T>() !; if (!entityDef.DatabaseWriteable) { throw Exceptions.NotWriteable(entityDef.EntityFullName, entityDef.DatabaseName); } try { WhereExpression <T> where = database.Where(whereExpr).And(t => !t.Deleted); var command = DbCommandBuilder.CreateDeleteCommand(database.EngineType, entityDef, where); await database.DatabaseEngine.ExecuteCommandNonQueryAsync(transactionContext?.Transaction, entityDef.DatabaseName !, command).ConfigureAwait(false); } catch (Exception ex) when(!(ex is DatabaseException)) { throw Exceptions.UnKown(entityDef.EntityFullName, whereExpr.ToString(), ex); } }
/// <summary> /// AddOrUpdateByIdAsync /// </summary> /// <param name="database"></param> /// <param name="item"></param> /// <param name="transContext"></param> /// <returns></returns> /// <exception cref="DatabaseException"></exception> public static async Task AddOrUpdateByIdAsync <T>(this IDatabase database, T item, TransactionContext?transContext = null) where T : DatabaseEntity, new() { ThrowIf.NotValid(item, nameof(item)); EntityDef entityDef = EntityDefFactory.GetDef <T>() !; if (!entityDef.DatabaseWriteable) { throw Exceptions.NotWriteable(entityDef.EntityFullName, entityDef.DatabaseName); } try { item.LastTime = TimeUtil.UtcNow; if (item.Version < 0) { item.Version = 0; } var command = DbCommandBuilder.CreateAddOrUpdateCommand(database.EngineType, entityDef, item); using var reader = await database.DatabaseEngine.ExecuteCommandReaderAsync(transContext?.Transaction, entityDef.DatabaseName !, command, true).ConfigureAwait(false); IList <T> entities = reader.ToEntities <T>(database.EngineType, entityDef); T newItem = entities[0]; item.CreateTime = newItem.CreateTime; item.Version = newItem.Version; item.LastUser = newItem.LastUser; } catch (Exception ex) when(!(ex is DatabaseException)) { throw Exceptions.UnKown(entityDef.EntityFullName, SerializeUtil.ToJson(item), ex); } }
public string GetEntityKey <T>(T item) where T : KVStoreEntity, new() { KVStoreEntityDef entityDef = EntityDefFactory.GetDef <T>(); return(GetEntityKey(item, entityDef)); }
public DefaultKVStore(IKVStoreEngine kvstoreEngine) { _engine = kvstoreEngine; EntityDefFactory.Initialize(kvstoreEngine); }
public async Task Test_EntityMapperPerformanceAsync(int index) { index++; IDatabase database = _sqlite; IList <BookEntity_Client>?books = Mocker.GetBooks_Client(500); TransactionContext?trans = await _sqlIteTransaction.BeginTransactionAsync <BookEntity_Client>().ConfigureAwait(false); IEnumerable <BookEntity_Client> re = await database.RetrieveAsync <BookEntity_Client>(b => b.Deleted, trans).ConfigureAwait(false); await database.AddAsync <BookEntity_Client>(Mocker.GetBooks_Client(1)[0], "", trans).ConfigureAwait(false); try { //await database.AddAsync<BookEntity>(books[0], "", trans); await database.BatchAddAsync(books, "x", trans).ConfigureAwait(false); await _sqlIteTransaction.CommitAsync(trans).ConfigureAwait(false); } catch { await _sqlIteTransaction.RollbackAsync(trans).ConfigureAwait(false); } Stopwatch stopwatch = new Stopwatch(); using SqliteConnection mySqlConnection = new SqliteConnection("Data Source=sqlite_test2.db"); //time = 0; int loop = 10; TimeSpan time0 = TimeSpan.Zero, time1 = TimeSpan.Zero, time2 = TimeSpan.Zero, time3 = TimeSpan.Zero; for (int cur = 0; cur < loop; ++cur) { await mySqlConnection.OpenAsync().ConfigureAwait(false); using SqliteCommand command0 = new SqliteCommand("select * from tb_bookentity_client limit 1000", mySqlConnection); SqliteDataReader?reader0 = await command0.ExecuteReaderAsync().ConfigureAwait(false); List <BookEntity_Client> list1 = new List <BookEntity_Client>(); List <BookEntity_Client> list2 = new List <BookEntity_Client>(); List <BookEntity_Client> list3 = new List <BookEntity_Client>(); int len = reader0.FieldCount; EntityPropertyDef[] propertyDefs = new EntityPropertyDef[len]; MethodInfo[] setMethods = new MethodInfo[len]; EntityDef definition = EntityDefFactory.GetDef <BookEntity_Client>() !; for (int i = 0; i < len; ++i) { propertyDefs[i] = definition.GetPropertyDef(reader0.GetName(i)) !; setMethods[i] = propertyDefs[i].SetMethod; } Func <IDataReader, object> mapper1 = EntityMapperDelegateCreator.CreateToEntityDelegate(definition, reader0, 0, definition.FieldCount, false, Database.Engine.EngineType.SQLite); //Warning: 如果用Dapper,小心DateTimeOffset的存储,会丢失offset,然后转回来时候,会加上当地时间的offset TypeHandlerHelper.AddTypeHandlerImpl(typeof(DateTimeOffset), new DateTimeOffsetTypeHandler(), false); Func <IDataReader, object> mapper2 = DataReaderTypeMapper.GetTypeDeserializerImpl(typeof(BookEntity_Client), reader0); Stopwatch stopwatch1 = new Stopwatch(); Stopwatch stopwatch2 = new Stopwatch(); Stopwatch stopwatch3 = new Stopwatch(); while (reader0.Read()) { stopwatch1.Start(); object obj1 = mapper1(reader0); list1.Add((BookEntity_Client)obj1); stopwatch1.Stop(); stopwatch2.Start(); object obj2 = mapper2(reader0); list2.Add((BookEntity_Client)obj2); stopwatch2.Stop(); stopwatch3.Start(); BookEntity_Client item = new BookEntity_Client(); for (int i = 0; i < len; ++i) { EntityPropertyDef property = propertyDefs[i]; object?value = TypeConvert.DbValueToTypeValue(reader0[i], property, Database.Engine.EngineType.SQLite); if (value != null) { setMethods[i].Invoke(item, new object?[] { value }); } } list3.Add(item); stopwatch3.Stop(); } time1 += stopwatch1.Elapsed; time2 += stopwatch2.Elapsed; time3 += stopwatch3.Elapsed; await reader0.DisposeAsync().ConfigureAwait(false); command0.Dispose(); await mySqlConnection.CloseAsync().ConfigureAwait(false); } _output.WriteLine("Emit Coding : " + (time1.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture)); _output.WriteLine("Dapper : " + (time2.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture)); _output.WriteLine("Reflection : " + (time3.TotalMilliseconds / (loop * 1.0)).ToString(CultureInfo.InvariantCulture)); }
public string ToStatement() { return($" FROM {EntityDefFactory.GetDef<T>()!.DbTableReservedName} {_statementBuilder}"); }
public Task <TransactionContext> BeginTransactionAsync <T>(IsolationLevel?isolationLevel = null) where T : DatabaseEntity { EntityDef entityDef = EntityDefFactory.GetDef <T>() !; return(BeginTransactionAsync(entityDef.DatabaseName !, isolationLevel)); }