123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- <?php namespace Maze\Data\Mongo;
- use Maze\Data\Sql;
- use Maze\File\Path;
- use Maze\Page\Main as Page;
- use Maze\Debug\Process as Debug;
- use Maze\Config\Load as Config;
- class Store
- {
- /**
- * read
- *
- * @var Maze\Data\Mongo\Connect
- */
- protected $read;
- /**
- * update
- *
- * @var Maze\Data\Mongo\Connect
- */
- protected $update;
- /**
- * table
- *
- * @var string
- */
- protected $table;
- /**
- * value
- *
- * @var array
- */
- protected $value = array();
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * getInstance
- *
- * @return Maze\Data\Mongo\Store;
- */
- static public function getInstance($config)
- {
- if(empty(self::$instance))
- {
- self::$instance = new self();
- }
- self::$instance->register($config);
- return self::$instance;
- }
- /**
- * __construct
- *
- * @return mixd
- */
- public function __construct($config)
- {
- $this->register($config);
- }
-
- /**
- * register
- *
- * @return mixd
- */
- private function register($config)
- {
- $this->connect = $this->update = Connect::getInstance($config);
- }
- /**
- * table
- *
- * @return object
- */
- public function table($table)
- {
- if(defined('MAZE_PROJECT') && MAZE_PROJECT != 'default')
- {
- $table = MAZE_PROJECT . '_' . $table;
- }
-
- $this->table = $this->connect->table($table);
- return $this;
- }
-
- /**
- * file
- *
- * @return mixed
- */
- private function file()
- {
- //$file = Path::create(MAZE_PATH . 'data/database/', $this->table . '.php');
-
- $file = Path::create(MAZE_PATH . 'data/database/', $this->table);
- return $file;
- }
- /**
- * create
- *
- * @return mixed
- */
- public function create($struct)
- {
- if(isset(Config::$global['base']['create']))
- {
- return false;
- }
- $file = $this->file();
- if(file_exists($file))
- {
- return false;
- }
-
- $data['time'] = MAZE_TIME;
-
- $data['table'] = $this->table;
-
- $data['create'] = $struct;
- $this->log($data);
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
-
- return true;
- }
-
- /**
- * create index
- *
- * @return mixed
- */
- public function index($index)
- {
- if(empty($index))
- {
- return false;
- }
- $file = $this->file();
- if(!file_exists($file))
- {
- return false;
- }
-
- $data = include($file);
-
- if(isset($index['version']))
- {
- $version = $index['version'];
-
- unset($index['version']);
- }
- else
- {
- $version = 1;
- }
-
- if(empty($data['index']) || (isset($data['index']) && $data['index'] < $version))
- {
- $return = $this->table->ensureIndex($index, array('name' => ''));
- $this->log($index);
-
- $data['index'] = $version;
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
-
-
- return true;
- }
- /**
- * alter table
- *
- * @return mixed
- */
- public function alter($alter)
- {
- return true;
- }
-
- /**
- * insert the default value
- *
- * @return mixed
- */
- public function inserts($value)
- {
- $file = $this->file();
- if(!file_exists($file))
- {
- return false;
- }
- if(isset($value['col']) && isset($value['value']))
- {
- $col = explode(',', $value['col']);
- $value = explode(',', $value['value']);
- foreach($col as $k => $v)
- {
- $this->value['add'][$v] = $value[$k];
- $this->insert();
- }
-
- $this->log($value);
-
- $data = include($file);
-
- $data['insert'] = $value;
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
- return true;
- }
- /**
- * all
- *
- * @return array
- */
- public function all($col)
- {
- $key = false;
- if(strpos($col, '|') !== false)
- {
- $array = explode('|', $col);
- $key = $array[1];
- $col = $array[0];
- }
- $data = $this->select($col, 'find');
- $result = array();
-
- if($data)
- {
- foreach($data as $k => $v)
- {
- $v['id'] = (array) $v['_id'];
- $v['id'] = $v['id']['$id'];
- if(isset($v[$key]))
- {
- if(isset($array[3]) && isset($v[$array[2]]))
- {
- $result[$v[$key]][$v[$array[2]]] = $v;
- }
- elseif(isset($array[2]) && isset($v[$array[2]]))
- {
- $result[$v[$key]] = $v[$array[2]];
- }
- elseif(isset($array[2]))
- {
- $result[$v[$key]][] = $v;
- }
- else
- {
- $result[$v[$key]] = $v;
- }
- }
- else
- {
- $result[] = $v;
- }
- }
- }
- return $result;
- }
- /**
- * one
- *
- * @return array
- */
- public function one($col = '')
- {
- $data = $this->select($col, 'findOne');
- if($data)
- {
- $data['id'] = (array) $data['_id'];
- $data['id'] = $data['id']['$id'];
- }
- return $data;
- }
- /**
- * count
- *
- * @return array
- */
- public function count($col = 'clear')
- {
- return $this->select($col, 'count');
- }
- /**
- * insert
- *
- * @return int
- */
- public function insert()
- {
- $insert = $this->value['add'];
-
- $return = $this->table->insert($insert);
- $this->log($this->value);
- $this->value = array();
- return $insert['_id'];
- }
- /**
- * update
- *
- * @return int
- */
- public function update()
- {
- $method = '$set';
- $return = $this->table->update($this->value['where'], array($method => $this->value['set']));
- $this->log($this->value);
- $this->value = array();
- return $return;
- }
- /**
- * delete
- *
- * @return int
- */
- public function delete()
- {
- $this->update('$unset');
- return $result;
- }
- /**
- * select
- *
- * @return array
- */
- private function select($col = '', $method = 'find')
- {
- if(isset($this->value['where']))
- {
- //print_r($this->value['where']);
- $return = $cursor = $this->table->$method($this->value['where']);
- }
- else
- {
- $return = $cursor = $this->table->$method();
- }
-
- if($method != 'count')
- {
- if(isset($this->value['order']))
- {
- $return = $cursor->sort($this->value['order']);
- }
-
- if(isset($this->value['limit']))
- {
- foreach($this->value['limit'] as $k => $v)
- {
- $return = $cursor->limit($k)->skip($v);
- }
- }
-
- if($col && $col != '*' && $col != 'clear')
- {
- if(is_string($col))
- {
- $temp = explode(',', $col);
- $col = array();
- foreach($temp as $k => $v)
- {
- $col[$v] = true;
- }
- }
- $return = $cursor->fields($col);
- }
- }
- $this->log($this->value);
- if($col != 'clear')
- {
- $this->value = array();
- }
- return $return;
- }
- /**
- * page
- *
- * @return object
- */
- public function page($num, $config = array())
- {
- empty($config[0]) && $config[0] = 'list';
- empty($config[1]) && $config[1] = 'current';
- empty($config[2]) && $config[2] = '';
- $page = Page::getInstance($config[1]);
- $page->template($config[0]);
- $page->link($config[2]);
- $page->total($this->count());
-
- $this->limit($num, $page->offset($num));
- return $this;
- }
- /**
- * __call
- *
- * @return object
- */
- public function __call($method, $param)
- {
- if(is_array($param[0]))
- {
- foreach($param[0] as $k => $v)
- {
- if($method == 'order')
- {
- $this->call($method, array($k, $v));
- }
- else
- {
- $this->call($method, $v);
- }
- }
- }
- else
- {
- $this->call($method, $param);
- }
- return $this;
- }
- /**
- * call
- *
- * @return mixd
- */
- private function call($method, $param)
- {
- if(is_array($param) && isset($param[0]))
- {
- if(is_string($param[0]) && strpos($param[0], ','))
- {
- $temp = explode(',', str_replace('`', '', $param[0]));
- $index = count($temp)-1;
- foreach($temp as $k => $v)
- {
- if($k == $index)
- {
- $param[0] = $v;
- }
- else
- {
- $array = explode(' ', $v);
- $this->call($method, $array);
- }
- }
- }
-
- if($param[0] == 'id')
- {
- $param[0] = '_id';
- }
-
- $func = 'convert_' . $method;
- if(method_exists($this, $func))
- {
- $this->$func($param);
- }
- $this->value[$method][$param[0]] = $param[1];
- }
- else
- {
- $this->value[$method] = $param;
- }
- }
- /**
- * convert_order
- *
- * @return mixed
- */
- private function convert_order(&$param)
- {
- switch($param[1])
- {
- case 'desc':
- $param[1] = -1;
- break;
- case 'asc':
- $param[1] = 1;
- break;
- }
- }
-
- /**
- * convert_group
- *
- * @return mixed
- */
- private function convert_group(&$param)
- {
- print_r($param);die;
- }
-
- /**
- * convert_where
- *
- * @return mixed
- */
- private function convert_where(&$param)
- {
- if(isset($param[2]))
- {
- $state = true;
- switch($param[2])
- {
- case 'like':
- # 模糊查询
- if(strpos($param[1], '%') !== false)
- {
- $param[1] = str_replace('%', '(.*?)', $param[1]);
- $param[1] = new \MongoRegex('/'.$param[1].'/i');
- }
- else
- {
- $param[1] = new \MongoRegex('/(.*?)'.$param[1].'(.*?)/i');
- }
- $state = false;
- break;
-
- case 'in':
- case 'nin':
- # in查询
- $param[1] = explode(',', $param[1]);
- if($param[0] == '_id')
- {
- foreach($param[1] as $k => $v)
- {
- $param[1][$k] = new \MongoId($v);
- }
- }
- $param[2] = '$' . $param[2];
- break;
-
- case '>':
- $param[2] = '$gt';
- break;
- case '>=':
- $param[2] = '$gte';
- break;
- case '<':
- $param[2] = '$lt';
- break;
- case '<=':
- $param[2] = '$lte';
- break;
- case '!=':
- $param[2] = '$ne';
- break;
- case '%':
- $param[2] = '$mod';
- break;
- case 'bt':
- $state = false;
- $param[1] = array('gt' => $param[1][0], 'lt' => $param[1][1]);
- break;
- case 'bte':
- $state = false;
- $param[1] = array('gte' => $param[1][0], 'lte' => $param[1][1]);
- break;
- default:
- $param[2] = '$' . $param[2];
- break;
- }
- if($state == true)
- {
- $param[1] = array($param[2] => $param[1]);
- }
- }
-
- if($param[0] == '_id' && is_string($param[1]))
- {
- $param[1] = new \MongoId($param[1]);
- }
- }
- /**
- * log
- *
- * @return log
- */
- private function log($value)
- {
- Debug::log(array('value' => $value));
- }
- }
|