RSCG – Gedaq

RSCG – Gedaq
 
 

name Gedaq
nuget https://www.nuget.org/packages/Gedaq/
link https://github.com/SoftStoneDevelop/Gedaq
author Vyacheslav Brevnov

Generating code from attribute query

 

This is how you can use Gedaq .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net7.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
	<ItemGroup>
	  <PackageReference Include="Gedaq" Version="1.4.10" OutputItemType="Analyzer" ReferenceOutputAssembly="True" />
	  <PackageReference Include="Gedaq.DbConnection" Version="1.2.4" />
	  <PackageReference Include="Gedaq.SqlClient" Version="0.2.4" />
	</ItemGroup>
</Project>


The code that you will use is


using GedaqDemoConsole;

var data = new GetData();

var list=data.GetPersons();





namespace GedaqDemoConsole;

public class Person
{
    public int Id { get; set; }

    public string? FirstName { get; set; }

    public string? MiddleName { get; set; }

    public string? LastName { get; set; }

}




using Microsoft.Data.SqlClient;

namespace GedaqDemoConsole;

public partial class GetData
{
    string connectionString = "asdasd";

    [Gedaq.SqlClient.Attributes.Query(
        @"
SELECT 
    p.id,
    p.firstname,
FROM person p
"
    ,
        "GetPersons",
        typeof(Person)
        ),
        ]
    public Person[] GetPersons()
    {
        using (var sql = new SqlConnection(connectionString))
        {
            sql.Open();

            return GetPersons(sql, 48).ToArray();//using generated method
        }
    }
}


 

The code that is generated is


using System;
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;

namespace GedaqDemoConsole
{
    public  partial class GetData
    {
        
        public  IEnumerable<GedaqDemoConsole.Person> GetPersons(

            SqlConnection connection
,
            int? timeout = null
,
            SqlTransaction transaction = null

        )
        {

            bool needClose = connection.State == ConnectionState.Closed;
            if(needClose)
            {
                connection.Open();
            }

            SqlCommand command = null;
            SqlDataReader reader = null;
            try
            {
                command =

                CreateGetPersonsCommand(connection

                , false)

                ;
                SetGetPersonsParametrs(
                    command
,
                    timeout
,
                    transaction

                    );

                reader = command.ExecuteReader();

                while (reader.Read())
                {

                    var item = new GedaqDemoConsole.Person();

                        if(!reader.IsDBNull(0))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.Id = reader.GetFieldValue<System.Int32>(0);

                        }

                        if(!reader.IsDBNull(1))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.FirstName = reader.GetFieldValue<System.String>(1);

                        }
 
                    yield return item;

                }

                while (reader.NextResult())
                {
                }

                reader.Dispose();
                reader = null;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        try 
                        {
                            command.Cancel();
                        }
                        catch { /* ignore */ }
                    }
                
                    reader.Dispose();
                }

                if (needClose)
                {
                    connection.Close();
                }

                if(command != null)
                {
                    command.Parameters.Clear();
                    command.Dispose();
                }
            }

        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public  SqlCommand CreateGetPersonsCommand(
            SqlConnection connection
,
            bool prepare = false

        )
        {
            var command = connection.CreateCommand();

            command.CommandText = @"

SELECT 
    p.id,
    p.firstname,
FROM person p

";

            if(prepare)
            {
                try
                {
                    command.Prepare();
                }
                catch
                {
                    command.Dispose();
                    throw;
                }
            }

            return command;
        }

        public  IEnumerable<GedaqDemoConsole.Person> ExecuteGetPersonsCommand(SqlCommand command)
        {

            SqlDataReader reader = null;
            try
            {

                reader = command.ExecuteReader();
                while (reader.Read())
                {

                    var item = new GedaqDemoConsole.Person();

                        if(!reader.IsDBNull(0))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.Id = reader.GetFieldValue<System.Int32>(0);

                        }

                        if(!reader.IsDBNull(1))
                        {

                            if(item == null)
                            {
                                 item = new GedaqDemoConsole.Person();
                            }

                            item.FirstName = reader.GetFieldValue<System.String>(1);

                        }
 
                    yield return item;

                }

                while (reader.NextResult())
                {
                }
                reader.Dispose();
                reader = null;
            }
            finally
            {
                if (reader != null)
                {
                    if (!reader.IsClosed)
                    {
                        try 
                        {
                            command.Cancel();
                        }
                        catch { /* ignore */ }
                    }
                
                    reader.Dispose();
                }
            }

        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public  void SetGetPersonsParametrs(
            SqlCommand command
,
            int? timeout = null

            ,
            SqlTransaction transaction = null

        )
        {

            if(timeout.HasValue)
            {
                command.CommandTimeout = timeout.Value;
            }

            if(transaction != null)
            {
                command.Transaction = transaction;
            }

        }

    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Gedaq