|
6 | 6 | <xsl:output method="text" omit-xml-declaration="yes" indent="no"/> |
7 | 7 | <xsl:preserve-space elements="*" /> |
8 | 8 |
|
9 | | -<xsl:template match="type"> |
| 9 | +<xsl:template match="type" mode="typeonly"> |
10 | 10 | <xsl:choose> |
11 | 11 | <xsl:when test="@custom"> |
12 | | - <xsl:value-of select="@custom"/> |
| 12 | + <xsl:value-of select="@custom"/><xsl:if test="@isnull = 1">?</xsl:if> |
13 | 13 | </xsl:when> |
14 | 14 | <xsl:otherwise> |
15 | 15 | <xsl:if test="not(@unsigned = 1)"> |
16 | 16 | <xsl:choose> |
17 | 17 | <xsl:when test=". = 'bigint'">long<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
18 | 18 | <xsl:when test=". = 'binary'">byte[]</xsl:when> |
19 | 19 | <xsl:when test=". = 'bit'">ulong<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
20 | | - <xsl:when test=". = 'blob'">byte[]</xsl:when> |
| 20 | + <xsl:when test=". = 'blob'">byte[]</xsl:when> |
| 21 | + <xsl:when test=". = 'bool'">bool<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
21 | 22 | <xsl:when test=". = 'char'">string</xsl:when> |
22 | 23 | <xsl:when test=". = 'date'">DateTime<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
23 | 24 | <xsl:when test=". = 'datetime'">DateTime<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
|
37 | 38 | <xsl:when test=". = 'time'">TimeSpan<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
38 | 39 | <xsl:when test=". = 'timestamp'">DateTime<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
39 | 40 | <xsl:when test=". = 'tinyblob'">byte[]</xsl:when> |
40 | | - <xsl:when test=". = 'tinyint'">bool<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
| 41 | + <xsl:when test=". = 'tinyint'">sbyte<xsl:if test="@isnull = 1">?</xsl:if></xsl:when> |
41 | 42 | <xsl:when test=". = 'tinytext'">string</xsl:when> |
42 | 43 | <xsl:when test=". = 'varbinary'">byte[]</xsl:when> |
43 | 44 | <xsl:when test=". = 'varchar'">string</xsl:when> |
|
58 | 59 | </xsl:if> |
59 | 60 | </xsl:otherwise> |
60 | 61 | </xsl:choose> |
| 62 | +</xsl:template> |
| 63 | + |
| 64 | +<xsl:template match="type" mode="input"> |
| 65 | + <xsl:if test="@array = 1">IEnumerable<</xsl:if> |
| 66 | + <xsl:apply-templates select="." mode="typeonly"/> |
| 67 | + <xsl:if test="@array = 1">></xsl:if> |
| 68 | + <xsl:if test="@array = 1 and . = 'char'">ERROR_SQL_INJECTION</xsl:if> |
| 69 | +</xsl:template> |
| 70 | + |
| 71 | +<xsl:template match="type"> |
| 72 | + <xsl:apply-templates select="." mode="typeonly"/> |
61 | 73 | <xsl:if test="@array = 1">[]</xsl:if> |
62 | 74 | <xsl:if test="@array = 1 and . = 'char'">ERROR_SQL_INJECTION</xsl:if> |
63 | 75 | </xsl:template> |
|
92 | 104 |
|
93 | 105 | <xsl:template match="output"> public <xsl:apply-templates select="./type"/><xsl:text> </xsl:text><xsl:value-of select="name"/> { get; set; } |
94 | 106 | </xsl:template> |
95 | | -<xsl:template match="input">, <xsl:apply-templates select="./type"/><xsl:text> </xsl:text><xsl:value-of select="name"/></xsl:template> |
| 107 | +<xsl:template match="input">, <xsl:apply-templates select="./type" mode="input"/><xsl:text> </xsl:text><xsl:value-of select="name"/></xsl:template> |
96 | 108 |
|
97 | 109 | <xsl:template match="sql"> |
98 | 110 | <xsl:variable name="nboutput" select="count(query[@ignore = 0]/output)"/> |
|
106 | 118 | { |
107 | 119 | Connection = conn, |
108 | 120 | Transaction = transaction, |
109 | | - CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", string.Join(",", <xsl:value-of select="../name"/>), StringComparison.Ordinal)</xsl:for-each> |
| 121 | + CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", (<xsl:value-of select="../name"/> != null && <xsl:value-of select="../name"/>.Any()) ? string.Join(",", <xsl:value-of select="../name"/>) : "NULL", StringComparison.Ordinal)</xsl:for-each> |
110 | 122 | }; |
111 | 123 | <xsl:for-each select="query/input"><xsl:if test="type[@array != 1]">sqlCmd.Parameters.AddWithValue("@<xsl:value-of select="name"/>", <xsl:value-of select="name"/>); |
112 | 124 | </xsl:if></xsl:for-each> |
|
119 | 131 | <xsl:variable name="returntype"> |
120 | 132 | <xsl:apply-templates select="query[@ignore = 0]/output/type"/> |
121 | 133 | </xsl:variable> |
122 | | - public static async Task<<xsl:value-of select="$returntype"/>> <xsl:value-of select="name"/>(MySqlConnection conn, MySqlTransaction transaction<xsl:apply-templates select="query/input"/>) |
| 134 | + public static async Task<<xsl:value-of select="$returntype"/>> <xsl:value-of select="name"/>(MySqlConnection conn, MySqlTransaction transaction<xsl:apply-templates select="query/input"/>, bool returnDefault = false) |
123 | 135 | { |
124 | 136 | using MySqlCommand sqlCmd = new MySqlCommand |
125 | 137 | { |
126 | 138 | Connection = conn, |
127 | 139 | Transaction = transaction, |
128 | | - CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", string.Join(",", <xsl:value-of select="../name"/>), StringComparison.Ordinal)</xsl:for-each> |
| 140 | + CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", (<xsl:value-of select="../name"/> != null && <xsl:value-of select="../name"/>.Any()) ? string.Join(",", <xsl:value-of select="../name"/>) : "NULL", StringComparison.Ordinal)</xsl:for-each> |
129 | 141 | }; |
130 | 142 | <xsl:for-each select="query/input"><xsl:if test="type[@array != 1]">sqlCmd.Parameters.AddWithValue("@<xsl:value-of select="name"/>", <xsl:value-of select="name"/>); |
131 | 143 | </xsl:if></xsl:for-each> |
132 | 144 | Object result = await sqlCmd.ExecuteScalarAsync(); |
133 | 145 | if (result != null) |
134 | | - return (<xsl:value-of select="$returntype"/>)result; |
| 146 | + <xsl:if test="not(query[@ignore = 0]/output/type[@custom])">return (<xsl:value-of select="$returntype"/>)result;</xsl:if> |
| 147 | + <xsl:if test="query[@ignore = 0]/output/type[@custom]">return (<xsl:value-of select="$returntype"/>)Convert.ChangeType(result, typeof(<xsl:value-of select="$returntype"/>), System.Globalization.CultureInfo.InvariantCulture);</xsl:if> |
| 148 | + if (!returnDefault) |
| 149 | + throw new InvalidOperationException("<xsl:value-of select="name"/> return no row"); |
135 | 150 | return default; |
136 | 151 | } |
137 | 152 | </xsl:if> |
|
148 | 163 | { |
149 | 164 | Connection = conn, |
150 | 165 | Transaction = transaction, |
151 | | - CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", String.Join(",", <xsl:value-of select="../name"/>), StringComparison.Ordinal)</xsl:for-each> |
| 166 | + CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", (<xsl:value-of select="../name"/> != null && <xsl:value-of select="../name"/>.Any()) ? string.Join(",", <xsl:value-of select="../name"/>) : "NULL", StringComparison.Ordinal)</xsl:for-each> |
152 | 167 | }; |
153 | 168 | <xsl:for-each select="query/input"><xsl:if test="type[@array != 1]">sqlCmd.Parameters.AddWithValue("@<xsl:value-of select="name"/>", <xsl:value-of select="name"/>); |
154 | 169 | </xsl:if></xsl:for-each> |
|
175 | 190 | { |
176 | 191 | Connection = conn, |
177 | 192 | Transaction = transaction, |
178 | | - CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", string.Join(",", <xsl:value-of select="../name"/>), StringComparison.Ordinal)</xsl:for-each> |
| 193 | + CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", (<xsl:value-of select="../name"/> != null && <xsl:value-of select="../name"/>.Any()) ? string.Join(",", <xsl:value-of select="../name"/>) : "NULL", StringComparison.Ordinal)</xsl:for-each> |
179 | 194 | }; |
180 | 195 | <xsl:for-each select="query/input"><xsl:if test="type[@array != 1]">sqlCmd.Parameters.AddWithValue("@<xsl:value-of select="name"/>", <xsl:value-of select="name"/>); |
181 | 196 | </xsl:if></xsl:for-each> |
|
201 | 216 | { |
202 | 217 | Connection = conn, |
203 | 218 | Transaction = transaction, |
204 | | - CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", string.Join(",", <xsl:value-of select="../name"/>), StringComparison.Ordinal)</xsl:for-each> |
| 219 | + CommandText = @"<xsl:value-of select="text"/>"<xsl:for-each select="query/input/type[@array = 1]">.Replace("@<xsl:value-of select="../name"/>", (<xsl:value-of select="../name"/> != null && <xsl:value-of select="../name"/>.Any()) ? string.Join(",", <xsl:value-of select="../name"/>) : "NULL", StringComparison.Ordinal)</xsl:for-each> |
205 | 220 | }; |
206 | 221 | <xsl:for-each select="query/input"><xsl:if test="type[@array != 1]">sqlCmd.Parameters.AddWithValue("@<xsl:value-of select="name"/>", <xsl:value-of select="name"/>); |
207 | 222 | </xsl:if></xsl:for-each> |
|
225 | 240 | using System; |
226 | 241 | using System.Collections.Generic; |
227 | 242 | using System.Data.Common; |
| 243 | +using System.Linq; |
228 | 244 | using System.Threading.Tasks; |
229 | 245 |
|
230 | 246 | namespace <xsl:value-of select="$namespace"/> |
|
0 commit comments