Пример #1
0
        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);
            }
        }
Пример #2
0
        public string DropOldTable(SqlHelper sqlHelper, DbTransaction tran, string className, string tableName, string oldTableName)
        {
            var columns    = IntrospectionManager.GetColumns(className);
            var columnsArr = string.Join(",", columns.Values.Select(x => "\"" + x + "\""));

            sqlHelper.ExecuteNonQuery(tran, string.Format("insert into \"{0}\"({2})  select {2} from \"{1}\"", tableName, oldTableName, columnsArr));
            sqlHelper.ExecuteNonQuery(tran, string.Format("drop table \"{0}\";", oldTableName));
            return("");
        }
Пример #3
0
        public void ModiyTable(SqlHelper sqlhelper, DbTransaction tran, string className, string newTableName, string oldTableName)
        {
            var columns    = IntrospectionManager.GetColumns(className);
            var columnsArr = string.Join(",", columns.Values.Select(x => "[" + x + "]"));

            CreateTable(sqlhelper, tran, className, newTableName);
            var sql = string.Format("insert into [{0}]({1}) select {1} from [{2}];", newTableName, columnsArr, oldTableName);

            sqlhelper.ExecuteNonQuery(tran, sql);
            sqlhelper.ExecuteNonQuery(tran, "drop table [" + oldTableName + "]");
        }
Пример #4
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);
            }
        }
Пример #5
0
        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);
            }
        }
Пример #6
0
        public void ModiyTable(SqlHelper sqlhelper, string className, string tableName)
        {
            DbConnection conn = sqlhelper.GetConn();

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

            try
            {
                var columns      = IntrospectionManager.GetColumns(className);
                var columnsArr   = string.Join(",", columns.Values.Select(x => "[" + x + "]"));
                var newTableName = tableName + DateTime.Now.ToString("yyyyMMdd_HHmmss");
                CreateTable(sqlhelper, tran, className, newTableName);
                var autoGrowName = IntrospectionManager.GetAutoGrowColumnName(className);
                var sql          = "";
                if (!string.IsNullOrWhiteSpace(autoGrowName))
                {
                    sql = "set identity_insert [" + newTableName + "] on;";
                }
                sql += string.Format("insert into [{0}]({1}) select {1} from [{2}];", newTableName, columnsArr, tableName);
                if (!string.IsNullOrWhiteSpace(autoGrowName))
                {
                    sql += "set identity_insert [" + newTableName + "] off;";
                }
                sqlhelper.ExecuteNonQuery(tran, sql);
                sqlhelper.ExecuteNonQuery(tran, "drop table [" + tableName + "]");
                sqlhelper.ExecuteNonQuery(tran, "sp_rename '" + newTableName + "','" + tableName + "'");
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                LogHelper.LogError(ex.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
Пример #7
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);
            }
        }