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

Issue 145 [Follow-up]: Use singleton for database connection #343

Merged
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
14 changes: 14 additions & 0 deletions auth/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}


protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand Down
2 changes: 1 addition & 1 deletion auth/admin/classes/common/DatabaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function init(NamedArguments $arguments) {
$this->primaryKeyName = $arguments->primaryKeyName;

$this->primaryKey = $arguments->primaryKey;
$this->db = new DBService;
$this->db = DBService::getInstance();
$this->defineRelationships();
//$this->defineAttributes();
$this->overridePrimaryKeyName();
Expand Down
14 changes: 14 additions & 0 deletions licensing/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}


protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand Down
2 changes: 1 addition & 1 deletion licensing/admin/classes/common/DatabaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function init(NamedArguments $arguments) {
$this->primaryKeyName = $arguments->primaryKeyName;

$this->primaryKey = $arguments->primaryKey;
$this->db = new DBService;
$this->db = DBService::getInstance();
$this->defineRelationships();
//$this->defineAttributes();
$this->overridePrimaryKeyName();
Expand Down
2 changes: 1 addition & 1 deletion licensing/admin/classes/common/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function fixLicenseFormEnter($editLicenseID){
if ($editLicenseID == ""){
//need to get the most recent added license since it will have been added but we didn''t get the resonse of the new license ID
//since this will have happened instantly we can be safe to assume this is the correct record
$this->db = new DBService;
$this->db = DBService::getInstance();

$result = $this->db->processQuery("select max(licenseID) max_licenseID from License;", 'assoc');

Expand Down
15 changes: 15 additions & 0 deletions management/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}



protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand Down
2 changes: 1 addition & 1 deletion management/admin/classes/common/DatabaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function init(NamedArguments $arguments) {
$this->primaryKeyName = $arguments->primaryKeyName;

$this->primaryKey = $arguments->primaryKey;
$this->db = new DBService;
$this->db = DBService::getInstance();
$this->defineRelationships();
//$this->defineAttributes();
$this->overridePrimaryKeyName();
Expand Down
2 changes: 1 addition & 1 deletion management/admin/classes/common/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function fixLicenseFormEnter($editLicenseID){
if ($editLicenseID == ""){
//need to get the most recent added license since it will have been added but we didn''t get the resonse of the new license ID
//since this will have happened instantly we can be safe to assume this is the correct record
$this->db = new DBService;
$this->db = DBService::getInstance();

$result = $this->db->processQuery("select max(licenseID) max_licenseID from License;", 'assoc');

Expand Down
15 changes: 15 additions & 0 deletions organizations/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}



protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand Down
2 changes: 1 addition & 1 deletion organizations/admin/classes/common/DatabaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function init(NamedArguments $arguments) {
$this->primaryKeyName = $arguments->primaryKeyName;

$this->primaryKey = $arguments->primaryKey;
$this->db = new DBService;
$this->db = DBService::getInstance();

$arguments->setDefaultValueForArgumentName('dbName',$this->db->config->database->name);
$this->dbName = $arguments->dbName;
Expand Down
15 changes: 15 additions & 0 deletions reports/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
class DBService {
protected static $db = null;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}


public function __construct($dbname = null){
Config::init();
if (!self::$db && !(self::$db = new mysqli(Config::$database->host, Config::$database->username, Config::$database->password))){
Expand Down
2 changes: 1 addition & 1 deletion reports/admin/classes/domain/ParameterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class ParameterFactory {
public static function makeParam($reportID,$reportParameterID) {
$parm = null;
$db = new DBService();
$db = DBService::getInstance();
$result = $db
->query("SELECT rp.*, rpm.parentReportParameterID
FROM ReportParameter rp, ReportParameterMap rpm
Expand Down
2 changes: 1 addition & 1 deletion reports/admin/classes/report/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function applyDateRange(array $dateRange) {
}

public function __construct($id){
$this->db = new DBService();
$this->db = DBService::getInstance();
$result = $this->db
->query("SELECT reportName, reportDatabaseName FROM Report WHERE reportID = '$id' LIMIT 1")
->fetchRow(MYSQLI_ASSOC);
Expand Down
2 changes: 1 addition & 1 deletion reports/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<?php
// get all reports for output in drop down

$db = new DBService();
$db = DBService::getInstance();
foreach ( $db->query("SELECT reportID, reportName FROM Report ORDER BY 2, 1")->fetchRows(MYSQLI_ASSOC) as $report ){
echo "<option value='" . $report['reportID'] . "' ";
if (isset($report['reportID']) && isset($_GET['reportID']) && $report['reportID'] === $_GET['reportID']){
Expand Down
15 changes: 15 additions & 0 deletions usage/admin/classes/common/DBService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ class DBService extends Object {
protected $db;
protected $config;
protected $error;
private static $_instance; //The single instance


/*
Get an instance of the Database
@return Instance
*/
public static function getInstance() {
if(!self::$_instance) { // If no instance then make one
self::$_instance = new self();
}
return self::$_instance;
}



protected function init(NamedArguments $arguments) {
parent::init($arguments);
Expand Down
2 changes: 1 addition & 1 deletion usage/admin/classes/common/DatabaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function init(NamedArguments $arguments) {
$this->primaryKey = $arguments->primaryKey;

$arguments->setDefaultValueForArgumentName('db',false);
$this->db = $arguments->db ? $arguments->db : new DBService;
$this->db = $arguments->db ? $arguments->db : DBService::getInstance();

$this->defineAttributes();
$this->overridePrimaryKeyName();
Expand Down
2 changes: 1 addition & 1 deletion usage/admin/classes/domain/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public function getPublisherPlatforms(){
$object = new PublisherPlatform(new NamedArguments(array('primaryKey' => $result['publisherPlatformID'])));
array_push($objects, $object);
}else{
$db = new DBService;
$db = DBService::getInstance();
foreach ($result as $row) {
$object = new PublisherPlatform(new NamedArguments(array('primaryKey' => $row['publisherPlatformID'],'db'=>$db)));
array_push($objects, $object);
Expand Down