public string UpdateSchema(Type type, bool execute) { var result = new StringBuilder(); var dbName = DBConnection.Database; var column = TypeMaper.GetProperties(type); //判断表是否存在 var exitSql = Adapter.GetTableExitSql(type, dbName); if (ExecuteScalar <int>(exitSql, null) <= 0) { result.Append(GetCreateTableSql(type)); } //判断字段是否存在 else { for (int i = 0; i < column.Count; i++) { var item = column[i]; var filedExitSql = Adapter.GetColumnExitSql(item, type, dbName); if (ExecuteScalar <int>(filedExitSql, null) <= 0) { result.Append(Adapter.GetCreateColumnSql(item, type, dbName)); } } } if (execute && result.Length > 0) { ExecuteNonQuery(result.ToString(), null); } return(result.ToString()); }
public string GetCreateTableSql(Type type, string dataBaseName) { var result = new StringBuilder(); result.Append($"CREATE TABLE {TypeMaper.GetTableName(type)} ("); result.Append(string.Join(",", TypeMaper.GetProperties(type).Select(p => $"{LeftEscapeChar}{p.Name.ToLower()}{RightEscapeChar} {GetType(p)}"))); result.Append($")"); return(result.ToString()); }
public async Task <int> UpdateAsync <T>(T entity) { var type = typeof(T); var keyProperties = TypeMaper.GetSingleKey <T>(); var tableName = TypeMaper.GetTableName <T>(); var allProperties = TypeMaper.GetProperties(type); var noKeyProperties = allProperties.Except(new List <PropertyInfo>() { keyProperties }).ToList(); var sql = _SQLBuilder.BuildUpdate(tableName, noKeyProperties.Select(p => p.Name).ToList()).Where().AppendColumnNameEqualsValue(keyProperties.Name).ToSql(); return(await ExecuteNonQueryAsync(sql.ToString(), entity)); }
public async Task <int> UpdateAsync <T>(T entity, string fields) { if (string.IsNullOrWhiteSpace(fields)) { return(0); } var type = typeof(T); var keyProperties = TypeMaper.GetSingleKey <T>(); var tableName = TypeMaper.GetTableName <T>(); var allProperties = TypeMaper.GetProperties(type); var arrayfields = fields.ToLower().Split(','); allProperties = allProperties.Where(p => arrayfields.Contains(p.Name.ToLower())).ToList(); var noKeyProperties = allProperties.Except(new List <PropertyInfo>() { keyProperties }).ToList(); var sql = _SQLBuilder.BuildUpdate(tableName, noKeyProperties.Select(p => p.Name).ToList()).Where().AppendColumnNameEqualsValue(keyProperties.Name).ToSql(); return(await ExecuteNonQueryAsync(sql.ToString(), entity)); }