forked from mysql-net/MySqlConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySqlParameter.cs
161 lines (147 loc) · 4.16 KB
/
MySqlParameter.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
namespace MySql.Data.MySqlClient
{
public sealed class MySqlParameter : DbParameter
{
public override DbType DbType { get; set; }
public override ParameterDirection Direction { get; set; }
public override bool IsNullable { get; set; }
public override string ParameterName
{
get
{
return m_name;
}
set
{
m_name = value;
NormalizedParameterName = NormalizeParameterName(m_name);
}
}
public override int Size { get; set; }
public override string SourceColumn
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override bool SourceColumnNullMapping
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
#if !NETSTANDARD1_3
public override DataRowVersion SourceVersion
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
#endif
public override object Value { get; set; }
public override void ResetDbType()
{
DbType = default(DbType);
}
internal string NormalizedParameterName { get; private set; }
internal void AppendSqlString(StringBuilder output, StatementPreparerOptions options)
{
if (Value == DBNull.Value)
{
output.Append("NULL");
}
else if (Value is string)
{
output.Append('\'');
output.Append(((string) Value).Replace("\\", "\\\\").Replace("'", "\\'"));
output.Append('\'');
}
else if (Value is byte || Value is sbyte || Value is short || Value is int || Value is long || Value is ushort || Value is uint || Value is ulong || Value is decimal)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", Value);
}
else if (Value is byte[])
{
// TODO: use a _binary'...' string for more efficient data transmission
output.Append("X'");
foreach (var by in (byte[]) Value)
output.AppendFormat(CultureInfo.InvariantCulture, "{0:X2}", by);
output.Append("'");
}
else if (Value is bool)
{
output.Append(((bool) Value) ? "true" : "false");
}
else if (Value is float || Value is double)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0:R}", Value);
}
else if (Value is DateTime)
{
output.AppendFormat(CultureInfo.InvariantCulture, "timestamp '{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss'.'ffffff}'", (DateTime) Value);
}
else if (Value is TimeSpan)
{
output.Append("time '");
var ts = (TimeSpan) Value;
if (ts.Ticks < 0)
{
output.Append('-');
ts = TimeSpan.FromTicks(-ts.Ticks);
}
output.AppendFormat(CultureInfo.InvariantCulture, "{0}:{1:mm':'ss'.'ffffff}'", ts.Days * 24 + ts.Hours, ts);
}
else if (Value is Guid)
{
if (options.HasFlag(StatementPreparerOptions.OldGuids))
{
output.Append("X'");
foreach (var by in ((Guid) Value).ToByteArray())
output.AppendFormat(CultureInfo.InvariantCulture, "{0:X2}", by);
output.Append("'");
}
else
{
output.AppendFormat("'{0:D}'", Value);
}
}
else if (DbType == DbType.Int16)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (short) Value);
}
else if (DbType == DbType.UInt16)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (ushort) Value);
}
else if (DbType == DbType.Int32)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (int) Value);
}
else if (DbType == DbType.UInt32)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (uint) Value);
}
else if (DbType == DbType.Int64)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (long) Value);
}
else if (DbType == DbType.UInt64)
{
output.AppendFormat(CultureInfo.InvariantCulture, "{0}", (ulong) Value);
}
else
{
throw new NotSupportedException("Parameter type {0} (DbType: {1}) not currently supported. Value: {2}".FormatInvariant(Value.GetType().Name, DbType, Value));
}
}
internal static string NormalizeParameterName(string name)
{
return string.IsNullOrEmpty(name) ? name :
name[0] == '?' || name[0] == '@' ? name.Substring(1) :
name;
}
string m_name;
}
}