public void Builder(ORMDBType dbType, string connectionStrings) { var metadatas = _dBMetadataWrapper.GetTableInfo(dbType, connectionStrings); List <ClassBuilderInfo> classInfos = new List <ClassBuilderInfo>(); int maxTableNameLength = metadatas.TableInfos.Max(x => x.TableName.Length) + "生成表: ".Length; foreach (var item in metadatas.TableInfos.OrderBy(x => x.TableName)) { Console.ForegroundColor = ConsoleColor.Green; var message = $"生成表: {item.TableName}"; //Console.Write(message.PadRight(100-message.Length,'.')); Console.Write(String.Format($"{{0,-{maxTableNameLength+1}}}", message)); var classString = BuildClass(dbType, item); classInfos.Add(new ClassBuilderInfo { ClassString = classString, TableInfo = item }); Console.Write("成功".PadLeft(20, '.')); Console.WriteLine(); Console.ResetColor(); } var result = new BuilderResult { Header = BuilderHead(), DBName = metadatas.DBName, ClassInfos = classInfos }; var saveToFile = _saveToFileFactory.GetSaveToFile(_options.MultipleFiles); saveToFile.Save(result); }
public IDBMetadataFactory GetDBObjectFactory(ORMDBType dbType) { if (!Cache.ContainsKey(dbType)) { lock (SynLock) { if (!Cache.ContainsKey(dbType)) { if (dbType == ORMDBType.MySql) { Cache.Add(dbType, new MysqlMetadataFactory()); } else if (dbType == ORMDBType.MsSql) { Cache.Add(dbType, new SqlServerMetadataFactory()); } else { throw new Exception("GetDBObjectFactory失败,没有配置对应的数据库类型"); } } } } return(Cache[dbType]); }
public static string GetUpdateSql(BaseEntity model, ORMDBType dbType) { if (model.FullUpdate) { return(GetUpdateFullSql(model, dbType)); } return(GetUpdateChangeColumnsSql(model, dbType)); }
private ConnectionManager(string connectionString, ORMDBType dbType) { _dbConnectionString = connectionString; _connection = ConnectionFactoryFactory.Instance.GetConnectionFactory().CreateConnection(connectionString, dbType); if (_connection.State != ConnectionState.Open) { _connection.Open(); } }
public static string GetDeleteByPkSql(BaseEntity model, ORMDBType dbType) { Type type = model.GetType(); if (!_DeleteByPkSqlCache.ContainsKey(type)) { EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(type); _DeleteByPkSqlCache.TryAdd(type, SQLBuilderFactory.Instance.GetSQLBuilder(dbType).BuildDeleteByPkSql(metadeta)); } return(_DeleteByPkSqlCache[type]); }
public static string GetAllColumns(Type entityType, ORMDBType dbType, string prefix) { string t = entityType.FullName + prefix; if (!_AllColumnsSqlCache.ContainsKey(t)) { EntityMeta metadata = EntityReflect.GetDefineInfoFromType(entityType); string sql = SQLBuilderFactory.Instance.GetSQLBuilder(dbType).GetAllColumns(metadata, prefix); _AllColumnsSqlCache.TryAdd(t, sql); } return(_AllColumnsSqlCache[t]); }
private static string GetUpdateFullSql(BaseEntity model, ORMDBType dbType) { Type type = model.GetType(); if (!_UpdateSqlCache.ContainsKey(type)) { EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(type); string sql = SQLBuilderFactory.Instance.GetSQLBuilder(dbType).BuildUpdateSql(metadeta); _UpdateSqlCache.TryAdd(type, sql); } return(_UpdateSqlCache[type]); }
public DBTransScope(string connectionStrings, ORMDBType dbTyp, TransScopeOption option) { //如果有多数据库,再开放个构造函数 传递数据库连接字符串 _connectionManager = ConnectionManager.GetManager(connectionStrings, dbTyp); if (!_connectionManager.IsExistDbTransaction() || option == TransScopeOption.RequiresNew) { _tran = _connectionManager.BeginTransaction(); _beginTransactionIsInCurrentTransScope = true; } else { _tran = _connectionManager.Transaction; } }
public static string GetReplaceInsertSQL(BaseEntity model, ORMDBType dbType) { Type t = model.GetType(); if (!_ReplaceSqlCache.ContainsKey(t)) { EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(t); string sql = SQLBuilderFactory.Instance.GetSQLBuilder(dbType).BuildReplaceInsertSQL(metadeta); if (!_ReplaceSqlCache.ContainsKey(t)) { _ReplaceSqlCache.TryAdd(t, sql); } } return(_ReplaceSqlCache[t]); }
public ISQLBuilder GetSQLBuilder(ORMDBType type) { switch (type) { case ORMDBType.MsSql: return(MsSql); case ORMDBType.MySql: return(MySql); case ORMDBType.Oracle: return(OracleSql); default: throw new Exception("不存在的数据库类型"); } }
public DBMetadataDTO GetTableInfo(ORMDBType dbType, string connectionString) { Dictionary <string, TableInfo> dict = new Dictionary <string, TableInfo>(); IDBMetadata dBMetadata = _dBObjectFactoryFactory.GetDBObjectFactory(dbType).GetDBMetadata(connectionString); string dbName = dBMetadata.GetDBName(); List <TableInfo> tables = dBMetadata.QueryTable(); List <ColumnInfo> columns = dBMetadata.QueryColumn(); List <PrimaryKey> primaryKeys = dBMetadata.QueryPrimaryKey(); foreach (var item in tables) { dict.Add(item.TableName, item); } foreach (var item in columns) { string tableName = item.TableName; if (!dict.ContainsKey(tableName)) { continue; } dict[tableName].Columns.Add(item); } foreach (var item in primaryKeys) { string tableName = item.TableName; if (!dict.ContainsKey(tableName)) { continue; } dict[tableName].PrimaryKeys.Add(item); } return(new DBMetadataDTO { DBName = dbName, TableInfos = dict.Values.ToList() }); }
public static ConnectionManager GetManager(string connectionString, ORMDBType dbType) { AssertUtils.IsNotEmpty(connectionString, "连接字符串为空"); ConnectionManager mgr = null; if (!ConnectionContext.Instance.Contains(connectionString)) { lock (_lock) { if (!ConnectionContext.Instance.Contains(connectionString)) { mgr = new ConnectionManager(connectionString, dbType); ConnectionContext.Instance.Set(connectionString, mgr); } } } mgr = ConnectionContext.Instance.Get(connectionString); mgr.AddRefCount(); return(mgr); }
public override string BuildClass(ORMDBType dbType, TableInfo table) { StringBuilder sb = new StringBuilder(); int space = 4; var dataTypeConvert = DBMetadataFactoryFactory.Instance.GetDBObjectFactory(dbType).GetDataTypeConvert(); // 注释 sb.AppendFormat("{0}/// <summary>", BuilderUtils.BuildSpace(space)); sb.AppendLine(); sb.AppendFormat("{0}/// {1}", BuilderUtils.BuildSpace(space), Helper.RemoveNewLine(table.TableComment)); sb.AppendLine(); sb.AppendFormat("{0}/// </summary>", BuilderUtils.BuildSpace(space)); sb.AppendLine(); //class 类名 sb.AppendFormat("{0}public partial class {1}", BuilderUtils.BuildSpace(space), Helper.GetClassName(table.TableName)); sb.AppendLine(); sb.AppendFormat("{0}{{", BuilderUtils.BuildSpace(space)); sb.AppendLine(); ////class 属性 foreach (var item in table.Columns) { sb.AppendFormat("{0}/// <summary>", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); sb.AppendFormat("{0}/// {1}", BuilderUtils.BuildSpace(space + 4), Helper.RemoveNewLine(item.ColumnComment)); sb.AppendLine(); sb.AppendFormat("{0}/// </summary>", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); string dateType = dataTypeConvert.ConvertDataType(item.DataType, item.ColumnIsNullable()); sb.AppendFormat("{0}public {1} {2} {{ get; set; }}", BuilderUtils.BuildSpace(space + 4), dateType, Helper.GetPropertyName(item.ColumnName)); sb.AppendLine(); } sb.AppendFormat("{0}}}", BuilderUtils.BuildSpace(space)); sb.AppendLine(); return(sb.ToString()); }
public IDbConnection CreateConnection(string connectionString, ORMDBType dbType) { IDbConnection connection = null; switch (dbType) { case ORMDBType.MsSql: connection = CreateMsSqlConnection(connectionString); break; case ORMDBType.MySql: connection = CreateMySqlConnection(connectionString); break; case ORMDBType.Oracle: connection = CreateOracleSqlSqlConnection(connectionString); break; default: break; } return(connection); }
public abstract string BuildClass(ORMDBType dbType, TableInfo table);
public override string BuildClass(ORMDBType dbType, TableInfo table) { StringBuilder sb = new StringBuilder(); int space = 4; var dataTypeConvert = DBMetadataFactoryFactory.Instance.GetDBObjectFactory(dbType).GetDataTypeConvert(); // 注释 sb.AppendFormat("{0}/// <summary>", BuilderUtils.BuildSpace(space)); sb.AppendLine(); sb.AppendFormat("{0}/// {1}", BuilderUtils.BuildSpace(space), Helper.RemoveNewLine(table.TableComment)); sb.AppendLine(); sb.AppendFormat("{0}/// </summary>", BuilderUtils.BuildSpace(space)); sb.AppendLine(); //class 类名 string baseEntityName = "BaseEntity"; sb.AppendFormat("{0}[Table(\"{1}\")]", BuilderUtils.BuildSpace(space), table.TableName); sb.AppendLine(); sb.AppendFormat("{0}public partial class {1} : {2}", BuilderUtils.BuildSpace(space), Helper.GetClassName(table.TableName), baseEntityName); sb.AppendLine(); sb.AppendFormat("{0}{{", BuilderUtils.BuildSpace(space)); sb.AppendLine(); //私有字段 foreach (var item in table.Columns) { string dateType = dataTypeConvert.ConvertDataType(item.DataType, item.ColumnIsNullable()); sb.AppendFormat("{0}private {1} {2}; ", BuilderUtils.BuildSpace(space + 4), dateType, GetFieldName(item.ColumnName)); sb.AppendLine(); } sb.AppendLine(); ////class 属性 foreach (var item in table.Columns) { sb.AppendFormat("{0}/// <summary>", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); sb.AppendFormat("{0}/// {1}", BuilderUtils.BuildSpace(space + 4), Helper.RemoveNewLine(item.ColumnComment + $" {item.ColumnType}")); sb.AppendLine(); sb.AppendFormat("{0}/// </summary>", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); var IsNullable = item.ColumnIsNullable() ? "true" : "false"; var defaultValue = !string.IsNullOrEmpty(item.DefaultValue) ? $",DefaultValue=\"{item.DefaultValue}\"" : ""; sb.AppendFormat("{0}[Column(\"{1}\",{2}{3})]", BuilderUtils.BuildSpace(space + 4), item.ColumnName, $"IsNullable={IsNullable}", defaultValue); sb.AppendLine(); if (table.PrimaryKeys.Exists(c => c.ColumnName == item.ColumnName)) { sb.AppendFormat("{0}[PrimaryKey]", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); } if (item.IsAutoIncrement()) { sb.AppendFormat("{0}[Identity]", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); } string dateType = dataTypeConvert.ConvertDataType(item.DataType, item.ColumnIsNullable()); sb.AppendFormat("{0}public {1} {2}", BuilderUtils.BuildSpace(space + 4), dateType, Helper.GetPropertyName(item.ColumnName)); sb.AppendLine(); sb.AppendFormat("{0}{{", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); sb.AppendFormat("{0}get {{ return {1}; }}", BuilderUtils.BuildSpace(space + 8), GetFieldName(item.ColumnName)); sb.AppendLine(); sb.AppendFormat("{0}set {{ {1} = value; OnPropertyChanged(\"{2}\"); }}", BuilderUtils.BuildSpace(space + 8), GetFieldName(item.ColumnName), item.ColumnName); sb.AppendLine(); sb.AppendFormat("{0}}}", BuilderUtils.BuildSpace(space + 4)); sb.AppendLine(); } sb.AppendFormat("{0}}}", BuilderUtils.BuildSpace(space)); sb.AppendLine(); return(sb.ToString()); }
private static string GetUpdateChangeColumnsSql(BaseEntity model, ORMDBType dbTyp) { EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(model.GetType()); return(SQLBuilderFactory.Instance.GetSQLBuilder(dbTyp).BuildUpdateSql(metadeta, model.GetPropertyChangedList())); }
public static string BuildDeleteSqlByProperty(BaseEntity model, List <string> propertyNames, ORMDBType dbType) { Type t = model.GetType(); EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(t); string sql = SQLBuilderFactory.Instance.GetSQLBuilder(dbType).BuildDeleteSqlByProperty(metadeta, propertyNames); return(sql); }
public DBTransScope(string connectionStrings, ORMDBType dbTyp) : this(connectionStrings, dbTyp, TransScopeOption.Required) { }
public static string GetDeleteSqlByChangeProperty(BaseEntity model, ORMDBType dbType) { EntityMeta metadeta = EntityReflect.GetDefineInfoFromType(model.GetType()); return(SQLBuilderFactory.Instance.GetSQLBuilder(dbType).BuildDeleteSqlByColumns(metadeta, model.GetPropertyChangedList())); }