Friday, July 21, 2023

CQLBr for Delph/Lazarus

It is so nice when you see how a small idea grows into a nice, rounded project!

Years age I wrote a unit that allowed you to write SQL statements as Pascal code (GpSQLBuilder). This has allowed me to write a code like this:

  query := CreateGpSQLBuilder;
query
.Select.All
.From(DB_TEST)
.OrderBy(
query.&Case
.When([COL_2, '< 0']).&Then(COL_3)
.&Else(COL_4)
.&End
);

It was a small project with minimum support -- as long as it generated SQL code that I've needed, I was fine with it. Much of the SQL language support was missing, there was no support for different SQL dialects and so on ...

Luckilly, Isaque Pinheiro liked the idea and converted it into a full-fledged library with support for multiple SQL dialects, much more complete SQL language support, units tests, installer, a ton of samples and more. 

Switching from GpSQLBuilder to CQLBr Library for Delphi/Lazarus should be quite simple as it looks quite similar to my implementation (and that is by design; Isaque was kind enough to ask whether they could just build upon my work):

  TCQL.New(dbnFirebird)
      .Select
      .Column('ID_CLIENTE')
      .Column('NOME_CLIENTE')
      .Column('TIPO_CLIENTE')
        .&Case
          .When('0').&Then(CQL.Q('FISICA'))
          .When('1').&Then(CQL.Q('JURIDICA'))
                    .&Else('''PRODUTOR''')
        .&End
        .&As('TIPO_PESSOA')
        .From('CLIENTES')
    .AsString);

In case you're interested in such approach, check out the CQLB library on GitHub!

2 comments:

  1. Anonymous20:11

    Paging query sql does not exist

    ReplyDelete
    Replies
    1. Anonymous12:39

      Right, the library looks very nice and complete, thaks for pointing to this.

      Only personally I dislike the &Set, &Case, &Then syntax, mainly because the "&" is hard to reach on the keyboard and also because it makes it harder to read by confusing the eye a lot.

      I would prefer to use _Set, _Case, _Then, ... as an alternative instead,
      maybe both API versions working side-by-side would make sense too.

      Rollo

      Delete