Skip to content

Commit

Permalink
SQLWrapper 2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
DAIKOZ committed Aug 2, 2024
1 parent 9f63431 commit dcb7bc4
Show file tree
Hide file tree
Showing 18 changed files with 1,899 additions and 58 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

## [2.2] (2024-08-02)

### SQLWrapper
- Replace isnull by nullable attribute for database type

### Template
- **Database VB**: add new template database-vb-ado.xslt and sql-vb-ado.xslt to generate Visual Basic .Net wrapper
- Unify isnull and nullable for database type
- **sql-csharp-ado.xslt**: Read data asyn (await reader.ReadAsync())
- **Rename template** to use this rules:
- database or sql: use database for template to apply to all database and use sql for template to apply on SQL queries
- language: charp, vb, ...
- type: ado
- **Template available**:
- **database-csharp-ado.xslt**: generate a database helper from schema xml in C# ADO
- **database-vb-ado.xslt**: generate a database helper from schema xml in Visual Basic ADO
- **sql-cshapr-ado.xslt**: generate a SQL query wrapper from schema xml and SQL query in C# ADO
- **sql-vb-ado.xslt**: generate a SQL query wrapper from schema xml and SQL query in Visual Basic ADO


## [2.1.1] (2024-07-17)

### Daikoz.SQLWrapper NuGet Package
Expand Down
28 changes: 18 additions & 10 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.1</Version>
<AssemblyVersion>2.1.1</AssemblyVersion>
<FileVersion>2.1.1</FileVersion>
<Version>2.2</Version>
<AssemblyVersion>2.2</AssemblyVersion>
<FileVersion>2.2</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,16 +31,24 @@ Overall, DAIKOZ.SQLWrapper is a handy tool for making SQL code easier to work wi
<PackageReleaseNotes>
# Changelog

## [2.1.1] (2024-07-17)
## [2.2] (2024-08-02)

### Daikoz.SQLWrapper NuGet Package
- Fix issue: database name is not provided to XSLT.
### SQLWrapper
- Replace isnull by nullable attribute for database type

