Skip to content

Commit 9f63431

Browse files
author
DAIKOZ
committed
SQLWrapper 2.1.1:
## [2.1.1] (2024-07-17) ### Daikoz.SQLWrapper NuGet Package - Fix issue: database name is not provided to XSLT. ### Template - **Database C#**: Fix return value for function - **Database C#**: Fix column, table and method name to follow microft recommendation to avoid message IDEXXXX - **Database C#**: Move UpdateIfModified method in database class to avoid compilation error with several database helper
1 parent 11afa83 commit 9f63431

File tree

7 files changed

+76
-94
lines changed

7 files changed

+76
-94
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## [2.1.1] (2024-07-17)
4+
5+
### Daikoz.SQLWrapper NuGet Package
6+
- Fix issue: database name is not provided to XSLT.
7+
8+
### Template
9+
- **Database C#**: Fix return value for function
10+
- **Database C#**: Fix column, table and method name to follow microft recommendation to avoid message IDEXXXX
11+
- **Database C#**: Move UpdateIfModified method in database class to avoid compilation error with several database helper
12+
13+
314
## [2.1] (2024-07-08)
415

516
### SQLWrapper

Daikoz.SQLWrapper/Daikoz.SQLWrapper.csproj

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1212
<PackageIcon>sqlwrapper.png</PackageIcon>
1313
<Title>SQL Wrapper Generator</Title>
14-
<Version>2.1</Version>
15-
<AssemblyVersion>2.1</AssemblyVersion>
16-
<FileVersion>2.1</FileVersion>
14+
<Version>2.1.1</Version>
15+
<AssemblyVersion>2.1.1</AssemblyVersion>
16+
<FileVersion>2.1.1</FileVersion>
1717
<Authors>DAIKOZ</Authors>
1818
<Description>SQLWrapper makes it easier to create code wrappers for SQL queries. It's a powerful tool that helps speed up development by reducing the need for manual coding. It works with databases various SQL database (MySQL, MariaDB, ...), checking the syntax and performance of SQL queries before you execute them.
1919

@@ -31,47 +31,16 @@ Overall, DAIKOZ.SQLWrapper is a handy tool for making SQL code easier to work wi
3131
<PackageReleaseNotes>
3232
# Changelog
3333

34-
## [2.1] (2024-07-08)
35-
36-
### SQLWrapper
37-
- **break changes** Modify command line name for better understanding
38-
- Add Linux support (Debian 12)
39-
- Generate stored procedure and function wrapper for mysql/mariadb
40-
- Fix error with mariadb/mysql function: UNIX_TIMESTAMP
41-
- Fix #3 error with mariadb/mysql function: SUBSTR
42-
- Fix error with mariadb/mysql: EXISTS
43-
- Fix line break when generate XML request
34+
## [2.1.1] (2024-07-17)
4435

4536
### Daikoz.SQLWrapper NuGet Package
46-
- **break changes** Modify sqlwrapper.json configuration for better understanding
47-
- Add Linux support (Debian 12)
37+
- Fix issue: database name is not provided to XSLT.
4838

4939
### Template
50-
- **SQL C# ADO**: Replace mysqlconnector by DbConnection to allow use this template with other database.
51-
- **Database C#**: Generate stored procedure and function wrapper for mysql/mariadb
52-
- **Database C#**: Replace mysqlconnector by DbConnection to allow use this template with other database.
53-
- **Database C#**: Fix formating
54-
55-
56-
## [2.0.1] (2024-04-12)
57-
58-
### SQLWrapper
59-
- Order caseinsentive input SQL variables.
60-
- Enhance display warning message
61-
- MariaDB/MySQL: Fix UNION column checking with BOOL, INT, INTEGER type
62-
63-
### Daikoz.SQLWrapper NuGet Package
64-
- Visual Studio can generate wrapper in background
65-
- Fix compilation error after clean the project, the generated source is now added to compile process. Don't need to build again the project.
66-
- Enhance display warning message
67-
- Update readme.md
68-
69-
### Template
70-
- **Database C#**: Use int type for length or long instead of uint to avoid int cast with index of string function.
71-
- **Database C#**: Rename SQLWrapper::UpdateIfModified to SQLWrapperHelper::UpdateIfModified method and move it in same namespace to avoid warning this Daikoz.SQLWrapper NuGet package.
72-
- **SQL C# ADO**: Fix tab/space mix
40+
- **Database C#**: Fix return value for function
41+
- **Database C#**: Fix column, table and method name to follow microft recommendation to avoid message IDEXXXX
42+
- **Database C#**: Move UpdateIfModified method in database class to avoid compilation error with several database helper
7343
- **SQL C# ADO**: Fix spaces
74-
7544
</PackageReleaseNotes>
7645
<PackageLicenseFile>license.txt</PackageLicenseFile>
7746
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>

