-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic framework for PDO migration created
Created a basic framework consisting of a class db and a class query. #20
- Loading branch information
Showing
5 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;") | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . ";"; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters