Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DataAccessProvider.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ public class Example
{
// For SQL Server
var parameters = new List<SqlParameter>();
parameters.AddParameter("@Id", DbType.Int32, 1);
parameters.AddParameter("@Id", DbType.Int32, 2);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 1);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 2);

// For PostgreSQL
var parameters = new List<NpgsqlParameter>();
parameters.AddParameter("@Name", DbType.String, "John Doe");
parameters.AddParameter("@Name", DataAccessDbType.String, "John Doe");

// Use parameters with your database command
}
Expand All @@ -330,4 +330,4 @@ Contributions are welcome! If you'd like to add support for additional databases

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
33 changes: 33 additions & 0 deletions DataAccessProvider.Core/Types/DataAccessDbType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace DataAccessProvider.Core.Types;

public enum DataAccessDbType
{
AnsiString,
AnsiStringFixedLength,
Binary,
Byte,
Boolean,
Currency,
Date,
DateTime,
DateTime2,
DateTimeOffset,
Decimal,
Double,
Guid,
Int16,
Int32,
Int64,
Json,
Object,
SByte,
Single,
String,
StringFixedLength,
Time,
UInt16,
UInt32,
UInt64,
VarNumeric,
Xml
}
9 changes: 9 additions & 0 deletions DataAccessProvider.Core/Types/DataAccessParameterDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DataAccessProvider.Core.Types;

public enum DataAccessParameterDirection
{
Input,
Output,
InputOutput,
ReturnValue
}
59 changes: 51 additions & 8 deletions DataAccessProvider.MSSQL/DbParameterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Data.SqlClient;
using DataAccessProvider.Core.Types;
using Microsoft.Data.SqlClient;
using System.Data;

namespace DataAccessProvider.MSSQL;
Expand All @@ -10,30 +11,72 @@ public static class DbParameterExtensions
/// </summary>
/// <param name="parameters">The list of SqlParameters to which the new parameter will be added.</param>
/// <param name="parameterName">The name of the parameter (e.g., @ParameterName).</param>
/// <param name="dbType">The SqlDbType representing the type of the parameter (e.g., SqlDbType.VarChar).</param>
/// <param name="dbType">The DataAccessDbType representing the type of the parameter (e.g., DataAccessDbType.String).</param>
/// <param name="value">The value of the parameter to be passed to the query or command.</param>
/// <param name="size">Optional size of the parameter (useful for string types); defaults to -1, which indicates no specific size.</param>
/// <returns>The updated list of SqlParameters with the new parameter added.</returns>
public static List<SqlParameter> AddParameter(this List<SqlParameter> parameters, string parameterName, SqlDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1)
public static List<SqlParameter> AddParameter(this List<SqlParameter> parameters, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1)
{
// Create the appropriate parameter and add it to the list
var parameter = new SqlParameter();
parameter.ParameterName = parameterName;
parameter.SqlDbType = dbType;
parameter.SqlDbType = MapDbType(dbType);
parameter.Value = value ?? DBNull.Value;
parameter.Size = size;
parameter.Direction = direction;
parameter.Direction = MapDirection(direction);

parameters.Add(parameter);
return parameters;
}

public static MSSQLSourceParams AddParameter(this MSSQLSourceParams sourceParams, string parameterName, SqlDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1)
public static MSSQLSourceParams AddParameter(this MSSQLSourceParams sourceParams, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1)
{
var parameters = sourceParams.Parameters ?? new List<SqlParameter>();
parameters.AddParameter(parameterName, dbType, value, direction, size);
return sourceParams;
}

