Files
SqlSugar/Src/Asp.Net/SqlSugar/Realization/Sqlite/DbMaintenance/SqliteDbMaintenance.cs

457 lines
15 KiB
C#
Raw Normal View History

2017-07-08 18:20:02 +08:00
using System;
using System.Collections.Generic;
2017-07-08 20:41:44 +08:00
using System.Data;
2017-07-09 14:56:24 +08:00
using System.Data.Common;
using System.Data.SQLite;
2019-05-17 21:44:53 +08:00
using System.IO;
2017-07-08 18:20:02 +08:00
using System.Linq;
using System.Text;
2019-06-04 16:03:25 +08:00
using System.Text.RegularExpressions;
2017-07-08 18:20:02 +08:00
namespace SqlSugar
{
public class SqliteDbMaintenance : DbMaintenanceProvider
{
#region DML
2019-05-17 20:34:53 +08:00
protected override string GetDataBaseSql
{
get
{
2019-05-17 21:44:53 +08:00
throw new NotSupportedException();
2019-05-17 20:34:53 +08:00
}
}
2017-07-08 18:20:02 +08:00
protected override string GetColumnInfosByTableNameSql
{
get
{
2017-07-08 20:41:44 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string GetTableInfoListSql
{
get
{
2017-07-09 00:58:56 +08:00
return @"select Name from sqlite_master where type='table' and name<>'sqlite_sequence' order by name;";
2017-07-08 18:20:02 +08:00
}
}
protected override string GetViewInfoListSql
{
get
{
2017-07-08 20:41:44 +08:00
return @"select Name from sqlite_master where type='view' order by name;";
2017-07-08 18:20:02 +08:00
}
}
#endregion
#region DDL
2019-05-17 20:34:53 +08:00
protected override string CreateDataBaseSql
{
get
{
return "CREATE DATABASE {0}";
}
}
2017-07-08 18:20:02 +08:00
protected override string AddPrimaryKeySql
{
get
{
2017-07-09 15:30:17 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string AddColumnToTableSql
{
get
{
2019-01-20 19:54:32 +08:00
return "ALTER TABLE {0} ADD COLUMN {1} {2}{3}";
2017-07-08 18:20:02 +08:00
}
}
protected override string AlterColumnToTableSql
{
get
{
// return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
2017-07-09 15:30:17 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string BackupDataBaseSql
{
get
{
2017-07-09 15:30:17 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string CreateTableSql
{
get
{
2022-05-23 15:12:23 +08:00
return "CREATE TABLE {0}(\r\n{1} $PrimaryKey )";
2017-07-08 18:20:02 +08:00
}
}
protected override string CreateTableColumn
{
get
{
return "{0} {1}{2} {3} {4} {5}";
}
}
protected override string TruncateTableSql
{
get
{
2017-07-09 19:58:35 +08:00
return "DELETE FROM {0}";
2017-07-08 18:20:02 +08:00
}
}
protected override string BackupTableSql
{
get
{
2017-07-09 17:38:16 +08:00
return " CREATE TABLE {0} AS SELECT * FROM {1} limit 0,{2}";
2017-07-08 18:20:02 +08:00
}
}
protected override string DropTableSql
{
get
{
return "DROP TABLE {0}";
}
}
protected override string DropColumnToTableSql
{
get
{
2017-07-09 15:30:17 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string DropConstraintSql
{
get
{
2017-07-09 17:38:16 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string RenameColumnSql
{
get
{
2017-07-09 15:30:17 +08:00
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string AddColumnRemarkSql
2017-07-08 18:20:02 +08:00
{
get
{
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string DeleteColumnRemarkSql
2017-07-08 18:20:02 +08:00
{
get
{
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string IsAnyColumnRemarkSql
2017-07-08 18:20:02 +08:00
{
get
{
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string AddTableRemarkSql
2017-07-08 18:20:02 +08:00
{
get
{
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
protected override string DeleteTableRemarkSql
2017-07-08 18:20:02 +08:00
{
get
{
throw new NotSupportedException();
2017-07-08 18:20:02 +08:00
}
}
2018-11-10 16:54:02 +08:00
protected override string IsAnyTableRemarkSql
2018-11-10 16:54:02 +08:00
{
get
{
2018-11-11 15:02:56 +08:00
throw new NotSupportedException();
2018-11-10 16:54:02 +08:00
}
}
protected override string RenameTableSql
2018-11-10 16:54:02 +08:00
{
get
{
2021-01-07 19:53:50 +08:00
return "alter table {0} rename to {1}";
2018-11-10 16:54:02 +08:00
}
}
protected override string CreateIndexSql
2018-11-10 16:54:02 +08:00
{
get
{
2021-01-07 19:53:50 +08:00
return "CREATE {3} INDEX Index_{0}_{2} ON {0}({1})";
2018-11-10 16:54:02 +08:00
}
}
protected override string AddDefaultValueSql
2018-11-10 16:54:02 +08:00
{
get
{
2022-04-09 12:42:29 +08:00
throw new NotSupportedException(" Sqlite no support default value");
2018-11-10 16:54:02 +08:00
}
}
protected override string IsAnyIndexSql
2018-11-10 16:54:02 +08:00
{
get
{
2021-01-07 18:41:55 +08:00
return "SELECT count(*) FROM sqlite_master WHERE name = '{0}'";
2018-11-10 16:54:02 +08:00
}
}
#endregion
2018-11-10 16:54:02 +08:00
#region Check
protected override string CheckSystemTablePermissionsSql
2018-11-10 16:54:02 +08:00
{
get
{
return "select Name from sqlite_master limit 0,1";
2018-11-10 16:54:02 +08:00
}
2019-02-03 18:01:10 +08:00
}
#endregion
2019-02-03 18:01:10 +08:00
#region Scattered
protected override string CreateTableNull
2019-02-03 18:01:10 +08:00
{
get
{
return "NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "AUTOINCREMENT";
2019-02-03 18:01:10 +08:00
}
2018-11-10 16:54:02 +08:00
}
2017-07-08 18:20:02 +08:00
#endregion
#region Methods
2023-03-20 00:22:01 +08:00
public override List<string> GetIndexList()
{
var sql = $"SELECT name FROM sqlite_master WHERE type='index'";
return this.Context.Ado.SqlQuery<string>(sql);
}
2022-12-03 22:25:59 +08:00
public override void AddDefaultValue(EntityInfo entityInfo)
{
2023-01-06 16:50:48 +08:00
//sqlite no support AddDefaultValue
2022-12-03 22:25:59 +08:00
}
public override bool AddDefaultValue(string tableName, string columnName, string defaultValue)
{
Console.WriteLine("sqlite no support AddDefaultValue");
return true;
}
2022-05-22 15:03:50 +08:00
public override bool TruncateTable(string tableName)
{
2022-05-24 20:00:18 +08:00
base.TruncateTable(tableName);//delete data
try
{
//clear sqlite identity
return this.Context.Ado.ExecuteCommand($"UPDATE sqlite_sequence SET seq = 0 WHERE name = '{tableName}'") > 0;
}
catch
{
//if no identity sqlite_sequence
return true;
}
2022-05-22 15:03:50 +08:00
}
2019-05-17 21:44:53 +08:00
/// <summary>
///by current connection string
/// </summary>
/// <param name="databaseDirectory"></param>
/// <returns></returns>
public override bool CreateDatabase(string databaseName, string databaseDirectory = null)
{
2019-06-04 16:03:25 +08:00
var connString=this.Context.CurrentConnectionConfig.ConnectionString;
2022-10-13 13:50:08 +08:00
if (connString == null)
{
throw new Exception("ConnectionString is null");
}
2019-06-04 16:03:25 +08:00
var path = Regex.Match(connString, @"[a-z,A-Z]\:\\.+\\").Value;
if (path.IsNullOrEmpty())
{
2020-05-14 17:05:32 +08:00
path = Regex.Match(connString, @"\/.+\/").Value;
2019-06-04 16:03:25 +08:00
}
2021-09-04 09:52:28 +08:00
if (path.IsNullOrEmpty())
{
path = Regex.Match(connString, @"[a-z,A-Z]\:\\").Value;
}
2019-06-04 16:03:25 +08:00
if (!FileHelper.IsExistDirectory(path))
{
FileHelper.CreateDirectory(path);
}
2019-05-17 21:44:53 +08:00
this.Context.Ado.Connection.Open();
this.Context.Ado.Connection.Close();
return true;
}
2018-02-08 18:27:14 +08:00
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
2017-07-08 20:41:44 +08:00
{
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
2017-07-19 00:31:27 +08:00
cacheKey = GetCacheKey(cacheKey);
2018-02-08 18:27:14 +08:00
if (!isCache)
{
2021-11-13 18:59:42 +08:00
return GetColumnsByTableName(tableName);
2018-02-08 18:27:14 +08:00
}
2017-09-28 16:21:46 +08:00
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
2018-02-08 18:27:14 +08:00
() =>
{
return GetColumnsByTableName(tableName);
2017-07-09 17:38:16 +08:00
2018-02-08 18:27:14 +08:00
});
}
2018-11-11 15:02:56 +08:00
public override bool AddRemark(EntityInfo entity)
{
return true;
}
2018-02-08 18:27:14 +08:00
private List<DbColumnInfo> GetColumnsByTableName(string tableName)
{
2019-06-04 16:03:25 +08:00
tableName = SqlBuilder.GetTranslationTableName(tableName);
2018-02-08 18:27:14 +08:00
string sql = "select * from " + tableName + " limit 0,1";
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false;
using (DbDataReader reader = (SQLiteDataReader)this.Context.Ado.GetDataReader(sql))
{
this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
List<DbColumnInfo> result = new List<DbColumnInfo>();
var schemaTable = reader.GetSchemaTable();
foreach (DataRow row in schemaTable.Rows)
{
DbColumnInfo column = new DbColumnInfo()
{
2022-01-09 16:24:43 +08:00
TableName =this.SqlBuilder.GetNoTranslationColumnName(tableName+""),
2018-02-08 18:27:14 +08:00
DataType = row["DataTypeName"].ToString().Trim(),
IsNullable = (bool)row["AllowDBNull"],
IsIdentity = (bool)row["IsAutoIncrement"],
ColumnDescription = null,
DbColumnName = row["ColumnName"].ToString(),
DefaultValue = row["defaultValue"].ToString(),
IsPrimarykey = (bool)row["IsKey"],
Length = Convert.ToInt32(row["ColumnSize"])
};
result.Add(column);
}
return result;
}
2017-07-08 20:41:44 +08:00
}
2018-10-12 16:56:13 +08:00
public override bool BackupTable(string oldTableName, string newTableName, int maxBackupDataRows = int.MaxValue)
{
oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName);
newTableName = this.SqlBuilder.GetTranslationTableName(newTableName);
string sql = string.Format(this.BackupTableSql, newTableName, oldTableName, maxBackupDataRows);
this.Context.Ado.ExecuteCommand(sql);
return true;
}
2018-01-11 17:34:06 +08:00
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
2017-07-08 18:20:02 +08:00
{
2017-10-10 19:49:16 +08:00
if (columns.HasValue())
2017-07-08 18:20:02 +08:00
{
foreach (var item in columns)
{
2019-05-10 20:37:40 +08:00
//if (item.DbColumnName.Equals("GUID", StringComparison.CurrentCultureIgnoreCase))
//{
// item.Length = 20;
//}
2017-07-09 17:38:16 +08:00
if (item.IsIdentity && !item.IsPrimarykey)
{
2022-09-05 12:20:23 +08:00
Check.Exception(true, "Identity only primary key");
2017-07-08 18:20:02 +08:00
}
}
}
string sql = GetCreateTableSql(tableName, columns);
2022-04-15 19:53:21 +08:00
string primaryKeyInfo = null;
if (!isCreatePrimaryKey || columns.Count(it => it.IsPrimarykey) > 1)
2018-02-08 18:27:14 +08:00
{
sql = sql.Replace("PRIMARY KEY AUTOINCREMENT", "").Replace("PRIMARY KEY", "");
2018-02-08 18:03:05 +08:00
}
2022-04-15 19:53:21 +08:00
if (columns.Count(it => it.IsPrimarykey) > 1 && isCreatePrimaryKey)
{
primaryKeyInfo = string.Format(",\r\n Primary key({0})", string.Join(",", columns.Where(it => it.IsPrimarykey).Select(it => this.SqlBuilder.GetTranslationColumnName(it.DbColumnName))));
primaryKeyInfo = primaryKeyInfo.Replace("`", "\"");
}
sql = sql.Replace("$PrimaryKey", primaryKeyInfo);
2017-07-08 18:20:02 +08:00
this.Context.Ado.ExecuteCommand(sql);
return true;
}
protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
{
List<string> columnArray = new List<string>();
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
foreach (var item in columns)
{
string columnName = item.DbColumnName;
string dataType = item.DataType;
2017-07-08 20:41:44 +08:00
if (dataType == "varchar" && item.Length == 0)
{
2017-07-08 18:20:02 +08:00
item.Length = 1;
}
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
2018-02-08 18:27:14 +08:00
string primaryKey = item.IsPrimarykey ? this.CreateTablePirmaryKey : null;
2017-07-08 18:20:02 +08:00
string identity = item.IsIdentity ? this.CreateTableIdentity : null;
string addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
columnArray.Add(addItem);
}
string tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
2017-07-09 17:38:16 +08:00
tableString = tableString.Replace("`", "\"");
2017-07-08 18:20:02 +08:00
return tableString;
}
public override bool IsAnyConstraint(string constraintName)
{
throw new NotSupportedException("MySql IsAnyConstraint NotSupportedException");
}
public override bool BackupDataBase(string databaseName, string fullFileName)
{
Check.ThrowNotSupportedException("MySql BackupDataBase NotSupported");
return false;
}
2017-07-08 20:41:44 +08:00
private List<T> GetListOrCache<T>(string cacheKey, string sql)
{
2017-09-28 16:21:46 +08:00
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
2017-09-28 15:50:14 +08:00
() =>
2017-07-08 20:41:44 +08:00
{
var isEnableLogEvent = this.Context.Ado.IsEnableLogEvent;
this.Context.Ado.IsEnableLogEvent = false;
var reval = this.Context.Ado.SqlQuery<T>(sql);
this.Context.Ado.IsEnableLogEvent = isEnableLogEvent;
return reval;
});
}
2017-07-08 18:20:02 +08:00
#endregion
}
}