aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2010-07-15 23:51:05 +0300
committerFilipp Lepalaan <filipp@mac.com>2010-07-15 23:51:05 +0300
commitad4a149c32bd42bf374b56c88cdf43b5ee801874 (patch)
tree98dfdd80228531b9c25b23a57607954f0aa715d1
parentb4cd2ad59440171ea330ddaad28b4743d4936b51 (diff)
downloadmain-ad4a149c32bd42bf374b56c88cdf43b5ee801874.tar.gz
main-ad4a149c32bd42bf374b56c88cdf43b5ee801874.tar.bz2
main-ad4a149c32bd42bf374b56c88cdf43b5ee801874.zip
sqlite support
-rw-r--r--MainApp.php11
-rw-r--r--MainController.php53
-rw-r--r--MainDb.php33
3 files changed, 71 insertions, 26 deletions
diff --git a/MainApp.php b/MainApp.php
index 96050b0..5a149a8 100644
--- a/MainApp.php
+++ b/MainApp.php
@@ -131,7 +131,8 @@ class MainApp
static function error($msg)
{
$err = array('error' => $msg);
- trigger_error($msg, E_USER_ERROR);
+ self::json($err);
+ trigger_error($msg, E_USER_NOTICE);
// And log it locally
self::log($msg);
}
@@ -280,8 +281,7 @@ class MainApp
// @return mixed false if cancelled or output of $function
static function auth($callback, $realm = 'Default')
{
- if (!isset($_SERVER['PHP_AUTH_USER']))
- {
+ if (!isset($_SERVER['PHP_AUTH_USER'])) {
header(sprintf('WWW-Authenticate: Basic realm="%s"', $realm));
header('HTTP/1.0 401 Unauthorized');
return false;
@@ -295,9 +295,8 @@ class MainApp
public function js($string)
{
header('Content-Type: text/javascript');
- print '<script type="text/javascript" charset="utf-8">
- ' . $string . '
- </script>';
+ header('Content-Length: '.strlen($string));
+ echo '<script type="text/javascript" charset="utf-8">'.$string.'</script>';
}
}
diff --git a/MainController.php b/MainController.php
index 81c68bf..4eea176 100644
--- a/MainController.php
+++ b/MainController.php
@@ -20,21 +20,18 @@ class MainController
// create controller object
function __construct($id = null)
{
- // child classes should always have the same name as their tables
+ // child classes typically have the same name as their tables
+ // but not always
$this->class = get_class($this);
- $this->table = eval("return {$this->class}::TableName;");
- $this->mainView = new MainView();
+// $this->table = eval("return {$this->class}::TableName;");
+ $this->table = static::TableName;
// table name not defined, default to class name
if (!$this->table) {
$this->table = strtolower($this->class);
}
-
- // populate indices
- $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
- foreach ($schema as $s) {
- $this->data[$s['Field']] = $s['Default'];
- }
+
+ $this->mainView = new MainView();
if ($id) {
return $this->get($id);
@@ -44,6 +41,25 @@ class MainController
}
+ public function init()
+ {
+ // populate indices
+ if ($_SESSION['config']['db.driver'] == 'mysql') {
+ $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
+ foreach ($schema as $s) {
+ $this->data[$s['Field']] = $s['Default'];
+ }
+ return $schema;
+ }
+ if ($_SESSION['config']['db.driver'] == 'sqlite') {
+ $sql = 'PRAGMA TABLE_INFO('.$this->table.')';
+ $schema = MainDb::fetch($sql);
+ foreach ($schema as $s) {
+ $this->data[$s['name']] = '';
+ }
+ }
+ }
+
////
// Get One Thing
public function get($where)
@@ -146,8 +162,12 @@ class MainController
{
$row = $result[$i];
$this->data[$i] = $row;
- $this->find_parent($row, $i);
- $this->find_children($row, $i);
+ if (static::ForeignKey) {
+ $this->find_parent($row, $i);
+ }
+ if (static::HasMany) {
+ $this->find_children($row, $i);
+ }
}
return $this->data;
@@ -248,8 +268,11 @@ class MainController
$insert = '';
$values = array();
- foreach($data as $k => $v)
- {
+ if (@empty($data['id'])) {
+ unset($data['id']);
+ }
+
+ foreach($data as $k => $v) {
$insert .= "`{$k}`, ";
$values[":{$k}"] = $v;
}
@@ -270,6 +293,10 @@ class MainController
return MainApp::error('Cannot delete without arguments');
}
+ if (!is_array($where)) {
+ $where = array('id' => $where);
+ }
+
list($key, $value) = each($where);
if ($limit) {
diff --git a/MainDb.php b/MainDb.php
index e672651..6417c19 100644
--- a/MainDb.php
+++ b/MainDb.php
@@ -21,13 +21,20 @@ class MainDb
if (!self::$instance)
{
try {
- self::$instance = new PDO(
- "{$c['db.driver']}:host={$c['db.host']};dbname={$c['db.name']}",
- $c['db.username'], $c['db.password'], array(PDO::ATTR_PERSISTENT => true)
- );
-
- // always use UTF-8?
- self::$instance->query('SET NAMES utf8');
+ switch ($c['db.driver']) {
+ case 'mysql':
+ self::$instance = new PDO(
+ "{$c['db.driver']}:host={$c['db.host']};dbname={$c['db.name']}",
+ $c['db.username'], $c['db.password'], array(PDO::ATTR_PERSISTENT => true)
+ );
+ // always use UTF-8?
+ self::$instance->query('SET NAMES utf8');
+ break;
+
+ case 'sqlite':
+ self::$instance = new PDO('sqlite:'.$c['db.path']);
+ break;
+ }
} catch (PDOException $e) {
exit(MainApp::error($e->getMessage()));
@@ -64,6 +71,13 @@ class MainDb
$pdo = self::getInstance();
$stmt = $pdo->prepare($sql);
+
+ if (!$stmt) {
+ list($ec, $dec, $emsg) = $pdo->errorInfo();
+ $error = $emsg ."\n" . print_r(debug_backtrace(), true);
+ return MainApp::error($error);
+ }
+
$result = $stmt->execute($data);
if (!$result) {
@@ -88,6 +102,11 @@ class MainDb
return $stmt;
}
+ // describe statements need the query results
+ if (preg_match('/^PRAGMA/i', $sql)) {
+ return $stmt;
+ }
+
if (empty($data[':id'])) {
$data[':id'] = $pdo->lastInsertId();
}