private static SqlDbType MapDbType(DataAccessDbType dbType) => dbType switch
{
DataAccessDbType.AnsiString => SqlDbType.VarChar,
DataAccessDbType.AnsiStringFixedLength => SqlDbType.Char,
DataAccessDbType.Binary => SqlDbType.VarBinary,
DataAccessDbType.Byte => SqlDbType.TinyInt,
DataAccessDbType.Boolean => SqlDbType.Bit,
DataAccessDbType.Currency => SqlDbType.Money,
DataAccessDbType.Date => SqlDbType.Date,
DataAccessDbType.DateTime => SqlDbType.DateTime,
DataAccessDbType.DateTime2 => SqlDbType.DateTime2,
DataAccessDbType.DateTimeOffset => SqlDbType.DateTimeOffset,
DataAccessDbType.Decimal => SqlDbType.Decimal,
DataAccessDbType.Double => SqlDbType.Float,
DataAccessDbType.Guid => SqlDbType.UniqueIdentifier,
DataAccessDbType.Int16 => SqlDbType.SmallInt,
DataAccessDbType.Int32 => SqlDbType.Int,
DataAccessDbType.Int64 => SqlDbType.BigInt,
DataAccessDbType.Json => SqlDbType.NVarChar,
DataAccessDbType.Object => SqlDbType.Variant,
DataAccessDbType.SByte => SqlDbType.SmallInt,
DataAccessDbType.Single => SqlDbType.Real,
DataAccessDbType.String => SqlDbType.NVarChar,
DataAccessDbType.StringFixedLength => SqlDbType.NChar,
DataAccessDbType.Time => SqlDbType.Time,
DataAccessDbType.UInt16 => SqlDbType.Int,
DataAccessDbType.UInt32 => SqlDbType.BigInt,
DataAccessDbType.UInt64 => SqlDbType.Decimal,
DataAccessDbType.VarNumeric => SqlDbType.Decimal,
DataAccessDbType.Xml => SqlDbType.Xml,
_ => throw new ArgumentOutOfRangeException(nameof(dbType), dbType, "Unsupported MSSQL data type.")
};

private static ParameterDirection MapDirection(DataAccessParameterDirection direction) => direction switch
{
DataAccessParameterDirection.Input => ParameterDirection.Input,
DataAccessParameterDirection.Output => ParameterDirection.Output,
DataAccessParameterDirection.InputOutput => ParameterDirection.InputOutput,
DataAccessParameterDirection.ReturnValue => ParameterDirection.ReturnValue,
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, "Unsupported parameter direction.")
};
}
6 changes: 3 additions & 3 deletions DataAccessProvider.MSSQL/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public class Example
{
// For SQL Server
var parameters = new List<SqlParameter>();
parameters.AddParameter("@Id", DbType.Int32, 1);
parameters.AddParameter("@Id", DbType.Int32, 2);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 1);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 2);

// Use parameters with your database command
}
Expand All @@ -165,4 +165,4 @@ Contributions are welcome! If you'd like to add support for additional databases

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
69 changes: 56 additions & 13 deletions DataAccessProvider.MySql/DbParameterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MySqlConnector;
using DataAccessProvider.Core.Types;
using MySqlConnector;
using System.Data;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand All @@ -7,15 +8,15 @@ namespace DataAccessProvider.MySql;

