private async Task <int> DeleteAsync(object filterEntity, string optionalConnConfig = "", bool isReplicating = false) { string sqlInstruction; int recordsAffected = 0; var entityType = filterEntity.GetType(); var entityProps = entityType.GetProperties(); if (keepConnection || base.Connect(optionalConnConfig)) { sqlInstruction = EntitySqlParser.ParseEntity(filterEntity, engine, PersistenceAction.Delete, filterEntity); recordsAffected = await ExecuteCommandAsync(sqlInstruction); PersistComposition(filterEntity, PersistenceAction.Delete); if (!keepConnection) { base.Disconnect(); } } CleanCacheableData(filterEntity); // Async persistence of database replicas if (base.replicationEnabled && !isReplicating) { DeleteReplicas(filterEntity, entityProps); } return(recordsAffected); }
private async Task <IEnumerable <object> > ListObjectsAsync(object filterEntity, PersistenceAction action, bool loadComposition = false, int recordLimit = 0, bool onlyListableAttributes = false, string showAttributes = null, Dictionary <string, double[]> rangeValues = null, string groupAttributes = null, string orderAttributes = null, bool orderDescending = false) { IEnumerable <object> returnList = null; var sqlInstruction = EntitySqlParser.ParseEntity(filterEntity, engine, action, filterEntity, recordLimit, onlyListableAttributes, showAttributes, rangeValues, groupAttributes, orderAttributes, orderDescending, _readUncommited); if (keepConnection || base.Connect()) { // Getting database return using Dapper returnList = await ExecuteQueryAsync(filterEntity.GetType(), sqlInstruction); } if (!keepConnection) { base.Disconnect(); } // Perform the composition data load when exists (Eager Loading) if (loadComposition && (returnList != null) && returnList.Any()) { var itemProps = returnList.First().GetType().GetProperties(); foreach (var item in returnList) { FillComposition(item, itemProps); } } return(returnList); }
public void GetByFilterTest() { var testFilter = new SampleEntity() { DocNumber = 12345 }; var result = EntitySqlParser.ParseEntity(testFilter, DatabaseEngine.SQLite, PersistenceAction.Get, testFilter); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("SELECT", result); Assert.EndsWith(string.Format("WHERE {0}.{1} = 12345", "sample_entity", "doc_number"), result); }
public void DeleteTest() { var filterEntity = new SampleEntity() { DocNumber = 12345 }; var result = EntitySqlParser.ParseEntity(filterEntity, DatabaseEngine.SQLite, PersistenceAction.Delete, filterEntity); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("DELETE FROM", result); Assert.EndsWith(string.Format("WHERE {0}.{1} = 12345", "sample_entity", "doc_number"), result); }
public void GetByPrimaryKeyTest() { var entityType = typeof(SampleEntity); var entityProps = entityType.GetProperties(); var testFilter = EntityReflector.GetFilterByPrimaryKey(entityType, entityProps, 12345); var result = EntitySqlParser.ParseEntity(testFilter, DatabaseEngine.SQLite, PersistenceAction.Get, testFilter); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("SELECT", result); Assert.EndsWith(string.Format("WHERE {0}.{1} = 12345", "sample_entity", "doc_number"), result); }
public void SearchTest() { var filterType = typeof(SampleEntity); var filterProps = filterType.GetProperties(); var testFilter = EntityReflector.GetFilterByFilterableColumns(typeof(SampleEntity), filterProps, "roberto"); var result = EntitySqlParser.ParseEntity(testFilter, DatabaseEngine.SQLite, PersistenceAction.List, testFilter); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("SELECT", result); Assert.Contains("FROM", result); Assert.EndsWith(string.Format("WHERE {0}.{1} LIKE '%roberto%'", "sample_entity", "name"), result); }
public void ListTest() { var testFilter = new SampleEntity() { Name = "roberto" }; var result = EntitySqlParser.ParseEntity(testFilter, DatabaseEngine.SQLite, PersistenceAction.List, testFilter); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("SELECT", result); Assert.Contains("FROM", result); Assert.EndsWith(string.Format("WHERE {0}.{1} LIKE '%roberto%'", "sample_entity", "name"), result); }
public void CreateTest() { var sampleEntity = new SampleEntity() { DocNumber = 12345 }; var result = EntitySqlParser.ParseEntity(sampleEntity, DatabaseEngine.SQLite, PersistenceAction.Create); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("INSERT INTO", result); Assert.Contains("VALUES", result); Assert.Contains("creation_date", result); Assert.Contains("name", result); Assert.Contains("active", result); }
public void EditTest() { var editedEntity = new SampleEntity() { Name = "roberto gomes", Age = 35 }; var filterEntity = new SampleEntity() { DocNumber = 12345 }; var result = EntitySqlParser.ParseEntity(editedEntity, DatabaseEngine.SQLite, PersistenceAction.Edit, filterEntity); result = result.Trim(); Assert.NotNull(result); Assert.StartsWith("UPDATE", result); Assert.Contains("SET", result); Assert.EndsWith(string.Format("WHERE {0}.{1} = 12345", "sample_entity", "doc_number"), result); }
public async Task <int> CountAsync(object filterEntity) { int result = 0; // Getting SQL statement from Helper var sqlInstruction = EntitySqlParser.ParseEntity(filterEntity, engine, PersistenceAction.Count, filterEntity); if (keepConnection || Connect()) { // Getting database return using Dapper result = await ExecuteCommandAsync(sqlInstruction); } if (!keepConnection) { Disconnect(); } return(result); }
private async Task <int> EditObjectAsync(object entity, object filterEntity, bool persistComposition, string optionalConnConfig = "", bool isReplicating = false) { int recordsAffected = 0; string sqlInstruction; var entityType = entity.GetType(); var entityProps = entityType.GetProperties(); if (keepConnection || base.Connect(optionalConnConfig)) { sqlInstruction = EntitySqlParser.ParseEntity(entity, engine, PersistenceAction.Edit, filterEntity); if (persistComposition) { base.StartTransaction(); } recordsAffected = await ExecuteCommandAsync(sqlInstruction); if (persistComposition) { PersistComposition(entity, PersistenceAction.Edit); } else if (!keepConnection) { base.Disconnect(); } } CleanCacheableData(entity); // Async persistence of database replicas if (base.replicationEnabled && !isReplicating) { EditReplicas(entity, filterEntity, entityProps, persistComposition); } return(recordsAffected); }
private async Task <int> CreateObjectAsync(object entity, bool persistComposition, string optionalConnConfig = "", bool isReplicating = false) { string sqlInstruction; int lastInsertedId = 0; var entityType = entity.GetType(); var entityProps = entityType.GetProperties(); if (keepConnection || base.Connect(optionalConnConfig)) { sqlInstruction = EntitySqlParser.ParseEntity(entity, engine, PersistenceAction.Create); if (persistComposition) { base.StartTransaction(); } lastInsertedId = await ExecuteCommandAsync(sqlInstruction); if (persistComposition) { PersistComposition(entity, PersistenceAction.Create); } else if (!keepConnection) { base.Disconnect(); } } CleanCacheableData(entity); // Async persistence of database replicas if (replicationEnabled && !isReplicating) { CreateReplicas(entity, entityProps, lastInsertedId, persistComposition); } return(lastInsertedId); }
private List <string> ParseComposition(object entity, PersistenceAction action, object filterEntity) { List <string> result = new List <string>(); object childEntityInstance = null; var entityType = entity.GetType(); IEnumerable <PropertyInfo> childEntities = entityType.GetProperties().Where(prp => prp.GetCustomAttributes(true) .Any(atb => atb.GetType().Name.Equals("RelatedEntity"))); foreach (PropertyInfo child in childEntities) { var relationAttrib = child.GetCustomAttributes(true) .FirstOrDefault(atb => atb.GetType().Name.Equals("RelatedEntity")) as RelatedEntity; childEntityInstance = child.GetValue(entity, null); object childEntityFilter = null; var entityParent = (action != PersistenceAction.Edit) ? entity : filterEntity; if (childEntityInstance != null) { var childEntityType = childEntityInstance.GetType(); if (!childEntityType.Name.Contains("List")) { var childProps = childEntityType.GetProperties(); action = EntitySqlParser.SetPersistenceAction(childEntityInstance, EntityReflector.GetKeyColumn(childProps)); childEntityFilter = Activator.CreateInstance(childEntityInstance.GetType()); if (action == PersistenceAction.Edit) { EntityReflector.MigrateEntityPrimaryKey(childEntityInstance, childProps, childEntityFilter); } SetEntityForeignKey(entityParent, child); result.Add(EntitySqlParser.ParseEntity(childEntityInstance, engine, action)); } else { var childListInstance = (IList)childEntityInstance; List <object> childFiltersList = new List <object>(); if (childListInstance.Count > 0) { foreach (var listItem in childListInstance) { if (relationAttrib.Cardinality == RelationCardinality.OneToMany) { var listItemType = listItem.GetType(); childEntityFilter = Activator.CreateInstance(listItemType); var listItemProps = listItemType.GetProperties(); action = EntitySqlParser.SetPersistenceAction(listItem, EntityReflector.GetKeyColumn(listItemProps)); if (action == PersistenceAction.Edit) { EntityReflector.MigrateEntityPrimaryKey(listItem, listItemProps, childEntityFilter); childFiltersList.Add(childEntityFilter); } SetEntityForeignKey(entityParent, listItem); result.Add(EntitySqlParser.ParseEntity(listItem, engine, action)); } else { var manyToEntity = EntitySqlParser.ParseManyToRelation(listItem, relationAttrib); SetEntityForeignKey(entityParent, manyToEntity); var existRelation = this.GetObject(manyToEntity); if (existRelation != null) { manyToEntity = existRelation; } var manyToEntityProps = manyToEntity.GetType().GetProperties(); action = EntitySqlParser.SetPersistenceAction(manyToEntity, EntityReflector.GetKeyColumn(manyToEntityProps)); object existFilter = null; if (action == PersistenceAction.Edit) { existFilter = Activator.CreateInstance(manyToEntity.GetType()); EntityReflector.MigrateEntityPrimaryKey(manyToEntity, manyToEntityProps, existFilter); childFiltersList.Add(existFilter); } result.Add(EntitySqlParser.ParseEntity(manyToEntity, engine, action)); } } } else { var childInstance = Activator.CreateInstance(childListInstance.GetType().GetGenericArguments()[0]); var childEntity = new object(); if (relationAttrib.Cardinality == RelationCardinality.ManyToMany) { childEntity = EntitySqlParser.ParseManyToRelation(childInstance, relationAttrib); } else { childEntity = childInstance; } SetEntityForeignKey(entityParent, childEntity); childFiltersList.Add(childEntity); } } } } if (result.Any(rst => rst.Contains(SQLStatements.SQL_ReservedWord_INSERT))) { result.Reverse(); } return(result); }