Skip to content

Instantly share code, notes, and snippets.

@madorin
Created December 8, 2016 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madorin/83300eaf3d9044bc6e56a710b3700368 to your computer and use it in GitHub Desktop.
Save madorin/83300eaf3d9044bc6e56a710b3700368 to your computer and use it in GitHub Desktop.
Firebird script parameters preprocessing
/* in order to support named params, which Firebird itself doesn't,
we need to replace :foo by ?, and store the name we just replaced */
new_sql = c = emalloc(sql_len+1);
for (l = in_quote = in_param = 0; l <= sql_len; ++l) {
if (!(in_quote ^= (sql[l] == '\''))) {
if (!in_param) {
switch (sql[l]) {
case ':':
in_param = 1;
ppname = pname;
*ppname++ = sql[l];
case '?':
*c++ = '?';
++pindex;
continue;
}
} else {
if ((in_param &= ((sql[l] >= 'A' && sql[l] <= 'Z') || (sql[l] >= 'a' && sql[l] <= 'z')
|| (sql[l] >= '0' && sql[l] <= '9') || sql[l] == '_' || sql[l] == '-'))) {
*ppname++ = sql[l];
continue;
} else {
*ppname++ = 0;
if (named_params) {
zval tmp;
ZVAL_LONG(&tmp, pindex);
zend_hash_str_update(named_params, pname, (unsigned int)(ppname - pname - 1), &tmp);
}
}
}
}
*c++ = sql[l];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment