[fpc-pascal] Built in Query language (or abuse)
L
L at z505.com
Tue Jun 3 09:12:55 CEST 2008
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!
More information about the fpc-pascal
mailing list