public void CreateTable(SqlHelper sqlhelper, DbTransaction tran, string className, string tableName) { var columns = IntrospectionManager.GetColumns(className); var columnsArr = columns.Select(x => { var memberType = IntrospectionManager.GetMemberType(className, x.Key); var columnType = IntrospectionManager.GetColumnType(className, x.Key); var length = IntrospectionManager.GetColumnLength(className, x.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, x.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, x.Key); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, x.Key); var nullAbleSql = isNullAble ? "" : "not null"; nullAbleSql = isPk || isAutoGrow ? "not null" : nullAbleSql; var autoGrowSql = isAutoGrow ? "identity(1,1)" : ""; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (isPk) { return(string.Format("[{0}] {1} primary key {2}", x.Value, lastColumnType, autoGrowSql)); } return(string.Format("[{0}] {1} {2} {3}", x.Value, lastColumnType, autoGrowSql, nullAbleSql)); }); var columnsSql = string.Join(",", columnsArr); var createTable = "create table [" + tableName + "](" + columnsSql + ")"; if (tran == null) { sqlhelper.ExecuteNonQuery(createTable); } else { sqlhelper.ExecuteNonQuery(tran, createTable); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { #region Alter var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); var needRebuild = false; foreach (var column in columns) { #region Alter var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName, lastColumnType, length, isNullAble, isPk, isRealPk, isAutoGrow, isRealAutoGrow)) { continue; } needRebuild = true; break; } else { needRebuild = true; break; } #endregion } if (needRebuild) { ModiyTable(sqlhelper, className, tableName); } #endregion } else { CreateTable(sqlhelper, null, className, tableName); } }
public void CreateTable(SqlHelper sqlhelper, string className, string tableName) { var columns = IntrospectionManager.GetColumns(className); var columnsArr = columns.Select(x => { var memberType = IntrospectionManager.GetMemberType(className, x.Key); var columnType = IntrospectionManager.GetColumnType(className, x.Key); var length = IntrospectionManager.GetColumnLength(className, x.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, x.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, x.Key); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, x.Key); var nullAbleSql = isNullAble ? "" : "not null"; nullAbleSql = isPk || isAutoGrow ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (isPk) { return(string.Format("\"{0}\" {1} primary key", x.Value, lastColumnType)); } return(string.Format("\"{0}\" {1} {2}", x.Value, lastColumnType, nullAbleSql)); }); var columnsSql = string.Join(",", columnsArr); var createTable = "create table \"" + tableName + "\"(" + columnsSql + ")"; sqlhelper.ExecuteNonQuery(createTable); var autoGrowName = IntrospectionManager.GetAutoGrowColumnName(className); if (!string.IsNullOrWhiteSpace(autoGrowName)) { var getconstraint = string.Format( "select sequence_name,increment_by,last_number from user_sequences where upper(sequence_name)=upper('{0}')", "SEQ_" + tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); if (!constraints.Any()) { var addconstraint = string.Format("create sequence \"SEQ_{0}\" increment by 1 start with 1 nomaxvalue nocycle", tableName); sqlhelper.ExecuteNonQuery(addconstraint); } var createtriggers = string.Format( "create or replace trigger \"TRIGGER_{0}_{1}\" before insert on \"{0}\" for each row " + "declare newid number(18,0);" + " begin " + "select \"{2}\".nextval into newid from dual; " + ":new.\"{1}\" := newid; " + "end;", tableName, autoGrowName, "SEQ_" + tableName); sqlhelper.ExecuteNonQuery(createtriggers); } }
public void CreateTable(SqlHelper sqlhelper, DbTransaction tran, string className, string tableName) { var columns = IntrospectionManager.GetColumns(className); var columnsArr = columns.Select(x => { var memberType = IntrospectionManager.GetMemberType(className, x.Key); var columnType = IntrospectionManager.GetColumnType(className, x.Key); var length = IntrospectionManager.GetColumnLength(className, x.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, x.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, x.Key); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, x.Key); var nullAbleSql = isNullAble ? "" : "not null"; nullAbleSql = isPk || isAutoGrow ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (memberType == typeof(long) || memberType == typeof(ulong)) { lastColumnType = isAutoGrow ? "bigserial" : lastColumnType; } else { lastColumnType = isAutoGrow ? "serial" : lastColumnType; } if (isPk) { return(string.Format("{0} {1} primary key", "\"" + x.Value + "\"", lastColumnType)); } return("\"" + x.Value + "\" " + lastColumnType + " " + nullAbleSql); }); var columnsSql = string.Join(",", columnsArr); var createTable = "create table \"" + tableName + "\"(" + columnsSql + ")"; if (tran == null) { sqlhelper.ExecuteNonQuery(createTable); } else { sqlhelper.ExecuteNonQuery(tran, createTable); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { #region Alter var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); var needRebuild = false; foreach (var column in columns) { var columnName = IntrospectionManager.GetColumnName(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); if (isAutoGrow != isRealAutoGrow) { needRebuild = true; } } if (needRebuild) { ModiyTable(sqlhelper, className, tableName); return; } foreach (var column in columns) { #region Alter var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var autoGrowSql = isAutoGrow ? "identity(1,1)" : ""; var nullAbleSql = isNullAble ? " null" : " not null"; nullAbleSql = isPk || isRealAutoGrow ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName, lastColumnType, length, isNullAble, isPk, isRealPk, isAutoGrow, isRealAutoGrow)) { continue; } if (isPk != isRealPk) { var getconstraint = string.Format("select constraint_name name from information_schema.key_column_usage where table_name='{0}'", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); foreach (var constraint in constraints) { var dropconstraint = "alter table [" + tableName + "] drop constraint [" + constraint.name + "]"; sqlhelper.ExecuteNonQuery(dropconstraint); } } if (isPk && isPk != isRealPk) { var alterSql = string.Format("alter table [{0}] alter column [{1}] {2} not null", tableName, columnName, lastColumnType); sqlhelper.ExecuteNonQuery(alterSql); alterSql = string.Format("alter table [{0}] add constraint [{1}] primary key([{2}])", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } else { var alterSql = string.Format("alter table [{0}] alter column [{1}] {2} {3}", tableName, columnName, lastColumnType, nullAbleSql); sqlhelper.ExecuteNonQuery(alterSql); } } else { if (isPk != isRealPk) { var getconstraint = string.Format("select name from sysobjects so " + "join sysconstraints sc " + "on so.id = sc.constid " + "where object_name(so.parent_obj) = '{0}'" + " and so.xtype = 'PK'", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); foreach (var constraint in constraints) { var dropconstraint = "alter table [" + tableName + "] drop constraint [" + constraint.name + "]"; sqlhelper.ExecuteNonQuery(dropconstraint); } } var alterSql = string.Format("alter table [{0}] add [{1}] {2} {3} {4}", tableName, columnName, lastColumnType, nullAbleSql, autoGrowSql); sqlhelper.ExecuteNonQuery(alterSql); if (isPk != isRealPk) { alterSql = string.Format("alter table [{0}] add constraint [{1}] primary key([{2}])", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } } #endregion } #endregion } else { CreateTable(sqlhelper, null, className, tableName); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); var needRebuild = false; foreach (var column in columns) { var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName, lastColumnType, length, isNullAble, isPk, isRealPk)) { continue; } needRebuild = true; break; } else { needRebuild = true; break; } } if (needRebuild) { DbConnection conn = sqlhelper.GetConn(); conn.Open(); DbTransaction tran = conn.BeginTransaction(); try { var oldName = RenameTable(sqlhelper, tran, tableName); CreateTable(sqlhelper, tran, className, tableName); DropOldTable(sqlhelper, tran, className, tableName, oldName); tran.Commit(); } catch (Exception ex) { tran.Rollback(); LogHelper.LogError(ex.ToString()); } finally { conn.Close(); } } } else { CreateTable(sqlhelper, null, className, tableName); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var realAutoGrowName = GetRealAutoGrowName(sqlhelper, tableName); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); foreach (var column in columns) { var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var autoGrowSql = isAutoGrow ? "auto_increment" : ""; var nullAbleSql = isNullAble ? " null" : " not null"; nullAbleSql = isPk ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName, lastColumnType, length, isNullAble, isPk, isRealPk, isAutoGrow, isRealAutoGrow)) { continue; } if (isPk != isRealPk) { if (realAutoGrowName != null) { var dropautogrow = string.Format("alter table `{0}` change `{1}` `{1}` {2} not null", tableName, realAutoGrowName, lastColumnType); sqlhelper.ExecuteNonQuery(dropautogrow); } var getconstraint = string.Format( "select constraint_name name from information_schema.key_column_usage where table_name='{0}'", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); if (constraints.Any()) { var dropconstraint = "alter table `" + tableName + "` drop primary key"; sqlhelper.ExecuteNonQuery(dropconstraint); } } if (isPk && isPk != isRealPk) { var alterSql = string.Format("alter table `{0}` add primary key(`{1}`)", tableName, columnName); sqlhelper.ExecuteNonQuery(alterSql); alterSql = string.Format("alter table `{0}` modify column `{1}` {2} not null {3}", tableName, columnName, lastColumnType, autoGrowSql); sqlhelper.ExecuteNonQuery(alterSql); } else { if (isRealPk) { if (realAutoGrowName != null && isRealAutoGrow) { var dropautogrow = string.Format("alter table `{0}` change `{1}` `{1}` {2} not null", tableName, realAutoGrowName, lastColumnType); sqlhelper.ExecuteNonQuery(dropautogrow); } var alterSql1 = string.Format("alter table `{0}` drop primary key", tableName); sqlhelper.ExecuteNonQuery(alterSql1); } var alterSql = string.Format("alter table `{0}` modify column `{1}` {2} {3}", tableName, columnName, lastColumnType, nullAbleSql); sqlhelper.ExecuteNonQuery(alterSql); } } else { if (isPk != isRealPk) { if (realAutoGrowName != null) { var dropautogrow = string.Format("alter table `{0}` change `{1}` `{1}` {2} not null", tableName, realAutoGrowName, lastColumnType); sqlhelper.ExecuteNonQuery(dropautogrow); } var getconstraint = string.Format( "select constraint_name name from information_schema.key_column_usage where table_name='{0}'", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); if (constraints.Any()) { var dropconstraint = "alter table `" + tableName + "` drop primary key"; sqlhelper.ExecuteNonQuery(dropconstraint); } } var alterSql = string.Format("alter table `{0}` add `{1}` {2} {3} {4}", tableName, columnName, lastColumnType, nullAbleSql, autoGrowSql); sqlhelper.ExecuteNonQuery(alterSql); if (isPk != isRealPk) { alterSql = string.Format("alter table `{0}` add primary key(`{1}`)", tableName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } } } } else { var columns = IntrospectionManager.GetColumns(className); var columnsArr = columns.Select(x => { var memberType = IntrospectionManager.GetMemberType(className, x.Key); var columnType = IntrospectionManager.GetColumnType(className, x.Key); var length = IntrospectionManager.GetColumnLength(className, x.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, x.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, x.Key); var autoGrowSql = isAutoGrow ? "auto_increment" : ""; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (isPk) { return(string.Format("`{0}` {1} primary key {2}", x.Value, lastColumnType, autoGrowSql)); } return(string.Format("`{0}` {1} {2}", x.Value, lastColumnType, autoGrowSql)); }); var columnsSql = string.Join(",", columnsArr); var createTable = "create table `" + tableName + "`(" + columnsSql + ")"; sqlhelper.ExecuteNonQuery(createTable); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { #region Alter var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); foreach (var column in columns) { #region Alter var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var nullAbleSql = isNullAble ? " null" : " not null"; nullAbleSql = isPk || isRealAutoGrow ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName, lastColumnType, length, isNullAble, isPk, isRealPk, isAutoGrow, isRealAutoGrow)) { continue; } if (isPk != isRealPk) { var getconstraint = string.Format("select constraint_name name from user_cons_columns where constraint_name=(select constraint_name from user_constraints where upper(table_name)=upper('{0}') and constraint_type='P')", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); foreach (var constraint in constraints) { var dropconstraint = "alter table \"" + tableName + "\" drop constraint \"" + constraint.NAME + "\""; sqlhelper.ExecuteNonQuery(dropconstraint); } } if (isPk && isPk != isRealPk) { var alterSql = string.Format("alter table \"{0}\" modify(\"{1}\" {2})", tableName, columnName, lastColumnType); sqlhelper.ExecuteNonQuery(alterSql); alterSql = string.Format("alter table \"{0}\" add constraint \"{1}\" primary key(\"{2}\")", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } else { var colsNew = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); var isNullableChange = !colsNew.Any(x => x.name.ToUpper() == columnName.ToUpper() && x.isnullable == isNullAble.ToYN()); var alterSql = string.Format("alter table \"{0}\" modify(\"{1}\" {2})", tableName, columnName, lastColumnType); sqlhelper.ExecuteNonQuery(alterSql); if (!isPk && isNullableChange) { alterSql = string.Format("alter table \"{0}\" modify(\"{1}\" {2})", tableName, columnName, nullAbleSql); sqlhelper.ExecuteNonQuery(alterSql); } } if (isAutoGrow != isRealAutoGrow) { var getconstraint = string.Format( "select sequence_name,increment_by,last_number from user_sequences where upper(sequence_name)=upper('{0}')", "SEQ_" + tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); if (!constraints.Any()) { var addconstraint = string.Format("create sequence \"SEQ_{0}\" increment by 1 start with 1 nomaxvalue nocycle", tableName); sqlhelper.ExecuteNonQuery(addconstraint); } var gettriggers = string.Format("select trigger_name from user_triggers where table_name='{0}' and trigger_name='{1}'", tableName, "TRIGGER_" + tableName + "_" + columnName); var triggers = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(gettriggers)); if (isAutoGrow && !triggers.Any()) { var createtriggers = string.Format("create or replace trigger \"TRIGGER_{0}_{1}\" before insert on \"{0}\" for each row " + "declare newid number(18,0);" + " begin " + "select \"{2}\".nextval into newid from dual; " + ":new.\"{1}\" := newId; " + "end;", tableName, columnName, "SEQ_" + tableName); sqlhelper.ExecuteNonQuery(createtriggers); } else if (!isAutoGrow && triggers.Any()) { var createtriggers = string.Format("drop trigger \"TRIGGER_{0}_{1}\" ", tableName, columnName); sqlhelper.ExecuteNonQuery(createtriggers); } } } else { if (isPk != isRealPk) { var getconstraint = string.Format("select constraint_name name from user_cons_columns where constraint_name=(select constraint_name from user_constraints where upper(table_name)=upper('{0}') and constraint_type='P')", tableName); var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint)); foreach (var constraint in constraints) { var dropconstraint = "alter table \"" + tableName + "\" drop constraint \"" + constraint.NAME + "\""; sqlhelper.ExecuteNonQuery(dropconstraint); } } var alterSql = string.Format("alter table \"{0}\" add (\"{1}\" {2} {3} )", tableName, columnName, lastColumnType, nullAbleSql); sqlhelper.ExecuteNonQuery(alterSql); if (isPk != isRealPk) { alterSql = string.Format("alter table \"{0}\" add constraint \"{1}\" primary key(\"{2}\")", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } } #endregion } #endregion } else { CreateTable(sqlhelper, className, tableName); } }
public void SyncDbInfo(string key, string className, string tableName) { var sqlhelper = IntrospectionManager.GetSqlHelperByKey(key); if (IsNeedAddTable(sqlhelper, tableName)) { var columns = IntrospectionManager.GetColumnAttributes(className).OrderByDescending(x => x.Value.IsPrimaryKey); var cols = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName)); var needRebuild = false; foreach (var column in columns) { var columnName = IntrospectionManager.GetColumnName(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); if (isAutoGrow != isRealAutoGrow) { needRebuild = true; } } if (needRebuild) { DbConnection conn = sqlhelper.GetConn(); conn.Open(); DbTransaction tran = conn.BeginTransaction(); try { var newTableName = tableName + DateTime.Now.ToString("yyyyMMdd_HHmmss"); ModiyTable(sqlhelper, tran, className, newTableName, tableName); ModiyTable(sqlhelper, tran, className, tableName, newTableName); tran.Commit(); } catch (Exception ex) { tran.Rollback(); LogHelper.LogError(ex.ToString()); } finally { conn.Close(); } return; } foreach (var column in columns) { var memberType = IntrospectionManager.GetMemberType(className, column.Key); var columnType = IntrospectionManager.GetColumnType(className, column.Key); var columnName = IntrospectionManager.GetColumnName(className, column.Key); var length = IntrospectionManager.GetColumnLength(className, column.Key); var isAutoGrow = IntrospectionManager.GetColumnIsAutoGrow(className, column.Key); //var isRealAutoGrow = IsRealAutoGrow(sqlhelper, tableName, columnName); var isNullAble = IntrospectionManager.GetColumnIsNullAble(className, column.Key); var isPk = IntrospectionManager.GetColumnIsPrimaryKey(className, column.Key); var isRealPk = IsRealPkInDb(sqlhelper, tableName, columnName); var autoGrowSql = isAutoGrow ? "autoincrement" : ""; var nullAbleSql = isNullAble ? " null" : " not null"; nullAbleSql = isPk ? " not null" : nullAbleSql; var lastColumnType = GetColumnTypeByMebmberType(memberType, columnType.ToLower(), length); if (cols.Any(x => x.name.ToUpper() == columnName.ToUpper())) { if (!IsNeedModiy(cols, memberType, columnName.ToUpper(), lastColumnType, length, isNullAble, isPk, isRealPk)) { continue; } if (isPk != isRealPk) { var constrintName = GetPrimaryKeyContrantName(sqlhelper, "", tableName); var dropconstraint = "alter table [" + tableName + "] drop constraint [" + constrintName + "]"; sqlhelper.ExecuteNonQuery(dropconstraint); } if (isPk && isPk != isRealPk) { var alterSql = string.Format("alter table [{0}] alter column [{1}] {2} not null", tableName, columnName, lastColumnType); sqlhelper.ExecuteNonQuery(alterSql); alterSql = string.Format("alter table [{0}] add constraint [{1}] primary key([{2}])", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } else { var alterSql = string.Format("alter table [{0}] alter column [{1}] {2} {3}", tableName, columnName, lastColumnType, nullAbleSql); sqlhelper.ExecuteNonQuery(alterSql); } } else { if (isPk) { var constrintName = GetPrimaryKeyContrantName(sqlhelper, "", tableName); var dropconstraint = "alter table [" + tableName + "] drop constraint [" + constrintName + "]"; sqlhelper.ExecuteNonQuery(dropconstraint); } var alterSql = string.Format("alter table [{0}] add [{1}] {2} {3} {4}", tableName, columnName, lastColumnType, nullAbleSql, autoGrowSql); sqlhelper.ExecuteNonQuery(alterSql); if (isPk) { alterSql = string.Format("alter table [{0}] add constraint [{1}] primary key([{2}])", tableName, "pk_" + tableName + "_" + columnName, columnName); sqlhelper.ExecuteNonQuery(alterSql); } } } } else { CreateTable(sqlhelper, null, className, tableName); } }