Exemplo 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
Exemplo 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
Exemplo n.º 3
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
Exemplo n.º 4
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