Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDI-1466: support official adodb5 library #131

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
67 changes: 53 additions & 14 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,12 +182,12 @@ 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'];
unset( $args[1]['tag'] );
}
unset( $args[1]['tag'] );
}
else
{
// No comment provided by programmer, set a default comment:
Expand Down Expand Up @@ -228,6 +225,13 @@ public function __call( $method, $args ) //call adodb methods

try
{
if (isset($args[1]) && is_array($args[1])) {
$args[1] = $this->fixQueryParamsForMultidimensionalArray($args[0], $args[1]);
if (count($args[1]) === 0) {
unset($args[1]);
}
}

$answer = call_user_func_array( array( self::$adodb[self::$destination_type], $method ), $args );
}
catch ( \ADODB_Exception $e )
Expand All @@ -254,7 +258,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 +275,23 @@ public function __call( $method, $args ) //call adodb methods
return $answer;
}

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

$query = str_replace(["\r", "\n", PHP_EOL], ' ', $query);
$param_count = preg_match_all('`(?:\s|,)(\?)(?:;|,|\)|)`', $query);
$params = is_array($params[0]) ? $params[0] : $params;

if (count($params) > $param_count) {
array_splice($params, -(count($params) - $param_count));
}

return $params;
}

function __get( $property )
{
$this->_lazyLoadAdodbConnection();
Expand Down Expand Up @@ -323,20 +352,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 +423,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