summaryrefslogtreecommitdiff
path: root/adodb-loadbalancer.inc.php
diff options
context:
space:
mode:
authorDamien Regad <dregad@mantisbt.org>2020-01-25 22:00:18 +0100
committerDamien Regad <dregad@mantisbt.org>2020-01-25 22:00:18 +0100
commita3817e67beead9c00447b96385a45664b8439460 (patch)
tree8cd664abcae27cdee873a81c4882f0ce83f9c1f1 /adodb-loadbalancer.inc.php
parent8bae5242fe0670e6cdcb069f342ba9224d063728 (diff)
downloadadodb-a3817e67beead9c00447b96385a45664b8439460.tar.gz
adodb-a3817e67beead9c00447b96385a45664b8439460.tar.bz2
adodb-a3817e67beead9c00447b96385a45664b8439460.zip
Various whitespace fixes
Diffstat (limited to 'adodb-loadbalancer.inc.php')
-rw-r--r--adodb-loadbalancer.inc.php359
1 files changed, 181 insertions, 178 deletions
diff --git a/adodb-loadbalancer.inc.php b/adodb-loadbalancer.inc.php
index 58c56be7..7cc71091 100644
--- a/adodb-loadbalancer.inc.php
+++ b/adodb-loadbalancer.inc.php
@@ -14,14 +14,14 @@
*
* Latest version is available at https://adodb.org/
*
- * @package ADOdb
- * @version v5.21.0-dev ??-???-2016
- * @author Mike Benoit
+ * @package ADOdb
+ * @version v5.21.0-dev ??-???-2016
+ * @author Mike Benoit
* @copyright (c) 2016 Mike Benoit and the ADOdb community
- * @license BSD-3-Clause
- * @license GNU Lesser General Public License (LGPL) v2.1 or later
- * @link https://adodb.org/
- * @since v5.21.0
+ * @license BSD-3-Clause
+ * @license GNU Lesser General Public License (LGPL) v2.1 or later
+ * @link https://adodb.org/
+ * @since v5.21.0
*/
/**
@@ -29,10 +29,10 @@
*/
class ADOdbLoadBalancer
{
- /**
- * @var bool Once a write or readonly connection is made, stick to that connection for the entire request.
- */
- public $enable_sticky_sessions = true;
+ /**
+ * @var bool Once a write or readonly connection is made, stick to that connection for the entire request.
+ */
+ public $enable_sticky_sessions = true;
/**
@@ -84,7 +84,7 @@ class ADOdbLoadBalancer
/**
* Defines SQL queries that are executed each time a new database connection is established.
*
- * @param $sql
+ * @param $sql
* @return bool
*/
public function setSessionInitSQL($sql)
@@ -97,7 +97,7 @@ class ADOdbLoadBalancer
/**
* Adds a new database connection to the pool, but no actual connection is made until its needed.
*
- * @param $obj
+ * @param $obj
* @return bool
* @throws Exception
*/
@@ -129,45 +129,45 @@ class ADOdbLoadBalancer
/**
* Removes a database connection from the pool.
*
- * @param $i
+ * @param $i
* @return bool
*/
public function removeConnection($i)
{
- if ( isset($this->connections[$i]) ) {
- $obj = $this->connections[ $i ];
+ if (isset($this->connections[$i]) ) {
+ $obj = $this->connections[ $i ];
- $this->total_connections[ $obj->type ]--;
- $this->total_connections['all']--;
+ $this->total_connections[ $obj->type ]--;
+ $this->total_connections['all']--;
- $this->total_connection_weights[ $obj->type ] -= abs($obj->weight);
- $this->total_connection_weights['all'] -= abs($obj->weight);
+ $this->total_connection_weights[ $obj->type ] -= abs($obj->weight);
+ $this->total_connection_weights['all'] -= abs($obj->weight);
- if ( $obj->type == 'write' ) {
- unset( $this->connections_write[array_search( $i, $this->connections_write)]);
- $this->connections_write = array_values($this->connections_write); //Reindex array.
- } else {
- unset( $this->connections_readonly[array_search( $i, $this->connections_readonly)]);
- $this->connections_readonly = array_values($this->connections_readonly); //Reindex array.
- }
+ if ($obj->type == 'write' ) {
+ unset($this->connections_write[array_search($i, $this->connections_write)]);
+ $this->connections_write = array_values($this->connections_write); //Reindex array.
+ } else {
+ unset($this->connections_readonly[array_search($i, $this->connections_readonly)]);
+ $this->connections_readonly = array_values($this->connections_readonly); //Reindex array.
+ }
- //Remove any sticky connections as well.
- if ( $this->last_connection_id[$obj->type] == $i ) {
- $this->last_connection_id[$obj->type] = false;
- }
+ //Remove any sticky connections as well.
+ if ($this->last_connection_id[$obj->type] == $i ) {
+ $this->last_connection_id[$obj->type] = false;
+ }
- unset( $this->connections[$i] );
+ unset($this->connections[$i]);
- return true;
- }
+ return true;
+ }
- return false;
- }
+ return false;
+ }
/**
* Returns a database connection of the specified type, but takes into account the connection weight for load balancing.
*
- * @param string $type Type of database connection, either: 'write' capable or 'readonly'
+ * @param string $type Type of database connection, either: 'write' capable or 'readonly'
* @return bool|int|string
*/
private function getConnectionByWeight($type)
@@ -198,7 +198,7 @@ class ADOdbLoadBalancer
/**
* Returns the proper database connection when taking into account sticky sessions and load balancing.
*
- * @param $type
+ * @param $type
* @return bool|int|mixed|string
*/
public function getLoadBalancedConnection($type)
@@ -223,7 +223,7 @@ class ADOdbLoadBalancer
/**
* Returns the ADODB connection object by connection_id and ensures that its connected and the session variables are executed.
*
- * @param $connection_id
+ * @param $connection_id
* @return bool|ADOConnection
* @throws Exception
*/
@@ -236,11 +236,15 @@ class ADOdbLoadBalancer
if (is_object($adodb_obj) && $adodb_obj->_connectionID == false) {
try {
if ($connection_obj->persistent_connection == true) {
- $adodb_obj->Pconnect($connection_obj->host, $connection_obj->user, $connection_obj->password,
- $connection_obj->database);
+ $adodb_obj->Pconnect(
+ $connection_obj->host, $connection_obj->user, $connection_obj->password,
+ $connection_obj->database
+ );
} else {
- $adodb_obj->Connect($connection_obj->host, $connection_obj->user, $connection_obj->password,
- $connection_obj->database);
+ $adodb_obj->Connect(
+ $connection_obj->host, $connection_obj->user, $connection_obj->password,
+ $connection_obj->database
+ );
}
} catch (Exception $e) {
//Connection error, see if there are other connections to try still.
@@ -264,36 +268,36 @@ class ADOdbLoadBalancer
/**
* Returns the ADODB connection object by database type and ensures that its connected and the session variables are executed.
*
- * @param string $type
- * @param null $pin_connection
+ * @param string $type
+ * @param null $pin_connection
* @return ADOConnection|bool
* @throws Exception
*/
public function getConnection($type = 'write', $pin_connection = null)
{
- while ( ($type == 'write' AND $this->total_connections['write'] > 0 ) OR ( $type == 'readonly' AND $this->total_connections['all'] > 0 ) ) {
- if ( $this->pinned_connection_id !== false ) {
- $connection_id = $this->pinned_connection_id;
- } else {
- $connection_id = $this->getLoadBalancedConnection( $type );
- }
+ while ( ($type == 'write' AND $this->total_connections['write'] > 0 ) OR ( $type == 'readonly' AND $this->total_connections['all'] > 0 ) ) {
+ if ($this->pinned_connection_id !== false ) {
+ $connection_id = $this->pinned_connection_id;
+ } else {
+ $connection_id = $this->getLoadBalancedConnection($type);
+ }
- if ( $connection_id !== FALSE ) {
- try {
- $adodb_obj = $this->_getConnection( $connection_id );
- //$connection_obj = $this->connections[$connection_id];
- break;
- } catch ( Exception $e ) {
- //Connection error, see if there are other connections to try still.
- $this->removeConnection( $connection_id );
- if ( ( $type == 'write' AND $this->total_connections['write'] == 0 ) OR ( $type == 'readonly' AND $this->total_connections['all'] == 0 ) ) {
- throw $e;
- }
- }
- } else {
- throw new Exception('Connection ID is invalid!');
- }
- }
+ if ($connection_id !== false ) {
+ try {
+ $adodb_obj = $this->_getConnection($connection_id);
+ //$connection_obj = $this->connections[$connection_id];
+ break;
+ } catch ( Exception $e ) {
+ //Connection error, see if there are other connections to try still.
+ $this->removeConnection($connection_id);
+ if (( $type == 'write' AND $this->total_connections['write'] == 0 ) OR ( $type == 'readonly' AND $this->total_connections['all'] == 0 ) ) {
+ throw $e;
+ }
+ }
+ } else {
+ throw new Exception('Connection ID is invalid!');
+ }
+ }
$this->last_connection_id[$type] = $connection_id;
@@ -314,7 +318,7 @@ class ADOdbLoadBalancer
* This is a hack to work around pass by reference error.
* Parameter 1 to ADOConnection::GetInsertSQL() expected to be a reference, value given in adodb-loadbalancer.inc.php on line 83
*
- * @param $arr
+ * @param $arr
* @return array
*/
private function makeValuesReferenced($arr)
@@ -335,10 +339,9 @@ class ADOdbLoadBalancer
* causing bloat/clutter when new connections are established. For example if the time_zone is set to many different
* ones through the course of a single connection, a new connection should only set it to the most recent value.
*
- *
- * @param $name
- * @param $value
- * @param bool $execute_immediately
+ * @param $name
+ * @param $value
+ * @param bool $execute_immediately
* @return array|bool|mixed
* @throws Exception
*/
@@ -356,7 +359,7 @@ class ADOdbLoadBalancer
/**
* Executes the session variables on a given ADODB object.
*
- * @param ADOConnection|bool $adodb_obj
+ * @param ADOConnection|bool $adodb_obj
* @return array|bool|mixed
* @throws Exception
*/
@@ -387,18 +390,18 @@ class ADOdbLoadBalancer
* Executes the same SQL QUERY on the entire cluster of connections.
* Would be used for things like SET SESSION TIME ZONE calls and such.
*
- * @param $sql
- * @param bool $inputarr
- * @param bool $return_all_results
- * @param bool $existing_connections_only
+ * @param $sql
+ * @param bool $inputarr
+ * @param bool $return_all_results
+ * @param bool $existing_connections_only
* @return array|bool|mixed
* @throws Exception
*/
public function ClusterExecute(
- $sql,
- $inputarr = false,
- $return_all_results = false,
- $existing_connections_only = true
+ $sql,
+ $inputarr = false,
+ $return_all_results = false,
+ $existing_connections_only = true
) {
if (is_array($this->connections) && count($this->connections) > 0) {
foreach ($this->connections as $key => $connection_obj) {
@@ -424,10 +427,10 @@ class ADOdbLoadBalancer
return $result_arr[0];
} else {
- //When using lazy connections, there are cases where setSessionVariable() is called early on, but there are no connections to execute the queries on yet.
- //This captures that case and forces a RETURN TRUE to occur. As likely the queries will be exectued as soon as a connection is established.
- return true;
- }
+ //When using lazy connections, there are cases where setSessionVariable() is called early on, but there are no connections to execute the queries on yet.
+ //This captures that case and forces a RETURN TRUE to occur. As likely the queries will be exectued as soon as a connection is established.
+ return true;
+ }
}
}
@@ -437,7 +440,7 @@ class ADOdbLoadBalancer
/**
* Determines if a SQL query is read-only or not.
*
- * @param string $sql SQL Query to test.
+ * @param string $sql SQL Query to test.
* @return bool
*/
public function isReadOnlyQuery($sql)
@@ -454,8 +457,8 @@ class ADOdbLoadBalancer
/**
* Use this instead of __call() as it significantly reduces the overhead of call_user_func_array().
*
- * @param $sql
- * @param bool $inputarr
+ * @param $sql
+ * @param bool $inputarr
* @return array|bool|mixed
* @throws Exception
*/
@@ -489,8 +492,8 @@ class ADOdbLoadBalancer
/**
* Magic method to intercept method and callback to the proper ADODB object for write/readonly connections.
*
- * @param string $method ADODB method to call.
- * @param array $args Arguments to the ADODB method.
+ * @param string $method ADODB method to call.
+ * @param array $args Arguments to the ADODB method.
* @return bool|mixed
* @throws Exception
*/
@@ -503,27 +506,27 @@ class ADOdbLoadBalancer
$method = strtolower($method);
switch ($method) {
//case 'execute': //This is the direct overloaded function above instead.
- case 'getone':
- case 'getrow':
- case 'getall':
- case 'getcol':
- case 'getassoc':
- case 'selectlimit':
- if ($this->isReadOnlyQuery(trim($args[0])) == true) {
- $type = 'readonly';
- }
- break;
- case 'cachegetone':
- case 'cachegetrow':
- case 'cachegetall':
- case 'cachegetcol':
- case 'cachegetassoc':
- case 'cacheexecute':
- case 'cacheselect':
- case 'pageexecute':
- case 'cachepageexecute':
+ case 'getone':
+ case 'getrow':
+ case 'getall':
+ case 'getcol':
+ case 'getassoc':
+ case 'selectlimit':
+ if ($this->isReadOnlyQuery(trim($args[0])) == true) {
$type = 'readonly';
- break;
+ }
+ break;
+ case 'cachegetone':
+ case 'cachegetrow':
+ case 'cachegetall':
+ case 'cachegetcol':
+ case 'cachegetassoc':
+ case 'cacheexecute':
+ case 'cacheselect':
+ case 'pageexecute':
+ case 'cachepageexecute':
+ $type = 'readonly';
+ break;
//case 'ignoreerrors':
// //When ignoreerrors is called, PIN to the connection until its called again.
// if ( !isset($args[0]) || ( isset($args[0]) && $args[0] == FALSE ) ) {
@@ -534,94 +537,94 @@ class ADOdbLoadBalancer
// break;
//Manual transactions
- case 'begintrans':
- case 'settransactionmode':
+ case 'begintrans':
+ case 'settransactionmode':
$pin_connection = true;
- break;
- case 'rollbacktrans':
- case 'committrans':
- $pin_connection = false;
- break;
+ break;
+ case 'rollbacktrans':
+ case 'committrans':
+ $pin_connection = false;
+ break;
//Smart transactions
- case 'starttrans':
- $pin_connection = true;
- break;
- case 'completetrans':
- case 'failtrans':
- //getConnection() will only unpin the transaction if we're exiting the last nested transaction
- $pin_connection = false;
- break;
+ case 'starttrans':
+ $pin_connection = true;
+ break;
+ case 'completetrans':
+ case 'failtrans':
+ //getConnection() will only unpin the transaction if we're exiting the last nested transaction
+ $pin_connection = false;
+ break;
- //Functions that don't require any connection and therefore shouldn't force a connection be established before they run.
- case 'qstr':
- case 'escape':
- case 'binddate':
- case 'bindtimestamp':
- case 'setfetchmode':
- $type = false; //No connection necessary.
- break;
+ //Functions that don't require any connection and therefore shouldn't force a connection be established before they run.
+ case 'qstr':
+ case 'escape':
+ case 'binddate':
+ case 'bindtimestamp':
+ case 'setfetchmode':
+ $type = false; //No connection necessary.
+ break;
- //Default to assuming write connection is required to be on the safe side.
- default:
- break;
+ //Default to assuming write connection is required to be on the safe side.
+ default:
+ break;
}
- if ( $type === false ) {
- if ( is_array($this->connections) AND count($this->connections) > 0 ) {
- foreach( $this->connections as $key => $connection_obj ) {
- $adodb_obj = $connection_obj->getADOdbObject();
- return call_user_func_array( array($adodb_obj, $method), $this->makeValuesReferenced( $args ) ); //Just makes the function call on the first object.
- }
- }
- } else {
- $adodb_obj = $this->getConnection( $type, $pin_connection );
- if ( is_object( $adodb_obj ) ) {
- $result = call_user_func_array( array($adodb_obj, $method), $this->makeValuesReferenced( $args ) );
+ if ($type === false ) {
+ if (is_array($this->connections) AND count($this->connections) > 0 ) {
+ foreach( $this->connections as $key => $connection_obj ) {
+ $adodb_obj = $connection_obj->getADOdbObject();
+ return call_user_func_array(array($adodb_obj, $method), $this->makeValuesReferenced($args)); //Just makes the function call on the first object.
+ }
+ }
+ } else {
+ $adodb_obj = $this->getConnection($type, $pin_connection);
+ if (is_object($adodb_obj) ) {
+ $result = call_user_func_array(array($adodb_obj, $method), $this->makeValuesReferenced($args));
- return $result;
- }
- }
- return false;
+ return $result;
+ }
+ }
+ return false;
}
/**
* Magic method to proxy property getter calls back to the proper ADODB object currently in use.
*
- * @param $property
+ * @param $property
* @return mixed
* @throws Exception
*/
public function __get($property)
{
- if ( is_array( $this->connections ) AND count( $this->connections ) > 0 ) {
- foreach ( $this->connections as $key => $connection_obj ) {
- return $connection_obj->getADOdbObject()->$property; //Just returns the property from the first object.
- }
- }
+ if (is_array($this->connections) AND count($this->connections) > 0 ) {
+ foreach ( $this->connections as $key => $connection_obj ) {
+ return $connection_obj->getADOdbObject()->$property; //Just returns the property from the first object.
+ }
+ }
- return false;
+ return false;
}
/**
* Magic method to proxy property setter calls back to the proper ADODB object currently in use.
*
- * @param $property
- * @param $value
+ * @param $property
+ * @param $value
* @return mixed
* @throws Exception
*/
public function __set($property, $value)
{
- //Special function to set object properties on all objects without initiating a connection to the database.
- if ( is_array( $this->connections ) AND count( $this->connections ) > 0 ) {
- foreach ( $this->connections as $key => $connection_obj ) {
- $connection_obj->getADOdbObject()->$property = $value;
- }
+ //Special function to set object properties on all objects without initiating a connection to the database.
+ if (is_array($this->connections) AND count($this->connections) > 0 ) {
+ foreach ( $this->connections as $key => $connection_obj ) {
+ $connection_obj->getADOdbObject()->$property = $value;
+ }
- return true;
- }
+ return true;
+ }
- return false;
+ return false;
}
/**
@@ -687,22 +690,22 @@ class ADOdbLoadBalancerConnection
*
* @param $driver
* @param string $type
- * @param int $weight
- * @param bool $persistent_connection
+ * @param int $weight
+ * @param bool $persistent_connection
* @param string $argHostname
* @param string $argUsername
* @param string $argPassword
* @param string $argDatabaseName
*/
public function __construct(
- $driver,
- $type = 'write',
- $weight = 1,
- $persistent_connection = false,
- $argHostname = '',
- $argUsername = '',
- $argPassword = '',
- $argDatabaseName = ''
+ $driver,
+ $type = 'write',
+ $weight = 1,
+ $persistent_connection = false,
+ $argHostname = '',
+ $argUsername = '',
+ $argPassword = '',
+ $argDatabaseName = ''
) {
if ($type !== 'write' && $type !== 'readonly') {
return false;