Add Sqlite

This commit is contained in:
sunkaixuan
2017-07-08 18:20:02 +08:00
parent d1fdabb70b
commit 04aa6645c6
15 changed files with 881 additions and 1 deletions

Binary file not shown.

View File

@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class SqliteCodeFirst : CodeFirstProvider
{
public override void NoExistLogic(EntityInfo entityInfo)
{
var tableName = GetTableName(entityInfo);
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.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);
}
}
}

View File

@@ -0,0 +1,79 @@
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>("int",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("mediumint",CSharpDataType.@int),
new KeyValuePair<string, CSharpDataType>("integer",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>("enum",CSharpDataType.@string),
new KeyValuePair<string, CSharpDataType>("tinyint",CSharpDataType.@byte),
new KeyValuePair<string, CSharpDataType>("smallint",CSharpDataType.@short),
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
new KeyValuePair<string, CSharpDataType>("bit",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>("year",CSharpDataType.@int),
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>("tinyblob",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varbinary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("binary",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("multipoint",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("geometry",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("multilinestring",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("polygon",CSharpDataType.byteArray),
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.Guid),
};
}
}
public override List<string> StringThrow
{
get
{
return new List<string>() { "int32", "datetime", "decimal", "double", "byte"};
}
}
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class SqliteDbFirst : DbFirstProvider
{
}
}

View File

@@ -0,0 +1,236 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class SqliteDbMaintenance : DbMaintenanceProvider
{
#region DML
protected override string GetColumnInfosByTableNameSql
{
get
{
string sql = @"SELECT
0 as TableId,
TABLE_NAME as TableName,
column_name AS DbColumnName,
CASE WHEN left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1)='' THEN COLUMN_TYPE ELSE left(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)-1) END AS DataType,
CAST(SUBSTRING(COLUMN_TYPE,LOCATE('(',COLUMN_TYPE)+1,LOCATE(')',COLUMN_TYPE)-LOCATE('(',COLUMN_TYPE)-1) AS signed) AS Length,
column_default AS `DefaultValue`,
column_comment AS `ColumnDescription`,
CASE WHEN COLUMN_KEY = 'PRI'
THEN true ELSE false END AS `IsPrimaryKey`,
CASE WHEN EXTRA='auto_increment' THEN true ELSE false END as IsIdentity,
CASE WHEN is_nullable = 'YES'
THEN true ELSE false END AS `IsNullable`
FROM
Information_schema.columns where TABLE_NAME='{0}' and TABLE_SCHEMA=(select database()) ORDER BY TABLE_NAME";
return sql;
}
}
protected override string GetTableInfoListSql
{
get
{
return @"select TABLE_NAME as Name,TABLE_COMMENT as Description from information_schema.tables
where TABLE_SCHEMA=(select database()) AND TABLE_TYPE='BASE TABLE'";
}
}
protected override string GetViewInfoListSql
{
get
{
return @"select TABLE_NAME as Name,TABLE_COMMENT as Description from information_schema.tables
where TABLE_SCHEMA=(select database()) AND TABLE_TYPE='VIEW'
";
}
}
#endregion
#region DDL
protected override string AddPrimaryKeySql
{
get
{
return "ALTER TABLE {0} ADD PRIMARY KEY({2}) /*{1}*/";
}
}
protected override string AddColumnToTableSql
{
get
{
return "ALTER TABLE {0} ADD {1} {2}{3} {4} {5} {6}";
}
}
protected override string AlterColumnToTableSql
{
get
{
// return "ALTER TABLE {0} ALTER COLUMN {1} {2}{3} {4} {5} {6}";
return "alter table {0} change column {1} {1} {2}{3} {4} {5} {6}";
}
}
protected override string BackupDataBaseSql
{
get
{
return "mysqldump.exe {0} -uroot -p > {1} ";
}
}
protected override string CreateTableSql
{
get
{
return "CREATE TABLE {0}(\r\n{1} $PrimaryKey)";
}
}
protected override string CreateTableColumn
{
get
{
return "{0} {1}{2} {3} {4} {5}";
}
}
protected override string TruncateTableSql
{
get
{
return "TRUNCATE TABLE {0}";
}
}
protected override string BackupTableSql
{
get
{
return "SELECT * INTO {1} FROM {2} limit 0,{0}";
}
}
protected override string DropTableSql
{
get
{
return "DROP TABLE {0}";
}
}
protected override string DropColumnToTableSql
{
get
{
return "ALTER TABLE {0} DROP COLUMN {1}";
}
}
protected override string DropConstraintSql
{
get
{
return "ALTER TABLE {0} drop primary key;";
}
}
protected override string RenameColumnSql
{
get
{
return "exec sp_rename '{0}.{1}','{2}','column';";
}
}
#endregion
#region Check
protected override string CheckSystemTablePermissionsSql
{
get
{
return "select 1 from Information_schema.columns limit 0,1";
}
}
#endregion
#region Scattered
protected override string CreateTableNull
{
get
{
return "DEFAULT NULL";
}
}
protected override string CreateTableNotNull
{
get
{
return "NOT NULL";
}
}
protected override string CreateTablePirmaryKey
{
get
{
return "PRIMARY KEY";
}
}
protected override string CreateTableIdentity
{
get
{
return "AUTO_INCREMENT";
}
}
#endregion
#region Methods
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 = 10;
}
}
}
string sql = GetCreateTableSql(tableName, columns);
string primaryKeyInfo = null;
if (columns.Any(it => it.IsIdentity)) {
primaryKeyInfo =string.Format( ", Primary key({0})",string.Join(",",columns.Where(it=>it.IsIdentity).Select(it=>this.SqlBuilder.GetTranslationColumnName(it.DbColumnName))));
}
sql = sql.Replace("$PrimaryKey", primaryKeyInfo);
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 = 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));
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;
}
#endregion
}
}

