Skip to content

Commit

Permalink
DB code refactoring around #553
Browse files Browse the repository at this point in the history
  • Loading branch information
kojoty committed Apr 20, 2016
1 parent 7d7687f commit 4c6ac88
Show file tree
Hide file tree
Showing 56 changed files with 122 additions and 124 deletions.
6 changes: 2 additions & 4 deletions GeoKretyAPI.php
Expand Up @@ -171,8 +171,6 @@ public function MakeGeokretInCacheSelector($cachename)
*/
public function LogGeokrety($GeokretyArray, $retry = false)
{

// dataBase::debugOC('#'.__line__.' ', $this->connectionTimeout);
if (!$GeokretyArray) { // array from datbase is epmty
$r['errors'][]['error'] = 'array from datbase is epmty';
$postdata = '';
Expand Down Expand Up @@ -276,7 +274,7 @@ private function connect($url, $operationType)

private function storeErrorsInDb($operationType, $dataSent, $response = null)
{
$db = new dataBase;
$db = OcDb::instance();
$query = "INSERT INTO `GeoKretyAPIerrors`(`dateTime`, `operationType`, `dataSent`, `response`)
VALUES (NOW(),:1,:2,:3)";
$db->multiVariableQuery($query, $operationType, addslashes(serialize($dataSent)), addslashes(serialize($response)));
Expand All @@ -296,7 +294,7 @@ public static function getErrorsFromDb()

public static function removeDbRows($rowsString)
{
$db = new dataBase;
$db = OcDb::instance();
$query = "DELETE FROM `GeoKretyAPIerrors` WHERE id in ($rowsString)";
$db->simpleQuery($query);
}
Expand Down
3 changes: 2 additions & 1 deletion admin_users.php
@@ -1,6 +1,7 @@
<?php

use Utils\Database\XDb;
use Utils\Database\OcDb;

//prepare the templates and include all neccessary
require_once('./lib/common.inc.php');
Expand Down Expand Up @@ -78,7 +79,7 @@
} else {
lib\Objects\User\AdminNote::addAdminNote($usr['userid'], $user->getUserId(), true, lib\Objects\User\AdminNote::IGNORE_FOUND_LIMIT_RM);
}
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery('INSERT INTO user_settings (user_id, newcaches_no_limit) VALUES (:2, :1) ON DUPLICATE KEY UPDATE newcaches_no_limit = :1', $newIgnoreFoundLimit, $user->getUserId());
$db->reset();
unset ($user);
Expand Down
3 changes: 2 additions & 1 deletion cache_stats.php
@@ -1,5 +1,6 @@
<?php

use Utils\Database\OcDb;
//prepare the templates and include all neccessary
require_once('./lib/common.inc.php');
//Preprocessing
Expand All @@ -21,7 +22,7 @@
$cachename = $geoCache->getCacheName();
tpl_set_var('cachename', $cachename);
$cachetime = $geoCache->getDatePlaced()->format('Y');
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$rsGeneralStatQuery = 'SELECT count(*) count FROM `cache_logs` WHERE cache_logs.deleted=0 AND (type=1 OR type=2) AND cache_id=:1 ';
$dbResult = $db->multiVariableQueryValue($rsGeneralStatQuery, 0, $cache_id);
if ($dbResult == 0) {
Expand Down
3 changes: 2 additions & 1 deletion cache_titled.php
@@ -1,6 +1,7 @@
<?php

use Utils\Database\XDb;
use Utils\Database\OcDb;

global $dateFormat;
require_once('./lib/common.inc.php');
Expand All @@ -11,7 +12,7 @@
if ( $usr != false )
$usrid = $usr['userid'];

$dbcLocCache = new dataBase();
$dbcLocCache = OcDb::instance();

$query="SELECT
caches.type cache_type, caches.name cacheName, caches.cache_id cache_id,
Expand Down
3 changes: 2 additions & 1 deletion cache_titled_add.php
@@ -1,14 +1,15 @@
<?php


use Utils\Database\OcDb;
global $titled_cache_nr_found, $titled_cache_period_prefix;

require_once('./lib/common.inc.php');

if ( !isset( $_REQUEST[ 'CRON' ] ) )
exit;

$dbc = new dataBase();
$dbc = OcDb::instance();

$queryMax = "SELECT max( date_alg ) dataMax FROM cache_titled";
$s = $dbc->simpleQuery($queryMax);
Expand Down
2 changes: 1 addition & 1 deletion confirmCacheCandidate.php
Expand Up @@ -69,7 +69,7 @@
function removeDbEntery($code)
{

$db = new dataBase;
$db = OcDb::instance();
$query = 'DELETE FROM `PowerTrail_cacheCandidate` WHERE `link` = :1';
$db->multiVariableQuery($query, $code);
}
Expand Down
13 changes: 6 additions & 7 deletions editcache.php
Expand Up @@ -92,7 +92,7 @@ function build_drop_seq($item_row, $selected_seq, $max_drop, $thisid, $drop_type
if ($pic_count_check > 0) {
if (isset($_POST['pic_seq_select1'])) { // check if in POST mode and in case any picture is attached (re-)update sequence value, providing it was changed - value of pic_seq_change_X)
if (!isset($dbc)) {
$dbc = new dataBase();
$dbc = OcDb::instance();
}
for ($i = 1; $i <= $pic_count_check; $i++) {
$this_seq = $_POST['pic_seq_select' . $i]; //get new seqence
Expand All @@ -114,9 +114,8 @@ function build_drop_seq($item_row, $selected_seq, $max_drop, $thisid, $drop_type
$mp3_count_check = $cache_record['mp3count'];
if ($mp3_count_check > 0) {
if (isset($_POST['mp3_seq_select1'])) { // check if in POST mode and in case any mp3 is attached (re-)update sequence value, providing it was changed - value of mp3_seq_change_X)
if (!isset($dbc)) {
$dbc = new dataBase();
}

$dbc = OcDb::instance();
for ($i = 1; $i <= $mp3_count_check; $i++) {
$this_seq = $_POST['mp3_seq_select' . $i]; //get new seqence
$this_mp3_id = $_POST['mp3_seq_id' . $i]; //get mp3 ID the new seq is applicable to
Expand Down Expand Up @@ -385,9 +384,9 @@ function build_drop_seq($item_row, $selected_seq, $max_drop, $thisid, $drop_type
$thatquery = "SELECT `attrib_id` FROM `caches_attributes` WHERE `cache_id`=:v1";
$params['v1']['value'] = (integer) $cache_id;
$params['v1']['data_type'] = 'integer';
if (!isset($dbc)) {
$dbc = new dataBase();
}

$dbc = OcDb::instance();

$s = $dbc->paramQuery($thatquery, $params);
unset($params);
$cache_attribs_count = $dbc->rowCount($s);
Expand Down
1 change: 0 additions & 1 deletion lib/ClassPathDictionary.php
Expand Up @@ -45,7 +45,6 @@ class ClassPathDictionary
private static $classDictionary = array(
'Smarty' => 'lib/Smarty/libs/Smarty.class.php',
'cache' => 'lib/cache.php',
'dataBase' => 'lib/Database/Db.php',
'PasswordManager' => 'lib/passwordManager.php',
'GeoKretyApi' => 'GeoKretyAPI.php',
'GetRegions' => 'GetRegions.php',
Expand Down
6 changes: 3 additions & 3 deletions lib/Controllers/LogEnteryController.php
Expand Up @@ -41,7 +41,7 @@ public function removeLog(GeoCacheLog $log, $request = null)
EmailController::sendRemoveLogNotification($log, $request, $loggedUser);
}
$updateQuery = "UPDATE `cache_logs` SET deleted = 1, `del_by_user_id` = :1 , `last_modified`=NOW(), `last_deleted`=NOW() WHERE `cache_logs`.`id`=:2 LIMIT 1";
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($updateQuery, $loggedUser->getUserId(), $log->getId());
$log->getUser()->recalculateAndUpdateStats();

Expand Down Expand Up @@ -112,7 +112,7 @@ private function buildLog($logId)

private function cacheScoreHandlingAfterRemoveLog(GeoCacheLog $log)
{
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();

// remove cache from users top caches, because the found log was deleted for some reason
$query = "DELETE FROM `cache_rating` WHERE `user_id` = :1 AND `cache_id` = :2 ";
Expand Down Expand Up @@ -171,7 +171,7 @@ private function handleMobileGeocachesAfterLogDelete(GeoCacheLog $log)
}
}

private function updateGeocacheAfterLogRemove(GeoCacheLog $log, \dataBase $db)
private function updateGeocacheAfterLogRemove(GeoCacheLog $log, OcDb $db)
{
$geoCache = $log->getGeoCache();
if ($log->getType() == GeoCacheLog::LOGTYPE_FOUNDIT || $log->getType() == GeoCacheLog::LOGTYPE_ATTENDED) {
Expand Down
3 changes: 1 addition & 2 deletions lib/Controllers/PowerTrailController.php
Expand Up @@ -2,7 +2,6 @@

namespace lib\Controllers;

use lib\Database\DataBaseSingleton;
use lib\Objects\PowerTrail\PowerTrail;
use Utils\Database\OcDb;

Expand Down Expand Up @@ -68,7 +67,7 @@ private function archiveAbandonPowerTrails()

private function freeCacheCandidates()
{
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$query = 'DELETE FROM `PowerTrail_cacheCandidate` WHERE `date` < DATE_SUB(curdate(), INTERVAL 2 WEEK)';
$db->simpleQuery($query);
$db->reset();
Expand Down
7 changes: 2 additions & 5 deletions lib/Objects/ApplicationContainer.php
Expand Up @@ -4,6 +4,7 @@

use lib\Objects\User\User;
use lib\Objects\OcConfig\OcConfig;
use Utils\Database\OcDb;

final class ApplicationContainer
{
Expand All @@ -13,16 +14,12 @@ final class ApplicationContainer
private $loggedUser = false;
private $ocConfig;

/**
*
* @var \dataBase $db
*/
public $db;

private function __construct()
{
$this->ocConfig = OcConfig::instance();
$this->db = \lib\Database\DataBaseSingleton::Instance();
$this->db = OcDb::instance();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Objects/GeoCache/Altitude.php
Expand Up @@ -66,7 +66,7 @@ private function storeAlitudeInDb()
VALUES (:2, :1, NOW())
ON DUPLICATE KEY UPDATE
`altitude` = :1, altitude_update_datetime = NOW()';
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($query, $this->altitude, $this->geoCache->getCacheId());
}

Expand Down
7 changes: 3 additions & 4 deletions lib/Objects/GeoCache/GeoCache.php
Expand Up @@ -4,10 +4,9 @@

use \lib\Objects\PowerTrail\PowerTrail;
use \lib\Objects\OcConfig\OcConfig;
use \lib\Database\DataBaseSingleton;
use Utils\Database\XDb;
use Utils\Database\OcDb;
//use \lib\Objects\GeoCache\CacheTitled;

/**
* Description of geoCache
*
Expand Down Expand Up @@ -546,7 +545,7 @@ public static function CacheIconByType($type, $status)
public function updateGeocacheLogenteriesStats()
{
$sqlQuery = "UPDATE `caches` SET `last_found`=:1, `founds`=:2, `notfounds`= :3, `notes`= :4 WHERE `cache_id`= :5";
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($sqlQuery, $this->lastFound, $this->founds, $this->notFounds, $this->notesCount, $this->id);
$db->reset();
}
Expand Down Expand Up @@ -898,7 +897,7 @@ public function getPicturesCount()
public function getMoveCount()
{
if($this->cacheType === self::TYPE_MOVING && $this->moveCount === -1){
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$sql = 'SELECT COUNT(*) FROM `cache_logs` WHERE type=4 AND cache_logs.deleted="0" AND cache_id=:1';
$this->moveCount = $db->multiVariableQueryValue($sql, 0, $this->id);
}
Expand Down
9 changes: 5 additions & 4 deletions lib/Objects/Medals/Medal.php
Expand Up @@ -3,7 +3,8 @@
namespace lib\Objects\Medals;

use \lib\Objects\User\User;
use \lib\Database\DataBaseSingleton;
use Utils\Database\OcDb;

/**
* Medal Factory - abstract interface
*
Expand Down Expand Up @@ -113,21 +114,21 @@ protected function setMedalPrizedTimeAndAcheivedLevel($level)
private function updateMedalRowInDb(User $user)
{
$query = ' UPDATE `medals` SET `prized_time`= NOW(), `medal_level`=:1 WHERE `user_id` = :2 AND `medal_type` = :3 ';
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($query, $this->level, $user->getUserId(), $this->medalId);
}

private function removeMedalFromUsersMedalsDb(User $user)
{
$query = 'DELETE FROM `medals` WHERE `user_id` = :1 AND `medal_type` = :2';
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($query, $user->getUserId(), $this->medalId);
}

private function addMedalToUserMedalsDb(User $user)
{
$query = 'INSERT INTO `medals`(`user_id`, `medal_type`, `prized_time`, `medal_level`) VALUES (:1, :2, :3, :4)';
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$db->multiVariableQuery($query, $user->getUserId(), $this->medalId, $this->prizedTime, $this->level);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Objects/Medals/MedalCachefound.php
Expand Up @@ -2,7 +2,7 @@

namespace lib\Objects\Medals;

use \lib\Database\DataBaseSingleton;

use \lib\Objects\User\User;
use Utils\Database\OcDb;
/**
Expand Down
1 change: 0 additions & 1 deletion lib/Objects/Medals/MedalGeographical.php
Expand Up @@ -3,7 +3,6 @@
namespace lib\Objects\Medals;

use \lib\Objects\User\User;
use \lib\Database\DataBaseSingleton;
use Utils\Database\OcDb;

/**
Expand Down
1 change: 0 additions & 1 deletion lib/Objects/Medals/MedalMaxAltitude.php
Expand Up @@ -2,7 +2,6 @@

namespace lib\Objects\Medals;

use \lib\Database\DataBaseSingleton;
use \lib\Objects\User\User;
use Utils\Database\OcDb;

Expand Down
7 changes: 2 additions & 5 deletions lib/Objects/PowerTrail/Log.php
Expand Up @@ -2,10 +2,7 @@

namespace lib\Objects\PowerTrail;

use \lib\Database\DataBaseSingleton;
//use \lib\Objects\User\User;
//use lib\Objects\PowerTrail\PowerTrail;

use Utils\Database\OcDb;
class Log
{
const TYPE_COMMENT = 1;
Expand Down Expand Up @@ -110,7 +107,7 @@ function setType($type)

public function storeInDb()
{
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
if($_REQUEST['type'] == Log::TYPE_CONQUESTED && $this->powerTrail->isAlreadyConquestedByUser($this->user)){ /* atempt to add second 'conquested' log */
return false;
}
Expand Down
7 changes: 3 additions & 4 deletions lib/Objects/PowerTrail/PowerTrail.php
Expand Up @@ -2,7 +2,6 @@

namespace lib\Objects\PowerTrail;

use \lib\Database\DataBaseSingleton;
use \lib\Objects\Coordinates\Coordinates;
use lib\Objects\GeoCache\Collection;
use lib\Objects\GeoCache\GeoCache;
Expand Down Expand Up @@ -403,7 +402,7 @@ public function disablePowerTrailBecauseCacheCountTooLow()
if ($this->cacheCount < $this->getPtMinCacheCountLimit()) {
// $text .= tr('pt227').tr('pt228');
print '[test only] geoPath #<a href="powerTrail.php?ptAction=showSerie&ptrail=' . $this->id . '">' . $this->id . '</a> (geoPtah cache count=' . $this->cacheCount . ' is lower than minimum=' . $this->getPtMinCacheCountLimit() . ') <br/>';
// $db = \lib\Database\DataBaseSingleton::Instance();
// $db = OcDb::instance();
// $queryStatus = 'UPDATE `PowerTrail` SET `status`= :1 WHERE `id` = :2';
// $db->multiVariableQuery($queryStatus, 4, $pt['id']);
// $query = 'INSERT INTO `PowerTrail_comments`(`userId`, `PowerTrailId`, `commentType`, `commentText`, `logDateTime`, `dbInsertDateTime`, `deleted`) VALUES
Expand Down Expand Up @@ -538,7 +537,7 @@ function getUnavailableGeocacheCount()
public function increaseConquestedCount()
{
$this->conquestedCount++;
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$query = 'UPDATE `PowerTrail` SET `PowerTrail`.`conquestedCount`= (SELECT COUNT(*) FROM `PowerTrail_comments` WHERE `PowerTrail_comments`.`PowerTrailId` = :1 AND `PowerTrail_comments`.`commentType` = 2 AND `PowerTrail_comments`.`deleted` = 0 ) WHERE `PowerTrail`.`id` = :1 ';
$db->multiVariableQuery($query, $this->id);
}
Expand All @@ -559,7 +558,7 @@ public function isAlreadyConquestedByUser(\lib\Objects\User\User $user)
public function setAndStoreStatus($status)
{
$this->status = $status;
$db = \lib\Database\DataBaseSingleton::Instance();
$db = OcDb::instance();
$query = 'UPDATE `PowerTrail` SET `status` = :1 WHERE `PowerTrail`.`id` = :2 ';
$db->multiVariableQuery($query, $status, $this->id);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/Objects/User/AdminNote.php
Expand Up @@ -8,7 +8,6 @@

namespace lib\Objects\User;

use \lib\Database\DataBaseSingleton;
use Utils\Database\OcDb;

class AdminNote
Expand Down Expand Up @@ -139,14 +138,14 @@ public function setCacheId($cache_id)

private function addNoteIntoDb()
{
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$query = "INSERT INTO `admin_user_notes`(`user_id`, `admin_id`, `automatic`, `content`) VALUES (:1, :2, :3, :4)";
$db->multiVariableQuery($query, $this->getUserId(), $this->getAdminId(), $this->getAutomatic(), $this->getContent());
}

private function addNoteIntoDbWithCacheId()
{
$db = DataBaseSingleton::Instance();
$db = OcDb::instance();
$query = "INSERT INTO `admin_user_notes`(`user_id`, `admin_id`, `cache_id`, `automatic`, `content`) VALUES (:1, :2, :3, :4, :5)";
$db->multiVariableQuery($query, $this->getUserId(), $this->getAdminId(), $this->getCacheId(), $this->getAutomatic(), $this->getContent());
}
Expand Down

0 comments on commit 4c6ac88

Please sign in to comment.