Skip to content
Browse files

added mysql support

some clean-up of db code
  • Loading branch information...
1 parent a53b575 commit a9f10f391d68883255c78dfb2285f584f93875f7 @rgigger committed Mar 1, 2008
View
7 examples/DbFunctions/config.php
@@ -1,10 +1,3 @@
<?php
define('zoop_dir', '../../framework');
define('app_dir', dirname(__file__));
-
-/*
-// db config
-define('db_driver', 'pdo');
-define('db_rdbms', 'sqlite');
-define('db_file', app_dir . '/data.db');
-*/
View
3 examples/mysql/config.php
@@ -0,0 +1,3 @@
+<?php
+define('zoop_dir', '../../framework');
+define('app_dir', dirname(__file__));
View
7 examples/mysql/config.yaml
@@ -0,0 +1,7 @@
+zoop:
+ db:
+ default:
+ driver: php_mysql
+ database: mysql_test
+ username: mysql_test
+
View
10 examples/mysql/index.php
@@ -0,0 +1,10 @@
+<?php
+include('config.php');
+include(zoop_dir . '/Zoop.php');
+Zoop::loadLib('db');
+
+$map = SqlFetchSimpleMap('select * from test', 'name', 'value', array());
+echo_r($map);
+
+$rows = SqlFetchRows('select * from test', array());
+echo_r($rows);
View
17 examples/mysql/init.sql
@@ -0,0 +1,17 @@
+begin;
+
+create database mysql_test;
+create user mysql_test;
+grant all privileges on mysql_test.* to mysql_test@localhost;
+
+create table test
+(
+ id int auto_increment primary key,
+ name text,
+ value text
+);
+
+insert into test (name, value) values ('one', 'first');
+insert into test (name, value) values ('two', 'second');
+
+commit;
View
37 framework/db/DbConnection.php
@@ -1,24 +1,27 @@
<?php
abstract class DbConnection
{
- var $params;
- var $types;
- var $conn;
- var $echo;
+ protected $params;
+ private $queryParams;
+ private $types;
+ private $conn;
+ private $echo;
function __construct($params, $connectionName)
{
- $this->validateParams($params, $connectionName);
+ $this->params = $params;
+ $this->validateParams($connectionName);
$echo = false;
+ $this->init();
}
- function validateParams($params, $connectionName)
+ function validateParams($connectionName)
{
// handle the required fields
$missing = array();
foreach($this->getRequireds() as $thisRequired)
{
- if(!isset($params[$thisRequired]))
+ if(!isset($this->params[$thisRequired]))
$missing[] = $thisRequired;
}
@@ -28,11 +31,13 @@ function validateParams($params, $connectionName)
// handle the defaults
foreach($this->getDefaults() as $name => $value)
{
- if(!isset($params[$name]))
- $params[$name] = $value;
+ if(!isset($this->params[$name]))
+ $this->params[$name] = $value;
}
-
- return $params;
+ }
+
+ protected function init()
+ {
}
function getRequireds()
@@ -85,11 +90,11 @@ function alterSchema($sql)
function query($sql, $params)
{
// do all of the variable replacements
- $this->params = array();
+ $this->queryParams = array();
foreach($params as $key => $value)
{
$parts = explode(':', $key);
- $this->params[$parts[0]] = $value;
+ $this->queryParams[$parts[0]] = $value;
if(isset($parts[1]))
$this->types[$parts[0]] = $parts[1];
}
@@ -121,13 +126,13 @@ function queryCallback($matches)
switch($type)
{
case 'string':
- $replaceString = $this->escapeString($this->params[$name]);
+ $replaceString = $this->escapeString($this->queryParams[$name]);
break;
case 'int':
- $replaceString = (int)$this->params[$name];
+ $replaceString = (int)$this->queryParams[$name];
break;
case 'keyword':
- $replaceString = $this->params[$name];
+ $replaceString = $this->queryParams[$name];
break;
default:
trigger_error("unknown param type: " . $type);
View
2 framework/db/DbFactory.php
@@ -2,7 +2,7 @@
// this all needs to be thought out better and redone
class DbFactory
{
- private static $classMap = array('php_pgsql' => 'DbPgsql', 'pdo' => 'DbPdo');
+ private static $classMap = array('php_pgsql' => 'DbPgsql', 'php_mysql' => 'DbMysql', 'pdo' => 'DbPdo');
static function getConnection($params, $name)
{
View
4 framework/db/DbModule.php
@@ -22,8 +22,8 @@ function getIncludes()
function getClasses()
{
- return array('DbConnection', 'DbPgResult', 'DbPgsql', 'DbPdo',
- 'DbPdoResult', 'DbObject', 'DbFactory', 'DbSchema');
+ return array('DbConnection', 'DbFactory', 'DbSchema', 'DbObject',
+ 'DbPdo', 'DbPdoResult', 'DbPgsql', 'DbPgResult', 'DbMysql', 'DbMysqlResult');
}
function configure()
View
41 framework/db/DbMysql.php
@@ -0,0 +1,41 @@
+<?php
+class DbMysql extends DbConnection
+{
+ private $connection;
+
+ public function getRequireds()
+ {
+ return array('database', 'username');
+ }
+
+ public function getDefaults()
+ {
+ return array('host' => 'localhost', 'port' => 3306, 'password' => NULL);
+ }
+
+ function escapeString($string)
+ {
+ return "'" . mysql_real_escape_string($string, $this->connection) . "'";
+ }
+
+ function _query($sql)
+ {
+ // lazy connection to the database
+ if(!$this->connection)
+ {
+ $this->connection = mysql_connect($this->params['host'], $this->params['username'], $this->params['password']);
+ mysql_select_db($this->params['database'], $this->connection)
+ or trigger_error(mysql_error());;
+ }
+
+ $result = mysql_query($sql, $this->connection)
+ or trigger_error(mysql_error());
+
+ return new DbMysqlResult($result);
+ }
+
+ function getLastInsertId()
+ {
+ return mysql_insert_id($this->connection);
+ }
+}
View
59 framework/db/DbMysqlResult.php
@@ -0,0 +1,59 @@
+<?php
+class DbMysqlResult
+{
+ var $res;
+ var $cur;
+ var $max;
+
+ function __construct($res)
+ {
+ $this->res = $res;
+ $this->cur = 0;
+ $this->max = mysql_num_rows($this->res) - 1;
+ }
+
+ function numRows()
+ {
+ return $this->max + 1;
+ }
+
+ function rewind()
+ {
+ $this->cur = 0;
+ }
+
+ function current()
+ {
+ if($this->max == -1)
+ return false;
+ mysql_data_seek($this->res, $this->cur);
+ return mysql_fetch_assoc($this->res);
+ }
+
+ function key()
+ {
+ return $this->cur;
+ }
+
+ function next()
+ {
+ $this->cur++;
+ if($this->cur > $this->max)
+ return false;
+ mysql_data_seek($this->res, $this->cur);
+ return mysql_fetch_assoc($this->res);
+ }
+
+ function valid()
+ {
+ if($this->cur > $this->max)
+ return false;
+
+ return true;
+ }
+
+ function affectedRows()
+ {
+ return mysql_affected_rows($this->res);
+ }
+}
View
49 framework/db/DbPdo.php
@@ -1,32 +1,23 @@
<?php
class DbPdo extends DbConnection
{
- function DbPdo($params)
+ function init()
{
- if($params['file'][0] != '/')
- $params['file'] = app_dir . '/var/' . $params['file'];
+ if($this->params['file'][0] != '/')
+ $this->params['file'] = app_dir . '/var/' . $this->params['file'];
- $this->conn = new PDO('sqlite:' . $params['file']);
- // try {
- // $this->conn = new PDO('sqlite:' . $params['file']);
- // }
- // catch(PDOException $e)
- // {
- // die('pdoexception');
- // }
+ $this->conn = new PDO('sqlite:' . $this->params['file']);
+ $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+ $this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NAMED);
}
function escapeString($string)
{
return $this->conn->quote($string);
- //return "'$string'";
}
function _query($sql)
{
- // echo $sql . '<br>';
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- $this->conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_NAMED);
$result = $this->conn->query($sql);
return new DbPdoResult($result);
}
@@ -36,31 +27,3 @@ function getLastInsertId()
return $this->conn->lastInsertId();
}
}
-
-/*
-try {
- $dbh = new PDO($params['file']);
- foreach($dbh->query('SELECT * from foo') as $row)
- {
- print_r($row);
- }
- $dbh = null;
-}
-catch (PDOException $e) {
- print "Error!: " . $e->getMessage() . "<br/>";
- die();
-}
-
-die();
-*/
-
-/*
-$connString = 'dbname=' . $params['database'];
-$connString .= ' user=' . $params['username'];
-if(isset($params['host']))
- $connString .= ' host=' . $params['host'];
-if(isset($params['port']))
- $connString .= ' port=' . $params['port'];
-
-$this->conn = pg_connect($connString, PGSQL_CONNECT_FORCE_NEW);
-*/
View
2 framework/db/DbPdoResult.php
@@ -61,4 +61,4 @@ function affectedRows()
return 0;
return $this->res->rowCount();
}
-}
+}
View
2 framework/db/DbPgResult.php
@@ -54,4 +54,4 @@ function affectedRows()
{
return pg_affected_rows($this->res);
}
-}
+}
View
34 framework/db/DbPgsql.php
@@ -4,41 +4,27 @@ class DbPgsql extends DbConnection
private $connectionString;
private $connection;
- function __construct($params, $name)
+ public function init()
{
- // the parent contructor will validate all of the paramaters
- // inlcuding handling error messages to the user for missing config options
- // and filling in default params
- parent::__construct($params, $name);
-
// create the connection string
- $connString = 'dbname=' . $params['database'];
- $connString .= ' user=' . $params['username'];
+ $connString = 'dbname=' . $this->params['database'];
+ $connString .= ' user=' . $this->params['username'];
if(isset($params['host']))
- $connString .= ' host=' . $params['host'];
+ $connString .= ' host=' . $this->params['host'];
if(isset($params['port']))
- $connString .= ' port=' . $params['port'];
+ $connString .= ' port=' . $this->params['port'];
$this->connectionString = $connString;
}
- /*
-
- function DbPgsql($params)
+
+ public function getRequireds()
{
- $connString = 'dbname=' . $params['database'];
- $connString .= ' user=' . $params['username'];
- if(isset($params['host']))
- $connString .= ' host=' . $params['host'];
- if(isset($params['port']))
- $connString .= ' port=' . $params['port'];
-
- $this->conn = pg_connect($connString, PGSQL_CONNECT_FORCE_NEW);
+ return array('database', 'username');
}
- */
function escapeString($string)
{
- return "'$string'";
+ return "'" . pg_escape_string($this->connection, $string) . "'";
}
function _query($sql)
@@ -55,4 +41,4 @@ function getLastInsertId()
{
return $this->fetchCell("select lastval()", array());
}
-}
+}
View
2 update_api_docs.sh
@@ -0,0 +1,2 @@
+#!/bin/bash
+phpdoc -d framework -t doc/api

0 comments on commit a9f10f3

Please sign in to comment.
Something went wrong with that request. Please try again.