You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
269 lines
9.0 KiB
269 lines
9.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Data.SQLite;
|
|
|
|
namespace SqlSugar
|
|
{
|
|
public class SqliteDbMaintenance : DbMaintenanceProvider
|
|
{
|
|
#region DML
|
|
protected override string GetColumnInfosByTableNameSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string GetTableInfoListSql
|
|
{
|
|
get
|
|
{
|
|
return @"select Name from sqlite_master where type='table' and name<>'sqlite_sequence' order by name;";
|
|
}
|
|
}
|
|
protected override string GetViewInfoListSql
|
|
{
|
|
get
|
|
{
|
|
return @"select Name from sqlite_master where type='view' order by name;";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DDL
|
|
protected override string AddPrimaryKeySql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string AddColumnToTableSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string AlterColumnToTableSql
|
|
{
|
|
get
|
|
{
|
|
// return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string BackupDataBaseSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string CreateTableSql
|
|
{
|
|
get
|
|
{
|
|
return "CREATE TABLE {0}(\r\n{1} )";
|
|
}
|
|
}
|
|
protected override string CreateTableColumn
|
|
{
|
|
get
|
|
{
|
|
return "{0} {1}{2} {3} {4} {5}";
|
|
}
|
|
}
|
|
protected override string TruncateTableSql
|
|
{
|
|
get
|
|
{
|
|
return "DELETE FROM {0}";
|
|
}
|
|
}
|
|
protected override string BackupTableSql
|
|
{
|
|
get
|
|
{
|
|
return " CREATE TABLE {0} AS SELECT * FROM {1} limit 0,{2}";
|
|
}
|
|
}
|
|
protected override string DropTableSql
|
|
{
|
|
get
|
|
{
|
|
return "DROP TABLE {0}";
|
|
}
|
|
}
|
|
protected override string DropColumnToTableSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string DropConstraintSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
protected override string RenameColumnSql
|
|
{
|
|
get
|
|
{
|
|
throw new NotSupportedException();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Check
|
|
protected override string CheckSystemTablePermissionsSql
|
|
{
|
|
get
|
|
{
|
|
return "select Name from sqlite_master limit 0,1";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Scattered
|
|
protected override string CreateTableNull
|
|
{
|
|
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";
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Methods
|
|
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName)
|
|
{
|
|
var cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
|
() =>
|
|
{
|
|
var 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;
|
|
var result = new List<DbColumnInfo>();
|
|
var schemaTable = reader.GetSchemaTable();
|
|
foreach (DataRow row in schemaTable.Rows)
|
|
{
|
|
var column = new DbColumnInfo
|
|
{
|
|
TableName = tableName,
|
|
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;
|
|
}
|
|
|
|
});
|
|
}
|
|
|
|
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
|
{
|
|
if (columns.HasValue())
|
|
{
|
|
foreach (var item in columns)
|
|
{
|
|
if (item.DbColumnName.Equals("GUID", StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
item.Length = 20;
|
|
}
|
|
if (item.IsIdentity && !item.IsPrimarykey)
|
|
{
|
|
item.IsPrimarykey = true;
|
|
Check.Exception(item.DataType == "integer", "Identity only integer type");
|
|
}
|
|
}
|
|
}
|
|
var sql = GetCreateTableSql(tableName, columns);
|
|
this.Context.Ado.ExecuteCommand(sql);
|
|
return true;
|
|
}
|
|
protected override string GetCreateTableSql(string tableName, List<DbColumnInfo> columns)
|
|
{
|
|
var columnArray = new List<string>();
|
|
Check.Exception(columns.IsNullOrEmpty(), "No columns found ");
|
|
foreach (var item in columns)
|
|
{
|
|
var columnName = item.DbColumnName;
|
|
var dataType = item.DataType;
|
|
if (dataType == "varchar" && item.Length == 0)
|
|
{
|
|
item.Length = 1;
|
|
}
|
|
var dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
|
var nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
|
var primaryKey = item.IsPrimarykey?this.CreateTablePirmaryKey:null;
|
|
var identity = item.IsIdentity ? this.CreateTableIdentity : null;
|
|
var addItem = string.Format(this.CreateTableColumn, this.SqlBuilder.GetTranslationColumnName(columnName), dataType, dataSize, nullType, primaryKey, identity);
|
|
columnArray.Add(addItem);
|
|
}
|
|
var tableString = string.Format(this.CreateTableSql, this.SqlBuilder.GetTranslationTableName(tableName), string.Join(",\r\n", columnArray));
|
|
tableString = tableString.Replace("`", "\"");
|
|
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;
|
|
}
|
|
private List<T> GetListOrCache<T>(string cacheKey, string sql)
|
|
{
|
|
return this.Context.Utilities.GetReflectionInoCacheInstance().GetOrCreate(cacheKey,
|
|
() =>
|
|
{
|
|
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;
|
|
});
|
|
}
|
|
#endregion
|
|
}
|
|
}
|