Skip to content
Snippets Groups Projects
Select Git revision
  • c5d9a9a6fff39e8384148921a38384f771d4aff4
  • master default protected
  • 9.0
  • 8.0
  • 7.8
  • 7.6
  • 7.4
  • 7.2
  • 7.0
  • 0.6
  • rosuav/latex-markdown-renderer
  • rxnpatch/rxnpatch
  • marcus/gobject-introspection
  • rxnpatch/8.0
  • rosuav/pre-listening-ports
  • nt-tools
  • rosuav/async-annotations
  • rosuav/pgsql-ssl
  • rxnpatch/rxnpatch-broken/2023-10-06T094250
  • grubba/fdlib
  • grubba/wip/sakura/8.0
  • v8.0.2000
  • v8.0.1998
  • v8.0.1996
  • v8.0.1994
  • v8.0.1992
  • v8.0.1990
  • v8.0.1988
  • v8.0.1986
  • rxnpatch/clusters/8.0/2025-04-29T124414
  • rxnpatch/2025-04-29T124414
  • v8.0.1984
  • v8.0.1982
  • v8.0.1980
  • v8.0.1978
  • v8.0.1976
  • v8.0.1974
  • v8.0.1972
  • v8.0.1970
  • v8.0.1968
  • v8.0.1966
41 results

Object.pmod

Blame
  • sql_util.pmod 6.22 KiB
    /*
     * Some SQL utility functions.
     * They are kept here to avoid circular references.
     *
     * Henrik Grubbström 1999-07-01
     */
    
    #pike __REAL_VERSION__
    
    //! Some SQL utility functions
    
    //! Quote a string so that it can safely be put in a query.
    //!
    //! @param s
    //!   String to quote.
    string quote(string s)
    {
      return replace(s, "\'", "\'\'");
    }
    
    //! Throw an error in case an unimplemented function is called.
    void fallback()
    {
      error( "Function not supported in this database." );
    }
    
    //! 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.
    //!
    //! @param bindings
    //!   Optional mapping containing the variable bindings. Make sure that
    //!   no confusion is possible in the query. If necessary, change the
    //!   variables' names.
    string emulate_bindings(string query, mapping(string|int:mixed)|void bindings,
                            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);
    }
    
    //! Result object wrapper performing utf8 decoding of all fields.