public static class DbParameterExtensions
{
public static List<MySqlParameter> AddParameter(this List<MySqlParameter> parameters, string parameterName, MySqlDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1)
public static List<MySqlParameter> AddParameter(this List<MySqlParameter> parameters, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1)
{
// Create the appropriate parameter and add it to the list
var parameter = new MySqlParameter();
parameter.ParameterName = parameterName;
parameter.MySqlDbType = dbType;
parameter.MySqlDbType = MapDbType(dbType);
parameter.Value = value ?? DBNull.Value;
parameter.Direction = direction;
parameter.Direction = MapDirection(direction);

if (size > 0)
{
Expand All @@ -26,16 +27,16 @@ public static List<MySqlParameter> AddParameter(this List<MySqlParameter> parame
return parameters;
}

public static MySQLSourceParams AddParameter(this MySQLSourceParams sourceParams, string parameterName, MySqlDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1)
public static MySQLSourceParams AddParameter(this MySQLSourceParams sourceParams, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1)
{
var parameters = sourceParams.Parameters ?? new List<MySqlParameter>();
parameters.AddParameter(parameterName, dbType, value, direction, size);
return sourceParams;
}

public static MySQLSourceParams<TValue> AddParameter<TValue>(this MySQLSourceParams<TValue> sourceParams, string parameterName, MySqlDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1) where TValue : class
public static MySQLSourceParams<TValue> AddParameter<TValue>(this MySQLSourceParams<TValue> sourceParams, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1) where TValue : class
{
var parameters = sourceParams.Parameters ?? new List<MySqlParameter>();
parameters.AddParameter(parameterName, dbType, value, direction, size);
Expand All @@ -53,7 +54,7 @@ public static MySQLSourceParams<TValue> AddParameter<TValue>(this MySQLSourcePar
public static MySQLSourceParams AddJSONParams(this MySQLSourceParams sourceParams, int operation, object? json = null!)
{
var parameters = sourceParams.Parameters ?? new List<MySqlParameter>();
parameters.AddParameter("Operation",MySqlDbType.UInt16, operation);
parameters.AddParameter("Operation", DataAccessDbType.UInt16, operation);

var options = new JsonSerializerOptions
{
Expand All @@ -62,7 +63,7 @@ public static MySQLSourceParams AddJSONParams(this MySQLSourceParams sourceParam

string? jsonPayload = json == null ? null : JsonSerializer.Serialize(json, options);

parameters.AddParameter("Params", MySqlDbType.JSON, jsonPayload!);
parameters.AddParameter("Params", DataAccessDbType.Json, jsonPayload!);
return sourceParams;
}
/// <summary>
Expand All @@ -77,15 +78,57 @@ public static MySQLSourceParams AddJSONParams(this MySQLSourceParams sourceParam
public static MySQLSourceParams<TValue> AddJSONParams<TValue>(this MySQLSourceParams<TValue> sourceParams, int operation, object? json = null!) where TValue : class
{
var parameters = sourceParams.Parameters ?? new List<MySqlParameter>();
parameters.AddParameter("Operation", MySqlDbType.UInt16, operation);
parameters.AddParameter("Operation", DataAccessDbType.UInt16, operation);
var options = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};

string? jsonPayload = json == null ? null : JsonSerializer.Serialize(json, options);

parameters.AddParameter("Params", MySqlDbType.JSON, jsonPayload!);
parameters.AddParameter("Params", DataAccessDbType.Json, jsonPayload!);
return sourceParams;
}

private static MySqlDbType MapDbType(DataAccessDbType dbType) => dbType switch
{
DataAccessDbType.AnsiString => MySqlDbType.VarChar,
DataAccessDbType.AnsiStringFixedLength => MySqlDbType.String,
DataAccessDbType.Binary => MySqlDbType.Blob,
DataAccessDbType.Byte => MySqlDbType.UByte,
DataAccessDbType.Boolean => MySqlDbType.Bool,
DataAccessDbType.Currency => MySqlDbType.Decimal,
DataAccessDbType.Date => MySqlDbType.Date,
DataAccessDbType.DateTime => MySqlDbType.DateTime,
DataAccessDbType.DateTime2 => MySqlDbType.DateTime,
DataAccessDbType.DateTimeOffset => MySqlDbType.Timestamp,
DataAccessDbType.Decimal => MySqlDbType.Decimal,
DataAccessDbType.Double => MySqlDbType.Double,
DataAccessDbType.Guid => MySqlDbType.Guid,
DataAccessDbType.Int16 => MySqlDbType.Int16,
DataAccessDbType.Int32 => MySqlDbType.Int32,
DataAccessDbType.Int64 => MySqlDbType.Int64,
DataAccessDbType.Json => MySqlDbType.JSON,
DataAccessDbType.Object => MySqlDbType.Blob,
DataAccessDbType.SByte => MySqlDbType.Byte,
DataAccessDbType.Single => MySqlDbType.Float,
DataAccessDbType.String => MySqlDbType.VarChar,
DataAccessDbType.StringFixedLength => MySqlDbType.String,
DataAccessDbType.Time => MySqlDbType.Time,
DataAccessDbType.UInt16 => MySqlDbType.UInt16,
DataAccessDbType.UInt32 => MySqlDbType.UInt32,
DataAccessDbType.UInt64 => MySqlDbType.UInt64,
DataAccessDbType.VarNumeric => MySqlDbType.Decimal,
DataAccessDbType.Xml => MySqlDbType.Text,
_ => throw new ArgumentOutOfRangeException(nameof(dbType), dbType, "Unsupported MySQL data type.")
};

private static ParameterDirection MapDirection(DataAccessParameterDirection direction) => direction switch
{
DataAccessParameterDirection.Input => ParameterDirection.Input,
DataAccessParameterDirection.Output => ParameterDirection.Output,
DataAccessParameterDirection.InputOutput => ParameterDirection.InputOutput,
DataAccessParameterDirection.ReturnValue => ParameterDirection.ReturnValue,
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, "Unsupported parameter direction.")
};
}
8 changes: 4 additions & 4 deletions DataAccessProvider.MySql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ public class Example
{
// For SQL Server
var parameters = new List<SqlParameter>();
parameters.AddParameter("@Id", DbType.Int32, 1);
parameters.AddParameter("@Id", DbType.Int32, 2);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 1);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 2);

// For PostgreSQL
var parameters = new List<NpgsqlParameter>();
parameters.AddParameter("@Name", DbType.String, "John Doe");
parameters.AddParameter("@Name", DataAccessDbType.String, "John Doe");

// Use parameters with your database command
}
Expand All @@ -171,4 +171,4 @@ Contributions are welcome! If you'd like to add support for additional databases

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
This project is licensed under the MIT License. See the [LICENSE](LICENSE.txt) file for details.
53 changes: 48 additions & 5 deletions DataAccessProvider.Oracle/DbParameterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
using Oracle.ManagedDataAccess.Client;
using DataAccessProvider.Core.Types;
using Oracle.ManagedDataAccess.Client;
using System.Data;

namespace DataAccessProvider.Oracle;

public static class DbParameterExtensions
{
public static List<OracleParameter> AddParameter(this List<OracleParameter> parameters, string parameterName, OracleDbType dbType,
object value, ParameterDirection direction = ParameterDirection.Input, int size = -1)
public static List<OracleParameter> AddParameter(this List<OracleParameter> parameters, string parameterName, DataAccessDbType dbType,
object value, DataAccessParameterDirection direction = DataAccessParameterDirection.Input, int size = -1)
{
// Create the appropriate parameter and add it to the list
var parameter = new OracleParameter();
parameter.ParameterName = parameterName;
parameter.OracleDbType = dbType;
parameter.OracleDbType = MapDbType(dbType);
parameter.Value = value ?? DBNull.Value;
parameter.Size = size;
parameter.Direction = direction;
parameter.Direction = MapDirection(direction);

parameters.Add(parameter);
return parameters;
}

private static OracleDbType MapDbType(DataAccessDbType dbType) => dbType switch
{
DataAccessDbType.AnsiString => OracleDbType.Varchar2,
DataAccessDbType.AnsiStringFixedLength => OracleDbType.Char,
DataAccessDbType.Binary => OracleDbType.Blob,
DataAccessDbType.Byte => OracleDbType.Byte,
DataAccessDbType.Boolean => OracleDbType.Boolean,
DataAccessDbType.Currency => OracleDbType.Decimal,
DataAccessDbType.Date => OracleDbType.Date,
DataAccessDbType.DateTime => OracleDbType.TimeStamp,
DataAccessDbType.DateTime2 => OracleDbType.TimeStamp,
DataAccessDbType.DateTimeOffset => OracleDbType.TimeStampTZ,
DataAccessDbType.Decimal => OracleDbType.Decimal,
DataAccessDbType.Double => OracleDbType.Double,
DataAccessDbType.Guid => OracleDbType.Raw,
DataAccessDbType.Int16 => OracleDbType.Int16,
DataAccessDbType.Int32 => OracleDbType.Int32,
DataAccessDbType.Int64 => OracleDbType.Int64,
DataAccessDbType.Json => OracleDbType.Json,
DataAccessDbType.Object => OracleDbType.Blob,
DataAccessDbType.SByte => OracleDbType.Int16,
DataAccessDbType.Single => OracleDbType.Single,
DataAccessDbType.String => OracleDbType.NVarchar2,
DataAccessDbType.StringFixedLength => OracleDbType.NChar,
DataAccessDbType.Time => OracleDbType.TimeStamp,
DataAccessDbType.UInt16 => OracleDbType.Int32,
DataAccessDbType.UInt32 => OracleDbType.Int64,
DataAccessDbType.UInt64 => OracleDbType.Decimal,
DataAccessDbType.VarNumeric => OracleDbType.Decimal,
DataAccessDbType.Xml => OracleDbType.XmlType,
_ => throw new ArgumentOutOfRangeException(nameof(dbType), dbType, "Unsupported Oracle data type.")
};

private static ParameterDirection MapDirection(DataAccessParameterDirection direction) => direction switch
{
DataAccessParameterDirection.Input => ParameterDirection.Input,
DataAccessParameterDirection.Output => ParameterDirection.Output,
DataAccessParameterDirection.InputOutput => ParameterDirection.InputOutput,
DataAccessParameterDirection.ReturnValue => ParameterDirection.ReturnValue,
_ => throw new ArgumentOutOfRangeException(nameof(direction), direction, "Unsupported parameter direction.")
};
}
Loading
Loading