From ea1aa7ba97fe13adceab0761447188386ea0741d Mon Sep 17 00:00:00 2001 From: Jeremy Dorn Date: Thu, 24 May 2012 23:41:35 -0700 Subject: [PATCH] Consolidated database config declarations and code. --- classes/report_types/MongoReportType.php | 52 +++++++++++++----------- classes/report_types/MysqlReportType.php | 45 ++++++++------------ config/config.php.sample | 25 ++++++------ lib/FileSystemCache | 2 +- lib/PhpReports/Report.php | 21 ++++++++++ 5 files changed, 80 insertions(+), 65 deletions(-) diff --git a/classes/report_types/MongoReportType.php b/classes/report_types/MongoReportType.php index 64fabd1e..7dda50c7 100644 --- a/classes/report_types/MongoReportType.php +++ b/classes/report_types/MongoReportType.php @@ -1,27 +1,26 @@ options['Database'] || !isset($mongo_connections[$report->options['Database']])) { - $report->options['Database'] = current(array_keys($mongo_connections)); - } + $databases = PhpReports::$config['databases']; - //set up list of all available databases for displaying form for switching between them - $report->options['Databases'] = array(); - foreach(array_keys($mongo_connections) as $name) { - $report->options['Databases'][] = array( - 'selected'=>($report->options['Database'] == $name), - 'name'=>$name - ); + if(!isset($databases[$report->options['Database']]['mongo'])) { + throw new Exception("No mongo info defined for database '".$report->options['Database']."'"); } - //add a host macro - if(isset($mongo_connections[$report->options['Database']]['webhost'])) $host = $mongo_connections[$report->options['Database']]['webhost']; - else $host = $mongo_connections[$report->options['Database']]['host']; + $mongo = $databases[$report->options['Database']]['mongo']; + + //default host macro to mysql's host if it isn't defined elsewhere + if(!isset($report->macros['host'])) $report->macros['host'] = $mongo['host']; - $report->macros['host'] = $host; + //if there are any included reports, add it to the top of the raw query + if(isset($report->options['Includes'])) { + $included_code = ''; + foreach($report->options['Includes'] as &$included_report) { + $included_code .= trim($included_report->raw_query)."\n"; + } + + $report->raw_query = $included_code . $report->raw_query; + } } public static function openConnection(&$report) { @@ -35,21 +34,26 @@ public static function closeConnection(&$report) { public static function run(&$report) { $eval = ''; foreach($report->macros as $key=>$value) { - $eval .= 'var '.$key.' = "'.addslashes($value).'";'; + $eval .= 'var '.$key.' = "'.addslashes($value).'";'."\n"; } + $eval .= $report->raw_query; + + - $mongo_connections = PhpReports::$config['mongo_connections']; - $config = $mongo_connections[$report->options['Database']]; + $databases = PhpReports::$config['databases']; + $config = $databases[$report->options['Database']]['mongo']; $mongo_database = isset($report->options['Mongodatabase'])? $report->options['Mongodatabase'] : ''; - $command = 'mongo '.$config['host'].':'.$config['port'].'/'.$mongo_database.' --quiet --eval "'.addslashes($eval).'" '.PhpReports::$config['reportDir'].'/'.$report->report; + $command = 'mongo '.$config['host'].':'.$config['port'].'/'.$mongo_database.' --quiet --eval '.escapeshellarg($eval); $report->options['Query'] = '$ '.$command."\n\n".$report->raw_query; $report->options['Query_Formatted'] = '
-
$ '.$command.'
' - .PhpReports::$config['reportDir'].'/'.$report->report. - '
'.$report->raw_query.'
+
$ '.
+			'mongo '.$config['host'].':'.$config['port'].'/'.$mongo_database.' --quiet --eval '."'...'".
+			'
'. + 'Eval String:'. + '
'.$eval.'
'; $result = shell_exec($command); diff --git a/classes/report_types/MysqlReportType.php b/classes/report_types/MysqlReportType.php index dcb043a4..343e6fc5 100644 --- a/classes/report_types/MysqlReportType.php +++ b/classes/report_types/MysqlReportType.php @@ -1,55 +1,46 @@ options['Database'] || !isset($mysql_connections[$report->options['Database']])) { - $report->options['Database'] = current(array_keys($mysql_connections)); + $databases = PhpReports::$config['databases']; + + if(!isset($databases[$report->options['Database']]['mysql'])) { + throw new Exception("No mysql info defined for database '".$report->options['Database']."'"); } - //add a host macro - if(isset($mysql_connections[$report->options['Database']]['webhost'])) $host = $mysql_connections[$report->options['Database']]['webhost']; - else $host = $mysql_connections[$report->options['Database']]['host']; + $mysql = $databases[$report->options['Database']]['mysql']; + + //default host macro to mysql's host if it isn't defined elsewhere + if(!isset($report->macros['host'])) $report->macros['host'] = $mysql['host']; - $report->raw_query = preg_replace('/([^\{])(\{[a-zA-Z0-9_\-]+\})([^\}])/','$1{{$2}}$3',$report->raw_query); - $report->macros['host'] = $host; + //replace shorthand {host} with {{host}} in query + $report->raw_query = preg_replace('/([^\{])\{host\}([^\}])/','$1{{host}}$3',$report->raw_query); //if there are any included reports, add the report sql to the top if(isset($report->options['Includes'])) { $included_sql = ''; foreach($report->options['Includes'] as &$included_report) { - $included_sql .= trim($included_report->raw_query); + $included_sql .= trim($included_report->raw_query)."\n"; } - $report->raw_query = $included_sql . "\n" . $report->raw_query; - } - - //set up list of all available databases for displaying form for switching between them - $report->options['Databases'] = array(); - foreach(array_keys($mysql_connections) as $name) { - $report->options['Databases'][] = array( - 'selected'=>($report->options['Database'] === $name), - 'name'=>$name - ); + $report->raw_query = $included_sql . $report->raw_query; } } public static function openConnection(&$report) { if(isset($report->conn)) return; - $mysql_connections = PhpReports::$config['mysql_connections']; - $config = $mysql_connections[$report->options['Database']]; + $databases = PhpReports::$config['databases']; + $config = $databases[$report->options['Database']]['mysql']; //the default is to use a user with read only privileges - $username = $config['username']; - $password = $config['password']; + $username = $config['user']; + $password = $config['pass']; $host = $config['host']; //if the report requires read/write privileges if(isset($report->options['access']) && $report->options['access']==='rw') { - if(isset($config['username_rw'])) $username = $config['username_rw']; - if(isset($config['password_rw'])) $password = $config['password_rw']; + if(isset($config['user_rw'])) $username = $config['user_rw']; + if(isset($config['pass_rw'])) $password = $config['pass_rw']; if(isset($config['host_rw'])) $host = $config['host_rw']; } diff --git a/config/config.php.sample b/config/config.php.sample index 79a3e38c..5ef1226e 100644 --- a/config/config.php.sample +++ b/config/config.php.sample @@ -2,20 +2,19 @@ return array( 'reportDir' => 'reports', 'cacheDir' => 'cache', - - 'mysql_connections' => array( - 'main' => array( + 'databases' => array( + 'main'=>array( 'host'=>'localhost', - 'username'=>'root', - 'password'=>'', - 'database'=>'test' - ) - ), - - 'mongo_connections' => array( - 'main' => array( - 'host'=>'localhost', - 'port'=>'27017' + 'mysql'=>array( + 'host'=>'localhost', + 'user'=>'root', + 'pass'=>'', + 'database'=>'test' + ), + 'mongo'=>array( + 'host'=>'localhost', + 'port'=>'27017' + ) ) ) ); diff --git a/lib/FileSystemCache b/lib/FileSystemCache index 7e626497..e9658301 160000 --- a/lib/FileSystemCache +++ b/lib/FileSystemCache @@ -1 +1 @@ -Subproject commit 7e626497e30065a329eca234ada30f286a5ebfac +Subproject commit e96583019909759cfd69eda1949a95e4bfffeb88 diff --git a/lib/PhpReports/Report.php b/lib/PhpReports/Report.php index 180ecac0..6be87fae 100644 --- a/lib/PhpReports/Report.php +++ b/lib/PhpReports/Report.php @@ -179,6 +179,27 @@ protected function applyFilters($column, $value) { } protected function initDb() { + //if the database isn't set, use the first defined one from config + $databases = PhpReports::$config['databases']; + if(!$this->options['Database']) { + $this->options['Database'] = current(array_keys($databases)); + } + + //set database options + $database_options = array(); + foreach($databases as $key=>$params) { + $database_options[] = array( + 'name'=>$key, + 'selected'=>$key===$this->options['Database'] + ); + } + $this->options['Databases'] = $database_options; + + //add a host macro + if(isset($databases[$this->options['Database']]['host'])) { + $this->macros['host'] = $databases[$this->options['Database']]['host']; + } + $classname = $this->options['Type'].'ReportType'; if(!class_exists($classname)) {