Daikoz.SQLWrapper/SQLWrapperLauncher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ private void GenerateHelper(Database wrapperdb)
365365
argument.Append("wrapper-database");
366366
argument.Append(" -s " + databaseFilePath);
367367
argument.Append(" -o " + outputFilePath);
368-
argument.Append(" -p namespace=" + rootNamespace);
368+
argument.Append(" -p namespace=" + rootNamespace + " database=" + wrapperdb.Schema);
369369
if (customType.Length > 0)
370370
{
371371
argument.Append(" -t ");

Daikoz.SQLWrapper/tools/SQLWrapper

0 Bytes
Binary file not shown.
3 KB
Binary file not shown.

Daikoz.SQLWrapper/tools/template/csharp/helper.xslt

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,29 @@
3636
<xsl:value-of select="$LB" />
3737
<xsl:value-of select="$ind2" />public static async Task&lt;int&gt; UpdateIfModified(DbConnection conn, DbTransaction transaction, object objToUpdate, object data)<xsl:value-of select="$LB" />
3838
<xsl:value-of select="$ind2" />{<xsl:value-of select="$LB" />
39-
<xsl:value-of select="$ind3" />return await SQLWrapperHelper.UpdateIfModified(conn, transaction, objToUpdate, data, "<xsl:value-of select="@name"/>", new string[] { <xsl:apply-templates select="column[@key != 'primarykey' or not(@key)]" mode="UpdateIfModified_Column" /> }, new string[] { <xsl:apply-templates select="column[@key = 'primarykey']" mode="UpdateIfModified_Column" /> });<xsl:value-of select="$LB" />
39+
<xsl:value-of select="$ind3" />return await <xsl:value-of select="$database" />Helper.UpdateIfModified(conn, transaction, objToUpdate, data, "<xsl:value-of select="@name"/>", [<xsl:apply-templates select="column[@key != 'primarykey' or not(@key)]" mode="UpdateIfModified_Column" />], [<xsl:apply-templates select="column[@key = 'primarykey']" mode="UpdateIfModified_Column" />]);<xsl:value-of select="$LB" />
4040
<xsl:value-of select="$ind2" />}<xsl:value-of select="$LB" />
4141
</xsl:if>
4242
</xsl:template>
4343

44-
<!-- COLUMN NAME-->
45-
<xsl:template match="column" mode="ColumnName">
44+
<!-- ID NAME-->
45+
<xsl:template match="database | table | column | routine" mode="IdName">
4646
<xsl:if test="@name = ../@name">_</xsl:if>
47-
<xsl:value-of select="translate(@name,' ','_')"/>
47+
<xsl:value-of select="translate(substring(@name, 1, 1), 'abcdefghijklmnopqrstuvwxyz ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ_')"/>
48+
<xsl:value-of select="translate(substring(@name, 2),' ','_')"/>
4849
</xsl:template>
4950

5051
<!-- COLUMN -->
5152
<xsl:template match="column" mode="TypeLength">
5253
<xsl:value-of select="$ind2" />public const <xsl:choose>
5354
<xsl:when test="@length &lt;= 2147483647">int </xsl:when>
5455
<xsl:otherwise>long </xsl:otherwise>
55-
</xsl:choose> <xsl:apply-templates select="." mode="ColumnName"/>Length = <xsl:value-of select="@length"/>;<xsl:value-of select="$LB" />
56+
</xsl:choose> <xsl:apply-templates select="." mode="IdName"/>Length = <xsl:value-of select="@length"/>;<xsl:value-of select="$LB" />
5657
</xsl:template>
5758

5859
<!-- PROPERTIES -->
5960
<xsl:template match="column" mode="Properties">
60-
<xsl:value-of select="$ind2" />public <xsl:apply-templates select="." mode="typeonly"/><xsl:text> </xsl:text><xsl:apply-templates select="." mode="ColumnName"/> { get; set; } = default!;<xsl:value-of select="$LB" />
61+
<xsl:value-of select="$ind2" />public <xsl:apply-templates select="." mode="typeonly"/><xsl:text> </xsl:text><xsl:apply-templates select="." mode="IdName"/> { get; set; } = default!;<xsl:value-of select="$LB" />
6162
</xsl:template>
6263

6364
<xsl:template match="column | parameter" mode="typeonly">
@@ -122,7 +123,7 @@
122123
<!-- TABLE -->
123124
<xsl:template match="table">
124125
<xsl:value-of select="$LB" />
125-
<xsl:value-of select="$ind" />public partial class <xsl:value-of select="@name"/><xsl:value-of select="$LB" />
126+
<xsl:value-of select="$ind" />public partial class <xsl:apply-templates select="." mode="IdName"/><xsl:value-of select="$LB" />
126127
<xsl:value-of select="$ind" />{<xsl:value-of select="$LB" />
127128
<xsl:apply-templates select="column[@length]" mode="TypeLength"/>
128129
<xsl:value-of select="$LB" />
@@ -137,7 +138,7 @@
137138
<xsl:value-of select="$LB" />
138139
<xsl:value-of select="$ind3" />DbParameter param<xsl:value-of select="position()" /> = sqlCmd.CreateParameter();<xsl:value-of select="$LB" />
139140
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.ParameterName = "@<xsl:value-of select="@name"/>";<xsl:value-of select="$LB" />
140-
<xsl:if test="@mode != 'out'">
141+
<xsl:if test="@mode != 'out' and @mode != 'return'">
141142
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Value = <xsl:value-of select="@name"/>;<xsl:value-of select="$LB" />
142143
</xsl:if>
143144
<xsl:if test="@mode = 'out'">
@@ -146,6 +147,9 @@
146147
<xsl:if test="@mode = 'inout'">
147148
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Direction = System.Data.ParameterDirection.InputOutput;<xsl:value-of select="$LB" />
148149
</xsl:if>
150+
<xsl:if test="@mode = 'return'">
151+
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Direction = System.Data.ParameterDirection.ReturnValue;<xsl:value-of select="$LB" />
152+
</xsl:if>
149153
<xsl:value-of select="$ind3" />sqlCmd.Parameters.Add(param<xsl:value-of select="position()" />);<xsl:value-of select="$LB" />
150154
</xsl:template>
151155

@@ -180,20 +184,20 @@
180184
<xsl:apply-templates select="parameter[@mode='return']" mode="typeonly"/>
181185
<xsl:text>&gt;</xsl:text>
182186
</xsl:if>
183-
<xsl:text> </xsl:text><xsl:value-of select="@name"/>(DbConnection conn, DbTransaction transaction<xsl:apply-templates select="parameter[@mode!='return']" mode="function"/>)<xsl:value-of select="$LB" />
187+
<xsl:text> </xsl:text><xsl:apply-templates select="." mode="IdName"/>(DbConnection conn, DbTransaction transaction<xsl:apply-templates select="parameter[@mode!='return']" mode="function"/>)<xsl:value-of select="$LB" />
184188
<xsl:value-of select="$ind2" />{<xsl:value-of select="$LB" />
185189
<xsl:value-of select="$ind3" />using DbCommand sqlCmd = conn.CreateCommand();<xsl:value-of select="$LB" />
186190
<xsl:value-of select="$ind3" />sqlCmd.Connection = conn;<xsl:value-of select="$LB" />
187191
<xsl:value-of select="$ind3" />sqlCmd.Transaction = transaction;<xsl:value-of select="$LB" />
188192
<xsl:value-of select="$ind3" />sqlCmd.CommandText = @"<xsl:value-of select="@name"/>";<xsl:value-of select="$LB" />
189193
<xsl:value-of select="$ind3" />sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;<xsl:value-of select="$LB" />
190-
<xsl:apply-templates select="parameter[@mode != 'return']" mode="call"/>
194+
<xsl:apply-templates select="parameter" mode="call"/>
191195
<xsl:value-of select="$LB" />
192196
<xsl:if test="parameter[@mode = 'return']">
193-
<xsl:if test="parameter[@mode='out'] or parameter[@mode='ref']">
197+
<xsl:if test="not(parameter[@mode='out'] or parameter[@mode='ref'])">
194198
<xsl:value-of select="$ind3" />return (<xsl:apply-templates select="parameter[@mode = 'return']" mode="typeonly"/>)await sqlCmd.ExecuteScalarAsync();<xsl:value-of select="$LB" />
195199
</xsl:if>
196-
<xsl:if test="not(parameter[@mode='out'] or parameter[@mode='ref'])">
200+
<xsl:if test="parameter[@mode='out'] or parameter[@mode='ref']">
197201
<xsl:value-of select="$ind3" />return (<xsl:apply-templates select="parameter[@mode = 'return']" mode="typeonly"/>)sqlCmd.ExecuteScalar();<xsl:value-of select="$LB" />
198202
</xsl:if>
199203
</xsl:if>
@@ -211,7 +215,7 @@
211215
<xsl:value-of select="$LB" />
212216
</xsl:template>
213217

214-
<!-- database -->
218+
<!-- DATABASE -->
215219
<xsl:template match="database">
216220
<xsl:text>using System;
217221
using System.Data.Common;
@@ -223,14 +227,14 @@ namespace </xsl:text>
223227
<xsl:value-of select="$namespace"/>
224228
<xsl:text>
225229
{
226-
static internal class SQLWrapperHelper
230+
static partial class </xsl:text>
231+
<xsl:apply-templates select="." mode="IdName"/>
232+
<xsl:text>Helper
227233
{
228234
internal static async Task&lt;int&gt; UpdateIfModified(DbConnection conn, DbTransaction transaction, object objToUpdate, object data, string tableName, string[] listColumnName, string[] listColumnPrimaryName)
229235
{
230-
if (objToUpdate == null)
231-
throw new ArgumentNullException(nameof(objToUpdate));
232-
if (data == null)
233-
throw new ArgumentNullException(nameof(data));
236+
ArgumentNullException.ThrowIfNull(objToUpdate);
237+
ArgumentNullException.ThrowIfNull(data);
234238

235239
using DbCommand sqlCmd = conn.CreateCommand();
236240
sqlCmd.Connection = conn;
@@ -240,7 +244,7 @@ namespace </xsl:text>
240244
Type typeObjToUpdate = objToUpdate.GetType();
241245

242246
bool hasValueModified = false;
243-
StringBuilder strQuery = new StringBuilder("UPDATE " + tableName + " SET");
247+
StringBuilder strQuery = new("UPDATE " + tableName + " SET");
244248
foreach (string colName in listColumnName)
245249
{
246250
PropertyInfo? propertyInfoData = typeObjToUpdate.GetProperty(colName);
@@ -279,9 +283,7 @@ namespace </xsl:text>
279283
strQuery.Append(" WHERE " + colNamePrimary + " = @" + colNamePrimary);
280284
else
281285
strQuery.Append(" AND " + colNamePrimary + " = @" + colNamePrimary);
282-
PropertyInfo? propertyInfo = typeObjToUpdate.GetProperty(colNamePrimary);
283-
if (propertyInfo == null)
284-
throw new ArgumentException("UpdateIfModified: objToUpdate doesn't contain primary key " + colNamePrimary);
286+
PropertyInfo propertyInfo = typeObjToUpdate.GetProperty(colNamePrimary) ?? throw new ArgumentException("UpdateIfModified: objToUpdate doesn't contain primary key " + colNamePrimary);
285287

286288
DbParameter param = sqlCmd.CreateParameter();
287289
param.ParameterName = colNamePrimary;
@@ -296,14 +298,13 @@ namespace </xsl:text>
296298

297299
return await sqlCmd.ExecuteNonQueryAsync();
298300
}
299-
}
300301
</xsl:text>
301-
<xsl:apply-templates select="table"/>
302302
<xsl:value-of select="$LB" />
303-
<xsl:value-of select="$ind" />public partial class <xsl:value-of select="@name"/><xsl:value-of select="$LB" />
304-
<xsl:value-of select="$ind" />{<xsl:value-of select="$LB" />
305303
<xsl:apply-templates select="routine"/>
306-
<xsl:value-of select="$ind" />}<xsl:value-of select="$LB" />
304+
<xsl:value-of select="$ind" />
305+
<xsl:text>}</xsl:text>
306+
<xsl:value-of select="$LB" />
307+
<xsl:apply-templates select="table"/>
307308
<xsl:text>}</xsl:text>
308309
</xsl:template>
309310

0 commit comments

Comments
 (0)