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.
 
 
 
 
 

175 lines
6.5 KiB

using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
namespace SqlSugar
{
public class OracleProvider : AdoProvider
{
public OracleProvider()
{
this.FormatSql = sql =>
{
if (sql.HasValue()&&sql.Contains("@")) {
var exceptionalCaseInfo = Regex.Matches(sql,@"\'.*?\@.*?\'");
if (exceptionalCaseInfo != null) {
foreach (var item in exceptionalCaseInfo.Cast<Match>())
{
sql = sql.Replace(item.Value, item.Value.Replace("@", UtilConstants.ReplaceKey));
}
}
sql = sql .Replace("@",":");
sql = sql.Replace(UtilConstants.ReplaceKey, "@");
}
return sql;
};
}
public override string SqlParameterKeyWord
{
get
{
return ":";
}
}
public override IDbConnection Connection
{
get
{
try
{
if (base._DbConnection == null)
{
base._DbConnection = new OracleConnection(base.Context.CurrentConnectionConfig.ConnectionString);
}
}
catch (Exception ex)
{
Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
}
return base._DbConnection;
}
set
{
base._DbConnection = value;
}
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="transactionName"></param>
public override void BeginTran(string transactionName)
{
((OracleConnection)this.Connection).BeginTransaction();
}
/// <summary>
/// Only SqlServer
/// </summary>
/// <param name="iso"></param>
/// <param name="transactionName"></param>
public override void BeginTran(IsolationLevel iso, string transactionName)
{
((OracleConnection)this.Connection).BeginTransaction(iso);
}
public override IDataAdapter GetAdapter()
{
return new OracleDataAdapter();
}
public override IDbCommand GetCommand(string sql, SugarParameter[] parameters)
{
var sqlCommand = new OracleCommand(sql, (OracleConnection)this.Connection)
{
BindByName = true,
CommandType = this.CommandType,
CommandTimeout = this.CommandTimeOut
};
if (this.Transaction != null)
{
sqlCommand.Transaction = (OracleTransaction)this.Transaction;
}
if (parameters.HasValue())
{
var ipars = ToIDbDataParameter(parameters);
sqlCommand.Parameters.AddRange((OracleParameter[])ipars);
}
CheckConnection();
return sqlCommand;
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, IDbCommand command)
{
((OracleDataAdapter)dataAdapter).SelectCommand = (OracleCommand)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;
var result = new OracleParameter[parameters.Length];
var index = 0;
foreach (var parameter in parameters)
{
if (parameter.Value == null) parameter.Value = DBNull.Value;
var sqlParameter = new OracleParameter
{
Size = parameter.Size == -1 ? 0 : parameter.Size,
ParameterName = parameter.ParameterName
};
if (sqlParameter.ParameterName[0] == '@')
{
sqlParameter.ParameterName = ':' + sqlParameter.ParameterName.Substring(1, sqlParameter.ParameterName.Length - 1);
}
if (this.CommandType == CommandType.StoredProcedure)
{
sqlParameter.ParameterName = sqlParameter.ParameterName.TrimStart(':');
}
if (parameter.IsRefCursor)
{
sqlParameter.OracleDbType = OracleDbType.RefCursor;
}
if (sqlParameter.DbType == System.Data.DbType.Guid)
{
sqlParameter.DbType = System.Data.DbType.String;
sqlParameter.Value = sqlParameter.Value.ObjToString();
}
else if (parameter.DbType == System.Data.DbType.Boolean)
{
sqlParameter.DbType = System.Data.DbType.Int16;
if (parameter.Value == DBNull.Value)
{
parameter.Value = 0;
}
else
{
sqlParameter.Value = (bool)parameter.Value ? 1 : 0;
}
}
else
{
if (parameter.Value != null && parameter.Value.GetType() == UtilConstants.GuidType)
{
parameter.Value = parameter.Value.ToString();
}
sqlParameter.Value = parameter.Value;
}
if (parameter.Direction != 0)
sqlParameter.Direction = parameter.Direction;
result[index] = sqlParameter;
if (sqlParameter.Direction.IsIn(ParameterDirection.Output, ParameterDirection.InputOutput))
{
if (this.OutputParameters == null) this.OutputParameters = new List<IDataParameter>();
this.OutputParameters.RemoveAll(it => it.ParameterName == sqlParameter.ParameterName);
this.OutputParameters.Add(sqlParameter);
}
++index;
}
return result;
}
}
}