Ejemplo n.º 1
0
def connect(url):
  """
   Initialise a database connection.
  
   Note that mysqli does not support persistent connections.
  """
  # Check if MySQLi support is present in PHP
  url = php.parse_url(url, 3306)
  # Decode url-encoded information in the db connection string
  url['user'] = php.urldecode(url['user'])
  # Test if database url has a password.
  url['pass'] = (php.urldecode(url['pass']) if php.isset(url, 'pass') else '')
  url['host'] = php.urldecode(url['host'])
  url['path'] = php.urldecode(url['path'])
  if (not php.isset(url, 'port')):
    url['port'] = None
  connection  = DrupyMySQL.mysqli_real_connect(\
    url['host'], url['user'], url['pass'], php.substr(url['path'], 1), \
    url['port'], '', DrupyMySQL.MYSQLI_CLIENT_FOUND_ROWS)
  if (DrupyMySQL.mysqli_connect_errno() > 0):
    _db_error_page(DrupyMySQL.mysqli_connect_error())
  # Force UTF-8.
  DrupyMySQL.mysqli_query(connection, 'SET NAMES "utf8"')
  # Require ANSI mode to improve SQL portability.
  DrupyMySQL.mysqli_query(connection, "SET php.SESSION sql_mode='ANSI'")
  return connection
Ejemplo n.º 2
0
def connect(url):
    """
   Initialise a database connection.
  
   Note that mysqli does not support persistent connections.
  """
    # Check if MySQLi support is present in PHP
    url = php.parse_url(url, 3306)
    # Decode url-encoded information in the db connection string
    url['user'] = php.urldecode(url['user'])
    # Test if database url has a password.
    url['pass'] = (php.urldecode(url['pass'])
                   if php.isset(url, 'pass') else '')
    url['host'] = php.urldecode(url['host'])
    url['path'] = php.urldecode(url['path'])
    if (not php.isset(url, 'port')):
        url['port'] = None
    connection  = DrupyMySQL.mysqli_real_connect(\
      url['host'], url['user'], url['pass'], php.substr(url['path'], 1), \
      url['port'], '', DrupyMySQL.MYSQLI_CLIENT_FOUND_ROWS)
    if (DrupyMySQL.mysqli_connect_errno() > 0):
        _db_error_page(DrupyMySQL.mysqli_connect_error())
    # Force UTF-8.
    DrupyMySQL.mysqli_query(connection, 'SET NAMES "utf8"')
    # Require ANSI mode to improve SQL portability.
    DrupyMySQL.mysqli_query(connection, "SET php.SESSION sql_mode='ANSI'")
    return connection
Ejemplo n.º 3
0
def version():
    """
   Returns the version of the database server currently in use.
  
   @return Database server version
  """
    version = php.explode('-', \
      DrupyMySQL.mysqli_get_server_info(lib_appglobals.active_db))
    return version
Ejemplo n.º 4
0
def version():
  """
   Returns the version of the database server currently in use.
  
   @return Database server version
  """
  version = php.explode('-', \
    DrupyMySQL.mysqli_get_server_info(lib_appglobals.active_db))
  return version
Ejemplo n.º 5
0
def result(result):
  """
   Return an individual result field from the previous query.
  
   Only use this function if exactly one field is being selected; otherwise,
   use db_fetch_object() or db_fetch_array().
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     The resulting field or False.
  """
  if (result and DrupyMySQL.mysqli_num_rows(result) > 0):
    # The DrupyMySQL.mysqli_fetch_row function has an optional second
    # parameter row
    # but that can't be used for compatibility with Oracle, DB2, etc.
    array_ = DrupyMySQL.mysqli_fetch_row(result)
    return array_[0]
  return False
Ejemplo n.º 6
0
def result(result):
    """
   Return an individual result field from the previous query.
  
   Only use this function if exactly one field is being selected; otherwise,
   use db_fetch_object() or db_fetch_array().
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     The resulting field or False.
  """
    if (result and DrupyMySQL.mysqli_num_rows(result) > 0):
        # The DrupyMySQL.mysqli_fetch_row function has an optional second
        # parameter row
        # but that can't be used for compatibility with Oracle, DB2, etc.
        array_ = DrupyMySQL.mysqli_fetch_row(result)
        return array_[0]
    return False
