Esempio n. 1
0
        private bool IsRealAutoGrow(SqlHelper sqlHelper, string tableName, string columnName)
        {
            var getconstraint = string.Format("select column_name from information_schema.columns where table_name='{0}' and extra='auto_increment' and column_name='{1}'", tableName, columnName);
            var constraints   = Warp.ShieldLogSql(() => sqlHelper.ExecuteDynamic(getconstraint));

            return(constraints.Any());
        }
Esempio n. 2
0
        private bool IsRealAutoGrow(SqlHelper sqlHelper, string tableName, string columnName)
        {
            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));

            return(triggers.Any());
        }
Esempio n. 3
0
        private bool IsRealAutoGrow(SqlHelper sqlHelper, string tableName, string columnName)
        {
            var getconstraint = string.Format("select name from sys.columns where object_id=object_id('{0}') and is_identity=1 and name='{1}'", tableName, columnName);
            var constraints   = Warp.ShieldLogSql(() => sqlHelper.ExecuteDynamic(getconstraint));

            return(constraints.Any());
        }
Esempio n. 4
0
        private bool IsRealPkInDb(SqlHelper sqlhelper, string tableName, string columnName)
        {
            var getconstraint = string.Format("select table_name,column_name from information_schema.key_column_usage where table_name='{0}' and column_name='{1}'",
                                              tableName, columnName);
            var constraints = Warp.ShieldLogSql(() => sqlhelper.ExecuteDynamic(getconstraint));

            return(constraints.Any());
        }
Esempio n. 5
0
        private bool IsRealPkInDb(SqlHelper sqlhelper, string tableName, string columnName)
        {
            var getconstraint = Warp.ShieldLogSql(() => GetPrimaryKey(sqlhelper, "", tableName));

            if (getconstraint == null)
            {
                return(false);
            }
            return(columnName.ToUpper() == getconstraint.ToString().ToUpper());
        }
Esempio n. 6
0
        private bool IsRealPkInDb(SqlHelper sqlhelper, string tableName, string columnName)
        {
            var pkName = Warp.ShieldLogSql(() => GetPrimaryKey(sqlhelper, "", tableName));

            if (pkName == null)
            {
                return(false);
            }
            return(columnName == pkName.ToString());
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
0
        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);
            }
        }
Esempio n. 9
0
        private string GetRealAutoGrowName(SqlHelper sqlHelper, string tableName)
        {
            var getconstraint  = string.Format("select column_name name from information_schema.columns where table_name='{0}' and extra='auto_increment'", tableName);
            var constraints    = Warp.ShieldLogSql(() => sqlHelper.ExecuteDynamic(getconstraint));
            var firstOrDefault = constraints.FirstOrDefault();

            if (firstOrDefault != null)
            {
                return(firstOrDefault.name);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 10
0
        private void ModiyTable(SqlHelper sqlhelper, string className, string tableName)
        {
            var newTableName = tableName + DateTime.Now.ToString("yyyyMMdd_HHmmss");

            DbConnection conn = sqlhelper.GetConn();

            conn.Open();
            DbTransaction tran = conn.BeginTransaction();

            try
            {
                CreateTable(sqlhelper, tran, className, newTableName);
                //var columns = IntrospectionManager.GetColumns(className);
                //var columnsArr = string.Join(",", columns.Values.Select(x => "\"" + x + "\""));
                var cols       = Warp.ShieldLogSql(() => GetColumns(sqlhelper, sqlhelper.DataBaseName, tableName));
                var columnsArr = string.Join(",", cols.Select(x => "\"" + x.name + "\""));
                sqlhelper.ExecuteNonQuery(tran, string.Format("insert into \"{0}\"({2}) select {2} from \"{1}\"", newTableName, tableName, columnsArr));
                sqlhelper.ExecuteNonQuery(tran, "drop table \"" + tableName + "\"");
                sqlhelper.ExecuteNonQuery(tran, string.Format("alter table \"{0}\" rename to \"{1}\"", newTableName, tableName));
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                LogHelper.LogError(ex.ToString());
            }
            finally
            {
                conn.Close();
            }

            //var pkName = Warp.ShieldLogSql(() => GetPrimaryKey(sqlhelper, "", tableName));
            var autoGrowName = IntrospectionManager.GetAutoGrowColumnName(className);

            if (!string.IsNullOrWhiteSpace(autoGrowName))
            {
                sqlhelper.ExecuteNonQuery(string.Format("alter sequence \"{0}_{1}_seq\" rename to \"{2}_{1}_seq\"", newTableName, autoGrowName, tableName));
            }
        }
Esempio n. 11
0
        private bool IsRealAutoGrow(SqlHelper sqlHelper, string tableName, string columnName)
        {
            var getconstraint = string.Format("select column_default from  information_schema.columns where table_name='{0}' and column_name='{1}'", tableName, columnName);
            var constraints   = Warp.ShieldLogSql(() => sqlHelper.ExecuteDynamic(getconstraint));

            if (constraints.Any())
            {
                var columnDefault = constraints.FirstOrDefault().column_default;
                if (columnDefault == string.Format("nextval('\"{0}_{1}_seq\"'::regclass)", tableName, columnName))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Esempio n. 12
0
        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);
            }
        }
Esempio n. 13
0
        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);
            }
        }
Esempio n. 14
0
        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);
            }
        }
Esempio n. 15
0
        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);
            }
        }
Esempio n. 16
0
        private bool IsNeedAddTable(SqlHelper sqlhelper, string tableName)
        {
            var tables = Warp.ShieldLogSql(() => GetTables(sqlhelper, sqlhelper.DataBaseName));

            return(tables.Any(x => x.name.ToUpper() == tableName.ToUpper()));
        }
Esempio n. 17
0
        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);
            }
        }