lith 4 年之前
父节点
当前提交
df33be102e

+ 57 - 9
Library/Vit.Db.DbMng/BaseDbMng.cs

@@ -33,7 +33,8 @@ namespace Vit.Db.DbMng
         /// </summary>
         /// <returns></returns>
         public abstract EDataBaseState GetDataBaseState();
-  
+
+        public abstract string GetDataBaseVersion(); 
 
         /// <summary>
         /// 创建数据库
@@ -73,6 +74,22 @@ namespace Vit.Db.DbMng
             Logger.Info(msg);
         }
 
+          
+
+        /// <summary>
+        /// 批量导入表数据(可以通过先停用索引,在导入数据后再启用来提高效率)
+        /// </summary>
+        /// <param name="dr"></param>
+        /// <param name="tableName"></param>
+        /// <returns></returns>
+        protected virtual int BulkImport(IDataReader dr, string tableName) 
+        {
+            return conn.BulkImport(dr, tableName);
+        }
+
+
+
+
         #region BackupSqler
         /// <summary>
         /// 备份数据库
@@ -100,8 +117,8 @@ namespace Vit.Db.DbMng
                 #region (x.1)创建建库语句文件(CreateDataBase.sql)
                 Log("");
                 Log(" --(x.1)创建建库语句文件(CreateDataBase.sql)");
-                var sqlPath = Path.Combine(tempPath, "CreateDataBase.sql");
                 var sqlText = BuildCreateDataBaseSql();
+                var sqlPath = Path.Combine(tempPath, "CreateDataBase.sql");          
                 File.WriteAllText(sqlPath, sqlText, System.Text.Encoding.UTF8);
 
                 Log("     成功");
@@ -136,12 +153,30 @@ namespace Vit.Db.DbMng
                         Log("");
                         Log($" ----[{tbIndex}/{sumTableCount}]backup table " + tableName);
 
-                        int rowCount;
-                        using (IDataReader dr = conn.ExecuteReader($"select * from {Quote(tableName)}"))
+                        int rowCount=0;
+
+                        bool ignoreTable = false;
+
+               
+                        #region 若表数据条数为0则跳过                   
+                        try
+                        {
+                            ignoreTable = 0 == conn.ExecuteScalar<int>("select count(*) from " + conn.Quote(tableName));
+                        }
+                        catch (Exception ex)
+                        {
+                            Logger.Info(ex);
+                        }
+                        #endregion
+
+                        if (!ignoreTable)
                         {
-                            connSqlite.Sqlite_CreateTable(dr, tableName);
+                            using (IDataReader dr = conn.ExecuteReader("select * from " + Quote(tableName)))
+                            {
+                                connSqlite.Sqlite_CreateTable(dr, tableName);
 
-                            rowCount = connSqlite.Import(dr, tableName, useTransaction: true);
+                                rowCount = connSqlite.Import(dr, tableName, useTransaction: true);
+                            }
                         }
                         sumRowCount += rowCount;
                         Log($"      table backuped. cur: " + rowCount + "  sum: " + sumRowCount);
@@ -157,12 +192,24 @@ namespace Vit.Db.DbMng
                 lastTime = DateTime.Now;
 
                 #endregion
-                               
 
+                #region (x.3)创建(info.json)
+                Log("");
+                Log(" --(x.3)创建(info.json)");
+                var backupInfo = new BackupInfo();
+                backupInfo.type = conn.GetDbType()?.ToString();
+                backupInfo.version = GetDataBaseVersion();
+                backupInfo.backupTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
+                backupInfo.cmd = BackupInfo.defaultCmd;         
+                File.WriteAllText(
+                    Path.Combine(tempPath, "info.json"),
+                    backupInfo.Serialize(),
+                    System.Text.Encoding.UTF8);
+                #endregion
 
-                #region (x.3)压缩备份文件
+                #region (x.4)压缩备份文件
                 Log("");
-                Log(" --(x.3)压缩备份文件");
+                Log(" --(x.4)压缩备份文件");
 
                 //待压缩文件夹
                 string input = tempPath;
@@ -286,6 +333,7 @@ namespace Vit.Db.DbMng
                 backuper.dirPath = tempPath;
                 backuper.conn = conn;
                 backuper.Log = Log;
+                backuper.BulkImport = this.BulkImport;
 
                 backuper.sqlSplit = RestoreSqler_SqlSplit;
 

+ 52 - 1
Library/Vit.Db.DbMng/MsSql/MsSqlDbMng.cs