Ejemplo n.º 7
0
def encode_blob(data):
    """
   Returns a properly formatted Binary Large Object value.
  
   @param data
     Data to encode.
   @return
    Encoded data.
  """
    return "'" +  DrupyMySQL.mysqli_real_escape_string(\
      lib_appglobals.active_db, data)  + "'"
Ejemplo n.º 8
0
def encode_blob(data):
  """
   Returns a properly formatted Binary Large Object value.
  
   @param data
     Data to encode.
   @return
    Encoded data.
  """
  return "'" +  DrupyMySQL.mysqli_real_escape_string(\
    lib_appglobals.active_db, data)  + "'"
Ejemplo n.º 9
0
def _query(query_, debug = 0):
  """
   Helper function for db_query().
  """
  if (lib_bootstrap.variable_get('dev_query', 0)):
    usec,sec = php.explode(' ', php.microtime())
    timer = float(usec) + float(sec)
    # If devel.plugin query logging is enabled, prepend a comment 
    # with the username and calling function
    # to the SQL string. This is useful when running mysql's 
    # SHOW PROCESSLIST to learn what exact
    # code is issueing the slow query.
    bt = debug_backtrace()
    # t() may not be available yet so we don't wrap 'Anonymous'
    name = (lib_appglobals.user.name if (lib_appglobals.user.uid > 0) else \
      variable_get('anonymous', 'Anonymous'))
    # php.str_replace() to prevent SQL injection via username
    # or anonymous name.
    name = php.str_replace(['*', '/'], '', name)
    query_ = '/* ' +  name  + ' : ' . bt[2]['function'] + ' */ ' + query_
  result = DrupyMySQL.mysqli_query(lib_appglobals.active_db, query_)
  if (lib_bootstrap.variable_get('dev_query', 0)):
    query_ = bt[2]['function'] +  "\n"  + query_
    usec,sec = php.explode(' ', php.microtime())
    stop = float(usec) + float(sec)
    diff = stop - timer
    lib_appglobals.queries.append( [query_, diff] )
  if (debug):
    print '<p>query: ' +  query_  + '<br />error:' + \
      DrupyMySQL.mysqli_error(lib_appglobals.active_db) + '</p>'
  if (not DrupyMySQL.mysqli_errno(lib_appglobals.active_db)):
    return result
  else:
    # Indicate to drupal_error_handler that this is a database error.
    DB_ERROR = True
    php.trigger_error(lib_bootstrap.check_plain(\
      DrupyMySQL.mysqli_error(lib_appglobals.active_db) +  \
      "\nquery: "  + query_), php.E_USER_WARNING)
    return False
Ejemplo n.º 10
0
def _query(query_, debug=0):
    """
   Helper function for db_query().
  """
    if (lib_bootstrap.variable_get('dev_query', 0)):
        usec, sec = php.explode(' ', php.microtime())
        timer = float(usec) + float(sec)
        # If devel.plugin query logging is enabled, prepend a comment
        # with the username and calling function
        # to the SQL string. This is useful when running mysql's
        # SHOW PROCESSLIST to learn what exact
        # code is issueing the slow query.
        bt = debug_backtrace()
        # t() may not be available yet so we don't wrap 'Anonymous'
        name = (lib_appglobals.user.name if (lib_appglobals.user.uid > 0) else \
          variable_get('anonymous', 'Anonymous'))
        # php.str_replace() to prevent SQL injection via username
        # or anonymous name.
        name = php.str_replace(['*', '/'], '', name)
        query_ = '/* ' + name + ' : '.bt[2]['function'] + ' */ ' + query_
    result = DrupyMySQL.mysqli_query(lib_appglobals.active_db, query_)
    if (lib_bootstrap.variable_get('dev_query', 0)):
        query_ = bt[2]['function'] + "\n" + query_
        usec, sec = php.explode(' ', php.microtime())
        stop = float(usec) + float(sec)
        diff = stop - timer
        lib_appglobals.queries.append([query_, diff])
    if (debug):
        print '<p>query: ' +  query_  + '<br />error:' + \
          DrupyMySQL.mysqli_error(lib_appglobals.active_db) + '</p>'
    if (not DrupyMySQL.mysqli_errno(lib_appglobals.active_db)):
        return result
    else:
        # Indicate to drupal_error_handler that this is a database error.
        DB_ERROR = True
        php.trigger_error(lib_bootstrap.check_plain(\
          DrupyMySQL.mysqli_error(lib_appglobals.active_db) +  \
          "\nquery: "  + query_), php.E_USER_WARNING)
        return False
Ejemplo n.º 11
0
def fetch_array(result):
  """
   Fetch one result row from the previous query as an array.
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     An associative array representing the next row of the result, or False.
     The keys of this object are the names of the table fields selected by the
     query, and the values are the field values for this result row.
  """
  if (result):
    array_ = DrupyMySQL.mysqli_fetch_array(result, DrupyMySQL.MYSQLI_ASSOC)
    return (array_ if (array_ != None) else False)
Ejemplo n.º 12
0
def fetch_array(result):
    """
   Fetch one result row from the previous query as an array.
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     An associative array representing the next row of the result, or False.
     The keys of this object are the names of the table fields selected by the
     query, and the values are the field values for this result row.
  """
    if (result):
        array_ = DrupyMySQL.mysqli_fetch_array(result, DrupyMySQL.MYSQLI_ASSOC)
        return (array_ if (array_ != None) else False)
Ejemplo n.º 13
0
def fetch_object(result):
  """
   Fetch one result row from the previous query as an object.
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     An object representing the next row of the result,
     or False. The attributes
     of this object are the table fields selected by the query.
  """
  if (result):
    object_ = DrupyMySQL.mysqli_fetch_object(result)
    return (object_ if (object_ != None) else False)
Ejemplo n.º 14
0
def fetch_object(result):
    """
   Fetch one result row from the previous query as an object.
  
   @param result
     A database query result resource, as returned from db_query().
   @return
     An object representing the next row of the result,
     or False. The attributes
     of this object are the table fields selected by the query.
  """
    if (result):
        object_ = DrupyMySQL.mysqli_fetch_object(result)
        return (object_ if (object_ != None) else False)
Ejemplo n.º 15
0
def escape_string(data):
  """
   Wrapper to escape a string
  """
  return DrupyMySQL.mysqli_real_escape_string(lib_appglobals.active_db, data)
Ejemplo n.º 16
0
def error():
    """
   Determine whether the previous query caused an error.
  """
    return DrupyMySQL.mysqli_errno(lib_appglobals.active_db)
Ejemplo n.º 17
0
def escape_string(text):
  """
   Prepare user input for use in a database query, preventing
   SQL injection attacks.
  """
  return DrupyMySQL.mysqli_real_escape_string(lib_appglobals.active_db, text)
Ejemplo n.º 18
0
def affected_rows():
    """
   Determine the number of rows changed by the preceding query.
  """
    return DrupyMySQL.mysqli_affected_rows(lib_appglobals.active_db)
Ejemplo n.º 19
0
def affected_rows():
  """
   Determine the number of rows changed by the preceding query.
  """
  return DrupyMySQL.mysqli_affected_rows(lib_appglobals.active_db)
Ejemplo n.º 20
0
def error():
  """
   Determine whether the previous query caused an error.
  """
  return DrupyMySQL.mysqli_errno(lib_appglobals.active_db)
Ejemplo n.º 21
0
def escape_string(text):
    """
   Prepare user input for use in a database query, preventing
   SQL injection attacks.
  """
    return DrupyMySQL.mysqli_real_escape_string(lib_appglobals.active_db, text)
Ejemplo n.º 22
0
def escape_string(data):
    """
   Wrapper to escape a string
  """
    return DrupyMySQL.mysqli_real_escape_string(lib_appglobals.active_db, data)