FireDAC – Passing Parameters To Firebird

The FireDAC components fix one of my longest standing frustrations with using Delphi with Firebird and Interbase databases.

As a security aware developer, I have always known the advantages of using parameters in my queries, not only do these make queries a little faster to run (as they can be prepared and reused multiple times), but they can assist with defending against SQL injection caused by incorrect formatting of values passed to the database.

Sometimes you may wish to use a SQL script (using execute block in Firebird), the problem here is that Firebird prefixes variables with the : character – the same character you would use to define a parameter in Delphi.

FireDAC has a little known feature however.  If you declare an execute block statement, all of the : identifiers before the first BEGIN statement are treated as parameters, all other : identifiers are ignored, and left as variables.

This means, for the first time, you can use one of the ‘out of the box’ data access controls to mix parameters and variables in Firebird queries.

In the code above :PARAM identifies a parameter we can pass from Delphi using ParamByName, :MY_VARIABLE is left untouched, and is used by Firebird as a variable.

Remove Quotes From String

A Delphi function that will remove enclosing and double quotes from quoted text, useful for reading CSV data.

Using RemoveQuotes:

Parameters:

S: A string value containing the quoted text.

QuoteChar: The character that is to be treated as the quoted text.

Return Value:

This function will return a string value containing the input text with double and enclosing quote characters removed.

Example:

 

Move A Directory To The Recycle Bin

A Delphi function that will move a directory to the recycle bin.

Using RecycleDir:

Parameters:

Dir: A string value specifying the name of the directory to be moved to the recycle bin.

Return Value:

The function will return the Boolean value True if successful.

If the function fails, it will return the Boolean value False.

Example:

 

Move A File To The Recycle Bin

A Delphi function that will move the specified file to the recycle bin.

Using RecycleFile

Parameters:

FileName: A String value specifying the name of the file to be moved to the recycle bin.

Return Value:

The function will return the Boolean value True if successful.

If the function fails, it will return the Boolean value False.

Example:

Free All Objects In A TList

When programming in Delphi, I often find myself creating objects and adding them to a TList.  To safely deallocate these objects I would need to loop throgh the list, destroying each object each time I cleared that list.  I found myself doing this so often I wrote the following function to take care of this for me:

Using FreeList

The FreeList function does not destroy the list itself, but will loop through the list destroying each object held within the list.  It will automatically skip any nil pointers held in the list.

Parameters:

List: A TList object containing references to the objects to be destroyed.

Return Value:

This procedure has no return value.

Example:

 

Is Key Pressed

A Delphi function to determine if a key on the keyboard is being held down by the user.

Using IsKeyDown:

Parameters:

VK: The virtual key code of the key you want to check.

Return Value:

Returns True if the key specified is pressed, False if the key in not pressed.

Example: