pike.git / lib / modules / Sql.pmod / sql_util.pmod

version» Context lines:

pike.git/lib/modules/Sql.pmod/sql_util.pmod:17:   {    return replace(s, "\'", "\'\'");   }      //! Throw an error in case an unimplemented function is called.   void fallback()   {    error( "Function not supported in this database." );   }    + //! Wrapper to handle zero. + //! + //! @seealso + //! @[zero] + protected class ZeroWrapper + { +  //! @returns +  //! Returns the following: +  //! @string +  //! @value "NULL" +  //! If @[fmt] is @expr{'s'@}. +  //! @value "ZeroWrapper()" +  //! If @[fmt] is @expr{'O'@}. +  //! @endstring +  //! Otherwise it formats a @expr{0@} (zero). +  protected string _sprintf(int fmt, mapping(string:mixed) params) +  { +  if (fmt == 's') return "NULL"; +  if (fmt == 'O') return "ZeroWrapper()"; +  return sprintf(sprintf("%%*%c", fmt), params, 0); +  } + } +  + //! Instance of @[ZeroWrapper] used by @[handle_extraargs()]. + protected ZeroWrapper zero = ZeroWrapper(); +  + protected class NullArg + { +  protected string _sprintf (int fmt) +  {return fmt == 'O' ? "NullArg()" : "NULL";} + } + protected NullArg null_arg = NullArg(); +  + //! Handle @[sprintf]-based quoted arguments + //! + //! @param query + //! The query as sent to one of the query functions. + //! + //! @param extraargs + //! The arguments following the query. + //! + //! @param bindings + //! Optional bindings mapping to which additional bindings will be + //! added. It's returned as the second element in the return value. + //! A new mapping is used if this isn't specified. + //! + //! @returns + //! Returns an array with two elements: + //! @array + //! @elem string 0 + //! The query altered to use bindings-syntax. + //! @elem mapping(string|int:mixed) 1 + //! A bindings mapping. Zero if no bindings were added. + //! @endarray + array(string|mapping(string|int:mixed)) +  handle_extraargs(string query, array(mixed) extraargs, +  void|mapping(string|int:mixed) bindings) { +  +  array(mixed) args=allocate(sizeof(extraargs)); +  if (!bindings) bindings = ([]); +  +  int a, new_bindings; +  foreach(extraargs; int j; mixed s) { +  if (stringp(s) || multisetp(s)) { +  string bind_name; +  do { +  bind_name = ":arg"+(a++); +  } while (!zero_type (bindings[bind_name])); +  args[j]=bind_name; +  bindings[bind_name] = s; +  new_bindings = 1; +  continue; +  } +  if (intp(s) || floatp(s)) { +  args[j] = s || zero; +  continue; +  } +  if (objectp (s) && s->is_val_null) { +  args[j] = null_arg; +  continue; +  } +  error("Wrong type to query argument %d: %O\n", j + 1, s); +  } +  +  return ({sprintf(query,@args), new_bindings && bindings}); + } +    //! Build a raw SQL query, given the cooked query and the variable bindings   //! It's meant to be used as an emulation engine for those drivers not   //! providing such a behaviour directly (i.e. Oracle).   //! The raw query can contain some variables (identified by prefixing   //! a colon to a name or a number (i.e. ":var" or ":2"). They will be   //! replaced by the corresponding value in the mapping.   //!   //! @param query   //! The query.   //!
pike.git/lib/modules/Sql.pmod/sql_util.pmod:42:    void|object driver)   {    array(string)k, v;    if (!bindings)    return query;    function my_quote=(driver&&driver->quote?driver->quote:quote);    v=map(values(bindings),    lambda(mixed m) {    if(zero_type(m))    return "NULL"; +  if (objectp (m) && m->is_val_null) +  // Note: Could need bug compatibility here - in some cases +  // we might be passed a null object that can be cast to +  // "", and before this it would be. This is an observed +  // compat issue in comment #7 in [bug 5900]. +  return "NULL";    if(multisetp(m))    return sizeof(m) ? indices(m)[0] : "";    return "'"+(intp(m)?(string)m:my_quote((string)m))+"'";    });    // Throws if mapping key is empty string.    k=map(indices(bindings),lambda(string s){    return ( (stringp(s)&&s[0]==':') ?    s : ":"+s);    });    return replace(query,k,v);
pike.git/lib/modules/Sql.pmod/sql_util.pmod:140:    return row;    }       //! JSON is always utf8 default, do nothing.    int|string fetch_json_result()    {    return master_result->fetch_json_result();    }   }    - #if constant (Mysql.mysql.HAVE_MYSQL_FIELD_CHARSETNR) + #if constant (___Mysql.mysql.HAVE_MYSQL_FIELD_CHARSETNR)      class MySQLUnicodeWrapper   //! Result wrapper for MySQL that performs UTF-8 decoding of all   //! nonbinary fields. Useful if the result charset of the connection   //! has been set to UTF-8.   //!   //! @note   //! There's normally no need to use this class directly. It's used   //! automatically when @[mysql.set_unicode_decode_mode] is activated.   {