Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-06

Built in Query language (or abuse)

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

L

A creative idea:

procedure Query(trick: Array of Const);
begin
  for i:=
    CASE
     // interesting code here
end;
const SELECT = 1; // token
      WHERE  = 2; // token
      FROM   = 3; // token
      INSERT = 4; // token
begin
  Query([SELECT, 'foo', FROM, 'bar'])
  Query([INSERT, INTO, 'bar', WHERE, 'name', '=', 'joe'])
end;

A database query language built right into the program.

Tokens like "SELECT" and "INSERT INTO" could be changed to "SEL" and "PUT" to make it more concise. (I've always found SQL too much like COBOL - large silly reserved words that get in the way).

The "array of const" parameters would have to be sorted, tokenized, and possibly some lightly parsed. Handling an "array of const" cleverly means one can still make use of strong typing in pascal. This has some similarities to parameterized queries or TDataset ideas.

I've seen other creative suggestions in the fpc community bulletin boards, such as extending the compiler to embed a database query language into it. The disadvantage of extending the compiler is obvious.. it takes time, work, and it takes someone with a brave soul to do it. It adds more bugs to the compiler.
http://community.freepascal.org:10000/bboards/message?message_id=219521&forum_id=24086#219531

By placing some sort of built in array of const query language into a "module", it means that this special ability of FPC is stored in a library and not in the compiler code base.

--Disadvantages of Array of Const Idea--
1. it is only a theory, I haven't tried it.
2. the abuse factor of it. forgive that for a moment
3. how to handle all the possible errors of bad array of const params
4. reminds me of Lisp, Ruby "domain specific languages" they call them
5. the 'quotes' add some noise here and there

Once someone gets a hold of these bag of tricks, they might obfuscate their programs with all sorts of silly hacks, and we might all end up like Paul Graham.. bragging about how FPC is better than all other languages.

--Advantages of Array Of Const idea--
A built in mini language would be bound to a real language. Kind of like how fpMake is powered by a real language, and not a poor gnu make style limited language. This analogy isn't quite exactly what I'm trying to get across.. but you might understand what I mean. One has the full power of a strongly typed language.. pascal.. and also his built in mini query language too. Not "either or" as in separate languages!

Verbose ugly concatenated SQL strings in our code aren't so type safe..This array of const idea remains type safe and hides details from the programmer.

--Another idea--
Remember the "file of Record" innovation in pascal?

Table of Record...

A "file of record" allows one to remain strongly typed. What about a "DatabaseTable of Record".

var
  // F: File Of DataRec;
  DB: Table of DataRec; // aka a RelVar
begin
  Connect(DB);
  Query(DB, SELECT, 'foo', FROM, 'bar');
  Close(DB);
end;

Crazy? Well even Write and WriteLn may have seemed crazy at one time - the revolutionary idea to introduce the "File" and "Text" data types into a language like pascal, instead of sticking with old Algol ideas.. may have seemed crazy. Give it some slack. Files are a poor storage systems for data. Times have changed - people thought up object relational mappers, TDataset, and languages such as Tutorial Dee. So keep an open mind.. and do tell if you have better ideas.

I'll mention a URL again:
http://community.freepascal.org:10000/bboards/message?message_id=219521&forum_id=24086#219531

Daniel and Michael were worried that those ideas from Atanas Atanassov could bastardize the pascal language or the system.pp unit. I would be worried too. Hence why some creative ideas are offered in this email that would place a query language into a module. Or, a new innovation like a File Of Record/Table Of Record type.

Someone reading this might even have some better ideas in mind. Tell them if you do!

Reply by email

Adrian Veith
to L: A creative idea: procedure Query(trick: Array of Const); begin for i:= CASE //…
Show quoted text 14 lines
A creative idea: --Another idea-- Remember the "file of Record" innovation in pascal? Table of Record... A "file of record" allows one to remain strongly typed. What about a "DatabaseTable of Record". var // F: File Of DataRec; DB: Table of DataRec; // aka a RelVar

We do something similar with our dbGonzales Database engine with standard pascal code. The tables are typed and have coresponding interface classes in oo-pascal. A table can hold any of the subclasses of its baseclass. If you access a "record" you get automatically the corresponding interface class and can use its methods.

A small demo to visualize this is here
http://www.db-gonzales.de/download/GDemo.zip. The demo draws objects (circle, line, rectangle, ellips) stored in database and animates them (or adds new). If you start more instances of the demo, you can see what the other instance is doing with the objects.

If you look at the source of the demo, you can see, that it is also possible to write queries with pascal notation, which are already checked at compile time - but I must admit, that this a little bit clumsy.

        qy:= TGdfFilteredTableOp.Create(tb, [],
            TfComp.New(tb.BaseClass.FieldByName('ParentID'),
TvVariant.Val(Null), [cvEQ]));

could also be written as:

        qy:= TGdfFilteredTableOp.Create(tb, [],
            TfComp.New(TPaintObject(tb.BaseInterfaceClass).ParentID,
TvVariant.Val(Null), [cvEQ]));

and is the same as:

        qy:= TGdfFilteredTableOp.Create(tb, [],
            'ParentID is null' );

which is better readable.

So my opinion is - we don't need to change pascal to achieve things like that, it's already in the language

Cheers, Adrian.

p.s: The information on the webpage about the database is outdated - i haven't found the time to write a documentation.

Reply by email