Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
RCold committed Oct 16, 2016
1 parent b83aece commit 653a814
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 90 deletions.
18 changes: 14 additions & 4 deletions includes/database.mysql.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function db_version() {
*
* @param string $url
* The database connection URL.
*
* @return resource
* The database connection.
*/
Expand All @@ -49,7 +50,9 @@ function db_connect($url) {
$db_name = substr(urldecode($url_parts['path']), 1);

// Allow for non-standard MySQL port.
$db_host .= ':'. isset($url_parts['port']) ? $url_parts['port'] : 3306;
if (isset($url_parts['port'])) {
$db_host .= $url_parts['port'];
}

// - TRUE makes mysql_connect() always open a new link, even if
// mysql_connect() was called before with the same parameters.
Expand Down Expand Up @@ -84,7 +87,7 @@ function _db_query($query, $debug = 0) {

if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$timer = (float)$usec + (float)$sec;
$timer = (float) $usec + (float) $sec;
// If devel.module 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.
Expand All @@ -101,7 +104,7 @@ function _db_query($query, $debug = 0) {
if (variable_get('dev_query', 0)) {
$query = $bt[2]['function'] ."\n". $query;
list($usec, $sec) = explode(' ', microtime());
$stop = (float)$usec + (float)$sec;
$stop = (float) $usec + (float) $sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
}
Expand All @@ -126,6 +129,7 @@ function _db_query($query, $debug = 0) {
*
* @param resource $result
* A database query result resource, as returned from db_query().
*
* @return object|bool
* An object representing the next row of the result, or FALSE. The attributes
* of this object are the table fields selected by the query.
Expand All @@ -139,6 +143,7 @@ function db_fetch_object($result) {
*
* @param resource $result
* A database query result resource, as returned from db_query().
*
* @return array|bool
* 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
Expand All @@ -156,6 +161,7 @@ function db_fetch_array($result) {
*
* @param resource $result
* A database query result resource, as returned from db_query().
*
* @return mixed
* The resulting field or FALSE.
*/
Expand Down Expand Up @@ -196,6 +202,7 @@ function db_affected_rows() {
*
* @param string $data
* Data to encode.
*
* @return string
* Encoded data.
*/
Expand All @@ -209,6 +216,7 @@ function db_encode_blob($data) {
*
* @param string $data
* Data to decode.
*
* @return string
* Decoded data.
*/
Expand All @@ -217,10 +225,12 @@ function db_decode_blob($data) {
}

/**
* Prepare user input for use in a database query, preventing SQL injection attacks.
* Prepare user input for use in a database query, preventing SQL injection
* attacks.
*
* @param string $text
* The string to escape.
*
* @return string
* The escaped string.
*/
Expand Down
24 changes: 17 additions & 7 deletions includes/database.mysqli.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

/**
* @file
* Database interface code for MySQL database servers using the mysqli client libraries. mysqli is included in PHP 5 by default and allows developers to use the advanced features of MySQL 4.1.x, 5.0.x and beyond.
* Database interface code for MySQL database servers using the mysqli client
* libraries. mysqli is included in PHP 5 by default and allows developers to
* use the advanced features of MySQL 4.1.x, 5.0.x and beyond.
*/

// Maintainers of this file should consult:
// http://www.php.net/manual/en/ref.mysqli.php
// Maintainers of this file should consult:
// http://www.php.net/manual/en/ref.mysqli.php

/**
* @ingroup database
Expand Down Expand Up @@ -35,6 +37,7 @@ function db_version() {
*
* @param string $url
* The database connection URL.
*
* @return mysqli
* The database connection.
*/
Expand All @@ -53,7 +56,7 @@ function db_connect($url) {
// Test if database URL has a password.
$db_pass = isset($url_parts['pass']) ? urldecode($url_parts['pass']) : '';
$db_name = substr(urldecode($url_parts['path']), 1);
$db_port = isset($url_parts['port']) ? $url_parts['port'] : 3306;
$db_port = isset($url_parts['port']) ? $url_parts['port'] : NULL;

$db_conn = mysqli_init();
@mysqli_real_connect($db_conn, $db_host, $db_user, $db_pass, $db_name, $db_port, NULL, MYSQLI_CLIENT_FOUND_ROWS);
Expand Down Expand Up @@ -83,7 +86,7 @@ function _db_query($query, $debug = 0) {

if (variable_get('dev_query', 0)) {
list($usec, $sec) = explode(' ', microtime());
$timer = (float)$usec + (float)$sec;
$timer = (float) $usec + (float) $sec;
// If devel.module 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.
Expand All @@ -100,7 +103,7 @@ function _db_query($query, $debug = 0) {
if (variable_get('dev_query', 0)) {
$query = $bt[2]['function'] ."\n". $query;
list($usec, $sec) = explode(' ', microtime());
$stop = (float)$usec + (float)$sec;
$stop = (float) $usec + (float) $sec;
$diff = $stop - $timer;
$queries[] = array($query, $diff);
}
Expand All @@ -125,6 +128,7 @@ function _db_query($query, $debug = 0) {
*
* @param mysqli_result $result
* A database query result resource, as returned from db_query().
*
* @return object|bool
* An object representing the next row of the result, or FALSE. The attributes
* of this object are the table fields selected by the query.
Expand All @@ -139,6 +143,7 @@ function db_fetch_object($result) {
*
* @param mysqli_result $result
* A database query result resource, as returned from db_query().
*
* @return array|bool
* 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
Expand All @@ -157,6 +162,7 @@ function db_fetch_array($result) {
*
* @param mysqli_result $result
* A database query result resource, as returned from db_query().
*
* @return mixed
* The resulting field or FALSE.
*/
Expand Down Expand Up @@ -197,6 +203,7 @@ function db_affected_rows() {
*
* @param string $data
* Data to encode.
*
* @return string
* Encoded data.
*/
Expand All @@ -210,6 +217,7 @@ function db_encode_blob($data) {
*
* @param string $data
* Data to decode.
*
* @return string
* Decoded data.
*/
Expand All @@ -218,10 +226,12 @@ function db_decode_blob($data) {
}

/**
* Prepare user input for use in a database query, preventing SQL injection attacks.
* Prepare user input for use in a database query, preventing SQL injection
* attacks.
*
* @param string $text
* The string to escape.
*
* @return string
* The escaped string.
*/
Expand Down
Loading

0 comments on commit 653a814

Please sign in to comment.