private static List <TableColumn> GetTableColumnsByReflection(Type type) { var entityColumns = new List <TableColumn>(); var tableColumns = new List <TableColumn>(); var properties = GetTypeProperties(type); foreach (var propertyInfo in properties) { var tableColumn = ValueUtils.GetTableColumn(propertyInfo); if (tableColumn == null) { continue; } if (Utilities.EqualsIgnoreCase(tableColumn.AttributeName, nameof(Entity.Id)) || Utilities.EqualsIgnoreCase(tableColumn.AttributeName, nameof(Entity.Guid)) || Utilities.EqualsIgnoreCase(tableColumn.AttributeName, nameof(Entity.ExtendValues)) || Utilities.EqualsIgnoreCase(tableColumn.AttributeName, nameof(Entity.CreatedDate)) || Utilities.EqualsIgnoreCase(tableColumn.AttributeName, nameof(Entity.LastModifiedDate))) { entityColumns.Add(tableColumn); } else { tableColumns.Add(tableColumn); } } var columns = new List <TableColumn>(); columns.AddRange(entityColumns); columns.AddRange(tableColumns); return(columns); }
public static async Task <int> InsertObjectAsync <T>(IDatabase database, string tableName, IEnumerable <TableColumn> tableColumns, IRedis redis, T dataInfo, Query query = null) where T : Entity { if (dataInfo == null) { return(0); } dataInfo.Guid = Utilities.GetGuid(); dataInfo.CreatedDate = DateTime.Now; dataInfo.LastModifiedDate = DateTime.Now; var identityInsert = false; if (dataInfo.Id > 0) { if (query != null) { var identityCondition = query.GetOneComponent <BasicCondition>("identity"); if (identityCondition != null) { identityInsert = true; } else { dataInfo.Id = 0; } } else { dataInfo.Id = 0; } } var dictionary = new Dictionary <string, object>(); foreach (var tableColumn in tableColumns) { if (!identityInsert && tableColumn.IsIdentity) { continue; } var value = ValueUtils.GetSqlValue(dataInfo, tableColumn); dictionary[tableColumn.AttributeName] = value; } var xQuery = NewQuery(tableName, query); xQuery.AsInsert(dictionary, !identityInsert); var compileInfo = await CompileAsync(database, tableName, redis, xQuery); if (identityInsert && database.DatabaseType == DatabaseType.SqlServer) { compileInfo.Sql = $@" SET IDENTITY_INSERT {database.GetQuotedIdentifier(tableName)} ON {compileInfo.Sql} SET IDENTITY_INSERT {database.GetQuotedIdentifier(tableName)} OFF "; } using (var connection = database.GetConnection()) { if (identityInsert) { await connection.ExecuteAsync(compileInfo.Sql, compileInfo.NamedBindings); } else { dataInfo.Id = await connection.QueryFirstAsync <int>(compileInfo.Sql, compileInfo.NamedBindings); } } return(dataInfo.Id); }