SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
9.8 KiB

using SqlSugar.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
3 years ago
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
2 years ago
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SqlSugar.Access
{
3 years ago
public class AccessProvider : AdoProvider
{
3 years ago
public AccessProvider() { }
public override IDbConnection Connection
{
get
{
if (base._DbConnection == null)
{
try
{
3 years ago
base._DbConnection = new OleDbConnection(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();
3 years ago
base.Transaction = ((OleDbConnection)this.Connection).BeginTransaction();
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="iso"></param>
/// <param name="transactionName"></param>
public override void BeginTran(IsolationLevel iso, string transactionName)
{
CheckConnection();
3 years ago
base.Transaction = ((OleDbConnection)this.Connection).BeginTransaction(iso);
}
public override IDataAdapter GetAdapter()
{
3 years ago
return new OleDbDataAdapter();
}
public override DbCommand GetCommand(string sql, SugarParameter[] parameters)
{
3 years ago
OleDbCommand sqlCommand = new OleDbCommand(sql, (OleDbConnection)this.Connection);
sqlCommand.CommandType = this.CommandType;
sqlCommand.CommandTimeout = this.CommandTimeOut;
if (this.Transaction != null)
{
3 years ago
sqlCommand.Transaction = (OleDbTransaction)this.Transaction;
}
if (parameters.HasValue())
{
3 years ago
OleDbParameter[] ipars = GetSqlParameter(parameters).Where(x=>sql.ToLower().Contains(x.ParameterName.ToLower())).ToArray();
3 years ago
if (ipars != null)
{
ipars = ipars.OrderBy(it => sql.IndexOf(it.ParameterName)).ToArray();
}
sqlCommand.Parameters.AddRange(ipars);
3 years ago
}
CheckConnection();
return sqlCommand;
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
{
3 years ago
((OleDbDataAdapter)dataAdapter).SelectCommand = (OleDbCommand)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;
3 years ago
OleDbParameter[] result = new OleDbParameter[parameters.Length];
int index = 0;
foreach (var parameter in parameters)
{
if (parameter.Value == null) parameter.Value = DBNull.Value;
3 years ago
var sqlParameter = new OleDbParameter();
sqlParameter.ParameterName = parameter.ParameterName;
3 years ago
//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;
}
2 years ago
public string SplitCommandTag = UtilConstants.ReplaceCommaKey;
public override int ExecuteCommand(string sql, SugarParameter[] parameters)
{
if (sql == null) throw new Exception("sql is null");
if (sql.IndexOf(this.SplitCommandTag) > 0)
{
var sqlParts = Regex.Split(sql, this.SplitCommandTag).Where(it => !string.IsNullOrEmpty(it)).ToList();
int result = 0;
foreach (var item in sqlParts)
{
if (item.TrimStart('\r').TrimStart('\n') != "")
{
result += base.ExecuteCommand(item, parameters);
}
}
return result;
}
else
{
2 years ago
if (sql.TrimStart('\r').TrimStart('\n') != "")
{
return base.ExecuteCommand(sql, parameters);
}
else
{
return 0;
}
2 years ago
}
}
public override async Task<int> ExecuteCommandAsync(string sql, SugarParameter[] parameters)
{
if (sql == null) throw new Exception("sql is null");
if (sql.IndexOf(this.SplitCommandTag) > 0)
{
var sqlParts = Regex.Split(sql, this.SplitCommandTag).Where(it => !string.IsNullOrEmpty(it)).ToList();
int result = 0;
foreach (var item in sqlParts)
{
if (item.TrimStart('\r').TrimStart('\n') != "")
{
result += await base.ExecuteCommandAsync(item, parameters);
}
}
return result;
}
else
{
return base.ExecuteCommand(sql, parameters);
}
}
/// <summary>
/// if mysql return MySqlParameter[] pars
/// if sqlerver return SqlParameter[] pars ...
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
3 years ago
public OleDbParameter[] GetSqlParameter(params SugarParameter[] parameters)
{
var isVarchar =IsVarchar();
if (parameters == null || parameters.Length == 0) return null;
3 years ago
OleDbParameter[] result = new OleDbParameter[parameters.Length];
int index = 0;
foreach (var parameter in parameters)
{
if (parameter.Value == null) parameter.Value = DBNull.Value;
3 years ago
var sqlParameter = new OleDbParameter();
sqlParameter.ParameterName = parameter.ParameterName;
3 years ago
//sqlParameter.UdtTypeName = parameter.UdtTypeName;
sqlParameter.Size = parameter.Size;
sqlParameter.Value = parameter.Value;
sqlParameter.DbType = parameter.DbType;
3 years ago
var isTime = parameter.DbType == System.Data.DbType.DateTime;
if (isTime)
{
sqlParameter.DbType = System.Data.DbType.String;
if (sqlParameter.Value != DBNull.Value)
{
sqlParameter.Value = sqlParameter.Value.ToString();
}
3 years ago
}
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;
3 years ago
//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;
}
}
}