@@ -19,6 +19,7 @@ using System.IO;
 using System.Linq;
 using System.Text;
 using System.Text.RegularExpressions;
+using Vit.Core.Module.Log;
 using Vit.Core.Util.Common;
 using Vit.Db.DbMng.Extendsions;
 using Vit.Extensions;
@@ -275,7 +276,7 @@ namespace Vit.Db.DbMng.MsSql
 
 
         #region GetDataBaseVersion      
-        public string GetDataBaseVersion() 
+        public override string GetDataBaseVersion() 
         {
             try
             {
@@ -514,6 +515,56 @@ deallocate   cDblogin
         #endregion
 
 
+        #region SqlerBackuper
+        /// <summary>
+        /// 批量导入表数据(可以通过先停用索引,在导入数据后再启用来提高效率)
+        /// </summary>
+        /// <param name="dr"></param>
+        /// <param name="tableName"></param>
+        /// <returns></returns>
+        protected override int BulkImport(IDataReader dr, string tableName)
+        {
+
+            #region (x.1)获取启用的索引
+            string sql = @"
+SELECT 'ALTER INDEX ' + QUOTENAME(I.name) + ' ON ' +  QUOTENAME(SCHEMA_NAME(T.schema_id))+'.'+ QUOTENAME(T.name)   -- + ' DISABLE' 
+FROM sys.indexes I
+INNER JOIN sys.tables T ON I.object_id = T.object_id
+WHERE I.type_desc = 'NONCLUSTERED'
+AND I.name IS NOT NULL
+AND I.is_disabled = 0
+and T.name=@tableName
+";
+            var sqlList = conn.Query<string>(sql,new { tableName}).ToList();
+            #endregion
+
+            if(sqlList.Count==0)
+                return conn.BulkImport(dr, tableName);
+
+            try
+            {
+                var sql_diableIndex = string.Join(" DISABLE;  ",sqlList) + " DISABLE;  ";
+                conn.Execute(sql_diableIndex);
+
+                return conn.BulkImport(dr, tableName);
+            }
+            finally
+            {
+                try
+                {
+                    var sql_rebuildIndex = string.Join(" REBUILD;  ", sqlList) + " REBUILD;  ";
+                    conn.Execute(sql_rebuildIndex);
+                }
+                catch (Exception ex)
+                {
+                    Logger.Error(ex);
+                }               
+            }
+
+           
+        }
+
+        #endregion
 
 
 

+ 3 - 1
Library/Vit.Db.DbMng/MySql/MySqlDbMng.cs

@@ -5,6 +5,7 @@ using System.Data;
 using System.IO;
 using System.Linq;
 using System.Text;
+using Vit.Core.Module.Log;
 using Vit.Core.Util.Common;
 using Vit.Extensions;
 using SqlConnection = MySql.Data.MySqlClient.MySqlConnection;
@@ -177,7 +178,7 @@ namespace Vit.Db.DbMng
 
                
         #region GetDataBaseVersion      
-        public string GetDataBaseVersion()
+        public override string GetDataBaseVersion()
         {
             try
             {
@@ -472,6 +473,7 @@ ORDER BY TABLE_NAME ASC, INDEX_NAME ASC;";
 
         #endregion
 
+         
 
 
         #region BackupSqler

+ 6 - 0
Library/Vit.Db.DbMng/SqlerFile/BackupInfo.cs

@@ -33,5 +33,11 @@ namespace Vit.Db.DbMng.SqlerFile
         /// </summary>
         public string sqlSplit;
 
+
+        public static string[][] defaultCmd => new[] {
+                       new[]{ "execSqlFile", "CreateDataBase.sql"},
+                        new[]{ "importSqlite", "Data.sqlite3" }
+                    };
+
     }
 }

+ 25 - 33
Library/Vit.Db.DbMng/SqlerFile/SqlerHelp.cs → Library/Vit.Db.DbMng/SqlerFile/SqlerBackuper.cs

@@ -19,7 +19,7 @@ namespace Vit.Db.DbMng.SqlerFile
         public string dirPath;
         public IDbConnection conn;
         public Action<string> Log;
-
+        public Func<IDataReader, string, int> BulkImport;
 
         public Regex sqlSplit = null;
 
@@ -39,10 +39,7 @@ namespace Vit.Db.DbMng.SqlerFile
             {
                 backupInfo = new BackupInfo
                 {
-                    cmd = new[] {
-                       new[]{ "execSqlFile", "CreateDataBase.sql"},
-                        new[]{ "importSqlite", "Data.sqlite3" }
-                    }
+                    cmd = BackupInfo.defaultCmd
                 };
             }
 
@@ -54,7 +51,7 @@ namespace Vit.Db.DbMng.SqlerFile
             Log("     数据库类型:" + backupInfo.type);
             Log("     数据库版本:" + backupInfo.version);
             Log("     备份时间  :" + backupInfo.backupTime);
-            Log("     sqlSplit  :" + sqlSplit.ToString());            
+            Log("     sqlSplit  :" + sqlSplit?.ToString());            
         }
 
 
@@ -74,35 +71,27 @@ namespace Vit.Db.DbMng.SqlerFile
                         //确保conn打开
                         conn.MakeSureOpen(() =>
                         {
-                            using (var tran = conn.BeginTransaction())
-                            {
-                                try
+                            conn.RunInTransaction((tran)=> {
+
+                                if (sqlSplit == null)
                                 {
-                                  
-                                    if (sqlSplit == null)
-                                    {
-                                        conn.Execute(sqlText, transaction: tran);
-                                    }
-                                    else
+                                    conn.Execute(sqlText, transaction: tran);
+                                }
+                                else
+                                {
+                                    var sqls = sqlSplit.Split(sqlText);
+                                    foreach (String sql in sqls)
                                     {
-                                        var sqls = sqlSplit.Split(sqlText);
-                                        foreach (String sql in sqls)
+                                        if (!String.IsNullOrEmpty(sql.Trim()))
                                         {
-                                            if (!String.IsNullOrEmpty(sql.Trim()))
-                                            {
-                                                conn.Execute(sql, transaction: tran);
-                                            }
+                                            conn.Execute(sql, transaction: tran);
                                         }
                                     }
-                                    tran.Commit();
                                 }
-                                catch (Exception ex)
-                                {
-                                    Logger.Error(ex);
-                                    tran.Rollback();
-                                    throw;
-                                }
-                            }
+
+                            },printErrorToLog:true);
+
+                           
                         });
 
                         Log("");
@@ -135,11 +124,14 @@ namespace Vit.Db.DbMng.SqlerFile
                                 Log("");
                                 Log($" ----[{tbIndex}/{sumTableCount}]import table " + tableName);
 
-                                int rowCount;
-                                using (var dr = connSqlite.ExecuteReader($"select * from {connSqlite.Quote(tableName)}"))
+                                int rowCount=0;
+                                if (0<connSqlite.ExecuteScalar<int>("select count(*) from " + connSqlite.Quote(tableName)))
                                 {
-                                    rowCount = conn.BulkImport(dr, tableName);
-                                    sumRowCount += rowCount;
+                                    using (var dr = connSqlite.ExecuteReader("select * from " + connSqlite.Quote(tableName)))
+                                    {
+                                        rowCount = BulkImport(dr, tableName);
+                                        sumRowCount += rowCount;
+                                    }
                                 }
                                 Log($"     table imported. row count: " + rowCount + ",  sum: " + sumRowCount);
 

+ 6 - 13
Sqler/Module/Sqler/Logical/SqlVersion/VersionManage.cs

@@ -93,17 +93,16 @@ namespace App.Module.Sqler.Logical.SqlVersion
             #region (x.3)执行语句函数
             void ExecSql()
             {        
-                using (var conn = SqlVersionHelp.CreateOpenedDbConnection())
-                using (var tran = conn.BeginTransaction())
+                using (var conn = SqlVersionHelp.CreateOpenedDbConnection())          
                 {
-                    try
-                    {
+                    conn.RunInTransaction((tran) => {
+
                         int index = 1;
                         //  /*GO*/GO 中间可出现多个空白字符,包括空格、制表符、换页符等          
                         //Regex reg = new Regex("/\\*GO\\*/\\s*GO");
                         Regex reg = new Regex("\\sGO\\s");
                         var sqls = reg.Split(versionResult.code);
-                        foreach (String sql in sqls)                      
+                        foreach (String sql in sqls)
                         {
                             if (String.IsNullOrEmpty(sql.Trim()))
                             {
@@ -119,14 +118,8 @@ namespace App.Module.Sqler.Logical.SqlVersion
                             }
                         }
 
-                        tran.Commit();
-                    }
-                    catch (Exception ex)
-                    {
-                        Logger.Error(ex);
-                        tran.Rollback();
-                        throw;
-                    }
+                    },printErrorToLog:true);
+                     
                 }
             }
             #endregion