mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-03 12:18:00 +08:00
Update Sqlite
This commit is contained in:
parent
6f28ffcac7
commit
2000738b35
@ -29,7 +29,7 @@ namespace SqlSugar
|
||||
csharpTypeName = "long";
|
||||
if (csharpTypeName == "Boolean")
|
||||
csharpTypeName = "bool";
|
||||
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName,StringComparison.CurrentCultureIgnoreCase));
|
||||
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName, StringComparison.CurrentCultureIgnoreCase));
|
||||
return mappings.IsValuable() ? mappings.First().Key : "varchar";
|
||||
}
|
||||
public string GetCsharpTypeName(string dbTypeName)
|
||||
@ -147,9 +147,15 @@ namespace SqlSugar
|
||||
{
|
||||
return "other";
|
||||
}
|
||||
else if (dbTypeName == "xml") {
|
||||
else if (dbTypeName == "xml")
|
||||
{
|
||||
return "string";
|
||||
}
|
||||
else if (propertyTypes == null || propertyTypes.Count() == 0)
|
||||
{
|
||||
Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName));
|
||||
return null;
|
||||
}
|
||||
else if (propertyTypes.First().Value == CSharpDataType.byteArray)
|
||||
{
|
||||
return "byte[]";
|
||||
|
@ -5,6 +5,8 @@ using System.Text;
|
||||
using System.Data;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
///<summary>
|
||||
@ -54,6 +56,8 @@ namespace SqlSugar
|
||||
private static readonly MethodInfo getConvertEnum_Null = typeof(IDataRecordExtensions).GetMethod("GetConvertEnum_Null");
|
||||
private static readonly MethodInfo getOtherNull = typeof(IDataRecordExtensions).GetMethod("GetOtherNull");
|
||||
private static readonly MethodInfo getOther = typeof(IDataRecordExtensions).GetMethod("GetOther");
|
||||
private static readonly MethodInfo getSqliteTypeNull = typeof(IDataRecordExtensions).GetMethod("GetSqliteTypeNull");
|
||||
private static readonly MethodInfo getSqliteType = typeof(IDataRecordExtensions).GetMethod("GetSqliteType");
|
||||
private static readonly MethodInfo getEntity = typeof(IDataRecordExtensions).GetMethod("GetEntity", new Type[] { typeof(SqlSugarClient) });
|
||||
private delegate T Load(IDataRecord dataRecord);
|
||||
private Load handler;
|
||||
@ -99,7 +103,7 @@ namespace SqlSugar
|
||||
string fileName = propertyInfo.Name;
|
||||
if (mappingColumns != null)
|
||||
{
|
||||
var mappInfo = mappingColumns.SingleOrDefault(it => it.EntityName==type.Name&&it.PropertyName.Equals(propertyInfo.Name));
|
||||
var mappInfo = mappingColumns.SingleOrDefault(it => it.EntityName == type.Name && it.PropertyName.Equals(propertyInfo.Name));
|
||||
if (mappInfo != null)
|
||||
{
|
||||
fileName = mappInfo.DbColumnName;
|
||||
@ -159,10 +163,47 @@ namespace SqlSugar
|
||||
MethodInfo method = null;
|
||||
Type bindPropertyType = PubMethod.GetUnderType(bindProperty, ref isNullableType);
|
||||
string dbTypeName = DataRecord.GetDataTypeName(ordinal);
|
||||
if (Regex.IsMatch(dbTypeName, @"\(.+\)"))
|
||||
{
|
||||
dbTypeName = Regex.Replace(dbTypeName, @"\(.+\)", "");
|
||||
}
|
||||
string propertyName = bindProperty.Name;
|
||||
string validPropertyName = bind.GetPropertyTypeName(dbTypeName);
|
||||
validPropertyName = validPropertyName == "byte[]" ? "byteArray" : validPropertyName;
|
||||
CSharpDataType validPropertyType = (CSharpDataType)Enum.Parse(typeof(CSharpDataType), validPropertyName);
|
||||
|
||||
#region Sqlite Logic
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.Sqlite)
|
||||
{
|
||||
if (bindPropertyType.IsEnum())
|
||||
{
|
||||
method = isNullableType ? getConvertEnum_Null.MakeGenericMethod(bindPropertyType) : getEnum.MakeGenericMethod(bindPropertyType);
|
||||
}
|
||||
else if (bindPropertyType == PubConst.IntType)
|
||||
{
|
||||
method = isNullableType ? getConvertInt32 : getInt32;
|
||||
}
|
||||
else if (bindPropertyType == PubConst.StringType)
|
||||
{
|
||||
method = getString;
|
||||
}
|
||||
else if (bindPropertyType == PubConst.ByteArrayType)
|
||||
{
|
||||
method = getValueMethod;
|
||||
generator.Emit(OpCodes.Call, method);
|
||||
generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
method = isNullableType ? getSqliteTypeNull.MakeGenericMethod(bindPropertyType) : getSqliteType.MakeGenericMethod(bindPropertyType);
|
||||
}
|
||||
generator.Emit(OpCodes.Call, method);
|
||||
return;
|
||||
};
|
||||
#endregion
|
||||
|
||||
#region Common Database Logic
|
||||
string bindProperyTypeName = bindPropertyType.Name.ToLower();
|
||||
bool isEnum = bindPropertyType.IsEnum();
|
||||
if (isEnum) { validPropertyType = CSharpDataType.@enum; }
|
||||
@ -170,7 +211,7 @@ namespace SqlSugar
|
||||
{
|
||||
case CSharpDataType.@int:
|
||||
CheckType(bind.IntThrow, bindProperyTypeName, validPropertyName, propertyName);
|
||||
if (bindProperyTypeName.IsContainsIn("int","int32","int64"))
|
||||
if (bindProperyTypeName.IsContainsIn("int", "int32", "int64"))
|
||||
method = isNullableType ? getConvertInt32 : getInt32;
|
||||
break;
|
||||
case CSharpDataType.@bool:
|
||||
@ -180,8 +221,9 @@ namespace SqlSugar
|
||||
case CSharpDataType.@string:
|
||||
CheckType(bind.StringThrow, bindProperyTypeName, validPropertyName, propertyName);
|
||||
method = getString;
|
||||
if (bindProperyTypeName == "guid") {
|
||||
method = getConvertStringGuid ;
|
||||
if (bindProperyTypeName == "guid")
|
||||
{
|
||||
method = getConvertStringGuid;
|
||||
}
|
||||
break;
|
||||
case CSharpDataType.DateTime:
|
||||
@ -224,16 +266,18 @@ namespace SqlSugar
|
||||
method = getValueMethod;
|
||||
break;
|
||||
}
|
||||
if (method == null&&bindPropertyType == PubConst.StringType) {
|
||||
if (method == null && bindPropertyType == PubConst.StringType)
|
||||
{
|
||||
method = getConvertString;
|
||||
}
|
||||
if (method == null)
|
||||
method =isNullableType? getOtherNull.MakeGenericMethod(bindPropertyType):getOther.MakeGenericMethod(bindPropertyType);
|
||||
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
|
||||
generator.Emit(OpCodes.Call, method);
|
||||
if (method == getValueMethod)
|
||||
{
|
||||
generator.Emit(OpCodes.Unbox_Any, bindProperty.PropertyType);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
private void CheckType(List<string> invalidTypes, string bindProperyTypeName, string validPropertyType, string propertyName)
|
||||
|
@ -8,9 +8,10 @@ namespace SqlSugar
|
||||
public static partial class IDataRecordExtensions
|
||||
{
|
||||
|
||||
#region Common Extensions
|
||||
public static Guid GetStringGuid(this IDataRecord dr, int i)
|
||||
{
|
||||
var reval =Guid.Parse(dr.GetValue(i).ToString());
|
||||
var reval = Guid.Parse(dr.GetValue(i).ToString());
|
||||
return reval;
|
||||
}
|
||||
|
||||
@ -182,5 +183,59 @@ namespace SqlSugar
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sqlite Extensions
|
||||
public static Nullable<T> GetSqliteTypeNull<T>(this IDataReader dr, int i) where T : struct
|
||||
{
|
||||
var type = PubMethod.GetUnderType(typeof(T));
|
||||
if (dr.IsDBNull(i))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return SqliteTypeConvert<T>(dr, i, type);
|
||||
}
|
||||
|
||||
public static T GetSqliteType<T>(this IDataReader dr, int i) where T : struct
|
||||
{
|
||||
var type = typeof(T);
|
||||
return SqliteTypeConvert<T>(dr, i, type);
|
||||
}
|
||||
|
||||
private static T SqliteTypeConvert<T>(IDataReader dr, int i, Type type) where T : struct
|
||||
{
|
||||
if (type.IsIn(PubConst.IntType))
|
||||
{
|
||||
return (T)((object)(dr.GetInt32(i)));
|
||||
}
|
||||
else if (type == PubConst.DateType)
|
||||
{
|
||||
return (T)Convert.ChangeType(Convert.ToDateTime(dr.GetString(i)), type);
|
||||
}
|
||||
else if (type == PubConst.DecType)
|
||||
{
|
||||
return (T)Convert.ChangeType(dr.GetDecimal(i), type);
|
||||
}
|
||||
else if (type == PubConst.DobType)
|
||||
{
|
||||
return (T)Convert.ChangeType(dr.GetDouble(i), type);
|
||||
}
|
||||
else if (type == PubConst.BoolType)
|
||||
{
|
||||
return (T)Convert.ChangeType(dr.GetBoolean(i), type);
|
||||
}
|
||||
else if (type == PubConst.GuidType)
|
||||
{
|
||||
string guidString = dr.GetString(i);
|
||||
string changeValue = guidString.IsNullOrEmpty() ? Guid.Empty.ToString() : guidString;
|
||||
return (T)Convert.ChangeType(Guid.Parse(changeValue), type);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (T)Convert.ChangeType((dr.GetString(i)), type);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -212,6 +212,10 @@ namespace SqlSugar
|
||||
{
|
||||
result = Regex.Match(result, @"^\(\'(.+)\'\)$").Groups[1].Value;
|
||||
}
|
||||
if (Regex.IsMatch(result, @"^\(\((.+)\)\)$"))
|
||||
{
|
||||
result = Regex.Match(result, @"^\(\((.+)\)\)$").Groups[1].Value;
|
||||
}
|
||||
if (Regex.IsMatch(result, @"^\((.+)\)$"))
|
||||
{
|
||||
result = Regex.Match(result, @"^\((.+)\)$").Groups[1].Value;
|
||||
|
@ -109,7 +109,10 @@ namespace SqlSugar
|
||||
string sql = this.CheckSystemTablePermissionsSql;
|
||||
try
|
||||
{
|
||||
var oldIsEnableLog = this.Context.Ado.IsEnableLogEvent;
|
||||
this.Context.Ado.IsEnableLogEvent = false;
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
this.Context.Ado.IsEnableLogEvent = oldIsEnableLog;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
@ -189,7 +192,7 @@ namespace SqlSugar
|
||||
{
|
||||
oldTableName = this.SqlBuilder.GetTranslationTableName(oldTableName);
|
||||
newTableName = this.SqlBuilder.GetTranslationTableName(newTableName);
|
||||
string sql = string.Format(this.BackupTableSql, maxBackupDataRows,newTableName , oldTableName);
|
||||
string sql = string.Format(this.BackupTableSql,newTableName , oldTableName, maxBackupDataRows);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ namespace SqlSugar
|
||||
return string.Format(SqlTemplate, GetTableNameString, columnsString, whereString);
|
||||
}
|
||||
|
||||
public object FormatValue(object value)
|
||||
public virtual object FormatValue(object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
|
@ -9,6 +9,12 @@ namespace SqlSugar
|
||||
{
|
||||
public class PubMethod
|
||||
{
|
||||
internal static Type GetUnderType(Type oldType)
|
||||
{
|
||||
Type type = Nullable.GetUnderlyingType(oldType);
|
||||
return type==null ? oldType : type;
|
||||
}
|
||||
|
||||
internal static Type GetUnderType(PropertyInfo propertyInfo, ref bool isNullable)
|
||||
{
|
||||
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
||||
@ -27,11 +33,11 @@ namespace SqlSugar
|
||||
internal static bool IsNullable(PropertyInfo propertyInfo)
|
||||
{
|
||||
Type unType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);
|
||||
return unType!=null;
|
||||
return unType != null;
|
||||
}
|
||||
|
||||
|
||||
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
|
||||
internal static T IsNullReturnNew<T>(T returnObj) where T : new()
|
||||
{
|
||||
if (returnObj.IsNullOrEmpty())
|
||||
{
|
||||
@ -40,7 +46,7 @@ namespace SqlSugar
|
||||
return returnObj;
|
||||
}
|
||||
|
||||
internal static T ChangeType<T>(T obj,Type type)
|
||||
internal static T ChangeType<T>(T obj, Type type)
|
||||
{
|
||||
return (T)Convert.ChangeType(obj, type);
|
||||
}
|
||||
|
@ -120,6 +120,7 @@ namespace SqlSugar
|
||||
{
|
||||
object reval = null;
|
||||
FieldInfo field = (FieldInfo)memberExpr.Member;
|
||||
Check.Exception(field.IsPrivate,string.Format(" Field \"{0}\" can't be private ",field.Name));
|
||||
reval = field.GetValue(memberExpr.Member);
|
||||
if (reval != null && reval.GetType().IsClass() && reval.GetType() != ExpressionConst.StringType)
|
||||
{
|
||||
@ -136,7 +137,7 @@ namespace SqlSugar
|
||||
}
|
||||
if (fieInfo == null && proInfo == null)
|
||||
{
|
||||
|
||||
Check.Exception(field.IsPrivate, string.Format(" Field \"{0}\" can't be private ", field.Name));
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
@ -162,7 +163,7 @@ namespace SqlSugar
|
||||
}
|
||||
if (fieInfo == null && proInfo == null)
|
||||
{
|
||||
|
||||
Check.Exception(true, string.Format(" Property \"{0}\" can't be private ", pro.Name));
|
||||
}
|
||||
}
|
||||
return reval;
|
||||
|
@ -1,8 +1,10 @@
|
||||
using MySql.Data.MySqlClient;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
@ -650,4 +652,132 @@ namespace SqlSugar
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 数据填充器
|
||||
/// </summary>
|
||||
public class SQLiteDataAdapter : IDataAdapter
|
||||
{
|
||||
private SQLiteCommand command;
|
||||
private string sql;
|
||||
private SQLiteConnection _sqlConnection;
|
||||
|
||||
/// <summary>
|
||||
/// SqlDataAdapter
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
public SQLiteDataAdapter(SQLiteCommand command)
|
||||
{
|
||||
this.command = command;
|
||||
}
|
||||
|
||||
public SQLiteDataAdapter()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SqlDataAdapter
|
||||
/// </summary>
|
||||
/// <param name="sql"></param>
|
||||
/// <param name="_sqlConnection"></param>
|
||||
public SQLiteDataAdapter(string sql, SQLiteConnection _sqlConnection)
|
||||
{
|
||||
this.sql = sql;
|
||||
this._sqlConnection = _sqlConnection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SelectCommand
|
||||
/// </summary>
|
||||
public SQLiteCommand SelectCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.command == null)
|
||||
{
|
||||
this.command = new SQLiteCommand(this.sql, this._sqlConnection);
|
||||
}
|
||||
return this.command;
|
||||
}
|
||||
set
|
||||
{
|
||||
this.command = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill
|
||||
/// </summary>
|
||||
/// <param name="dt"></param>
|
||||
public void Fill(DataTable dt)
|
||||
{
|
||||
if (dt == null)
|
||||
{
|
||||
dt = new DataTable();
|
||||
}
|
||||
var columns = dt.Columns;
|
||||
var rows = dt.Rows;
|
||||
using (SqliteDataReader dr = command.ExecuteReader())
|
||||
{
|
||||
for (int i = 0; i < dr.FieldCount; i++)
|
||||
{
|
||||
string name = dr.GetName(i).Trim();
|
||||
if (!columns.ContainsKey(name))
|
||||
columns.Add(new DataColumn(name, dr.GetFieldType(i)));
|
||||
}
|
||||
|
||||
while (dr.Read())
|
||||
{
|
||||
DataRow daRow = new DataRow();
|
||||
for (int i = 0; i < columns.Count; i++)
|
||||
{
|
||||
if (!daRow.ContainsKey(columns[i].ColumnName))
|
||||
daRow.Add(columns[i].ColumnName, dr.GetValue(i));
|
||||
}
|
||||
dt.Rows.Add(daRow);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fill
|
||||
/// </summary>
|
||||
/// <param name="ds"></param>
|
||||
public void Fill(DataSet ds)
|
||||
{
|
||||
if (ds == null)
|
||||
{
|
||||
ds = new DataSet();
|
||||
}
|
||||
using (SqliteDataReader dr = command.ExecuteReader())
|
||||
{
|
||||
do
|
||||
{
|
||||
var dt = new DataTable();
|
||||
var columns = dt.Columns;
|
||||
var rows = dt.Rows;
|
||||
for (int i = 0; i < dr.FieldCount; i++)
|
||||
{
|
||||
string name = dr.GetName(i).Trim();
|
||||
if (!columns.ContainsKey(name))
|
||||
columns.Add(new DataColumn(name, dr.GetFieldType(i)));
|
||||
}
|
||||
|
||||
while (dr.Read())
|
||||
{
|
||||
DataRow daRow = new DataRow();
|
||||
for (int i = 0; i < columns.Count; i++)
|
||||
{
|
||||
if (!daRow.ContainsKey(columns[i].ColumnName))
|
||||
daRow.Add(columns[i].ColumnName, dr.GetValue(i));
|
||||
}
|
||||
dt.Rows.Add(daRow);
|
||||
}
|
||||
ds.Tables.Add(dt);
|
||||
} while (dr.NextResult());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace System.Data.SQLite
|
||||
{
|
||||
public static class AdoCore
|
||||
{
|
||||
public static List<DbColumnInfo> GetColumnInfosByTableName(string tableName, DbDataReader dataReader)
|
||||
{
|
||||
List<DbColumnInfo> result = new List<DbColumnInfo>();
|
||||
var columns = dataReader.GetColumnSchema();
|
||||
foreach (var row in columns)
|
||||
{
|
||||
DbColumnInfo column = new DbColumnInfo()
|
||||
{
|
||||
TableName = tableName,
|
||||
DataType = row.DataTypeName,
|
||||
IsNullable = row.AllowDBNull.ObjToBool(),
|
||||
IsIdentity = row.IsAutoIncrement.ObjToBool(),
|
||||
ColumnDescription = null,
|
||||
DbColumnName = row.ColumnName,
|
||||
DefaultValue = null,
|
||||
IsPrimarykey = row.IsKey.ObjToBool(),
|
||||
Length = row.ColumnSize.ObjToInt()
|
||||
};
|
||||
result.Add(column);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public class SQLiteConnection : SqliteConnection
|
||||
{
|
||||
public SQLiteConnection(string connectionString) : base(connectionString)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class SQLiteCommand : SqliteCommand
|
||||
{
|
||||
public SQLiteCommand(string sql, SQLiteConnection connection) : base(sql, connection)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public class SQLiteParameter : SqliteParameter
|
||||
{
|
||||
public SQLiteParameter() { }
|
||||
public SQLiteParameter(string name, string parameter) :base(name,parameter){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -226,6 +226,11 @@ namespace SqlSugar
|
||||
{
|
||||
throw new NotSupportedException("MySql IsAnyConstraint NotSupportedException");
|
||||
}
|
||||
public override bool BackupDataBase(string databaseName, string fullFileName)
|
||||
{
|
||||
Check.ThrowNotSupportedException("MySql BackupDataBase NotSupported");
|
||||
return false;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -6,16 +6,18 @@ namespace SqlSugar
|
||||
{
|
||||
public class MySqlBuilder : SqlBuilderProvider
|
||||
{
|
||||
|
||||
public override string GetTranslationTableName(string name)
|
||||
{
|
||||
if (name.Contains("`")) return name;
|
||||
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
||||
var context = this.Context;
|
||||
var mappingInfo = context
|
||||
.MappingTables
|
||||
.FirstOrDefault(it => it.EntityName.Equals(name, StringComparison.CurrentCultureIgnoreCase));
|
||||
return "`" + (mappingInfo == null ? name : mappingInfo.DbTableName) + "`";
|
||||
name = (mappingInfo == null ? name : mappingInfo.DbTableName);
|
||||
if (name.Contains("`"))
|
||||
return name;
|
||||
else
|
||||
return "`" + name + "`";
|
||||
}
|
||||
public override string GetTranslationColumnName(string entityName, string propertyName)
|
||||
{
|
||||
|
@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteCodeFirst : CodeFirstProvider
|
||||
{
|
||||
public override void ExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
string backupName = tableName + DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||
if (entityInfo.Columns.IsValuable())
|
||||
{
|
||||
foreach (var item in entityInfo.Columns)
|
||||
{
|
||||
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||
columns.Add(dbColumnInfo);
|
||||
}
|
||||
}
|
||||
this.Context.DbMaintenance.BackupTable(tableName, backupName, int.MaxValue);
|
||||
this.Context.DbMaintenance.DropTable(tableName);
|
||||
this.Context.DbMaintenance.CreateTable(tableName,columns);
|
||||
}
|
||||
public override void NoExistLogic(EntityInfo entityInfo)
|
||||
{
|
||||
var tableName = GetTableName(entityInfo);
|
||||
string backupName=tableName+DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
Check.Exception(entityInfo.Columns.Where(it => it.IsPrimarykey).Count() > 1, "Use Code First ,The primary key must not exceed 1");
|
||||
List<DbColumnInfo> columns = new List<DbColumnInfo>();
|
||||
if (entityInfo.Columns.IsValuable())
|
||||
{
|
||||
foreach (var item in entityInfo.Columns.Where(it=>it.IsIgnore==false))
|
||||
{
|
||||
DbColumnInfo dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
|
||||
columns.Add(dbColumnInfo);
|
||||
}
|
||||
}
|
||||
this.Context.DbMaintenance.CreateTable(tableName, columns);
|
||||
}
|
||||
protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
||||
{
|
||||
var result = new DbColumnInfo()
|
||||
{
|
||||
DataType = this.Context.Ado.DbBind.GetDbTypeName(PubMethod.GetUnderType(item.PropertyInfo).Name),
|
||||
TableId = entityInfo.Columns.IndexOf(item),
|
||||
DbColumnName = item.DbColumnName.IsValuable() ? item.DbColumnName : item.PropertyName,
|
||||
IsPrimarykey = item.IsPrimarykey,
|
||||
IsIdentity = item.IsIdentity,
|
||||
TableName = tableName,
|
||||
IsNullable = item.IsNullable,
|
||||
DefaultValue = item.DefaultValue,
|
||||
ColumnDescription = item.ColumnDescription,
|
||||
Length = item.Length
|
||||
};
|
||||
if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
|
||||
{
|
||||
result.Length = 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected override void ConvertColumns(List<DbColumnInfo> dbColumns)
|
||||
{
|
||||
foreach (var item in dbColumns)
|
||||
{
|
||||
if (item.DataType == "DateTime")
|
||||
{
|
||||
item.Length = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
|
||||
{
|
||||
this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
|
||||
if (!item.IsPrimarykey)
|
||||
this.Context.DbMaintenance.DropConstraint(tableName,null);
|
||||
if (item.IsPrimarykey)
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteDbBind : DbBindProvider
|
||||
{
|
||||
public override string GetDbTypeName(string csharpTypeName)
|
||||
{
|
||||
if (csharpTypeName == PubConst.ByteArrayType.Name)
|
||||
{
|
||||
return "blob";
|
||||
}
|
||||
if (csharpTypeName == "Int32")
|
||||
csharpTypeName = "int";
|
||||
if (csharpTypeName == "Int16")
|
||||
csharpTypeName = "short";
|
||||
if (csharpTypeName == "Int64")
|
||||
csharpTypeName = "long";
|
||||
if (csharpTypeName == "Boolean")
|
||||
csharpTypeName = "bool";
|
||||
var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName, StringComparison.CurrentCultureIgnoreCase));
|
||||
return mappings.IsValuable() ? mappings.First().Key : "varchar";
|
||||
}
|
||||
public override List<KeyValuePair<string, CSharpDataType>> MappingTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<KeyValuePair<string, CSharpDataType>>()
|
||||
{
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("integer",CSharpDataType.@int),
|
||||
new KeyValuePair<string, CSharpDataType>("int",CSharpDataType.@int),
|
||||
new KeyValuePair<string, CSharpDataType>("int32",CSharpDataType.@int),
|
||||
new KeyValuePair<string, CSharpDataType>("integer32",CSharpDataType.@int),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("tinyint",CSharpDataType.@byte),
|
||||
new KeyValuePair<string, CSharpDataType>("UNSIGNEDINTEGER8",CSharpDataType.@byte),
|
||||
new KeyValuePair<string, CSharpDataType>("smallint",CSharpDataType.@short),
|
||||
new KeyValuePair<string, CSharpDataType>("int16",CSharpDataType.@short),
|
||||
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
|
||||
new KeyValuePair<string, CSharpDataType>("int64",CSharpDataType.@long),
|
||||
new KeyValuePair<string, CSharpDataType>("integer64",CSharpDataType.@long),
|
||||
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.@bool),
|
||||
new KeyValuePair<string, CSharpDataType>("bool",CSharpDataType.@bool),
|
||||
new KeyValuePair<string, CSharpDataType>("boolean",CSharpDataType.@bool),
|
||||
new KeyValuePair<string, CSharpDataType>("real",CSharpDataType.@double),
|
||||
new KeyValuePair<string, CSharpDataType>("double",CSharpDataType.@double),
|
||||
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.@float),
|
||||
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("datetime",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("date",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("blob",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("raw",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("oleobject",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("binary",CSharpDataType.byteArray),
|
||||
|
||||
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.Guid),
|
||||
new KeyValuePair<string, CSharpDataType>("guid",CSharpDataType.Guid)
|
||||
};
|
||||
}
|
||||
}
|
||||
public override List<string> StringThrow
|
||||
{
|
||||
get
|
||||
{
|
||||
return new List<string>() { "int32", "datetime", "decimal", "double", "byte"};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteDbFirst : DbFirstProvider
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,261 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
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)
|
||||
{
|
||||
string cacheKey = "DbMaintenanceProvider.GetColumnInfosByTableName." + this.SqlBuilder.GetNoTranslationColumnName(tableName).ToLower();
|
||||
return this.Context.RewritableMethods.GetCacheInstance<List<DbColumnInfo>>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[cacheKey];
|
||||
|
||||
}, (cm, key) =>
|
||||
{
|
||||
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;
|
||||
return AdoCore.GetColumnInfosByTableName(tableName, reader);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public override bool CreateTable(string tableName, List<DbColumnInfo> columns)
|
||||
{
|
||||
if (columns.IsValuable())
|
||||
{
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
string sql = GetCreateTableSql(tableName, columns);
|
||||
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;
|
||||
if (dataType == "varchar" && item.Length == 0)
|
||||
{
|
||||
item.Length = 1;
|
||||
}
|
||||
string dataSize = item.Length > 0 ? string.Format("({0})", item.Length) : null;
|
||||
string nullType = item.IsNullable ? this.CreateTableNull : CreateTableNotNull;
|
||||
string primaryKey = item.IsPrimarykey?this.CreateTablePirmaryKey:null;
|
||||
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));
|
||||
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.RewritableMethods.GetCacheInstance<List<T>>().Func(cacheKey,
|
||||
(cm, key) =>
|
||||
{
|
||||
return cm[cacheKey];
|
||||
|
||||
}, (cm, key) =>
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class DbType
|
||||
{
|
||||
public const string Sqlite = "Sqlite";
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteQueryable<T>:QueryableProvider<T>
|
||||
{
|
||||
public override ISugarQueryable<T> With(string withString)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
public class SqliteQueryable<T,T2> : QueryableProvider<T,T2>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2,T3> : QueryableProvider<T, T2,T3>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2,T3,T4> : QueryableProvider<T, T2,T3,T4>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2, T3, T4, T5> : QueryableProvider<T, T2, T3, T4, T5>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2, T3, T4, T5, T6> : QueryableProvider<T, T2, T3, T4, T5, T6>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2, T3, T4, T5, T6, T7> : QueryableProvider<T, T2, T3, T4, T5, T6, T7>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqliteQueryable<T, T2, T3, T4, T5, T6, T7, T8> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteBuilder : SqlBuilderProvider
|
||||
{
|
||||
|
||||
public override string GetTranslationTableName(string name)
|
||||
{
|
||||
if (name.Contains("`")) return name;
|
||||
Check.ArgumentNullException(name, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
||||
var context = this.Context;
|
||||
var mappingInfo = context
|
||||
.MappingTables
|
||||
.FirstOrDefault(it => it.EntityName.Equals(name, StringComparison.CurrentCultureIgnoreCase));
|
||||
return "`" + (mappingInfo == null ? name : mappingInfo.DbTableName) + "`";
|
||||
}
|
||||
public override string GetTranslationColumnName(string entityName, string propertyName)
|
||||
{
|
||||
Check.ArgumentNullException(entityName, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
||||
Check.ArgumentNullException(propertyName, string.Format(ErrorMessage.ObjNotExist, "Column Name"));
|
||||
var context = this.Context;
|
||||
var mappingInfo = context
|
||||
.MappingColumns
|
||||
.FirstOrDefault(it =>
|
||||
it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase) &&
|
||||
it.PropertyName.Equals(propertyName, StringComparison.CurrentCultureIgnoreCase));
|
||||
return (mappingInfo == null ? "`" + propertyName + "`" : "`" + mappingInfo.DbColumnName + "`");
|
||||
}
|
||||
|
||||
public override string GetTranslationColumnName(string propertyName)
|
||||
{
|
||||
if (propertyName.Contains("`")) return propertyName;
|
||||
else
|
||||
return "`" + propertyName + "`";
|
||||
}
|
||||
|
||||
public override string GetNoTranslationColumnName(string name)
|
||||
{
|
||||
if (!name.Contains("`")) return name;
|
||||
return name == null ? string.Empty : Regex.Match(name, @"\`(.*?)\`").Groups[1].Value;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteDeleteBuilder : DeleteBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,235 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteExpressionContext : ExpressionContext, ILambdaExpressions
|
||||
{
|
||||
public SqlSugarClient Context { get; set; }
|
||||
public SqliteExpressionContext()
|
||||
{
|
||||
base.DbMehtods = new SqliteMethod();
|
||||
}
|
||||
public override string GetTranslationTableName(string entityName, bool isMapping = true)
|
||||
{
|
||||
Check.ArgumentNullException(entityName, string.Format(ErrorMessage.ObjNotExist, "Table Name"));
|
||||
if (IsTranslationText(entityName)) return entityName;
|
||||
if (isMapping && this.MappingTables.IsValuable())
|
||||
{
|
||||
if (entityName.Contains("."))
|
||||
{
|
||||
var columnInfo = entityName.Split('.');
|
||||
var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(columnInfo.Last(), StringComparison.CurrentCultureIgnoreCase));
|
||||
if (mappingInfo != null)
|
||||
{
|
||||
columnInfo[columnInfo.Length - 1] = mappingInfo.EntityName;
|
||||
}
|
||||
return string.Join(".", columnInfo.Select(it => GetTranslationText(it)));
|
||||
}
|
||||
else
|
||||
{
|
||||
var mappingInfo = this.MappingTables.FirstOrDefault(it => it.EntityName.Equals(entityName, StringComparison.CurrentCultureIgnoreCase));
|
||||
return "`" + (mappingInfo == null ? entityName : mappingInfo.EntityName) + "`";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (entityName.Contains("."))
|
||||
{
|
||||
return string.Join(".", entityName.Split('.').Select(it => GetTranslationText(it)));
|
||||
}
|
||||
else
|
||||
{
|
||||
return GetTranslationText(entityName);
|
||||
}
|
||||
}
|
||||
}
|
||||
public override bool IsTranslationText(string name)
|
||||
{
|
||||
return name.Contains("`") && name.Contains("`");
|
||||
}
|
||||
public override string GetTranslationText(string name)
|
||||
{
|
||||
return "`" + name + "`";
|
||||
}
|
||||
}
|
||||
public class SqliteMethod : DefaultDbMethod, IDbMethods
|
||||
{
|
||||
public override string Length(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format("LENGTH({0})", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string Contains(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
return string.Format(" ({0} like '%'||{1}||'%') ", parameter.MemberName, parameter2.MemberName);
|
||||
}
|
||||
|
||||
public override string StartsWith(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
return string.Format(" ({0} like {1}||'%') ", parameter.MemberName, parameter2.MemberName);
|
||||
}
|
||||
|
||||
public override string EndsWith(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
return string.Format(" ({0} like '%'||{1}) ", parameter.MemberName, parameter2.MemberName);
|
||||
}
|
||||
|
||||
public override string ToInt32(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS INTEGER)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToInt64(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS INTEGER)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToString(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS TEXT)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToGuid(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS TEXT)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToDouble(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToBool(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS SIGNED)", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToDecimal(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" CAST({0} AS DECIMAL(18,4))", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string ToDate(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format(" DATETIME({0})", parameter.MemberName);
|
||||
}
|
||||
|
||||
public override string DateAddDay(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var parameter2 = model.Args[1];
|
||||
if (parameter2.MemberValue.ObjToInt() < 0)
|
||||
{
|
||||
return string.Format(" DATE(DATETIME({0}), '-{1} days')", parameter.MemberName, Math.Abs(parameter2.MemberValue.ObjToInt()));
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format(" DATE(DATETIME({0}), '+{1} days')", parameter.MemberName, parameter2.MemberValue);
|
||||
}
|
||||
}
|
||||
|
||||
public override string DateAddByType(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0].MemberName;
|
||||
var parameter2 = model.Args[1].MemberValue;
|
||||
var parameter3 = model.Args[2].MemberValue;
|
||||
if (parameter2.ObjToInt() < 0)
|
||||
{
|
||||
return string.Format(" DATETIME(DATETIME({0}), '+{1} {2}s')", parameter, Math.Abs(parameter2.ObjToInt()), parameter3);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Format(" DATETIME(DATETIME({0}), '+{1} {2}s')", parameter, parameter2, parameter3);
|
||||
}
|
||||
}
|
||||
|
||||
public override string DateValue(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
var typeName = model.Args[1].MemberValue.ToString();
|
||||
var parameter2 = typeName;
|
||||
var type = (DateType)Enum.Parse(typeof(DateType), parameter2, false);
|
||||
switch (type)
|
||||
{
|
||||
case DateType.Year:
|
||||
parameter2 = "%Y";
|
||||
break;
|
||||
case DateType.Month:
|
||||
parameter2 = "%m";
|
||||
break;
|
||||
case DateType.Day:
|
||||
parameter2 = "%d";
|
||||
break;
|
||||
case DateType.Hour:
|
||||
parameter2 = "%H";
|
||||
break;
|
||||
case DateType.Second:
|
||||
parameter2 = "%S";
|
||||
break;
|
||||
case DateType.Minute:
|
||||
parameter2 = "%M";
|
||||
break;
|
||||
case DateType.Millisecond:
|
||||
default:
|
||||
Check.ThrowNotSupportedException(typeName);
|
||||
break;
|
||||
}
|
||||
return string.Format(" CAST(STRFTIME('{1}', DATETIME(DATETIME({0}), 'LOCALTIME')) AS INTEGER)", parameter.MemberName, parameter2);
|
||||
}
|
||||
|
||||
public override string DateIsSameDay(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0].MemberName;
|
||||
var parameter2 = model.Args[1].MemberName;
|
||||
int time = 1;
|
||||
return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)", parameter, parameter2, time);
|
||||
}
|
||||
public override string DateIsSameByType(MethodCallExpressionModel model)
|
||||
{
|
||||
var parameter = model.Args[0].MemberName;
|
||||
var parameter2 = model.Args[1].MemberName;
|
||||
var parameter3 = model.Args[2].MemberValue;
|
||||
var type = (DateType)Enum.Parse(typeof(DateType), parameter3.ObjToString(), false);
|
||||
int time = 1;
|
||||
switch (type)
|
||||
{
|
||||
case DateType.Year:
|
||||
time = time * 1 / 365;
|
||||
break;
|
||||
case DateType.Month:
|
||||
time = time * 1 / 30;
|
||||
break;
|
||||
case DateType.Day:
|
||||
break;
|
||||
case DateType.Hour:
|
||||
time = time * 24;
|
||||
break;
|
||||
case DateType.Second:
|
||||
time = time * 24 * 60 * 60;
|
||||
break;
|
||||
case DateType.Minute:
|
||||
time = time * 24 * 60;
|
||||
break;
|
||||
case DateType.Millisecond:
|
||||
time = time * 24 * 60 * 60 * 1000;
|
||||
break;
|
||||
}
|
||||
return string.Format(" Cast((JulianDay({0}) - JulianDay({1})) *{2} As INTEGER)", parameter, parameter2, time);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteInsertBuilder : InsertBuilder
|
||||
{
|
||||
public override string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
if (IsReturnIdentity)
|
||||
{
|
||||
return @"INSERT INTO {0}
|
||||
({1})
|
||||
VALUES
|
||||
({2}) ;SELECT LAST_INSERT_ROWID();";
|
||||
}
|
||||
else
|
||||
{
|
||||
return @"INSERT INTO {0}
|
||||
({1})
|
||||
VALUES
|
||||
({2}) ;";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override string SqlTemplateBatch
|
||||
{
|
||||
get
|
||||
{
|
||||
return "INSERT INTO {0} ({1})";
|
||||
}
|
||||
}
|
||||
|
||||
public override object FormatValue(object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
if (type == PubConst.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < Convert.ToDateTime("1900-1-1"))
|
||||
{
|
||||
date = Convert.ToDateTime("1900-1-1");
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
else if (type == PubConst.BoolType)
|
||||
{
|
||||
return value.ObjToBool() ? "1" : "0";
|
||||
}
|
||||
else if (type == PubConst.StringType || type == PubConst.ObjType)
|
||||
{
|
||||
return "'" + value.ToString().ToSqlFilter() + "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "'"+value.ToString() + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public partial class SqliteQueryBuilder : QueryBuilder
|
||||
{
|
||||
#region Sql Template
|
||||
public override string PageTempalte
|
||||
{
|
||||
get
|
||||
{
|
||||
/*
|
||||
SELECT * FROM TABLE WHERE CONDITION ORDER BY ID DESC LIMIT 0,10
|
||||
*/
|
||||
var template = "SELECT {0} FROM {1} {2} {3} {4} LIMIT {5},{6}";
|
||||
return template;
|
||||
}
|
||||
}
|
||||
public override string DefaultOrderByTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return "ORDER BY DATETIME('now') ";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Common Methods
|
||||
|
||||
public override string ToSqlString()
|
||||
{
|
||||
sql = new StringBuilder();
|
||||
sql.AppendFormat(SqlTemplate, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString);
|
||||
if (IsCount) { return sql.ToString(); }
|
||||
if (Skip != null && Take == null)
|
||||
{
|
||||
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
|
||||
return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, (Skip != null || Take != null) ? null : GetOrderByString, Skip.ObjToInt(), long.MaxValue);
|
||||
}
|
||||
else if (Skip == null && Take != null)
|
||||
{
|
||||
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
|
||||
return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, 1, Take.ObjToInt());
|
||||
}
|
||||
else if (Skip != null && Take != null)
|
||||
{
|
||||
if (this.OrderByValue == "ORDER BY ") this.OrderByValue += GetSelectValue.Split(',')[0];
|
||||
return string.Format(PageTempalte, GetSelectValue, GetTableNameString, GetWhereValueString, GetGroupByString + HavingInfos, GetOrderByString, Skip.ObjToInt() > 0 ? Skip.ObjToInt(): 0, Take);
|
||||
}
|
||||
else
|
||||
{
|
||||
return sql.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get SQL Partial
|
||||
public override string GetSelectValue
|
||||
{
|
||||
get
|
||||
{
|
||||
if (this.IsCount) return "COUNT(1) AS `Count` ";
|
||||
string reval = string.Empty;
|
||||
if (this.SelectValue == null || this.SelectValue is string)
|
||||
{
|
||||
reval = GetSelectValueByString();
|
||||
}
|
||||
else
|
||||
{
|
||||
reval = GetSelectValueByExpression();
|
||||
}
|
||||
if (this.SelectType == ResolveExpressType.SelectMultiple)
|
||||
{
|
||||
this.SelectCacheKey = this.SelectCacheKey + string.Join("-", this.JoinQueryInfos.Select(it => it.TableName));
|
||||
}
|
||||
return reval;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteUpdateBuilder : UpdateBuilder
|
||||
{
|
||||
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
|
||||
{
|
||||
throw new Exception("Batch updates are not supported for the time being. Please wait for updates");
|
||||
}
|
||||
public override object FormatValue(object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return "NULL";
|
||||
}
|
||||
else
|
||||
{
|
||||
var type = value.GetType();
|
||||
if (type == PubConst.DateType)
|
||||
{
|
||||
var date = value.ObjToDate();
|
||||
if (date < Convert.ToDateTime("1900-1-1"))
|
||||
{
|
||||
date = Convert.ToDateTime("1900-1-1");
|
||||
}
|
||||
return "'" + date.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
|
||||
}
|
||||
else if (type == PubConst.BoolType)
|
||||
{
|
||||
return value.ObjToBool() ? "1" : "0";
|
||||
}
|
||||
else if (type == PubConst.StringType || type == PubConst.ObjType)
|
||||
{
|
||||
return "'" + value.ToString().ToSqlFilter() + "'";
|
||||
}
|
||||
else
|
||||
{
|
||||
return "'" + value.ToString() + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Data.SQLite;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SqliteProvider : AdoProvider
|
||||
{
|
||||
public SqliteProvider() { }
|
||||
public override IDbConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbConnection == null)
|
||||
{
|
||||
var SQLiteConnectionString = base.Context.CurrentConnectionConfig.ConnectionString;
|
||||
base._DbConnection = new SQLiteConnection(SQLiteConnectionString);
|
||||
}
|
||||
return base._DbConnection;
|
||||
}
|
||||
set
|
||||
{
|
||||
base._DbConnection = value;
|
||||
}
|
||||
}
|
||||
|
||||
public override void BeginTran(string transactionName)
|
||||
{
|
||||
((SQLiteConnection)this.Connection).BeginTransaction();
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="iso"></param>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(IsolationLevel iso, string transactionName)
|
||||
{
|
||||
((SQLiteConnection)this.Connection).BeginTransaction(iso);
|
||||
}
|
||||
public override IDataAdapter GetAdapter()
|
||||
{
|
||||
return new SQLiteDataAdapter();
|
||||
}
|
||||
public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
SQLiteCommand sqlCommand = new SQLiteCommand(sql, (SQLiteConnection)this.Connection);
|
||||
sqlCommand.CommandType = this.CommandType;
|
||||
sqlCommand.CommandTimeout = this.CommandTimeOut;
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
sqlCommand.Transaction = (SqliteTransaction)this.Transaction;
|
||||
}
|
||||
if (parameters.IsValuable())
|
||||
{
|
||||
IDataParameter[] ipars = ToIDbDataParameter(parameters);
|
||||
sqlCommand.Parameters.AddRange((SQLiteParameter[])ipars);
|
||||
}
|
||||
CheckConnection();
|
||||
return sqlCommand;
|
||||
}
|
||||
public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
|
||||
{
|
||||
((SQLiteDataAdapter)dataAdapter).SelectCommand = (SQLiteCommand)command;
|
||||
}
|
||||
/// <summary>
|
||||
/// if SQLite return SQLiteParameter[] pars
|
||||
/// if sqlerver return SqlParameter[] pars ...
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
public override IDataParameter[] ToIDbDataParameter(params SugarParameter[] parameters)
|
||||
{
|
||||
if (parameters == null || parameters.Length == 0) return null;
|
||||
SQLiteParameter[] result = new SQLiteParameter[parameters.Length];
|
||||
int index = 0;
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
if (parameter.Value == null) parameter.Value = DBNull.Value;
|
||||
var sqlParameter = new SQLiteParameter();
|
||||
sqlParameter.ParameterName = parameter.ParameterName;
|
||||
sqlParameter.Size = parameter.Size;
|
||||
sqlParameter.Value = parameter.Value;
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
result[index] = sqlParameter;
|
||||
if (sqlParameter.Direction == ParameterDirection.Output) {
|
||||
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
|
||||
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
|
||||
this.OutputParameters.Add(sqlParameter);
|
||||
}
|
||||
if (sqlParameter.DbType == System.Data.DbType.Guid) {
|
||||
sqlParameter.DbType = System.Data.DbType.String;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"Microsoft.Data.Sqlite": "1.0.0",
|
||||
"MySql.Data": "7.0.7-m61",
|
||||
"NETStandard.Library": "1.6.0",
|
||||
"Newtonsoft.Json": "9.0.1",
|
||||
|
Loading…
Reference in New Issue
Block a user