Skip to content

Commit

Permalink
SQLWrapper 2.1.1:
Browse files Browse the repository at this point in the history
## [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
  • Loading branch information
DAIKOZ committed Jul 16, 2024
1 parent 11afa83 commit 9f63431
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 94 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [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


## [2.1] (2024-07-08)

### SQLWrapper
Expand Down
47 changes: 8 additions & 39 deletions Daikoz.SQLWrapper/Daikoz.SQLWrapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>sqlwrapper.png</PackageIcon>
<Title>SQL Wrapper Generator</Title>
<Version>2.1</Version>
<AssemblyVersion>2.1</AssemblyVersion>
<FileVersion>2.1</FileVersion>
<Version>2.1.1</Version>
<AssemblyVersion>2.1.1</AssemblyVersion>
<FileVersion>2.1.1</FileVersion>
<Authors>DAIKOZ</Authors>
<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.

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

## [2.1] (2024-07-08)

### SQLWrapper
- **break changes** Modify command line name for better understanding
- Add Linux support (Debian 12)
- Generate stored procedure and function wrapper for mysql/mariadb
- Fix error with mariadb/mysql function: UNIX_TIMESTAMP
- Fix #3 error with mariadb/mysql function: SUBSTR
- Fix error with mariadb/mysql: EXISTS
- Fix line break when generate XML request
## [2.1.1] (2024-07-17)

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

### Template
- **SQL C# ADO**: Replace mysqlconnector by DbConnection to allow use this template with other database.
- **Database C#**: Generate stored procedure and function wrapper for mysql/mariadb
- **Database C#**: Replace mysqlconnector by DbConnection to allow use this template with other database.
- **Database C#**: Fix formating


## [2.0.1] (2024-04-12)

### SQLWrapper
- Order caseinsentive input SQL variables.
- Enhance display warning message
- MariaDB/MySQL: Fix UNION column checking with BOOL, INT, INTEGER type

### Daikoz.SQLWrapper NuGet Package
- Visual Studio can generate wrapper in background
- Fix compilation error after clean the project, the generated source is now added to compile process. Don't need to build again the project.
- Enhance display warning message
- Update readme.md

### Template
- **Database C#**: Use int type for length or long instead of uint to avoid int cast with index of string function.
- **Database C#**: Rename SQLWrapper::UpdateIfModified to SQLWrapperHelper::UpdateIfModified method and move it in same namespace to avoid warning this Daikoz.SQLWrapper NuGet package.
- **SQL C# ADO**: Fix tab/space mix
- **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
- **SQL C# ADO**: Fix spaces

</PackageReleaseNotes>
<PackageLicenseFile>license.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
2 changes: 1 addition & 1 deletion Daikoz.SQLWrapper/SQLWrapperLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private void GenerateHelper(Database wrapperdb)
argument.Append("wrapper-database");
argument.Append(" -s " + databaseFilePath);
argument.Append(" -o " + outputFilePath);
argument.Append(" -p namespace=" + rootNamespace);
argument.Append(" -p namespace=" + rootNamespace + " database=" + wrapperdb.Schema);
if (customType.Length > 0)
{
argument.Append(" -t ");
Expand Down
Binary file modified Daikoz.SQLWrapper/tools/SQLWrapper
Binary file not shown.
Binary file modified Daikoz.SQLWrapper/tools/SQLWrapper.exe
Binary file not shown.
55 changes: 28 additions & 27 deletions Daikoz.SQLWrapper/tools/template/csharp/helper.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,29 @@
<xsl:value-of select="$LB" />
<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" />
<xsl:value-of select="$ind2" />{<xsl:value-of select="$LB" />
<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" />
<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" />
<xsl:value-of select="$ind2" />}<xsl:value-of select="$LB" />
</xsl:if>
</xsl:template>

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

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

<!-- PROPERTIES -->
<xsl:template match="column" mode="Properties">
<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" />
<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" />
</xsl:template>

<xsl:template match="column | parameter" mode="typeonly">
Expand Down Expand Up @@ -122,7 +123,7 @@
<!-- TABLE -->
<xsl:template match="table">
<xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />public partial class <xsl:value-of select="@name"/><xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />public partial class <xsl:apply-templates select="." mode="IdName"/><xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />{<xsl:value-of select="$LB" />
<xsl:apply-templates select="column[@length]" mode="TypeLength"/>
<xsl:value-of select="$LB" />
Expand All @@ -137,7 +138,7 @@
<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />DbParameter param<xsl:value-of select="position()" /> = sqlCmd.CreateParameter();<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.ParameterName = "@<xsl:value-of select="@name"/>";<xsl:value-of select="$LB" />
<xsl:if test="@mode != 'out'">
<xsl:if test="@mode != 'out' and @mode != 'return'">
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Value = <xsl:value-of select="@name"/>;<xsl:value-of select="$LB" />
</xsl:if>
<xsl:if test="@mode = 'out'">
Expand All @@ -146,6 +147,9 @@
<xsl:if test="@mode = 'inout'">
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Direction = System.Data.ParameterDirection.InputOutput;<xsl:value-of select="$LB" />
</xsl:if>
<xsl:if test="@mode = 'return'">
<xsl:value-of select="$ind3" />param<xsl:value-of select="position()" />.Direction = System.Data.ParameterDirection.ReturnValue;<xsl:value-of select="$LB" />
</xsl:if>
<xsl:value-of select="$ind3" />sqlCmd.Parameters.Add(param<xsl:value-of select="position()" />);<xsl:value-of select="$LB" />
</xsl:template>

Expand Down Expand Up @@ -180,20 +184,20 @@
<xsl:apply-templates select="parameter[@mode='return']" mode="typeonly"/>
<xsl:text>&gt;</xsl:text>
</xsl:if>
<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" />
<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" />
<xsl:value-of select="$ind2" />{<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />using DbCommand sqlCmd = conn.CreateCommand();<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />sqlCmd.Connection = conn;<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />sqlCmd.Transaction = transaction;<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />sqlCmd.CommandText = @"<xsl:value-of select="@name"/>";<xsl:value-of select="$LB" />
<xsl:value-of select="$ind3" />sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;<xsl:value-of select="$LB" />
<xsl:apply-templates select="parameter[@mode != 'return']" mode="call"/>
<xsl:apply-templates select="parameter" mode="call"/>
<xsl:value-of select="$LB" />
<xsl:if test="parameter[@mode = 'return']">
<xsl:if test="parameter[@mode='out'] or parameter[@mode='ref']">
<xsl:if test="not(parameter[@mode='out'] or parameter[@mode='ref'])">
<xsl:value-of select="$ind3" />return (<xsl:apply-templates select="parameter[@mode = 'return']" mode="typeonly"/>)await sqlCmd.ExecuteScalarAsync();<xsl:value-of select="$LB" />
</xsl:if>
<xsl:if test="not(parameter[@mode='out'] or parameter[@mode='ref'])">
<xsl:if test="parameter[@mode='out'] or parameter[@mode='ref']">
<xsl:value-of select="$ind3" />return (<xsl:apply-templates select="parameter[@mode = 'return']" mode="typeonly"/>)sqlCmd.ExecuteScalar();<xsl:value-of select="$LB" />
</xsl:if>
</xsl:if>
Expand All @@ -211,7 +215,7 @@
<xsl:value-of select="$LB" />
</xsl:template>

<!-- database -->
<!-- DATABASE -->
<xsl:template match="database">
<xsl:text>using System;
using System.Data.Common;
Expand All @@ -223,14 +227,14 @@ namespace </xsl:text>
<xsl:value-of select="$namespace"/>
<xsl:text>
{
static internal class SQLWrapperHelper
static partial class </xsl:text>
<xsl:apply-templates select="." mode="IdName"/>
<xsl:text>Helper
{
internal static async Task&lt;int&gt; UpdateIfModified(DbConnection conn, DbTransaction transaction, object objToUpdate, object data, string tableName, string[] listColumnName, string[] listColumnPrimaryName)
{
if (objToUpdate == null)
throw new ArgumentNullException(nameof(objToUpdate));
if (data == null)
throw new ArgumentNullException(nameof(data));
ArgumentNullException.ThrowIfNull(objToUpdate);
ArgumentNullException.ThrowIfNull(data);

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

bool hasValueModified = false;
StringBuilder strQuery = new StringBuilder("UPDATE " + tableName + " SET");
StringBuilder strQuery = new("UPDATE " + tableName + " SET");
foreach (string colName in listColumnName)
{
PropertyInfo? propertyInfoData = typeObjToUpdate.GetProperty(colName);
Expand Down Expand Up @@ -279,9 +283,7 @@ namespace </xsl:text>
strQuery.Append(" WHERE " + colNamePrimary + " = @" + colNamePrimary);
else
strQuery.Append(" AND " + colNamePrimary + " = @" + colNamePrimary);
PropertyInfo? propertyInfo = typeObjToUpdate.GetProperty(colNamePrimary);
if (propertyInfo == null)
throw new ArgumentException("UpdateIfModified: objToUpdate doesn't contain primary key " + colNamePrimary);
PropertyInfo propertyInfo = typeObjToUpdate.GetProperty(colNamePrimary) ?? throw new ArgumentException("UpdateIfModified: objToUpdate doesn't contain primary key " + colNamePrimary);

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

return await sqlCmd.ExecuteNonQueryAsync();
}
}
</xsl:text>
<xsl:apply-templates select="table"/>
<xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />public partial class <xsl:value-of select="@name"/><xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />{<xsl:value-of select="$LB" />
<xsl:apply-templates select="routine"/>
<xsl:value-of select="$ind" />}<xsl:value-of select="$LB" />
<xsl:value-of select="$ind" />
<xsl:text>}</xsl:text>
<xsl:value-of select="$LB" />
<xsl:apply-templates select="table"/>
<xsl:text>}</xsl:text>
</xsl:template>

Expand Down
Loading

0 comments on commit 9f63431

Please sign in to comment.