Skip to content

Commit

Permalink
Consolidated database config declarations and code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdorn committed May 25, 2012
1 parent 6376607 commit ea1aa7b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 65 deletions.
52 changes: 28 additions & 24 deletions classes/report_types/MongoReportType.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
<?php
abstract class MongoReportType extends ReportTypeBase {
public static function init(&$report) {
$mongo_connections = PhpReports::$config['mongo_connections'];

//if the database isn't set or doesn't exist, use the first defined one
if(!$report->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) {
Expand All @@ -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'] = '<div>
<pre style="background-color: black; color: white; padding: 10px 5px;">$ '.$command.'</pre>'
.PhpReports::$config['reportDir'].'/'.$report->report.
'<pre style="border-left: 1px solid black; padding-left: 20px;">'.$report->raw_query.'</pre>
<pre style="background-color: black; color: white; padding: 10px 5px;">$ '.
'mongo '.$config['host'].':'.$config['port'].'/'.$mongo_database.' --quiet --eval '."'...'".
'</pre>'.
'Eval String:'.
'<pre style="border-left: 1px solid black; padding-left: 20px;">'.$eval.'</pre>
</div>';

$result = shell_exec($command);
Expand Down
45 changes: 18 additions & 27 deletions classes/report_types/MysqlReportType.php
Original file line number Diff line number Diff line change
@@ -1,55 +1,46 @@
<?php
class MysqlReportType extends ReportTypeBase {
public static function init(&$report) {
$mysql_connections = PhpReports::$config['mysql_connections'];

//if the database isn't set or doesn't exist, use the first defined one
if(!$report->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'];
}

Expand Down
25 changes: 12 additions & 13 deletions config/config.php.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion lib/FileSystemCache
Submodule FileSystemCache updated 3 files
+127 −5 FileSystemCache.php
+98 −54 README.md
+23 −0 examples/get_and_modify.php
21 changes: 21 additions & 0 deletions lib/PhpReports/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit ea1aa7b

Please sign in to comment.