Skip to content

Commit

Permalink
Changed last refences to old db code and removed database.inc.php
Browse files Browse the repository at this point in the history
Issue#20
  • Loading branch information
jeroenrnl committed Nov 2, 2015
1 parent e1e7314 commit 0ed92c0
Show file tree
Hide file tree
Showing 21 changed files with 158 additions and 214 deletions.
2 changes: 1 addition & 1 deletion UnitTests/configTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function testUnkownConfItem() {
$qry=new select(array("co" => "conf"));
$qry->where(new clause("conf_id=:conf_id"));
$qry->addParam(new param(":conf_id", "test.unknown", PDO::PARAM_STR));
$result=query($qry);
$result=db::query($qry);
$records=$result->fetch(PDO::FETCH_ASSOC);

$this->assertFalse($records);
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/photoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public function testGetNear($photo, $distance, $limit, $entity, $exp_count) {
*/
private function buildTrack() {
conf::set("maps.provider", "googlev3")->update();
query("truncate zoph_point");
db::SQL("truncate zoph_point");
$track=new track();

$track->set("name", "Test Track");
Expand Down
18 changes: 10 additions & 8 deletions UnitTests/userTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ public function testCreateAndDelete() {

unset($user);

$sql="SELECT count(user_id) as count FROM zoph_users WHERE user_id=" . $id;
$qry=new select(array("users"));
$qry->addFunction(array("count" => "COUNT(user_id)"));
$qry->where(new clause("user_id=:userid"));
$qry->addParam(new param(":userid", $id, PDO::PARAM_INT));

$result=query($sql);
$row=mysql_fetch_row($result);
$this->assertEquals($row[0], 1);
$this->assertEquals($qry->getCount(), 1);

// Test Password

Expand All @@ -104,11 +105,12 @@ public function testCreateAndDelete() {

$new_user->delete();

$sql="SELECT count(user_id) as count FROM zoph_users WHERE user_id=" . $id;
$result=query($sql);
$row=mysql_fetch_row($result);
$this->assertEquals($row[0], 0);
$qry=new select(array("users"));
$qry->addFunction(array("count" => "COUNT(user_id)"));
$qry->where(new clause("user_id=:userid"));
$qry->addParam(new param(":userid", $id, PDO::PARAM_INT));

$this->assertEquals($qry->getCount(), 0);
}

public function testSetPasswordOnUpdate() {
Expand Down
8 changes: 4 additions & 4 deletions php/classes/album.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function getDetails() {
$qry->where($where);


$result=query($qry);
$result=db::query($qry);
if ($result) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
Expand Down Expand Up @@ -239,7 +239,7 @@ public function getPhotoCount() {
}

$qry->where($where);
$count=static::getCountFromQuery($qry);
$count=$qry->getCount();
$this->photoCount=$count;
return $count;
}
Expand All @@ -265,7 +265,7 @@ public function getTotalPhotoCount() {
}
$qry->where($where);

return static::getCountFromQuery($qry);
return $qry->getCount();
}

/**
Expand Down Expand Up @@ -484,7 +484,7 @@ public static function getCount() {
$qry->addParam(new param(":userid", $user->getId(), PDO::PARAM_INT));
$qry->where($where);
}
return static::getCountFromQuery($qry);
return $qry->getCount();
}
}

Expand Down
6 changes: 3 additions & 3 deletions php/classes/category.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function getPhotoCount() {
}

$qry->where($where);
$count=static::getCountFromQuery($qry);
$count=$qry->getCount();
$this->photoCount=$count;
return $count;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ public function getTotalPhotoCount() {
$qry->where($where);
}

$count=static::getCountFromQuery($qry);
$count=$qry->getCount();
$this->photoTotalCount=$count;
return $count;
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public function getDetails() {
$qry->where($where);


$result=query($qry);
$result=db::query($qry);
if ($result) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
Expand Down
9 changes: 6 additions & 3 deletions php/classes/conf.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* @author Jeroen Roos
*/

require_once "database.inc.php";
require_once "user.inc.php";

/**
Expand Down Expand Up @@ -58,7 +57,11 @@ public static function loadFromDB() {
$qry=new select(array("co" => "conf"));
$qry->addFields(array("conf_id", "value"));

$result=query($qry, "Cannot load configuration from database");
try {
$result=db::query($qry);
} catch (PDOException $e) {
log::msg("Cannot load configuration from database", log::FATAL, log::CONFIG | log::DB);
}

while($row = $result->fetch(PDO::FETCH_NUM)) {
$key=$row[0];
Expand All @@ -78,7 +81,7 @@ public static function loadFromDB() {
$qry=new delete(array("co" => "conf"));
$qry->where(new clause("conf_id=:confid"));
$qry->addParam(new param(":confid", $key, PDO::PARAM_STR));
query($qry);
$qry->execute();
}

}
Expand Down
2 changes: 1 addition & 1 deletion php/classes/confItem.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ final public function update() {
$qry->where(new clause("conf_id=:confid"));
$qry->addParam(new param(":confid", $this->fields["conf_id"], PDO::PARAM_STR));

if (static::getCountFromQuery($qry) > 0) {
if ($qry->getCount() > 0) {
parent::update();
} else {
parent::insert();
Expand Down
54 changes: 36 additions & 18 deletions php/classes/db.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class db {
* @param string password
*/
private function __construct($dsn, $dbuser, $dbpass) {
self::$connection=new PDO($dsn,$dbuser,$dbpass);
self::$connection->setAttribute(
static::$connection=new PDO($dsn,$dbuser,$dbpass);
static::$connection->setAttribute(
PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$connection->setAttribute(
static::$connection->setAttribute(
PDO::ATTR_EMULATE_PREPARES,false);
}

Expand All @@ -65,10 +65,10 @@ private function __construct($dsn, $dbuser, $dbpass) {
* Make connection first, if it has not been made
*/
public static function getHandle() {
if(!self::$connection) {
self::connect();
if (!static::$connection) {
static::connect();
}
return self::$connection;
return static::$connection;
}

/**
Expand All @@ -80,29 +80,29 @@ public static function getHandle() {
* @param string database table prefix
*/
public static function setLoginDetails($dbhost, $dbname, $dbuser, $dbpass, $dbprefix) {
self::$dbhost=$dbhost;
self::$dbname=$dbname;
self::$dbuser=$dbuser;
self::$dbpass=$dbpass;
self::$dbprefix=$dbprefix;
static::$dbhost=$dbhost;
static::$dbname=$dbname;
static::$dbuser=$dbuser;
static::$dbpass=$dbpass;
static::$dbprefix=$dbprefix;
}

/**
* Get table prefix
*/
public static function getPrefix() {
return self::$dbprefix;
return static::$dbprefix;
}

/**
* Connect to database
* @param string PDO DSN
*/
private static function connect($dsn=null) {
if(!$dsn) {
$dsn=self::getDSN();
if (!$dsn) {
$dsn=static::getDSN();
}
new db($dsn, self::$dbuser, self::$dbpass);
new db($dsn, static::$dbuser, static::$dbpass);
}

/**
Expand All @@ -113,20 +113,20 @@ private static function connect($dsn=null) {
private static function getDSN() {
$db="mysql";

return sprintf("%s:host=%s;dbname=%s", $db, self::$dbhost, self::$dbname);
return sprintf("%s:host=%s;dbname=%s", $db, static::$dbhost, static::$dbname);
}

/**
* Run a query
* @param query Query to run
*/
public static function query(query $query) {
$db=self::getHandle();
$db=static::getHandle();

try {
$stmt=$db->prepare($query);
foreach($query->getParams() as $param) {
if($param instanceof param) {
if ($param instanceof param) {
$stmt->bindValue($param->getName(), $param->getValue(), $param->getType());
}
}
Expand All @@ -140,5 +140,23 @@ public static function query(query $query) {

return $stmt;
}

/**
* Execute an SQL query
* This is meant to execute queries that cannot be handled via the query builder
* it should not be used for SELECT, UPDATE, DELETE or INSERT queries,
* these can be handled via their respective objects
*/
public static function SQL($sql) {
try {
$db=static::getHandle();
$stmt=$db->prepare($sql);
$stmt->execute();
} catch (PDOException $e) {
echo $e->getMessage() . "\n";
log::msg("SQL failed", log::FATAL, log::DB);
}
}

}

2 changes: 1 addition & 1 deletion php/classes/pageset.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function getPageCount() {
$qry->addFunction(array("count" => "COUNT(page_id)"));
$qry->where(new clause("pageset_id=:pagesetid"));
$qry->addParam(new param(":pagesetid", $this->getId(), PDO::PARAM_INT));
return static::getCountFromQuery($qry);
return $qry->getCount();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions php/classes/person.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function removePhoto(photo $photo) {
$qry->where($where);
$qry->addParams($params);

$result=query($qry)->fetch(PDO::FETCH_ASSOC);
$result=db::query($qry)->fetch(PDO::FETCH_ASSOC);
$pos=$result["position"];

$qry=new delete("photo_people");
Expand Down Expand Up @@ -452,7 +452,7 @@ public function getDetails() {

$qry->where($where);

$result=query($qry);
$result=db::query($qry);
if ($result) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
Expand Down
6 changes: 3 additions & 3 deletions php/classes/photo.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function getLastPersonPos() {
$qry->addFunction(array("pos" => "max(position)"));
$qry->where(new clause("photo_id=:photoid"));
$qry->addParam(new param(":photoid", (int) $this->getId(), PDO::PARAM_INT));
$result=query($qry)->fetch(PDO::FETCH_ASSOC);
$result=db::query($qry)->fetch(PDO::FETCH_ASSOC);
return (int) $result["pos"];
}

Expand Down Expand Up @@ -521,7 +521,7 @@ public function getImageTag($type = null) {

list($width, $height, $filetype, $size)=getimagesize($file);

$alt = escape_string($this->get("title"));
$alt = e($this->get("title"));

return new block("img", array(
"src" => $image_href,
Expand Down Expand Up @@ -1544,7 +1544,7 @@ public static function getFields() {
public static function getTotalSize() {
$qry=new select(array("p" => "photos"));
$qry->addFunction(array("total" => "sum(size)"));
return static::getCountFromQuery($qry);
return $qry->getCount();
}

/**
Expand Down
27 changes: 20 additions & 7 deletions php/classes/place.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,36 @@ public function delete() {
$qry->addParam($locid);
$qry->addParam($locidNull);

query($qry, "Could not remove references:");
try {
db::query($qry);
} catch (PDOException $e) {
log::msg("Could not remove references", log::FATAL, log::DB);
}


$qry=new update(array("ppl" => "people"));
$qry->where(new clause("home_id=:locid"));
$qry->addSet("home_id", "locidnull");
$qry->addParam($locid);
$qry->addParam($locidNull);

query($qry, "Could not remove references:");
try {
db::query($qry);
} catch (PDOException $e) {
log::msg("Could not remove references", log::FATAL, log::DB);
}

$qry=new update(array("ppl" => "people"));
$qry->where(new clause("work_id=:locid"));
$qry->addSet("work_id", "locidnull");
$qry->addParam($locid);
$qry->addParam($locidNull);

query($qry, "Could not remove references:");
try {
db::query($qry);
} catch (PDOException $e) {
log::msg("Could not remove references", log::FATAL, log::DB);
}

parent::delete();
}
Expand Down Expand Up @@ -287,7 +300,7 @@ public function getPhotoCount() {
}
$qry->where($where);

return photo::getCountFromQuery($qry);
return $qry->getCount();
}

/**
Expand All @@ -311,7 +324,7 @@ public function getTotalPhotoCount() {
}
$qry->where($where);

return static::getCountFromQuery($qry);
return $qry->getCount();
}

/**
Expand Down Expand Up @@ -398,7 +411,7 @@ public function getDetails() {
$qry->where($where);


$result=query($qry);
$result=db::query($qry);
if ($result) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
Expand Down Expand Up @@ -579,7 +592,7 @@ public static function getCount() {
$qry->addFunction(array("count" => "COUNT(DISTINCT location_id)"));
list($qry, $where)=selectHelper::expandQueryForUser($qry);
$qry->where($where);
return static::getCountFromQuery($qry);
return $qry->getCount();

}
}
Expand Down

1 comment on commit 0ed92c0

@jeroenrnl
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should reference issue #20

Please sign in to comment.