mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-23 04:23:47 +08:00
Update SqlSugar.Access
This commit is contained in:
185
Src/Asp.Net/SqlSugar.Access/Access/AccessProvider.cs
Normal file
185
Src/Asp.Net/SqlSugar.Access/Access/AccessProvider.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using SqlSugar.Access;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerProvider : AdoProvider
|
||||
{
|
||||
public SqlServerProvider() { }
|
||||
public override IDbConnection Connection
|
||||
{
|
||||
get
|
||||
{
|
||||
if (base._DbConnection == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
base._DbConnection = new SqlConnection(base.Context.CurrentConnectionConfig.ConnectionString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Check.Exception(true,ex.Message);
|
||||
}
|
||||
}
|
||||
return base._DbConnection;
|
||||
}
|
||||
set
|
||||
{
|
||||
base._DbConnection = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(string transactionName)
|
||||
{
|
||||
CheckConnection();
|
||||
base.Transaction = ((SqlConnection)this.Connection).BeginTransaction(transactionName);
|
||||
}
|
||||
/// <summary>
|
||||
/// Only SqlServer
|
||||
/// </summary>
|
||||
/// <param name="iso"></param>
|
||||
/// <param name="transactionName"></param>
|
||||
public override void BeginTran(IsolationLevel iso, string transactionName)
|
||||
{
|
||||
CheckConnection();
|
||||
base.Transaction = ((SqlConnection)this.Connection).BeginTransaction(iso, transactionName);
|
||||
}
|
||||
public override IDataAdapter GetAdapter()
|
||||
{
|
||||
return new SqlDataAdapter();
|
||||
}
|
||||
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
|
||||
{
|
||||
SqlCommand sqlCommand = new SqlCommand(sql, (SqlConnection)this.Connection);
|
||||
sqlCommand.CommandType = this.CommandType;
|
||||
sqlCommand.CommandTimeout = this.CommandTimeOut;
|
||||
if (this.Transaction != null)
|
||||
{
|
||||
sqlCommand.Transaction = (SqlTransaction)this.Transaction;
|
||||
}
|
||||
if (parameters.HasValue())
|
||||
{
|
||||
SqlParameter[] ipars = GetSqlParameter(parameters);
|
||||
sqlCommand.Parameters.AddRange(ipars);
|
||||
}
|
||||
CheckConnection();
|
||||
return sqlCommand;
|
||||
}
|
||||
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
|
||||
{
|
||||
((SqlDataAdapter)dataAdapter).SelectCommand = (SqlCommand)command;
|
||||
}
|
||||
/// <summary>
|
||||
/// if mysql return MySqlParameter[] 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;
|
||||
SqlParameter[] result = new SqlParameter[parameters.Length];
|
||||
int index = 0;
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
if (parameter.Value == null) parameter.Value = DBNull.Value;
|
||||
var sqlParameter = new SqlParameter();
|
||||
sqlParameter.ParameterName = parameter.ParameterName;
|
||||
sqlParameter.UdtTypeName = parameter.UdtTypeName;
|
||||
sqlParameter.Size = parameter.Size;
|
||||
sqlParameter.Value = parameter.Value;
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
sqlParameter.Direction = parameter.Direction;
|
||||
result[index] = sqlParameter;
|
||||
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput,ParameterDirection.ReturnValue))
|
||||
{
|
||||
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
|
||||
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
|
||||
this.OutputParameters.Add(sqlParameter);
|
||||
}
|
||||
++index;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// if mysql return MySqlParameter[] pars
|
||||
/// if sqlerver return SqlParameter[] pars ...
|
||||
/// </summary>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
public SqlParameter[] GetSqlParameter(params SugarParameter[] parameters)
|
||||
{
|
||||
var isVarchar =IsVarchar();
|
||||
if (parameters == null || parameters.Length == 0) return null;
|
||||
SqlParameter[] result = new SqlParameter[parameters.Length];
|
||||
int index = 0;
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
if (parameter.Value == null) parameter.Value = DBNull.Value;
|
||||
var sqlParameter = new SqlParameter();
|
||||
sqlParameter.ParameterName = parameter.ParameterName;
|
||||
sqlParameter.UdtTypeName = parameter.UdtTypeName;
|
||||
sqlParameter.Size = parameter.Size;
|
||||
sqlParameter.Value = parameter.Value;
|
||||
sqlParameter.DbType = parameter.DbType;
|
||||
var isTime = parameter.DbType == System.Data.DbType.Time;
|
||||
if (isTime)
|
||||
{
|
||||
sqlParameter.SqlDbType = SqlDbType.Time;
|
||||
sqlParameter.Value=DateTime.Parse(parameter.Value?.ToString()).TimeOfDay;
|
||||
}
|
||||
if (sqlParameter.Value!=null&& sqlParameter.Value != DBNull.Value && sqlParameter.DbType == System.Data.DbType.DateTime)
|
||||
{
|
||||
var date = Convert.ToDateTime(sqlParameter.Value);
|
||||
if (date==DateTime.MinValue)
|
||||
{
|
||||
sqlParameter.Value = Convert.ToDateTime("1753/01/01");
|
||||
}
|
||||
}
|
||||
if (parameter.Direction == 0)
|
||||
{
|
||||
parameter.Direction = ParameterDirection.Input;
|
||||
}
|
||||
sqlParameter.Direction = parameter.Direction;
|
||||
result[index] = sqlParameter;
|
||||
if (parameter.TypeName.HasValue()) {
|
||||
sqlParameter.TypeName = parameter.TypeName;
|
||||
sqlParameter.SqlDbType = SqlDbType.Structured;
|
||||
sqlParameter.DbType = System.Data.DbType.Object;
|
||||
}
|
||||
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput, ParameterDirection.ReturnValue))
|
||||
{
|
||||
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
|
||||
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
|
||||
this.OutputParameters.Add(sqlParameter);
|
||||
}
|
||||
|
||||
if (isVarchar&&sqlParameter.DbType== System.Data.DbType.String)
|
||||
{
|
||||
sqlParameter.DbType =System.Data.DbType.AnsiString;
|
||||
}
|
||||
|
||||
++index;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private bool IsVarchar()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.MoreSettings != null && this.Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerCodeFirst:CodeFirstProvider
|
||||
{
|
||||
protected override string GetTableName(EntityInfo entityInfo)
|
||||
{
|
||||
var table= this.Context.EntityMaintenance.GetTableName(entityInfo.EntityName);
|
||||
var tableArray = table.Split('.');
|
||||
var noFormat = table.Split(']').Length==1;
|
||||
if (tableArray.Length > 1 && noFormat)
|
||||
{
|
||||
var dbMain = new SqlServerDbMaintenance() { Context = this.Context };
|
||||
var schmes = dbMain.GetSchemas();
|
||||
if (!schmes.Any(it => it.EqualCase(tableArray.First())))
|
||||
{
|
||||
return tableArray.Last();
|
||||
}
|
||||
else
|
||||
{
|
||||
return dbMain.SqlBuilder.GetTranslationTableName(table);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
Src/Asp.Net/SqlSugar.Access/Access/DbBind/AccessDbBind.cs
Normal file
61
Src/Asp.Net/SqlSugar.Access/Access/DbBind/AccessDbBind.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerDbBind : DbBindProvider
|
||||
{
|
||||
public override List<KeyValuePair<string, CSharpDataType>> MappingTypes
|
||||
{
|
||||
get
|
||||
{
|
||||
var extService = this.Context.CurrentConnectionConfig.ConfigureExternalServices;
|
||||
if (extService != null&& extService.AppendDataReaderTypeMappings.HasValue())
|
||||
{
|
||||
return extService.AppendDataReaderTypeMappings.Union(MappingTypesConst).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
return MappingTypesConst;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static List<KeyValuePair<string, CSharpDataType>> MappingTypesConst = new List<KeyValuePair<string, CSharpDataType>>()
|
||||
{
|
||||
new KeyValuePair<string, CSharpDataType>("int",CSharpDataType.@int),
|
||||
new KeyValuePair<string, CSharpDataType>("varchar",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("nvarchar",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("sql_variant",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("text",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("char",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("ntext",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("nchar",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("hierarchyid",CSharpDataType.@string),
|
||||
new KeyValuePair<string, CSharpDataType>("bigint",CSharpDataType.@long),
|
||||
new KeyValuePair<string, CSharpDataType>("bit",CSharpDataType.@bool),
|
||||
new KeyValuePair<string, CSharpDataType>("datetime",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("time",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("smalldatetime",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("timestamp",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("datetime2",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("date",CSharpDataType.DateTime),
|
||||
new KeyValuePair<string, CSharpDataType>("decimal",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("single",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("money",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("numeric",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("smallmoney",CSharpDataType.@decimal),
|
||||
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.@double),
|
||||
new KeyValuePair<string, CSharpDataType>("float",CSharpDataType.Single),
|
||||
new KeyValuePair<string, CSharpDataType>("real",CSharpDataType.@float),
|
||||
new KeyValuePair<string, CSharpDataType>("smallint",CSharpDataType.@short),
|
||||
new KeyValuePair<string, CSharpDataType>("tinyint",CSharpDataType.@byte),
|
||||
new KeyValuePair<string, CSharpDataType>("uniqueidentifier",CSharpDataType.Guid),
|
||||
new KeyValuePair<string, CSharpDataType>("binary",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("image",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("varbinary",CSharpDataType.byteArray),
|
||||
new KeyValuePair<string, CSharpDataType>("datetimeoffset", CSharpDataType.DateTimeOffset),
|
||||
new KeyValuePair<string, CSharpDataType>("datetimeoffset", CSharpDataType.DateTime)};
|
||||
};
|
||||
|
||||
}
|
11
Src/Asp.Net/SqlSugar.Access/Access/DbFirst/AccessDbFirst.cs
Normal file
11
Src/Asp.Net/SqlSugar.Access/Access/DbFirst/AccessDbFirst.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerDbFirst : DbFirstProvider
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,495 @@
|
||||
using SqlSugar.Access;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerDbMaintenance : DbMaintenanceProvider
|
||||
{
|
||||
#region DML
|
||||
protected override string GetDataBaseSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT NAME FROM MASTER.DBO.SYSDATABASES ORDER BY NAME";
|
||||
}
|
||||
}
|
||||
protected override string GetColumnInfosByTableNameSql
|
||||
{
|
||||
get
|
||||
{
|
||||
string sql = @"SELECT sysobjects.name AS TableName,
|
||||
syscolumns.Id AS TableId,
|
||||
syscolumns.name AS DbColumnName,
|
||||
systypes.name AS DataType,
|
||||
COLUMNPROPERTY(syscolumns.id,syscolumns.name,'PRECISION') as [length],
|
||||
isnull(COLUMNPROPERTY(syscolumns.id,syscolumns.name,'Scale'),0) as Scale,
|
||||
isnull(COLUMNPROPERTY(syscolumns.id,syscolumns.name,'Scale'),0) as DecimalDigits,
|
||||
sys.extended_properties.[value] AS [ColumnDescription],
|
||||
syscomments.text AS DefaultValue,
|
||||
syscolumns.isnullable AS IsNullable,
|
||||
columnproperty(syscolumns.id,syscolumns.name,'IsIdentity')as IsIdentity,
|
||||
(CASE
|
||||
WHEN EXISTS
|
||||
(
|
||||
select 1
|
||||
from sysindexes i
|
||||
join sysindexkeys k on i.id = k.id and i.indid = k.indid
|
||||
join sysobjects o on i.id = o.id
|
||||
join syscolumns c on i.id=c.id and k.colid = c.colid
|
||||
where o.xtype = 'U'
|
||||
and exists(select 1 from sysobjects where xtype = 'PK' and name = i.name)
|
||||
and o.name=sysobjects.name and c.name=syscolumns.name
|
||||
) THEN 1
|
||||
ELSE 0
|
||||
END) AS IsPrimaryKey
|
||||
FROM syscolumns
|
||||
INNER JOIN systypes ON syscolumns.xtype = systypes.xtype
|
||||
LEFT JOIN sysobjects ON syscolumns.id = sysobjects.id
|
||||
LEFT OUTER JOIN sys.extended_properties ON (sys.extended_properties.minor_id = syscolumns.colid
|
||||
AND sys.extended_properties.major_id = syscolumns.id)
|
||||
LEFT OUTER JOIN syscomments ON syscolumns.cdefault = syscomments.id
|
||||
WHERE syscolumns.id IN
|
||||
(SELECT id
|
||||
FROM sysobjects
|
||||
WHERE upper(xtype) IN('U',
|
||||
'V') )
|
||||
AND (systypes.name <> 'sysname')
|
||||
AND sysobjects.name='{0}'
|
||||
AND systypes.name<>'geometry'
|
||||
AND systypes.name<>'geography'
|
||||
ORDER BY syscolumns.colid";
|
||||
return sql;
|
||||
}
|
||||
}
|
||||
protected override string GetTableInfoListSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
|
||||
FROM sysobjects s
|
||||
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('U') ";
|
||||
}
|
||||
}
|
||||
protected override string GetViewInfoListSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT s.Name,Convert(varchar(max),tbp.value) as Description
|
||||
FROM sysobjects s
|
||||
LEFT JOIN sys.extended_properties as tbp ON s.id=tbp.major_id and tbp.minor_id=0 AND (tbp.Name='MS_Description' OR tbp.Name is null) WHERE s.xtype IN('V') ";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region DDL
|
||||
protected override string CreateDataBaseSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"create database {0} ";
|
||||
}
|
||||
}
|
||||
protected override string AddPrimaryKeySql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY({2})";
|
||||
}
|
||||
}
|
||||
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}";
|
||||
}
|
||||
}
|
||||
protected override string BackupDataBaseSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"USE master;BACKUP DATABASE {0} TO disk = '{1}'";
|
||||
}
|
||||
}
|
||||
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 "TRUNCATE TABLE {0}";
|
||||
}
|
||||
}
|
||||
protected override string BackupTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT TOP {0} * INTO {1} FROM {2}";
|
||||
}
|
||||
}
|
||||
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 CONSTRAINT {1}";
|
||||
}
|
||||
}
|
||||
protected override string RenameColumnSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "exec sp_rename '{0}.{1}','{2}','column';";
|
||||
}
|
||||
}
|
||||
protected override string AddColumnRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXECUTE sp_addextendedproperty N'MS_Description', '{2}', N'user', N'dbo', N'table', N'{1}', N'column', N'{0}'"; ;
|
||||
}
|
||||
}
|
||||
|
||||
protected override string DeleteColumnRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','{1}','column','{0}'";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override string IsAnyColumnRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT" +
|
||||
" A.name AS table_name," +
|
||||
" B.name AS column_name," +
|
||||
" C.value AS column_description" +
|
||||
" FROM sys.tables A" +
|
||||
" LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id" +
|
||||
" LEFT JOIN sys.columns B ON B.object_id = A.object_id AND C.minor_id = B.column_id" +
|
||||
" INNER JOIN sys.schemas SC ON SC.schema_id = A.schema_id AND SC.name = 'dbo'" +
|
||||
" WHERE A.name = '{1}' and b.name = '{0}'";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected override string AddTableRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXECUTE sp_addextendedproperty N'MS_Description', '{1}', N'user', N'dbo', N'table', N'{0}', NULL, NULL";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string DeleteTableRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXEC sp_dropextendedproperty 'MS_Description','user',dbo,'table','{0}' ";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override string IsAnyTableRemarkSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return @"SELECT C.class_desc
|
||||
FROM sys.tables A
|
||||
LEFT JOIN sys.extended_properties C ON C.major_id = A.object_id
|
||||
INNER JOIN sys.schemas SC ON SC.schema_id=A.schema_id AND SC.name='dbo'
|
||||
WHERE A.name = '{0}' AND minor_id=0";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected override string RenameTableSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "EXEC sp_rename '{0}','{1}'";
|
||||
}
|
||||
}
|
||||
|
||||
protected override string CreateIndexSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "CREATE {3} NONCLUSTERED INDEX Index_{0}_{2} ON {0}({1})";
|
||||
}
|
||||
}
|
||||
protected override string AddDefaultValueSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "alter table {0} ADD DEFAULT '{2}' FOR {1}";
|
||||
}
|
||||
}
|
||||
protected override string IsAnyIndexSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "select count(*) from sys.indexes where name='{0}'";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Check
|
||||
protected override string CheckSystemTablePermissionsSql
|
||||
{
|
||||
get
|
||||
{
|
||||
return "select top 1 id from sysobjects";
|
||||
}
|
||||
}
|
||||
#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 "IDENTITY(1,1)";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
public List<string> GetSchemas()
|
||||
{
|
||||
return this.Context.Ado.SqlQuery<string>("SELECT name FROM sys.schemas where name <> 'dbo'");
|
||||
}
|
||||
public override bool DeleteTableRemark(string tableName)
|
||||
{
|
||||
string sql = string.Format(this.DeleteTableRemarkSql, tableName);
|
||||
if (tableName.Contains("."))
|
||||
{
|
||||
var schemas = GetSchemas();
|
||||
var tableSchemas = this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
|
||||
if (schemas.Any(y => y.EqualCase(tableSchemas)))
|
||||
{
|
||||
sql = string.Format(this.DeleteTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()));
|
||||
sql = sql.Replace(",dbo,", $",{tableSchemas},").Replace("'user'", "'SCHEMA'");
|
||||
}
|
||||
}
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
public override bool IsAnyTableRemark(string tableName)
|
||||
{
|
||||
string sql = string.Format(this.IsAnyTableRemarkSql, tableName);
|
||||
if (tableName.Contains("."))
|
||||
{
|
||||
var schemas = GetSchemas();
|
||||
var tableSchemas = this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
|
||||
if (schemas.Any(y => y.EqualCase(tableSchemas)))
|
||||
{
|
||||
sql = string.Format(this.IsAnyTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()));
|
||||
sql = sql.Replace("'dbo'", $"'{tableSchemas}'");
|
||||
}
|
||||
}
|
||||
var dt = this.Context.Ado.GetDataTable(sql);
|
||||
return dt.Rows != null && dt.Rows.Count > 0;
|
||||
}
|
||||
public override bool AddTableRemark(string tableName, string description)
|
||||
{
|
||||
string sql = string.Format(this.AddTableRemarkSql, tableName, description);
|
||||
if (tableName.Contains("."))
|
||||
{
|
||||
var schemas = GetSchemas();
|
||||
var tableSchemas =this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').First());
|
||||
if (schemas.Any(y => y.EqualCase(tableSchemas)))
|
||||
{
|
||||
sql = string.Format(this.AddTableRemarkSql, this.SqlBuilder.GetNoTranslationColumnName(tableName.Split('.').Last()), description);
|
||||
sql = sql.Replace("N'dbo'", $"N'{tableSchemas}'").Replace("N'user'", "N'SCHEMA'");
|
||||
}
|
||||
}
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
public override bool AddDefaultValue(string tableName, string columnName, string defaultValue)
|
||||
{
|
||||
if (defaultValue == "''")
|
||||
{
|
||||
defaultValue = "";
|
||||
}
|
||||
var template = AddDefaultValueSql;
|
||||
if (defaultValue != null && defaultValue.ToLower() == "getdate()")
|
||||
{
|
||||
template = template.Replace("'{2}'", "{2}");
|
||||
}
|
||||
string sql = string.Format(template, tableName, columnName, defaultValue);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///by current connection string
|
||||
/// </summary>
|
||||
/// <param name="databaseDirectory"></param>
|
||||
/// <returns></returns>
|
||||
public override bool CreateDatabase(string databaseName, string databaseDirectory = null)
|
||||
{
|
||||
if (databaseDirectory != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!FileHelper.IsExistDirectory(databaseDirectory))
|
||||
{
|
||||
FileHelper.CreateDirectory(databaseDirectory);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
//Databases and sites are not in the same service
|
||||
}
|
||||
}
|
||||
var oldDatabaseName = this.Context.Ado.Connection.Database;
|
||||
var connection = this.Context.CurrentConnectionConfig.ConnectionString;
|
||||
connection = connection.Replace(oldDatabaseName, "master");
|
||||
var newDb = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = this.Context.CurrentConnectionConfig.DbType,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = connection
|
||||
});
|
||||
if (!GetDataBaseList(newDb).Any(it => it.Equals(databaseName, StringComparison.CurrentCultureIgnoreCase)))
|
||||
{
|
||||
var sql = CreateDataBaseSql;
|
||||
if (databaseDirectory.HasValue())
|
||||
{
|
||||
sql += @"on primary
|
||||
(
|
||||
name = N'{0}',
|
||||
filename = N'{1}\{0}.mdf',
|
||||
size = 10mb,
|
||||
maxsize = 100mb,
|
||||
filegrowth = 1mb
|
||||
),
|
||||
(
|
||||
name = N'{0}_ndf',
|
||||
filename = N'{1}\{0}.ndf',
|
||||
size = 10mb,
|
||||
maxsize = 100mb,
|
||||
filegrowth = 10 %
|
||||
)
|
||||
log on
|
||||
(
|
||||
name = N'{0}_log',
|
||||
filename = N'{1}\{0}.ldf',
|
||||
size = 100mb,
|
||||
maxsize = 1gb,
|
||||
filegrowth = 10mb
|
||||
); ";
|
||||
}
|
||||
newDb.Ado.ExecuteCommand(string.Format(sql, databaseName, databaseDirectory));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public override bool CreateTable(string tableName, List<DbColumnInfo> columns, bool isCreatePrimaryKey = true)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
foreach (var item in columns)
|
||||
{
|
||||
if (item.DataType == "decimal" && item.DecimalDigits == 0 && item.Length == 0)
|
||||
{
|
||||
item.DecimalDigits = 4;
|
||||
item.Length = 18;
|
||||
}
|
||||
}
|
||||
string sql = GetCreateTableSql(tableName, columns);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
if (isCreatePrimaryKey)
|
||||
{
|
||||
var pkColumns = columns.Where(it => it.IsPrimarykey).ToList();
|
||||
if (pkColumns.Count > 1)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKeys(tableName, pkColumns.Select(it => it.DbColumnName).ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in pkColumns)
|
||||
{
|
||||
this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public override List<DbColumnInfo> GetColumnInfosByTableName(string tableName, bool isCache = true)
|
||||
{
|
||||
tableName = SqlBuilder.GetNoTranslationColumnName(tableName);
|
||||
var result= base.GetColumnInfosByTableName(tableName, isCache);
|
||||
return result;
|
||||
}
|
||||
public override bool RenameColumn(string tableName, string oldColumnName, string newColumnName)
|
||||
{
|
||||
tableName = this.SqlBuilder.GetTranslationTableName(tableName);
|
||||
oldColumnName = this.SqlBuilder.GetTranslationColumnName(oldColumnName);
|
||||
newColumnName = this.SqlBuilder.GetNoTranslationColumnName(newColumnName);
|
||||
string sql = string.Format(this.RenameColumnSql, tableName, oldColumnName, newColumnName);
|
||||
this.Context.Ado.ExecuteCommand(sql);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerQueryable<T>:QueryableProvider<T>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T,T2> : QueryableProvider<T,T2>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2,T3> : QueryableProvider<T, T2,T3>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2,T3,T4> : QueryableProvider<T, T2,T3,T4>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4,T5> : QueryableProvider<T, T2, T3, T4,T5>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5,T6> : QueryableProvider<T, T2, T3, T4, T5,T6>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6,T7> : QueryableProvider<T, T2, T3, T4, T5, T6,T7>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6, T7,T8> : QueryableProvider<T, T2, T3, T4, T5, T6, T7,T8>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>
|
||||
{
|
||||
|
||||
}
|
||||
public class SqlServerQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> : QueryableProvider<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>
|
||||
{
|
||||
|
||||
}
|
||||
}
|
154
Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessBlukCopy.cs
Normal file
154
Src/Asp.Net/SqlSugar.Access/Access/SqlBuilder/AccessBlukCopy.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerBlukCopy
|
||||
{
|
||||
internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
|
||||
internal SqlSugarProvider Context { get; set; }
|
||||
internal ISqlBuilder Builder { get; set; }
|
||||
internal InsertBuilder InsertBuilder { get; set; }
|
||||
internal object[] Inserts { get; set; }
|
||||
|
||||
public int ExecuteBulkCopy()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
if (Inserts.First().GetType() == typeof(DataTable))
|
||||
{
|
||||
return WriteToServer();
|
||||
}
|
||||
DataTable dt = GetCopyData();
|
||||
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
try
|
||||
{
|
||||
bulkCopy.WriteToServer(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CloseDb();
|
||||
throw ex;
|
||||
}
|
||||
CloseDb();
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
public async Task<int> ExecuteBulkCopyAsync()
|
||||
{
|
||||
if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
|
||||
|
||||
if (Inserts.First().GetType() == typeof(DataTable))
|
||||
{
|
||||
return WriteToServer();
|
||||
}
|
||||
DataTable dt=GetCopyData();
|
||||
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
|
||||
try
|
||||
{
|
||||
await bulkCopy.WriteToServerAsync(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CloseDb();
|
||||
throw ex;
|
||||
}
|
||||
CloseDb();
|
||||
return DbColumnInfoList.Count;
|
||||
}
|
||||
|
||||
private int WriteToServer()
|
||||
{
|
||||
var dt = this.Inserts.First() as DataTable;
|
||||
if (dt == null)
|
||||
return 0;
|
||||
Check.Exception(dt.TableName == "Table", "dt.TableName can't be null ");
|
||||
dt = GetCopyWriteDataTable(dt);
|
||||
SqlBulkCopy copy = GetBulkCopyInstance();
|
||||
copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
|
||||
copy.WriteToServer(dt);
|
||||
CloseDb();
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
private DataTable GetCopyWriteDataTable(DataTable dt)
|
||||
{
|
||||
var result = this.Context.Ado.GetDataTable("select top 0 * from " + this.Builder.GetTranslationColumnName(dt.TableName));
|
||||
foreach (DataRow item in dt.Rows)
|
||||
{
|
||||
DataRow dr= result.NewRow();
|
||||
foreach (DataColumn column in result.Columns)
|
||||
{
|
||||
|
||||
if (dt.Columns.Cast<DataColumn>().Select(it => it.ColumnName.ToLower()).Contains(column.ColumnName.ToLower()))
|
||||
{
|
||||
dr[column.ColumnName] = item[column.ColumnName];
|
||||
if (dr[column.ColumnName] == null)
|
||||
{
|
||||
dr[column.ColumnName] = DBNull.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
result.Rows.Add(dr);
|
||||
}
|
||||
result.TableName = dt.TableName;
|
||||
return result;
|
||||
}
|
||||
private SqlBulkCopy GetBulkCopyInstance()
|
||||
{
|
||||
SqlBulkCopy copy;
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
|
||||
}
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
copy.BulkCopyTimeout = this.Context.Ado.CommandTimeOut;
|
||||
return copy;
|
||||
}
|
||||
private DataTable GetCopyData()
|
||||
{
|
||||
var dt = this.Context.Ado.GetDataTable("select top 0 * from " + InsertBuilder.GetTableNameString);
|
||||
foreach (var rowInfos in DbColumnInfoList)
|
||||
{
|
||||
var dr = dt.NewRow();
|
||||
foreach (var value in rowInfos)
|
||||
{
|
||||
if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
|
||||
{
|
||||
if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString())
|
||||
{
|
||||
value.Value = Convert.ToDateTime("1753/01/01");
|
||||
}
|
||||
}
|
||||
if (value.Value == null)
|
||||
{
|
||||
value.Value = DBNull.Value;
|
||||
}
|
||||
dr[value.DbColumnName] = value.Value;
|
||||
}
|
||||
dt.Rows.Add(dr);
|
||||
}
|
||||
return dt;
|
||||
}
|
||||
private void CloseDb()
|
||||
{
|
||||
if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
|
||||
{
|
||||
this.Context.Ado.Connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerBuilder : SqlBuilderProvider
|
||||
{
|
||||
public override string SqlTranslationLeft { get { return "["; } }
|
||||
public override string SqlTranslationRight { get { return "]"; } }
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerDeleteBuilder: DeleteBuilder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public partial class SqlServerExpressionContext : ExpressionContext, ILambdaExpressions
|
||||
{
|
||||
public SqlSugarProvider Context { get; set; }
|
||||
public SqlServerExpressionContext()
|
||||
{
|
||||
base.DbMehtods = new SqlServerMethod();
|
||||
}
|
||||
|
||||
}
|
||||
public partial class SqlServerMethod : DefaultDbMethod, IDbMethods
|
||||
{
|
||||
public override string HasValue(MethodCallExpressionModel model)
|
||||
{
|
||||
if (model.Args[0].Type == UtilConstants.GuidType)
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format("( {0} IS NOT NULL )", parameter.MemberName);
|
||||
}
|
||||
else
|
||||
{
|
||||
var parameter = model.Args[0];
|
||||
return string.Format("( {0}<>'' AND {0} IS NOT NULL )", parameter.MemberName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
|
||||
public class SqlServerFastBuilder:FastBuilder,IFastBuilder
|
||||
{
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
|
||||
SqlBulkCopy bulkCopy = GetBulkCopyInstance();
|
||||
bulkCopy.DestinationTableName = dt.TableName;
|
||||
try
|
||||
{
|
||||
await bulkCopy.WriteToServerAsync(dt);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
CloseDb();
|
||||
throw ex;
|
||||
}
|
||||
CloseDb();
|
||||
return dt.Rows.Count;
|
||||
}
|
||||
public SqlBulkCopy GetBulkCopyInstance()
|
||||
{
|
||||
SqlBulkCopy copy;
|
||||
if (this.Context.Ado.Transaction == null)
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection);
|
||||
}
|
||||
else
|
||||
{
|
||||
copy = new SqlBulkCopy((SqlConnection)this.Context.Ado.Connection, SqlBulkCopyOptions.CheckConstraints, (SqlTransaction)this.Context.Ado.Transaction);
|
||||
}
|
||||
if (this.Context.Ado.Connection.State == ConnectionState.Closed)
|
||||
{
|
||||
this.Context.Ado.Connection.Open();
|
||||
}
|
||||
copy.BulkCopyTimeout = this.Context.Ado.CommandTimeOut;
|
||||
return copy;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerInsertBuilder:InsertBuilder
|
||||
{
|
||||
public override string ToSqlString()
|
||||
{
|
||||
if (IsNoInsertNull)
|
||||
{
|
||||
DbColumnInfoList = DbColumnInfoList.Where(it => it.Value != null).ToList();
|
||||
}
|
||||
var groupList = DbColumnInfoList.GroupBy(it => it.TableId).ToList();
|
||||
var isSingle = groupList.Count() == 1;
|
||||
string columnsString = string.Join(",", groupList.First().Select(it => Builder.GetTranslationColumnName(it.DbColumnName)));
|
||||
if (isSingle)
|
||||
{
|
||||
string columnParametersString = string.Join(",", this.DbColumnInfoList.Select(it => Builder.SqlParameterKeyWord + it.DbColumnName));
|
||||
return string.Format(SqlTemplate, GetTableNameString, columnsString, columnParametersString);
|
||||
}
|
||||
else
|
||||
{
|
||||
StringBuilder batchInsetrSql = new StringBuilder();
|
||||
int pageSize = 200;
|
||||
if (this.EntityInfo.Columns.Count > 30)
|
||||
{
|
||||
pageSize = 50;
|
||||
}
|
||||
else if (this.EntityInfo.Columns.Count > 20)
|
||||
{
|
||||
pageSize = 100;
|
||||
}
|
||||
int pageIndex = 1;
|
||||
int totalRecord = groupList.Count;
|
||||
int pageCount = (totalRecord + pageSize - 1) / pageSize;
|
||||
while (pageCount >= pageIndex)
|
||||
{
|
||||
batchInsetrSql.AppendFormat(SqlTemplateBatch, GetTableNameString, columnsString);
|
||||
int i = 0;
|
||||
foreach (var columns in groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList())
|
||||
{
|
||||
var isFirst = i == 0;
|
||||
if (!isFirst)
|
||||
{
|
||||
batchInsetrSql.Append(SqlTemplateBatchUnion);
|
||||
}
|
||||
batchInsetrSql.Append("\r\n SELECT " + string.Join(",", columns.Select(it => string.Format(SqlTemplateBatchSelect, FormatValue(it.Value), Builder.GetTranslationColumnName(it.DbColumnName)))));
|
||||
++i;
|
||||
}
|
||||
pageIndex++;
|
||||
batchInsetrSql.Append("\r\n;\r\n");
|
||||
}
|
||||
var result = batchInsetrSql.ToString();
|
||||
if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)
|
||||
{
|
||||
result += "select SCOPE_IDENTITY();";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerQueryBuilder: QueryBuilder
|
||||
{
|
||||
public override string SqlTemplate
|
||||
{
|
||||
get
|
||||
{
|
||||
return "SELECT {0}{"+UtilConstants.ReplaceKey+"} FROM {1}{2}{3}{4}";
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToSqlString()
|
||||
{
|
||||
string oldOrderBy = this.OrderByValue;
|
||||
string externalOrderBy = oldOrderBy;
|
||||
var isIgnoreOrderBy = this.IsCount && this.PartitionByValue.IsNullOrEmpty();
|
||||
AppendFilter();
|
||||
sql = new StringBuilder();
|
||||
if (this.OrderByValue == null && (Skip != null || Take != null)) this.OrderByValue = " ORDER BY GetDate() ";
|
||||
if (this.PartitionByValue.HasValue())
|
||||
{
|
||||
this.OrderByValue = this.PartitionByValue + this.OrderByValue;
|
||||
}
|
||||
var isFirst = (Skip == 0 || Skip == null) && Take == 1 && DisableTop == false;
|
||||
var isRowNumber = (Skip != null || Take != null) && !isFirst;
|
||||
var rowNumberString = string.Format(",ROW_NUMBER() OVER({0}) AS RowIndex ", GetOrderByString);
|
||||
string groupByValue = GetGroupByString + HavingInfos;
|
||||
string orderByValue = (!isRowNumber && this.OrderByValue.HasValue()) ? GetOrderByString : null;
|
||||
if (isIgnoreOrderBy) { orderByValue = null; }
|
||||
sql.AppendFormat(SqlTemplate, isFirst ? (" TOP 1 " + GetSelectValue) : GetSelectValue, GetTableNameString, GetWhereValueString, groupByValue, orderByValue);
|
||||
sql.Replace(UtilConstants.ReplaceKey, isRowNumber ? (isIgnoreOrderBy ? null : rowNumberString) : null);
|
||||
if (isIgnoreOrderBy) { this.OrderByValue = oldOrderBy; return sql.ToString(); }
|
||||
var result = isFirst ? sql.ToString() : ToPageSql(sql.ToString(), this.Take, this.Skip);
|
||||
if (ExternalPageIndex > 0)
|
||||
{
|
||||
if (externalOrderBy.IsNullOrEmpty())
|
||||
{
|
||||
externalOrderBy = " ORDER BY GetDate() ";
|
||||
}
|
||||
result = string.Format("SELECT *,ROW_NUMBER() OVER({0}) AS RowIndex2 FROM ({1}) ExternalTable ", GetExternalOrderBy(externalOrderBy), result);
|
||||
result = ToPageSql2(result, ExternalPageIndex, ExternalPageSize, true);
|
||||
}
|
||||
this.OrderByValue = oldOrderBy;
|
||||
if (!string.IsNullOrEmpty(this.Offset))
|
||||
{
|
||||
if (this.OrderByValue.IsNullOrEmpty())
|
||||
{
|
||||
result += " ORDER BY GETDATE() ";
|
||||
}
|
||||
result += this.Offset;
|
||||
}
|
||||
result = GetSqlQuerySql(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar.Access
|
||||
{
|
||||
public class SqlServerUpdateBuilder: UpdateBuilder
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user