Skip to content

Commit

Permalink
Basic framework for PDO migration created
Browse files Browse the repository at this point in the history
Created a basic framework consisting of a class db and a class query.

#20
  • Loading branch information
jeroenrnl committed Apr 26, 2014
1 parent c0e0143 commit 46df67f
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
67 changes: 67 additions & 0 deletions php/UnitTests/PDOdatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Test the database classes
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package ZophUnitTest
* @author Jeroen Roos
*/

/**
* Test class that tests the database classes
* @package ZophUnitTest
* @author Jeroen Roos
*/
class PDOdatabaseTest extends ZophDataBaseTestCase {
/**
* Create queries
* @dataProvider getQueries();
*/
public function testCreateQuery($table, $fields, $exp_sql) {
if(is_array($fields)) {
$sql=(string) new query($table, $fields);
} else {
$sql=(string) new query($table);
}
$this->assertEquals($exp_sql, $sql);
}

/**
* Run queries
* @dataProvider getQueries();
*/
public function testRunQuery($table, $fields, $exp_sql) {
// not used
$exp_sql=null;
if(is_array($fields)) {
$result=db::query(new query($table, $fields));
} else {
$result=db::query(new query($table));
}
$this->assertInstanceOf("PDOStatement", $result);
}

public function getQueries() {
return array(
array("photos", array("photo_id"), "SELECT zoph_photos.photo_id FROM zoph_photos;"),
array("photos", null, "SELECT * FROM zoph_photos;"),
array("photos", array("photo_id", "name"),
"SELECT zoph_photos.photo_id, zoph_photos.name FROM zoph_photos;")
);
}

}
1 change: 1 addition & 0 deletions php/UnitTests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<testsuites>
<testsuite name="Zoph">
<file>php/UnitTests/XMLdataTest.php</file>
<file>php/UnitTests/PDOdatabaseTest.php</file>
<file>php/UnitTests/userTest.php</file>
<file>php/UnitTests/anonymousUserTest.php</file>
<file>php/UnitTests/albumTest.php</file>
Expand Down
116 changes: 116 additions & 0 deletions php/classes/db.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Database connection class
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/

/**
* The db object is used to connect to the database
* Example code:
* $qry=new query("photos", array("photo_id", "name"));
* var_dump(db::query($qry));
*
* @package Zoph
* @author Jeroen Roos
*/
class db {

private static $connection=false;

private static $dbhost;
private static $dbname;
private static $dbuser;
private static $dbpass;
private static $dbprefix;

/**
* Make database connection
* @param string DSN
* @param string username
* @param string password
*/
private function __construct($dsn, $dbuser, $dbpass) {
self::$connection=new PDO($dsn,$dbuser,$dbpass);
self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

/**
* Get handle to database
* Make connection first, if it has not been made
*/
public static function getHandle() {
if(!self::$connection) {
self::connect();
}
return self::$connection;
}

/**
* Set login details
* @param string database hostname
* @param string database name
* @param string database user
* @param string database password
* @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;
}

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

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

/**
* Get the Data Source Name for the database connection
* Currently hardcoded to MySQL, in the future this might change
*/

private static function getDSN() {
$db="mysql";

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

public static function query(query $query) {
$db=self::getHandle();
return $db->query($query);
}
}

60 changes: 60 additions & 0 deletions php/classes/query.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Database query class
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/

/**
* The query object is used to create queries
*
* @package Zoph
* @author Jeroen Roos
*/
class query {

private $table;
private $fields=null;

public function __construct($table, array $fields=null) {
$table=db::getPrefix() . $table;
if(is_array($fields)) {
foreach ($fields as $field) {
$this->fields[]=$table . "." . $field;
}
}
$this->table=$table;

}

public function __toString() {
$sql = "SELECT ";

if(is_array($this->fields)) {
$sql.=implode(", ", $this->fields);
} else {
$sql.="*";
}

$sql .= " FROM " . $this->table;

return $sql . ";";
}
}

8 changes: 8 additions & 0 deletions php/settings.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ public static function parseINI($i) {
define("DB_USER", $i["db_user"]);
define("DB_PASS", $i["db_pass"]);
define("DB_PREFIX", $i["db_prefix"]);

db::setLoginDetails(
$i["db_host"],
$i["db_name"],
$i["db_user"],
$i["db_pass"],
$i["db_prefix"]
);
return true;
}
}
Expand Down

0 comments on commit 46df67f

Please sign in to comment.