### 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
- **SQL C# ADO**: Fix spaces
- **Database VB**: add new template database-vb-ado.xslt and sql-vb-ado.xslt to generate Visual Basic .Net wrapper
- Unify isnull and nullable for database type
- **sql-csharp-ado.xslt**: Read data asyn (await reader.ReadAsync())
- **Rename template** to use this rules:
* database or sql: use database for template to apply to all database and use sql for template to apply on SQL queries
* language: charp, vb, ...
* type: ado
- **Template available**:
* **database-csharp-ado.xslt**: generate a database helper from schema xml in C# ADO
* **database-vb-ado.xslt**: generate a database helper from schema xml in Visual Basic ADO
* **sql-cshapr-ado.xslt**: generate a SQL query wrapper from schema xml and SQL query in C# ADO
* **sql-vb-ado.xslt**: generate a SQL query wrapper from schema xml and SQL query in Visual Basic ADO
</PackageReleaseNotes>
<PackageLicenseFile>license.txt</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
Expand Down
1 change: 0 additions & 1 deletion Daikoz.SQLWrapper/ErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ internal class ErrorMessage
public static readonly (string ErrorCode, string Message) MsgConfigurationWrapperXSLTNotFound = ("SW00514", "XSLT wrapper file not found: {0}");
}


}
47 changes: 40 additions & 7 deletions Daikoz.SQLWrapper/SQLWrapperLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Daikoz.SQLWrapper
{
public class SQLWrapperLauncher
{
public enum LanguageTarget
{
csharp,
visualbasic,
fsharp
}

public List<string> GeneratedSources { get; set; } = [];

private readonly string _ConfigurationFilePath;
Expand All @@ -23,17 +30,19 @@ public class SQLWrapperLauncher
private readonly SQLWrapperConfig _Config;
private readonly TaskLoggingHelper _Log;
private readonly bool _IsCleanning;
private readonly LanguageTarget _LanguageTarget;

private readonly Regex _RegexDatabaseName = new("^[a-zA-Z0-9-_]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private readonly Regex _RegexSQLWrapperErrorLog = new(@"(?<code>SW[0-9]{5}):(?<message>.*)", RegexOptions.Compiled);
private readonly Regex _RegexSQLWrapperErrorSQL = new(@"(?<filepath>.*):\((?<line>.*),(?<position>\d+)\):(?<code>[^:]*):(?<message>.*)", RegexOptions.Compiled);

public SQLWrapperLauncher(string configurationFilePath, string rootNamespace, TaskLoggingHelper log, bool isCleanning)
public SQLWrapperLauncher(string configurationFilePath, string rootNamespace, TaskLoggingHelper log, bool isCleanning, LanguageTarget languageTarget)
{
_ConfigurationFilePath = configurationFilePath;
_RootNamespace = rootNamespace;
_Log = log ?? throw new ArgumentNullException(nameof(log));
_IsCleanning = isCleanning;
_LanguageTarget = languageTarget;

try
{
Expand Down Expand Up @@ -135,8 +144,11 @@ private void StartProcess(string arguments, string logCategory, string logFile)
{
// Find sqlwrapper executable path
string assemblyDirectory = GetAssemblyDirectory();
string sqlWrapperPath = Path.Combine(assemblyDirectory, "tools", "SQLWrapper");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) sqlWrapperPath += ".exe";
string sqlWrapperPath = Path.Combine(assemblyDirectory, "tools", "bin");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
sqlWrapperPath = Path.Combine(sqlWrapperPath, "win-x64", "SQLWrapper.exe");
else
sqlWrapperPath = Path.Combine(sqlWrapperPath, "linux-x64", "SQLWrapper");
if (!File.Exists(sqlWrapperPath))
throw new SQLWrapperException(ErrorMessage.MsgConfigurationSQLWrapperToolNotFound.ErrorCode, logFile, String.Format(ErrorMessage.MsgConfigurationSQLWrapperToolNotFound.Message, sqlWrapperPath));

Expand Down Expand Up @@ -326,7 +338,12 @@ private void GenerateHelper(Database wrapperdb)

// XSLT
string assemblyDirectory = GetAssemblyDirectory();
string xlstFilePath = Path.Combine(assemblyDirectory, "tools", "template", "csharp", "helper.xslt");
string xlstFilePath = _LanguageTarget switch
{
LanguageTarget.visualbasic => Path.Combine(assemblyDirectory, "tools", "template", "database-vb-ado.xslt"),
_ => Path.Combine(assemblyDirectory, "tools", "template", "database-csharp-ado.xslt")
};

if (wrapperdb.XLST != null && !string.IsNullOrWhiteSpace(wrapperdb.XLST))
xlstFilePath = wrapperdb.XLST;
if (!Path.IsPathRooted(xlstFilePath))
Expand Down Expand Up @@ -404,7 +421,13 @@ private void GenerateWrapper(SQL wrappersql)
{
foreach (string sqlFilePath in listSQLFiles)
{
string codeFilePath = sqlFilePath + ".cs";
string codeFilePath = sqlFilePath + (
_LanguageTarget switch
{
LanguageTarget.visualbasic => ".vb",
LanguageTarget.fsharp => ".f",
_ => ".cs"
});
LogMessage("Remove wrapper: " + codeFilePath);
File.Delete(codeFilePath);
}
Expand All @@ -424,7 +447,11 @@ private void GenerateWrapper(SQL wrappersql)

// XSLT
string assemblyDirectory = GetAssemblyDirectory();
string xlstFilePath = Path.Combine(assemblyDirectory, "tools", "template", "csharp", "ADO.xslt");
string xlstFilePath = _LanguageTarget switch
{
LanguageTarget.visualbasic => Path.Combine(assemblyDirectory, "tools", "template", "sql-vb-ado.xslt"),
_ => Path.Combine(assemblyDirectory, "tools", "template", "sql-csharp-ado.xslt")
};
if (wrappersql.XLST != null && !string.IsNullOrWhiteSpace(wrappersql.XLST))
xlstFilePath = wrappersql.XLST;
if (!Path.IsPathRooted(xlstFilePath))
Expand All @@ -443,7 +470,13 @@ private void GenerateWrapper(SQL wrappersql)

foreach (string sqlFilePath in listSQLFiles)
{
string outputFilePath = sqlFilePath + ".cs";
string outputFilePath = sqlFilePath + (
_LanguageTarget switch
{
LanguageTarget.visualbasic => ".vb",
LanguageTarget.fsharp => ".f",
_ => ".cs"
});

// Check uptodate
if (File.Exists(outputFilePath))
Expand Down
11 changes: 10 additions & 1 deletion Daikoz.SQLWrapper/SQLWrapperTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Build.Utilities;
using System;
using System.IO;
using static Daikoz.SQLWrapper.SQLWrapperLauncher;

namespace Daikoz.SQLWrapper
{
Expand All @@ -10,6 +11,7 @@ public class SQLWrapperTask : Task
public string? ConfigurationFilePath { get; set; }
public string RootNamespace { get; set; } = "Daikoz.SQLWrapper";
public bool IsCleanning { get; set; }
public string LanguageTarget { get; set; } = ".csproj";

[Output]
public string[] GeneratedSources { get; set; } = [];
Expand All @@ -32,7 +34,14 @@ public override bool Execute()
return false;
}

Daikoz.SQLWrapper.SQLWrapperLauncher sqlWrapper = new(ConfigurationFilePath, RootNamespace, Log, IsCleanning);
SQLWrapperLauncher.LanguageTarget languageTarget = SQLWrapperLauncher.LanguageTarget.csharp;
languageTarget = LanguageTarget switch
{
".vbproj" => SQLWrapperLauncher.LanguageTarget.visualbasic,
".fsharp" => SQLWrapperLauncher.LanguageTarget.fsharp,
_ => SQLWrapperLauncher.LanguageTarget.csharp,
};
Daikoz.SQLWrapper.SQLWrapperLauncher sqlWrapper = new(ConfigurationFilePath, RootNamespace, Log, IsCleanning, languageTarget);
bool result = sqlWrapper.Execute();
GeneratedSources = [.. sqlWrapper.GeneratedSources];
return result;
Expand Down
12 changes: 9 additions & 3 deletions Daikoz.SQLWrapper/build/Daikoz.SQLWrapper.targets
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<!--
Add cs file under .sql or mysql file
Add cs/vb file under .sql or mysql file
-->
<ItemGroup>
<Compile Update="**\*.mysql.cs">
Expand All @@ -27,6 +27,12 @@
<Compile Update="**\*.sql.cs">
<DependentUpon>$([System.String]::Copy('%(Filename)'))</DependentUpon>
</Compile>
<Compile Update="**\*.mysql.vb">
<DependentUpon>$([System.String]::Copy('%(Filename)'))</DependentUpon>
</Compile>
<Compile Update="**\*.sql.vb">
<DependentUpon>$([System.String]::Copy('%(Filename)'))</DependentUpon>
</Compile>
</ItemGroup>

<!--
Expand All @@ -35,7 +41,7 @@
<UsingTask TaskName="Daikoz.SQLWrapper.SQLWrapperTask" AssemblyFile="..\lib\netstandard2.0\Daikoz.SQLWrapper.dll" />

<Target Name="SQLWrapperBuild" BeforeTargets="CoreCompile">
<Daikoz.SQLWrapper.SQLWrapperTask ConfigurationFilePath="$(MSBuildProjectDirectory)\sqlwrapper.json" IsCleanning="false" RootNamespace="$(RootNamespace)">
<Daikoz.SQLWrapper.SQLWrapperTask ConfigurationFilePath="$(MSBuildProjectDirectory)\sqlwrapper.json" IsCleanning="false" RootNamespace="$(RootNamespace)" LanguageTarget="$(MSBuildProjectExtension)">
<Output TaskParameter="GeneratedSources" ItemName="GeneratedSources" />
</Daikoz.SQLWrapper.SQLWrapperTask>
<ItemGroup>
Expand All @@ -44,7 +50,7 @@
</Target>

<Target Name="SQLWrapperClean" AfterTargets="BeforeClean">
<Daikoz.SQLWrapper.SQLWrapperTask ConfigurationFilePath="$(MSBuildProjectDirectory)\sqlwrapper.json" IsCleanning="true" RootNamespace="$(RootNamespace)" />
<Daikoz.SQLWrapper.SQLWrapperTask ConfigurationFilePath="$(MSBuildProjectDirectory)\sqlwrapper.json" IsCleanning="true" RootNamespace="$(RootNamespace)" LanguageTarget="$(MSBuildProjectExtension)" />
</Target>

</Project>
20 changes: 14 additions & 6 deletions Daikoz.SQLWrapper/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Overall, SQLWrapper is a handy tool for making SQL code easier to work with, sav

| Database | language | function wrapper | stored procedure wrapper | SQL query wrapper | check SQL query syntax |
| --- | :---: | :---: | :---: | :---: | :---: |
| **mysql** | C# .Net |||||
| **mariadb** | C# .Net |||||
| **mysql** | C# .Net<br/>Visual Basic .Net |||||
| **mariadb** | C# .Net<br/>Visual Basic .Net |||||

## Getting started with package NuGet Daikoz.SQLWrapper

Expand Down Expand Up @@ -162,7 +162,7 @@ Copyright (C) DAIKOZ. All rights reserved.
USAGE:
Generate code from sql request:
SQLWrapper wrapper-sql --inputfiles request1.mysql request2.mysql --outputfile mysqlrequest.cs --params
namespace=DAIKOZ classname=SQLWrapper --schema sqlwrapper-cachedb.xml --xslt Template\CSharp\charpADO.xslt
namespace=DAIKOZ classname=SQLWrapper --schema sqlwrapper-cachedb.xml --xslt Template\sql-cshapr-ado.xslt
-s, --schema Required. XML file of cache database schema to load. Generate it before with database command
Expand All @@ -186,7 +186,7 @@ Generate code from sql request:
**Example:**

``` dos
SQLWrapper wrapper-sql --schema sqlwrapper-cachedb.xml --inputfiles request1.mysql request2.mysql --outputfile mysqlrequest.cs --params namespace=DAIKOZ classname=SQLWrapper --xslt template\csharp\ADO.xslt
SQLWrapper wrapper-sql --schema sqlwrapper-cachedb.xml --inputfiles request1.mysql request2.mysql --outputfile mysqlrequest.cs --params namespace=DAIKOZ classname=SQLWrapper --xslt template\sql-cshapr-ado.xslt
```

This command create a **SQL wrapper** from **database** for 2 queries defined in **inputfiles**. It use the **XSLT** file to generate the **outputfile**. **params** give parameters defined in **XLST** file (here the namespace).
Expand All @@ -201,7 +201,7 @@ SQL Wrapper Generator 2.0.1.1+fe5be6893053615a1ac4a351bb4da3c110a6355d
Copyright (C) DAIKOZ. All rights reserved.
USAGE:
Generate code helper to access database:
SQLWrapper wrapper-database --outputfile helper.cs --schema sqlwrapper-cachedb.xml --xslt Template\CSharp\helper.xslt
SQLWrapper wrapper-database --outputfile helper.cs --schema sqlwrapper-cachedb.xml --xslt Template\database-csharp-ado.xslt
-s, --schema Required. XML file of cache database schema to load. Generate it before with database command
Expand All @@ -223,7 +223,7 @@ Generate code helper to access database:
**Example:**

``` dos
SQLWrapper wrapper-database --schema sqlwrapper-cachedb.xml --outputfile MyDatabaseHelper.cs --xslt template\csharp\helper.xslt
SQLWrapper wrapper-database --schema sqlwrapper-cachedb.xml --outputfile MyDatabaseHelper.cs --xslt template\database-csharp-ado.xslt
```

This command create a **helper** for **database**. It use **XLST** file to generate the **outputfile**.
Expand All @@ -235,4 +235,12 @@ In template section, you can found several XLST files to generate SQL and databa
You can create or modify your own and use it with **--xslt** parameter.
DAIKOZ can help you to create your own XLST. Contact us.

### Template available

Generate a **database helper** from XML Schema in:
- C# .Net: **database-csharp-ado.xslt**
- Visual Basic .Net: **database-vb-ado.xslt**

Generate a **SQL query wrapper** from XML schema and SQL query in:
- C# .Net: **sql-cshapr-ado.xslt**
- Visual Basic: **sql-vb-ado.xslt**
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit dcb7bc4

Please sign in to comment.