-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbString.cs
94 lines (89 loc) · 3.15 KB
/
DbString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Data;
namespace Dapper
{
/// <summary>
/// This class represents a SQL string, it can be used if you need to denote your parameter is a Char vs VarChar vs nVarChar vs nChar
/// </summary>
public sealed class DbString : SqlMapper.ICustomQueryParameter
{
/// <summary>
/// Default value for IsAnsi.
/// </summary>
public static bool IsAnsiDefault { get; set; }
/// <summary>
/// A value to set the default value of strings
/// going through Dapper. Default is 4000, any value larger than this
/// field will not have the default value applied.
/// </summary>
public const int DefaultLength = 4000;
/// <summary>
/// Create a new DbString
/// </summary>
public DbString()
{
Length = -1;
IsAnsi = IsAnsiDefault;
}
/// <summary>
/// Ansi vs Unicode
/// </summary>
public bool IsAnsi { get; set; }
/// <summary>
/// Fixed length
/// </summary>
public bool IsFixedLength { get; set; }
/// <summary>
/// Length of the string -1 for max
/// </summary>
public int Length { get; set; }
/// <summary>
/// The value of the string
/// </summary>
public string Value { get; set; }
/// <summary>
/// Gets a string representation of this DbString.
/// </summary>
public override string ToString() =>
$"Dapper.DbString (Value: '{Value}', Length: {Length}, IsAnsi: {IsAnsi}, IsFixedLength: {IsFixedLength})";
/// <summary>
/// Add the parameter to the command... internal use only
/// </summary>
/// <param name="command"></param>
/// <param name="name"></param>
public void AddParameter(IDbCommand command, string name)
{
if (IsFixedLength && Length == -1)
{
throw new InvalidOperationException("If specifying IsFixedLength, a Length must also be specified");
}
bool add = !command.Parameters.Contains(name);
IDbDataParameter param;
if (add)
{
param = command.CreateParameter();
param.ParameterName = name;
}
else
{
param = (IDbDataParameter)command.Parameters[name];
}
#pragma warning disable 0618
param.Value = SqlMapper.SanitizeParameterValue(Value);
#pragma warning restore 0618
if (Length == -1 && Value != null && Value.Length <= DefaultLength)
{
param.Size = DefaultLength;
}
else
{
param.Size = Length;
}
param.DbType = IsAnsi ? (IsFixedLength ? DbType.AnsiStringFixedLength : DbType.AnsiString) : (IsFixedLength ? DbType.StringFixedLength : DbType.String);
if (add)
{
command.Parameters.Add(param);
}
}
}
}