Skip to content

SQL-to-Code Macro

OK, I’m not trying to steal Joe White’s thunder here on Delphi macros, but when I plugged Joe’s series, I mentioned that I use Delphi macros for converting SQL created in a query analysis tool into Delphi constants, and commenter Jack asked for more details on this. I thought it would make a better post than comment.

The general idea is that we want to start with an SQL statement, like this:

SELECT
  *
FROM
  MY_TABLE;

…and turn it into a Delphi constant, like this:

const
  MySQL = ‘SELECT ‘
        + ‘  * ‘
        + ‘FROM ‘
        + ‘  MY_TABLE;’;

The first step is to paste the SQL into the code at the appropriate place. We need to type the non-repeating portion of the constant, and then paste in the SQL:

const
  MySQL = ‘SELECT
  *
FROM
  MY_TABLE;

Now, record the macro:

  1. Press the End key to go to the end of the line
  2. Press Ctrl+Shift+R to start recording
  3. Type a space, then the closing single quote
  4. Press the down arrow, then the Home key to go to the beginning of the next line
  5. Type the appropriate whitespace, then a ‘+’ sign (for string concatenation), then the open single quote character
  6. Press the End key to go to the end of line, to be ready for the next time the macro is played back
  7. Press Ctrl+Shift+R to stop recording

To use the macro, press and hold Ctrl+Shift+P. The keyboard auto-repeat who repeatedly play back the macro, formatting the rest of the query. Don’t worry about going too far, since you can always press Ctrl+Z to undo if you do. Since the final closing single quote and semicolon are different from how the rest of the lines are formatted, just type them.

By the way, the Delphi editor works really well for editing plain SQL. If you are editing a file with the ‘. SQL’ extension, it does syntax highlighting (and printing) for SQL. You can also use macros like the one described above when creating your SQL or DDL. For example, I often use macros to turn an INSERT statement into a UPDATE statement with parameters or trigger variable references, and similarly to turn a SELECT into a CREATE VIEW statement.

{ 3 } Comments

  1. Ken White | April 4, 2008 at 12:28 pm | Permalink

    Nice post.

    I have a better way, though. Peter Below posted code for a console app a while back called "cliptoconst", which takes text off the clipboard and automatically formats it for the IDE (including + and ; in the proper places). I added it to the RAD Studio 2007 Tools menu, assigning a mnemonic shortcut key (C, for "ClipToConst"). So going from SQL Server Management Studio or Visual Query Builder to the code editor is simple:

    1. Select the text in the query tool and then Ctrl+C.
    2. Switch to the IDE, type "const" and Enter.
    3. Alt+T+C (for Tools->ClipToConst)
    4. Ctrl+V to paste.

    Voila! Now I have a properly formatted (and indented) constant declaration.

  2. Jan Derk | April 6, 2008 at 3:11 pm | Permalink

    Hopefully, CodeGear will add support for multi-line strings to Delphi, like all other modern languages already have.

    That way we the developers would not have to resort to hacks like this. Plus it will make the source code more readable.

    Andreas Hausladen already created an extension which does this, but I like to see it integrated:
    http://andy.jgknet.de/blog/archives/50-Multiline-string-constants.html

  3. James | April 24, 2008 at 1:20 am | Permalink

    I think the free online sql formatter can help you do is instantly.

    http://www.wangz.net/gsqlparser/sqlpp/sqlformat.htm

Post a Comment

Your email is never published nor shared. Required fields are marked *
Close