Permalink
Browse files

namespace implemented

  • Loading branch information...
mstdokumaci committed Jun 30, 2012
1 parent 66d4758 commit 4e498764fbb6f23d8b04ca38259a92d3597c1ea6
Showing with 430 additions and 430 deletions.
  1. +16 −16 lib/adb.php
  2. +4 −4 lib/cache.php
  3. +62 −62 lib/db.php
  4. +108 −108 lib/ddl.php
  5. +236 −236 lib/item.php
  6. +4 −4 sample/social/config.php
View
@@ -1,4 +1,4 @@
-<?php
+<?php namespace arraydb;
require_once('cache.php');
require_once('db.php');
@@ -78,7 +78,7 @@ static function init ($DM) {
static function get_instance () {
if (!isset(self::$instance))
- throw new Exception('You have to initialize this class before using');
+ throw new \Exception('You have to initialize this class before using');
return self::$instance;
}
@@ -89,7 +89,7 @@ function create_tables () {
}
function load ($name, $id) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
if (isset($this->ROW[$name][$id]))
return new ITEM($name, $this->DM[$name], $id, $this->ROW[$name][$id]);
@@ -98,7 +98,7 @@ function load ($name, $id) {
}
function create ($name, $data) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$item_model=$this->DM[$name];
@@ -128,7 +128,7 @@ function create ($name, $data) {
}
function delete ($name, $id, $delete_belongings=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$item_model=$this->DM[$name];
@@ -170,7 +170,7 @@ function delete ($name, $id, $delete_belongings=false) {
}
function relate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
if (empty($m2m)) return $this->self_relate($name, $local_name, $id1, $id2);
@@ -189,7 +189,7 @@ function relate ($name, $local_name, $id1, $id2) {
}
function unrelate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$m2m=array_shift(array_filter($this->DM[$name]['many_to_many'], function ($m2m) use ($local_name) {return $m2m['local_name']==$local_name;}));
if (empty($m2m)) return $this->self_unrelate($name, $local_name, $id1, $id2);
@@ -205,10 +205,10 @@ function unrelate ($name, $local_name, $id1, $id2) {
}
private function self_relate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
- throw new Exception('No defined relation to relate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
+ throw new \Exception('No defined relation to relate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
$item1=$this->load($name, $id1);
$item2=$this->load($name, $id2);
@@ -224,10 +224,10 @@ private function self_relate ($name, $local_name, $id1, $id2) {
}
private function self_unrelate ($name, $local_name, $id1, $id2) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
if (!(in_array($local_name, $this->DM[$name]['self_ref'])))
- throw new Exception('No defined relation to unrelate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
+ throw new \Exception('No defined relation to unrelate ' . $name . ' (' . $id1 . ') to ' . $local_name . ' (' . $id2 . ')');
$condition="(`" . $name . "1`='" . $id1 . "' AND `" . $name . "2`='" . $id2 . "') OR (`" . $name . "1`='" . $id2 . "' AND `" . $name . "2`='" . $id1 . "')";
$this->db->delete($local_name, $condition);
@@ -240,10 +240,10 @@ private function self_unrelate ($name, $local_name, $id1, $id2) {
}
function find_unique ($name, $field, $value) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
if (!isset($this->DM[$name]['fields'][$field]))
- throw new Exception('No field as ' . $field . ' found for item ' . $name);
+ throw new \Exception('No field as ' . $field . ' found for item ' . $name);
$sql="SELECT * FROM `" . $name . "` WHERE `" . $field . "`='" . $this->db->escape($value) . "'";
$result=$this->db->select($sql);
@@ -314,7 +314,7 @@ function id_list_join ($name, $table, $condition=false, $order=false, $limit=fal
}
private function prepare_select ($name, $condition=false, $order=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$item_model=$this->DM[$name];
$sql='SELECT * FROM `' . $name . '`';
@@ -326,7 +326,7 @@ private function prepare_select ($name, $condition=false, $order=false) {
}
private function prepare_join_select ($name, $table, $condition=false, $order=false) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$item_model=$this->DM[$name];
$sql='SELECT `' . $name . '`.* FROM `' . $name . '`, `' . $table . '`';
@@ -338,7 +338,7 @@ private function prepare_join_select ($name, $table, $condition=false, $order=fa
}
private function prepare_order ($name, $order) {
- if (!isset($this->DM[$name])) throw new Exception('Undefined item name: ' . $name);
+ if (!isset($this->DM[$name])) throw new \Exception('Undefined item name: ' . $name);
$item_model=$this->DM[$name];
foreach (explode(',', $order) as $p) {
View
@@ -1,4 +1,4 @@
-<?php
+<?php namespace arraydb;
class CACHE {
private static $instance;
@@ -10,15 +10,15 @@ function __construct ($conf) {
switch ($this->type) {
case 'apc':
if (!function_exists('apc_fetch'))
- throw new Exception('APC extension is not installed');
+ throw new \Exception('APC extension is not installed');
break;
case 'memcached':
$this->conn=memcache_connect($conf['host'], $conf['port'], $conf['timeout']);
break;
case 'file':
$this->path=$conf['path'];
if (!(is_readable($this->path) && is_writable($this->path)))
- throw new Exception($this->path . ' directory must be readable and writable');
+ throw new \Exception($this->path . ' directory must be readable and writable');
break;
default:
$this->type='no-cache';
@@ -32,7 +32,7 @@ static function init ($conf=false) {
static function get_instance () {
if (!isset(self::$instance))
- throw new Exception('You have to initialize this class before using');
+ throw new \Exception('You have to initialize this class before using');
return self::$instance;
}
View
@@ -1,63 +1,63 @@
-<?php
-
- class DB {
- private static $instance;
- private $conn;
-
- function __construct ($conf) {
- if (!$this->conn=mysqli_connect($conf['hostname'], $conf['username'], $conf['password'], $conf['database']))
- throw new Exception('MySQL connection unsuccessfull. Config: ' . json_encode($conf) . ', Error: ' . mysqli_connect_error());
- }
-
- static function init ($conf) {
- self::$instance=new DB ($conf);
- }
-
- static function get_instance () {
- if (!isset(self::$instance))
- throw new Exception('You have to initialize this class before using');
- return self::$instance;
- }
-
- function insert ($table, $data) {
- foreach ($data as $k=>$v) $data[$k]="`" . $k . "`='" . $this->escape($v) . "'";
- if (!mysqli_query($this->conn, "INSERT INTO `" . $table . "` SET " . implode(', ', $data)))
- throw new Exception('MySQL insert query error: ' . mysqli_error($this->conn));
- return mysqli_insert_id($this->conn);
- }
-
- function update ($table, $data, $condition='TRUE') {
- foreach ($data as $k=>$v) $data[$k]="`" . $k . "`='" . $this->escape($v) . "'";
- if (!mysqli_query($this->conn, "UPDATE `" . $table . "` SET " . implode(', ', $data) . " WHERE " . $condition))
- throw new Exception('MySQL update query error: ' . mysqli_error($this->conn));
- }
-
- function delete ($table, $condition) {
- if (!mysqli_query($this->conn, "DELETE FROM `" . $table . "` WHERE " . $condition))
- throw new Exception('MySQL delete query error: ' . mysqli_error($this->conn));
- }
-
- function table ($sql) {
- if (!mysqli_query($this->conn, $sql))
- throw new Exception('MySQL query error: ' . mysqli_error($this->conn));
- }
-
- function select ($sql) {
- if (!$result=mysqli_query($this->conn, $sql))
- throw new Exception('MySQL select query error: ' . mysqli_error($this->conn));
-
- $all=array();
- while ($row=mysqli_fetch_assoc($result)) $all[]=$row;
- return $all;
- }
-
- function count ($sql) {
- if (!$result=mysqli_query($this->conn, $sql))
- throw new Exception('MySQL select query error: ' . mysqli_error($this->conn));
- return mysqli_num_rows($result, MYSQLI_ASSOC);
- }
-
- function escape ($value) {
- return mysqli_real_escape_string($this->conn, $value);
- }
+<?php namespace arraydb;
+
+ class DB {
+ private static $instance;
+ private $conn;
+
+ function __construct ($conf) {
+ if (!$this->conn=mysqli_connect($conf['hostname'], $conf['username'], $conf['password'], $conf['database']))
+ throw new \Exception('MySQL connection unsuccessfull. Config: ' . json_encode($conf) . ', Error: ' . mysqli_connect_error());
+ }
+
+ static function init ($conf) {
+ self::$instance=new DB ($conf);
+ }
+
+ static function get_instance () {
+ if (!isset(self::$instance))
+ throw new \Exception('You have to initialize this class before using');
+ return self::$instance;
+ }
+
+ function insert ($table, $data) {
+ foreach ($data as $k=>$v) $data[$k]="`" . $k . "`='" . $this->escape($v) . "'";
+ if (!mysqli_query($this->conn, "INSERT INTO `" . $table . "` SET " . implode(', ', $data)))
+ throw new \Exception('MySQL insert query error: ' . mysqli_error($this->conn));
+ return mysqli_insert_id($this->conn);
+ }
+
+ function update ($table, $data, $condition='TRUE') {
+ foreach ($data as $k=>$v) $data[$k]="`" . $k . "`='" . $this->escape($v) . "'";
+ if (!mysqli_query($this->conn, "UPDATE `" . $table . "` SET " . implode(', ', $data) . " WHERE " . $condition))
+ throw new \Exception('MySQL update query error: ' . mysqli_error($this->conn));
+ }
+
+ function delete ($table, $condition) {
+ if (!mysqli_query($this->conn, "DELETE FROM `" . $table . "` WHERE " . $condition))
+ throw new \Exception('MySQL delete query error: ' . mysqli_error($this->conn));
+ }
+
+ function table ($sql) {
+ if (!mysqli_query($this->conn, $sql))
+ throw new \Exception('MySQL query error: ' . mysqli_error($this->conn));
+ }
+
+ function select ($sql) {
+ if (!$result=mysqli_query($this->conn, $sql))
+ throw new \Exception('MySQL select query error: ' . mysqli_error($this->conn));
+
+ $all=array();
+ while ($row=mysqli_fetch_assoc($result)) $all[]=$row;
+ return $all;
+ }
+
+ function count ($sql) {
+ if (!$result=mysqli_query($this->conn, $sql))
+ throw new \Exception('MySQL select query error: ' . mysqli_error($this->conn));
+ return mysqli_num_rows($result, MYSQLI_ASSOC);
+ }
+
+ function escape ($value) {
+ return mysqli_real_escape_string($this->conn, $value);
+ }
}
Oops, something went wrong.

0 comments on commit 4e49876

Please sign in to comment.