Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

namespace implemented

  • Loading branch information...
commit 4e498764fbb6f23d8b04ca38259a92d3597c1ea6 1 parent 66d4758
@mstdokumaci authored
View
32 lib/adb.php
@@ -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
8 lib/cache.php
@@ -1,4 +1,4 @@
-<?php
+<?php namespace arraydb;
class CACHE {
private static $instance;
@@ -10,7 +10,7 @@ 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']);
@@ -18,7 +18,7 @@ function __construct ($conf) {
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
124 lib/db.php
@@ -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);
+ }
}
View
216 lib/ddl.php
@@ -1,108 +1,108 @@
-<?php
-
- class DDL {
- private $DM, $table;
- private $db;
-
- function __construct ($DM) {
- $this->db=DB::get_instance();
- $this->DM=$DM;
- }
-
- function create_tables () {
- foreach ($this->DM as $name=>$table)
- $this->table[$name]=$this->prepare_table($name, $table);
-
- foreach ($this->table as $name=>$table) {
- $sql="DROP TABLE IF EXISTS `" . $name . "`";
- $this->db->table($sql);
- $sql="CREATE TABLE `" . $name . "` (" . implode(', ', array_merge($table['fields'], $table['keys'])) . ") ENGINE=InnoDB";
- $this->db->table($sql);
- }
- }
-
- private function prepare_table ($name, $table) {
- $conf=$table['conf'];
- $db_table['fields'][]='id ' . $this->get_field_type('numeric', $conf['len']) . ' NOT NULL AUTO_INCREMENT';
- $db_table['keys'][]='PRIMARY KEY (id)';
-
- $db_table['fields'][]="create_date INT(11) UNSIGNED NOT NULL DEFAULT '0'";
- $db_table['keys'][]='KEY (create_date)';
- $db_table['fields'][]="update_date INT(11) UNSIGNED NOT NULL DEFAULT '0'";
- $db_table['keys'][]='KEY (update_date)';
-
- foreach ($table['fields'] as $field_name=>$field) {
- list($db_field, $db_key)=$this->prepare_field($field_name, $field);
- $db_table['fields'][]=$db_field;
- if (!empty($db_key)) $db_table['keys'][]=$db_key;
- }
-
- foreach ($table['many_to_many'] as $m2m) {
- if (isset($this->table[$m2m['relation_name']]['created'])) {continue;}
- $this->table[$m2m['relation_name']]['created']=true;
-
- $this->table[$m2m['relation_name']]['fields'][]='`' . $m2m['foreign_name'] . '` ' . $this->get_field_type('numeric', $conf['len']) . " NOT NULL DEFAULT '0'";
- $this->table[$m2m['relation_name']]['fields'][]='`' . $m2m['local_name'] . '` ' . $this->get_field_type('numeric', $this->DM[$m2m['type']]['conf']['len']) . " NOT NULL DEFAULT '0'";
- $this->table[$m2m['relation_name']]['keys'][]='PRIMARY KEY (`' . $m2m['foreign_name'] . '`, `' . $m2m['local_name'] . '`)';
- }
-
- foreach ($table['self_ref'] as $self_ref) {
- $type=$this->get_field_type('numeric', $conf['len']);
- $this->table[$self_ref]['fields'][]='`' . $name . '1` ' . $type . " NOT NULL DEFAULT '0'";
- $this->table[$self_ref]['fields'][]='`' . $name . '2` ' . $type . " NOT NULL DEFAULT '0'";
- $this->table[$self_ref]['keys'][]='PRIMARY KEY (`' . $name . '1`, `' . $name . '2`)';
- }
-
- return $db_table;
- }
-
- private function prepare_field ($name, $field) {
- if ($field['type']=='numeric')
- $type=$this->get_field_type($field['type'], $field['len'], $field['signed'], $field['decimal']);
- else
- $type=$this->get_field_type($field['type'], $field['len']);
-
- $db_field='`' . $name . '` ' . $type . " NOT NULL DEFAULT '" . ($field['type']=='numeric' ? '0' : '') . "'";
- $db_key='';
-
- if ($field['unique'])
- $db_key='UNIQUE KEY (`' . $name . '`)';
- elseif ($field['index'])
- $db_key='KEY (`' . $name . '`)';
-
- return array($db_field, $db_key);
- }
-
- private function get_field_type ($type, $len, $signed=false, $decimal=0) {
- if ($type=='numeric' && $decimal) {
- if ($decimal>=$len)
- throw new Exception('Decimal length must be less than total length');
- if ($len>65)
- throw new Exception('Decimal field length can be 65 at most');
- return 'DECIMAL (' . $len . ', ' . $decimal . ')';
- }
-
- static $type_map=array('pass'=>'text');
- if (isset($type_map[$type])) {$type=$type_map[$type];}
-
- static $types=array(
- 'numeric'=>array(3=>'TINYINT', 5=>'SMALLINT', 7=>'MEDIUMINT', 10=>'INT', 19=>'BIGINT'),
- 'text'=>array(21000=>'VARCHAR', 9000001=>'MEDIUMTEXT')
- );
-
- $more_types=array_filter(array_keys($types[$type]), function ($maxlen) use ($len) {return $maxlen>=$len;});
- sort($more_types);
- $name=$types[$type][$more_types[0]];
-
- if (empty($name))
- throw new Exception('Field length is too much');
-
- if ($signed) $len++;
-
- if ($len<35001) $name .= '(' . $len . ')';
-
- if ($type=='numeric' && !$signed) $name.=' UNSIGNED';
-
- return $name;
- }
- }
+<?php namespace arraydb;
+
+ class DDL {
+ private $DM, $table;
+ private $db;
+
+ function __construct ($DM) {
+ $this->db=DB::get_instance();
+ $this->DM=$DM;
+ }
+
+ function create_tables () {
+ foreach ($this->DM as $name=>$table)
+ $this->table[$name]=$this->prepare_table($name, $table);
+
+ foreach ($this->table as $name=>$table) {
+ $sql="DROP TABLE IF EXISTS `" . $name . "`";
+ $this->db->table($sql);
+ $sql="CREATE TABLE `" . $name . "` (" . implode(', ', array_merge($table['fields'], $table['keys'])) . ") ENGINE=InnoDB";
+ $this->db->table($sql);
+ }
+ }
+
+ private function prepare_table ($name, $table) {
+ $conf=$table['conf'];
+ $db_table['fields'][]='id ' . $this->get_field_type('numeric', $conf['len']) . ' NOT NULL AUTO_INCREMENT';
+ $db_table['keys'][]='PRIMARY KEY (id)';
+
+ $db_table['fields'][]="create_date INT(11) UNSIGNED NOT NULL DEFAULT '0'";
+ $db_table['keys'][]='KEY (create_date)';
+ $db_table['fields'][]="update_date INT(11) UNSIGNED NOT NULL DEFAULT '0'";
+ $db_table['keys'][]='KEY (update_date)';
+
+ foreach ($table['fields'] as $field_name=>$field) {
+ list($db_field, $db_key)=$this->prepare_field($field_name, $field);
+ $db_table['fields'][]=$db_field;
+ if (!empty($db_key)) $db_table['keys'][]=$db_key;
+ }
+
+ foreach ($table['many_to_many'] as $m2m) {
+ if (isset($this->table[$m2m['relation_name']]['created'])) {continue;}
+ $this->table[$m2m['relation_name']]['created']=true;
+
+ $this->table[$m2m['relation_name']]['fields'][]='`' . $m2m['foreign_name'] . '` ' . $this->get_field_type('numeric', $conf['len']) . " NOT NULL DEFAULT '0'";
+ $this->table[$m2m['relation_name']]['fields'][]='`' . $m2m['local_name'] . '` ' . $this->get_field_type('numeric', $this->DM[$m2m['type']]['conf']['len']) . " NOT NULL DEFAULT '0'";
+ $this->table[$m2m['relation_name']]['keys'][]='PRIMARY KEY (`' . $m2m['foreign_name'] . '`, `' . $m2m['local_name'] . '`)';
+ }
+
+ foreach ($table['self_ref'] as $self_ref) {
+ $type=$this->get_field_type('numeric', $conf['len']);
+ $this->table[$self_ref]['fields'][]='`' . $name . '1` ' . $type . " NOT NULL DEFAULT '0'";
+ $this->table[$self_ref]['fields'][]='`' . $name . '2` ' . $type . " NOT NULL DEFAULT '0'";
+ $this->table[$self_ref]['keys'][]='PRIMARY KEY (`' . $name . '1`, `' . $name . '2`)';
+ }
+
+ return $db_table;
+ }
+
+ private function prepare_field ($name, $field) {
+ if ($field['type']=='numeric')
+ $type=$this->get_field_type($field['type'], $field['len'], $field['signed'], $field['decimal']);
+ else
+ $type=$this->get_field_type($field['type'], $field['len']);
+
+ $db_field='`' . $name . '` ' . $type . " NOT NULL DEFAULT '" . ($field['type']=='numeric' ? '0' : '') . "'";
+ $db_key='';
+
+ if ($field['unique'])
+ $db_key='UNIQUE KEY (`' . $name . '`)';
+ elseif ($field['index'])
+ $db_key='KEY (`' . $name . '`)';
+
+ return array($db_field, $db_key);
+ }
+
+ private function get_field_type ($type, $len, $signed=false, $decimal=0) {
+ if ($type=='numeric' && $decimal) {
+ if ($decimal>=$len)
+ throw new \Exception('Decimal length must be less than total length');
+ if ($len>65)
+ throw new \Exception('Decimal field length can be 65 at most');
+ return 'DECIMAL (' . $len . ', ' . $decimal . ')';
+ }
+
+ static $type_map=array('pass'=>'text');
+ if (isset($type_map[$type])) {$type=$type_map[$type];}
+
+ static $types=array(
+ 'numeric'=>array(3=>'TINYINT', 5=>'SMALLINT', 7=>'MEDIUMINT', 10=>'INT', 19=>'BIGINT'),
+ 'text'=>array(21000=>'VARCHAR', 9000001=>'MEDIUMTEXT')
+ );
+
+ $more_types=array_filter(array_keys($types[$type]), function ($maxlen) use ($len) {return $maxlen>=$len;});
+ sort($more_types);
+ $name=$types[$type][$more_types[0]];
+
+ if (empty($name))
+ throw new \Exception('Field length is too much');
+
+ if ($signed) $len++;
+
+ if ($len<35001) $name .= '(' . $len . ')';
+
+ if ($type=='numeric' && !$signed) $name.=' UNSIGNED';
+
+ return $name;
+ }
+ }
View
472 lib/item.php
@@ -1,237 +1,237 @@
-<?php
-
- class ITEM implements arrayaccess {
- private static $ITEM;
- private $name, $model, $id, $data;
- private $db, $adb, $cache;
-
- function __construct ($name, $model, $id, $row=false) {
- $this->db=DB::get_instance();
- $this->adb=ADB::get_instance();
- $this->cache=CACHE::get_instance();
-
- $this->name=$name;
- $this->model=$model;
- $this->id=$id;
-
- if (isset(self::$ITEM[$name][$id])) {
- $this->data=self::$ITEM[$name][$id];
- return;
- }
-
- if ($data=$this->cache->get('item_' . $this->name . '_' . $this->id)) {
- $this->data=$data;
- return;
- }
-
- if ($row!==false) {
- $this->data=$row;
- } else {
- $sql="SELECT * FROM `" . $this->name . "` WHERE id='" . $this->id . "'";
- $result=$this->db->select($sql);
- if (!count($result))
- throw new Exception('No ' . $this->name . ' found with id ' . $this->id);
-
- $this->data=$result[0];
- }
-
- foreach ($this->model['has_many'] as $has_many)
- $this->data[$has_many['local_name']]=$this->adb->id_list($has_many['type'], $has_many['foreign_name'] . "='" . $this->id . "'");
-
- foreach ($this->model['many_to_many'] as $m2m) {
- $sql="SELECT `" . $m2m['local_name'] . "` FROM `" . $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`='" . $id . "'";
- $this->data[$m2m['local_name']]=array();
- foreach ($this->db->select($sql) as $row) {
- $this->data[$m2m['local_name']][]=intval($row[$m2m['local_name']]);
- }
- }
- foreach ($this->model['self_ref'] as $self_ref) {
- $sql="(SELECT `" . $this->name . "1` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "2`='" . $this->id . "')";
- $sql.="UNION (SELECT `" . $this->name . "2` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "1`='" . $this->id . "')";
- $this->data[$self_ref]=array();
- foreach ($this->db->select($sql) as $row) {
- $this->data[$self_ref][]=intval($row['id']);
- }
- }
-
- $this->save();
- }
-
-
- /** OFFSET FONKSİYONLARI **/
-
- function offsetexists ($field) {return isset($this->data[$field]);}
-
- function offsetget ($field) {
- if (!isset($this->data[$field]))
- throw new Exception('No field found as ' . $field);
-
- return $this->data[$field];
- }
-
- function offsetset ($field, $value) {
- if (!(isset($this->model['fields'][$field])))
- throw new Exception('No field found as ' . $field);
-
- $field_model=$this->model['fields'][$field];
- if (isset($field_model['filter']) && function_exists($field_model['filter'])) {$value=eval('return ' . $field_model['filter'] . '($value);');}
-
- $update[$field]=$value;
- $update['update_date']=$_SERVER['REQUEST_TIME'];
-
- $this->db->update($this->name, $update, "id='" . $this->id . "'");
-
- if ($field_model['foreign']!==false) {
- $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($this->data[$field]));
- $foreign_item->delete_relation($field_model['foreign']['field'], $this->id);
-
- if ($value!=0) {
- $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($value));
- $foreign_item->add_relation($field_model['foreign']['field'], $this->id);
- }
- }
- $this->data[$field]=$value;
- $this->data['update_date']=$_SERVER['REQUEST_TIME'];
-
- unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
- $this->save();
- }
-
- function offsetunset ($field) {
- throw new Exception('Field unset is not allowed.');
- }
-
- function add_relation ($field, $id) {
- $id=intval($id);
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- if (in_array($id, $this->data[$field])) return false;
-
- $this->data[$field][]=$id;
- $this->save();
- }
-
- function delete_relation ($field, $id) {
- $id=intval($id);
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- if (!in_array($id, $this->data[$field])) return false;
-
- unset($this->data[$field][array_search($id, $this->data[$field])]);
- $this->save();
- }
-
- function update ($data) {
- $update=$foreigns=array();
-
- foreach ($data as $k=>$v) {
- if (!isset($this->model['fields'][$k])) {continue;}
-
- $field=$item_model['fields'][$k];
- if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
-
- $update[$k]=$v;
-
- if ($field['foreign']!==false) {
- $field['foreign']['id']=$v;
- $foreigns[$k]=$field['foreign'];
- }
- }
- $update['update_date']=$_SERVER['REQUEST_TIME'];
-
- $this->db->update($this->name, $update, "id='" . $this->id . "'");
-
- foreach ($foreigns as $k=>$foreign) {
- $foreign_item=$this->load($foreign['type'], intval($this->data[$k]));
- $foreign_item->delete_relation($foreign['field'], $this->id);
-
- $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
- $foreign_item->add_relation($foreign['field'], $this->id);
- }
-
- $this->data=$update + $this->data;
-
- unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
- $this->save();
- }
-
- function count ($field, $condition=false, $order=false) {
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- $sql=array();
-
- foreach ($this->model['has_many'] as $has_many) {
- if ($field!=$has_many['local_name']) {continue;}
- $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
- return $this->adb->count($has_many['type'], implode(' AND ', $sql), $order);
- }
-
- foreach ($this->model['many_to_many'] as $m2m) {
- if ($field!=$m2m['local_name']) {continue;}
-
- $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
- $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->count_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order);
- }
-
- foreach ($this->model['self_ref'] as $self_ref) {
- if ($field!=$self_ref) {continue;}
-
- $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
- $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->count_join($this->name, $self_ref, implode(' AND ', $sql), $order);
- }
- }
-
- function id_list ($field, $condition=false, $order=false, $limit=false) {
- if (!isset($this->data[$field]) || !is_array($this->data[$field]))
- throw new Exception($field . ' is not a relation field');
-
- $sql=array();
-
- foreach ($this->model['has_many'] as $has_many) {
- if ($field!=$has_many['local_name']) {continue;}
- $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
- return $this->adb->id_list($has_many['type'], implode(' AND ', $sql), $order, $limit);
- }
-
- foreach ($this->model['many_to_many'] as $m2m) {
- if ($field!=$m2m['local_name']) {continue;}
-
- $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
- $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->id_list_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order, $limit);
- }
-
- foreach ($this->model['self_ref'] as $self_ref) {
- if ($field!=$self_ref) {continue;}
-
- $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
- $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
- if ($condition!==false) {$sql[]='(' . $condition . ')';}
-
- return $this->adb->id_list_join($this->name, $self_ref, implode(' AND ', $sql), $order, $limit);
- }
- }
-
- private function save () {
- $this->cache->set('item_' . $this->name . '_' . $this->id, $this->data, $this->model['conf']['ttl']);
- self::$ITEM[$this->name][$this->id]=$this->data;
- }
-
- public function delete () {
- unset(self::$ITEM[$this->name][$this->id]);
- $this->cache->delete('item_' . $this->name . '_' . $this->id);
- }
+<?php namespace arraydb;
+
+ class ITEM implements \arrayaccess {
+ private static $ITEM;
+ private $name, $model, $id, $data;
+ private $db, $adb, $cache;
+
+ function __construct ($name, $model, $id, $row=false) {
+ $this->db=DB::get_instance();
+ $this->adb=ADB::get_instance();
+ $this->cache=CACHE::get_instance();
+
+ $this->name=$name;
+ $this->model=$model;
+ $this->id=$id;
+
+ if (isset(self::$ITEM[$name][$id])) {
+ $this->data=self::$ITEM[$name][$id];
+ return;
+ }
+
+ if ($data=$this->cache->get('item_' . $this->name . '_' . $this->id)) {
+ $this->data=$data;
+ return;
+ }
+
+ if ($row!==false) {
+ $this->data=$row;
+ } else {
+ $sql="SELECT * FROM `" . $this->name . "` WHERE id='" . $this->id . "'";
+ $result=$this->db->select($sql);
+ if (!count($result))
+ throw new \Exception('No ' . $this->name . ' found with id ' . $this->id);
+
+ $this->data=$result[0];
+ }
+
+ foreach ($this->model['has_many'] as $has_many)
+ $this->data[$has_many['local_name']]=$this->adb->id_list($has_many['type'], $has_many['foreign_name'] . "='" . $this->id . "'");
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ $sql="SELECT `" . $m2m['local_name'] . "` FROM `" . $m2m['relation_name'] . "` WHERE `" . $m2m['foreign_name'] . "`='" . $id . "'";
+ $this->data[$m2m['local_name']]=array();
+ foreach ($this->db->select($sql) as $row) {
+ $this->data[$m2m['local_name']][]=intval($row[$m2m['local_name']]);
+ }
+ }
+ foreach ($this->model['self_ref'] as $self_ref) {
+ $sql="(SELECT `" . $this->name . "1` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "2`='" . $this->id . "')";
+ $sql.="UNION (SELECT `" . $this->name . "2` AS id FROM `" . $self_ref . "` WHERE `" . $this->name . "1`='" . $this->id . "')";
+ $this->data[$self_ref]=array();
+ foreach ($this->db->select($sql) as $row) {
+ $this->data[$self_ref][]=intval($row['id']);
+ }
+ }
+
+ $this->save();
+ }
+
+
+ /** OFFSET FONKSİYONLARI **/
+
+ function offsetexists ($field) {return isset($this->data[$field]);}
+
+ function offsetget ($field) {
+ if (!isset($this->data[$field]))
+ throw new \Exception('No field found as ' . $field);
+
+ return $this->data[$field];
+ }
+
+ function offsetset ($field, $value) {
+ if (!(isset($this->model['fields'][$field])))
+ throw new \Exception('No field found as ' . $field);
+
+ $field_model=$this->model['fields'][$field];
+ if (isset($field_model['filter']) && function_exists($field_model['filter'])) {$value=eval('return ' . $field_model['filter'] . '($value);');}
+
+ $update[$field]=$value;
+ $update['update_date']=$_SERVER['REQUEST_TIME'];
+
+ $this->db->update($this->name, $update, "id='" . $this->id . "'");
+
+ if ($field_model['foreign']!==false) {
+ $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($this->data[$field]));
+ $foreign_item->delete_relation($field_model['foreign']['field'], $this->id);
+
+ if ($value!=0) {
+ $foreign_item=$this->adb->load($field_model['foreign']['type'], intval($value));
+ $foreign_item->add_relation($field_model['foreign']['field'], $this->id);
+ }
+ }
+ $this->data[$field]=$value;
+ $this->data['update_date']=$_SERVER['REQUEST_TIME'];
+
+ unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
+ $this->save();
+ }
+
+ function offsetunset ($field) {
+ throw new \Exception('Field unset is not allowed.');
+ }
+
+ function add_relation ($field, $id) {
+ $id=intval($id);
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new \Exception($field . ' is not a relation field');
+
+ if (in_array($id, $this->data[$field])) return false;
+
+ $this->data[$field][]=$id;
+ $this->save();
+ }
+
+ function delete_relation ($field, $id) {
+ $id=intval($id);
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new \Exception($field . ' is not a relation field');
+
+ if (!in_array($id, $this->data[$field])) return false;
+
+ unset($this->data[$field][array_search($id, $this->data[$field])]);
+ $this->save();
+ }
+
+ function update ($data) {
+ $update=$foreigns=array();
+
+ foreach ($data as $k=>$v) {
+ if (!isset($this->model['fields'][$k])) {continue;}
+
+ $field=$item_model['fields'][$k];
+ if (isset($field['filter']) && function_exists($field['filter'])) {$v=eval('return ' . $field['filter'] . '($v);');}
+
+ $update[$k]=$v;
+
+ if ($field['foreign']!==false) {
+ $field['foreign']['id']=$v;
+ $foreigns[$k]=$field['foreign'];
+ }
+ }
+ $update['update_date']=$_SERVER['REQUEST_TIME'];
+
+ $this->db->update($this->name, $update, "id='" . $this->id . "'");
+
+ foreach ($foreigns as $k=>$foreign) {
+ $foreign_item=$this->load($foreign['type'], intval($this->data[$k]));
+ $foreign_item->delete_relation($foreign['field'], $this->id);
+
+ $foreign_item=$this->load($foreign['type'], intval($foreign['id']));
+ $foreign_item->add_relation($foreign['field'], $this->id);
+ }
+
+ $this->data=$update + $this->data;
+
+ unset($this->adb->ROW[$this->name][$this->id], $this->adb->LIST[$this->name], $this->adb->COUNT[$this->name]);
+ $this->save();
+ }
+
+ function count ($field, $condition=false, $order=false) {
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new \Exception($field . ' is not a relation field');
+
+ $sql=array();
+
+ foreach ($this->model['has_many'] as $has_many) {
+ if ($field!=$has_many['local_name']) {continue;}
+ $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+ return $this->adb->count($has_many['type'], implode(' AND ', $sql), $order);
+ }
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ if ($field!=$m2m['local_name']) {continue;}
+
+ $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
+ $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->count_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order);
+ }
+
+ foreach ($this->model['self_ref'] as $self_ref) {
+ if ($field!=$self_ref) {continue;}
+
+ $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
+ $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->count_join($this->name, $self_ref, implode(' AND ', $sql), $order);
+ }
+ }
+
+ function id_list ($field, $condition=false, $order=false, $limit=false) {
+ if (!isset($this->data[$field]) || !is_array($this->data[$field]))
+ throw new \Exception($field . ' is not a relation field');
+
+ $sql=array();
+
+ foreach ($this->model['has_many'] as $has_many) {
+ if ($field!=$has_many['local_name']) {continue;}
+ $sql[]=$has_many['foreign_name'] . "='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+ return $this->adb->id_list($has_many['type'], implode(' AND ', $sql), $order, $limit);
+ }
+
+ foreach ($this->model['many_to_many'] as $m2m) {
+ if ($field!=$m2m['local_name']) {continue;}
+
+ $sql[]="`" . $m2m['type'] . "`.id=`" . $m2m['relation_name'] . "`.`" . $m2m['local_name'] . "`";
+ $sql[]="`" . $m2m['relation_name'] . "`.`" . $m2m['foreign_name'] . "`='" . $this->id . "'";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->id_list_join($m2m['type'], $m2m['relation_name'], implode(' AND ', $sql), $order, $limit);
+ }
+
+ foreach ($this->model['self_ref'] as $self_ref) {
+ if ($field!=$self_ref) {continue;}
+
+ $sql[0]="((`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "1` AND `" . $self_ref . "`.`" . $this->name . "2`='" . $this->id . "')";
+ $sql[0].=" OR (`" . $this->name . "`.id=`" . $self_ref . "`.`" . $this->name . "2` AND `" . $self_ref . "`.`" . $this->name . "1`='" . $this->id . "'))";
+ if ($condition!==false) {$sql[]='(' . $condition . ')';}
+
+ return $this->adb->id_list_join($this->name, $self_ref, implode(' AND ', $sql), $order, $limit);
+ }
+ }
+
+ private function save () {
+ $this->cache->set('item_' . $this->name . '_' . $this->id, $this->data, $this->model['conf']['ttl']);
+ self::$ITEM[$this->name][$this->id]=$this->data;
+ }
+
+ public function delete () {
+ unset(self::$ITEM[$this->name][$this->id]);
+ $this->cache->delete('item_' . $this->name . '_' . $this->id);
+ }
}
View
8 sample/social/config.php
@@ -11,9 +11,9 @@
'type'=>'file', 'path'=>dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'cache'
);
- DB::init($db_config);
- CACHE::init($cache_config);
+ arraydb\DB::init($db_config);
+ arraydb\CACHE::init($cache_config);
- ADB::init($model);
+ arraydb\ADB::init($model);
- $adb=ADB::get_instance();
+ $adb=arraydb\ADB::get_instance();
Please sign in to comment.
Something went wrong with that request. Please try again.