View File

@@ -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";
}
}

View File

@@ -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>
{
}
}

View File

@@ -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;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace SqlSugar
{
public class MySqlDeleteBuilder : DeleteBuilder
{
}
}

View File

@@ -0,0 +1,59 @@
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
{
}
}

View File

@@ -0,0 +1,27 @@
namespace SqlSugar
{
public class SqliteInsertBuilder : InsertBuilder
{
public override string SqlTemplate
{
get
{
if (IsReturnIdentity)
{
return @"INSERT INTO {0}
({1})
VALUES
({2}) ;SELECT LAST_INSERT_ID();";
}
else
{
return @"INSERT INTO {0}
({1})
VALUES
({2}) ;";
}
}
}
}
}

View File

@@ -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 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
}
}

View File

@@ -0,0 +1,91 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SqlSugar
{
public class SqliteUpdateBuilder : UpdateBuilder
{
public override string SqlTemplateBatch
{
get
{
return @"UPDATE {1} S {2} INNER JOIN ${{0}} SET {0} ";
}
}
public override string SqlTemplateJoin
{
get
{
return @" (
{0}
) T ON {1}
";
}
}
protected override string TomultipleSqlString(List<IGrouping<int, DbColumnInfo>> groupList)
{
Check.Exception(PrimaryKeys == null || PrimaryKeys.Count == 0, " Update List<T> need Primary key");
int pageSize = 200;
int pageIndex = 1;
int totalRecord = groupList.Count;
int pageCount = (totalRecord + pageSize - 1) / pageSize;
StringBuilder batchUpdateSql = new StringBuilder();
while (pageCount >= pageIndex)
{
StringBuilder updateTable = new StringBuilder();
string setValues = string.Join(",", groupList.First().Where(it => it.IsPrimarykey == false && (it.IsIdentity == false || (IsOffIdentity && it.IsIdentity))).Select(it =>
{
if (SetValues.IsValuable())
{
var setValue = SetValues.Where(sv => sv.Key == Builder.GetTranslationColumnName(it.DbColumnName));
if (setValue != null && setValue.Any())
{
return setValue.First().Value;
}
}
var result = string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(it.DbColumnName));
return result;
}));
batchUpdateSql.AppendFormat(SqlTemplateBatch.ToString(), setValues, GetTableNameStringNoWith, TableWithString);
int i = 0;
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
{
var isFirst = i == 0;
if (!isFirst)
{
updateTable.Append(SqlTemplateBatchUnion);
}
updateTable.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), it.DbColumnName))));
++i;
}
pageIndex++;
updateTable.Append("\r\n");
string whereString = null;
if (this.WhereValues.IsValuable())
{
foreach (var item in WhereValues)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += item;
}
}
else if (PrimaryKeys.IsValuable())
{
foreach (var item in PrimaryKeys)
{
var isFirst = whereString == null;
whereString += (isFirst ? null : " AND ");
whereString += string.Format("S.{0}=T.{0}", Builder.GetTranslationColumnName(item));
}
}
var format= string.Format(SqlTemplateJoin, updateTable, whereString);
batchUpdateSql.Replace("${0}",format);
batchUpdateSql.Append(";");
}
return batchUpdateSql.ToString();
}
}
}

View File

@@ -0,0 +1,104 @@
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;
if (!SQLiteConnectionString.ToLower().Contains("charset"))
{
SQLiteConnectionString=SQLiteConnectionString.Trim().TrimEnd(';') + ";charset=utf8;";
}
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;
sqlParameter.Direction = parameter.Direction;
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);
}
++index;
}
return result;
}
}
}

View File

@@ -41,6 +41,10 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.102.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>Lib\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -67,6 +71,19 @@
<Compile Include="Realization\MySql\CodeFirst\MySqlCodeFirst.cs" />
<Compile Include="Realization\MySql\DbFirst\MySqlDbFirst.cs" />
<Compile Include="Realization\MySql\DbMaintenance\MySqlDbMaintenance.cs" />
<Compile Include="Realization\Sqlite\CodeFirst\SqliteCodeFirst.cs" />
<Compile Include="Realization\Sqlite\DbBind\SqliteDbBind.cs" />
<Compile Include="Realization\Sqlite\DbFirst\SqliteDbFirst.cs" />
<Compile Include="Realization\Sqlite\DbMaintenance\SqliteDbMaintenance.cs" />
<Compile Include="Realization\Sqlite\DbType.cs" />
<Compile Include="Realization\Sqlite\SqliteProvider.cs" />
<Compile Include="Realization\Sqlite\Queryable\SqliteQueryable.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteBuilder.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteDeleteBuilder.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteExpressionContext.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteInsertBuilder.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteQueryBuilder.cs" />
<Compile Include="Realization\Sqlite\SqlBuilder\SqliteUpdateBuilder.cs" />
<Compile Include="SimpleClient.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableProvider.cs" />
<Compile Include="Entities\ModelContext.cs" />
@@ -192,10 +209,10 @@
<ItemGroup>
<Content Include="Lib\MySql.Data.dll" />
<Content Include="Lib\Newtonsoft.Json.dll" />
<Content Include="Lib\System.Data.SQLite.dll" />
</ItemGroup>
<ItemGroup>
<Folder Include="Realization\Oracle\" />
<Folder Include="Realization\Sqlite\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.