Skip to content

Commit

Permalink
MDI-1466: support official adodb5 library
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Jan 14, 2022
1 parent 401eae1 commit 8517773
Show file tree
Hide file tree
Showing 154 changed files with 54 additions and 55,980 deletions.
10 changes: 10 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.github export-ignore
/instances export-ignore
/test export-ignore
/CHANGELOG.md export-ignore
/constants.php export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon export-ignore
/phpstan-baseline.neon export-ignore
/phpunit.xml.dist export-ignore
/rector.php export-ignore
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"require": {
"php": ">=7.2",
"adodb/adodb-php": "^5.21",
"geoip/geoip": "^1.17",
"neutron/sphinxsearch-api": "^2.0",
"phpmailer/phpmailer": "^6.0",
Expand All @@ -46,7 +47,9 @@
"sort-packages": true
},
"autoload": {
"classmap" : ["src/Sifo"]
"classmap" : [
"src/Sifo"
]
},
"autoload-dev": {
"classmap" : ["test"]
Expand Down
52 changes: 40 additions & 12 deletions src/Sifo/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ private function _lazyLoadAdodbConnection()
// When adodb is instantiated for the first time the object becomes in an array with a type of operation.
if ( !is_array( self::$adodb ) )
{
$version = Config::getInstance()->getLibrary( 'adodb' );
include_once( ROOT_PATH . "/vendor/sifophp/sifo/src/$version/adodb-exceptions.inc.php" ); //include exceptions for php5
include_once( ROOT_PATH . "/vendor/sifophp/sifo/src/$version/adodb.inc.php" );

if ( !isset( $db_params['profile'] ) )
{
// No Master/Slave schema expected:
Expand Down Expand Up @@ -149,16 +145,17 @@ private function _lazyLoadAdodbConnection()
$db_params = $db_profiles['slaves'][$selected_slave];
}
}

self::$adodb[self::$destination_type] = \NewADOConnection( $db_params['db_driver'] );
self::$adodb[self::$destination_type]->Connect( $db_params['db_host'], $db_params['db_user'], $db_params['db_password'], $db_params['db_name'] ); //connect to database constants are taken from config
if ( isset( $db_params['db_init_commands'] ) && is_array( $db_params['db_init_commands'] ) )
{
foreach ( $db_params['db_init_commands'] as $command )
{
self::$adodb[self::$destination_type]->Execute( $command );
self::$adodb[self::$destination_type]->Execute( $command );
}
}
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
self::$adodb[self::$destination_type]->fetchMode = ADODB_FETCH_ASSOC;
}
// If connection to database fails throw a SIFO 500 error.
catch ( \ADODB_Exception $e )
Expand All @@ -185,7 +182,7 @@ public function escapeSqlString( $string )

public function __call( $method, $args ) //call adodb methods
{
// Method provides a valid comment to associate to this query:
// Method provides a valid comment to associate to this query:
if ( isset( $args[1] ) && is_array( $args[1] ) && key_exists( 'tag', $args[1] ) ) // Arg could be a single string, not an array. Do not do isset($args[1]['tag'])
{
$tag = $args[1]['tag'];
Expand Down Expand Up @@ -228,6 +225,10 @@ public function __call( $method, $args ) //call adodb methods

try
{
if (isset($args[1])) {
$args[1] = $this->fixQueryParamsForMultidimensionalArray($args[1]);
}

$answer = call_user_func_array( array( self::$adodb[self::$destination_type], $method ), $args );
}
catch ( \ADODB_Exception $e )
Expand All @@ -254,7 +255,15 @@ public function __call( $method, $args ) //call adodb methods
$resultset = $answer;
}

$this->queryDebug( $resultset, $tag, $method, $read_operation, isset( $error ) ? $error : null );
$this->queryDebug(
$resultset,
$tag,
$method,
$read_operation,
isset( $error ) ? $error : null,
$args[0] ?? null,
$args[1] ?? null
);

// Reset queries in master flag:
self::$launch_in_master = false;
Expand All @@ -263,6 +272,15 @@ public function __call( $method, $args ) //call adodb methods
return $answer;
}

private function fixQueryParamsForMultidimensionalArray(array $params): array
{
if (false === isset($params[0])) {
return $params;
}

return is_array($params[0]) ? $params[0] : $params;
}

function __get( $property )
{
$this->_lazyLoadAdodbConnection();
Expand Down Expand Up @@ -323,20 +341,20 @@ protected function closeConnectionDatabase()
* @param $error
* @return void
*/
protected function queryDebug( $resultset, $tag, $method, $read_operation, $error )
protected function queryDebug( $resultset, $tag, $method, $read_operation, $error, $query = '', $params = [])
{
if ( !Domains::getInstance()->getDebugMode() )
{
return false;
}

$query = self::$adodb[self::$destination_type]->_querySQL;

$query_time = Benchmark::getInstance()->timingCurrentToRegistry( 'db_queries' );

$debug_query = array(
"tag" => $tag,
"sql" => in_array( strtolower($method), $this->methods_whitout_duplicated_validation ) ? $method : $query,
"sql" => in_array( strtolower($method), $this->methods_whitout_duplicated_validation )
? $method
: $this->printQuery($query, $params),
"type" => ( $read_operation ? 'read' : 'write' ),
"destination" => self::$destination_type,
"host" => self::$adodb[self::$destination_type]->host,
Expand Down Expand Up @@ -394,6 +412,16 @@ protected function queryDebug( $resultset, $tag, $method, $read_operation, $erro
}
}

private function printQuery(string $query, array $params)
{
$params_to_print = [];
foreach ($params as $key => $param) {
$params_to_print[] = sprintf('* %s: %s', $key, $param);
}

return sprintf('%s\n%s', $query, implode(PHP_EOL, $params_to_print));
}

/**
* Build the caller classes stack.
* @return string
Expand Down
Loading

0 comments on commit 8517773

Please sign in to comment.