// Syntax: xquery(query_template_text, argument's_1_value, argument's_2_value, ...) // OR: xquery(ADOConnection, query_template_text, argument's_1_value, argument's_2_value, ...) // (procedure checks the type of the 1st argument and if it's string parses the 1st syntax otherwise the 2nd) // Special characters for a query template: // ^@TableName - indicates that the combination ^@ is to be replaced with table prefix // ^N - numeric parameter (is not to be quoted) // ^S - string parameter (is to be quoted) // ^0 - "NULL" or "NOT NULL" // (c) Kamnev Artjom (Kamnium), Mesilov Maxim (Severus) // http://life.screenshots.ru // PRIVATE! this replaces a special chars combination with a specified string function repl(qtext, pos, withwhat){ return qtext.substr(0, pos) + withwhat + qtext.substr(pos+2); }// End of repl function xquery(){ // opt_table_prefix - prefix for tables // opt_conn - connection object var Conn; // there'll be connection object reference // getting the list of function's arguments var args=new Array(); args=xquery.arguments; // Check what type of syntax to use if (typeof(args[0])=='string'){ qtext=args[0]; // the first argument is always query template text curArg=1; // bypassing query text only Conn=opt_conn; }else{ qtext=args[1]; // the first argument is always query template text curArg=2; // bypassing connection object reference and query text Conn=args[0]; } qtext=qtext.replace('^@', opt_table_prefix+'_'); // replacing with table prefixes i=0; while (i=args.length){ return false; // too many parameters in the query template! } switch (qtext.charAt(i+1)){ case 'N': { if (args[curArg]==null){ return false; // no nulls in the middle of JS arglist! } if (isNaN(args[curArg])){ return false; // incorrect parameter, numbers only! } qtext=repl(qtext, i, args[curArg]); break; } case 'S': { if (args[curArg]==null){ return false; // no nulls in the middle of JS arglist! } args[curArg]=args[curArg].toString(); args[curArg]=args[curArg].replace("'", "`"); args[curArg]=args[curArg].replace('^', '!'); qtext=repl(qtext, i, "'" + args[curArg] + "'"); break; } case '0': { if (args[curArg]==null){ return false; // no nulls in the middle of JS arglist! } args[curArg]=args[curArg].toString(); args[curArg]=args[curArg].toUpperCase(); if ( (args[curArg]!='NULL') && (args[curArg]!='NOT NULL') ){ return false; // incorrect parameter, "NULL" or "NOT NULL" only! } qtext=repl(qtext, i, args[curArg]); break; } default: { qtext=repl(qtext, i, ' '); // hmm, don't worry! } } curArg++; }else{ i++; } } try{ ResultData=Conn.Execute(qtext); return ResultData; }catch(foo){ if (opt_debug_mode){ print('

SQL error:' + "
\r\n"); var dbg_err=new Enumerator(Conn.Errors); for (; !dbg_err.atEnd(); dbg_err.moveNext()){ print('Error #' + dbg_err.item().Number + "
\r\n"); print(' ' + dbg_err.item().Description + "
\r\n"); print(' Source: ' + dbg_err.item().Source + "
\r\n"); print(' SQL State: ' + dbg_err.item().SQLState + "
\r\n"); print(' NativeError: ' + dbg_err.item().NativeError + "
\r\n"); } print("
Query string:
\r\n" + qtext + '

'); } return false; } }// End of xquery