123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- <?php namespace Maze\Data\Mysql;
- 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\Mysql\Connect
- */
- protected $read;
- /**
- * update
- *
- * @var Maze\Data\Mysql\Connect
- */
- protected $update;
- /**
- * table
- *
- * @var string
- */
- protected $table;
- /**
- * value
- *
- * @var array
- */
- protected $value = array();
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * getInstance
- *
- * @return Maze\Data\Mysql\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);
-
- $this->sql = Sql::getInstance();
- }
-
- /**
- * register
- *
- * @return mixd
- */
- private function register($config)
- {
- # read update
- if(strpos($config['host'], ',') !== false)
- {
- $host = explode(',', $config['host']);
- $config['host'] = $host[0];
- $this->read = Connect::getInstance($config);
- $config['host'] = $host[1];
- $this->update = Connect::getInstance($config);
- }
- else
- {
- $this->read = $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 = $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 include($file);
- }
- $sql = $this->sql->create($this->table, $struct);
- $this->update->query($sql);
- $this->log($sql, 'create');
-
- $data['time'] = MAZE_TIME;
-
- $data['table'] = $this->table;
-
- $data['create'] = $sql;
- $data['struct'] = array_flip(array_keys($struct));
- 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))
- {
- $sql = $this->sql->showIndex($this->table);
-
- $handle = $this->update->query($sql);
-
- $info = $handle->fetchAll();
-
- if($info)
- {
- foreach($info as $k => $v)
- {
- if($v['Key_name'] != 'PRIMARY')
- {
- $sql = $this->sql->dropIndex($this->table, $v['Key_name']);
- $this->update->query($sql);
- }
- }
- }
-
- $sql = $this->sql->index($this->table, $index);
- $this->update->query($sql);
- $this->log($sql, 'index');
-
- $data['index'] = $version;
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
-
-
- return true;
- }
- /**
- * alter table
- *
- * @return mixed
- */
- public function alter($alter, $struct = array())
- {
- if(empty($alter))
- {
- return false;
- }
- $file = $this->file();
- if(!file_exists($file))
- {
- return false;
- }
-
- $data = include($file);
- if($struct)
- {
- $sql = $this->sql->alter($this->table, $alter);
- $this->update->query($sql);
- $this->log($sql, 'alter');
-
- $data['struct'] = array_flip(array_keys($struct));
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
- else
- {
- if(isset($alter['version']))
- {
- $version = $alter['version'];
- }
- else
- {
- $version = 1;
- }
-
- if(isset($alter[$version]) && (empty($data['alter']) || (isset($data['alter']) && $data['alter'] != $version)))
- {
- $sql = $this->sql->alter($this->table, $alter[$version]);
- $this->update->query($sql);
- $this->log($sql, 'alter');
-
- $data['alter'] = $version;
- file_put_contents($file, '<?php return ' . var_export($data, true) . ';');
- }
- }
-
- 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']))
- {
- $sql = $this->sql->inserts($this->table, $value['col'], $value['value']);
- $this->update->query($sql);
-
- $this->log($sql, 'inserts');
-
- $data = include($file);
-
- $data['insert'] = $sql;
- 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, 'fetchAll');
- if($data && $key)
- {
- $result = array();
- foreach($data as $k => $v)
- {
- 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;
- }
- }
- }
- return $result;
- }
- return $data;
- }
- /**
- * one
- *
- * @return array
- */
- public function one($col)
- {
- return $this->select($col);
- }
- /**
- * count
- *
- * @return array
- */
- public function count($col = 'clear')
- {
- return $this->select($col, 'fetchColumn', 'count');
- }
- /**
- * insert
- *
- * @return int
- */
- public function insert()
- {
- $sql = $this->sql->insert($this->table);
- $handle = $this->update->prepare($sql);
- $handle->execute($this->value);
- $id = $this->update->id();
- $this->log($sql, $this->value);
- $this->value = array();
- return $id;
- }
- /**
- * update
- *
- * @return int
- */
- public function update()
- {
- $sql = $this->sql->update($this->table);
- $result = false;
- if($sql)
- {
- $handle = $this->update->prepare($sql);
- $handle->execute($this->value);
- $result = $handle->rowCount();
- $this->log($sql, $this->value);
- }
- $this->value = array();
- return $result;
- }
- /**
- * delete
- *
- * @return int
- */
- public function delete()
- {
- $sql = $this->sql->delete($this->table);
- $result = false;
- if($sql)
- {
- $handle = $this->update->prepare($sql);
- $handle->execute($this->value);
- $result = $handle->rowCount();
- $this->log($sql, $this->value);
- }
- $this->value = array();
- return $result;
- }
- /**
- * select
- *
- * @return array
- */
- private function select($col = '', $method = 'fetch', $type = 'select')
- {
- $sql = $this->sql->{$type}($this->table, $col);
-
- if($type == 'count' && strpos($sql, 'group by `'))
- {
- $method = 'fetchAll';
- }
- $handle = $this->read->prepare($sql);
- $handle->execute($this->value);
- $data = $handle->$method();
- $this->log($sql, $this->value);
- if($col != 'clear')
- {
- $this->value = array();
- }
- return $data;
- }
- /**
- * page
- *
- * @return object
- */
- public function page($num, $config = array())
- {
- $this->reset('limit');
- 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]) && $method != 'order')
- {
- foreach($param[0] as $k => $v)
- {
- $this->call($method, $v);
- }
- }
- else
- {
- $this->call($method, $param);
- }
- return $this;
- }
- /**
- * call
- *
- * @return mixd
- */
- private function call($method, $param)
- {
- if($method == 'where' || $method == 'set' || $method == 'add')
- {
- # 特殊处理in
- if(isset($param[2]) && $param[2] == 'in')
- {
- if(is_string($param[1]))
- {
- $param[1] = explode(',', $param[1]);
- }
-
- $prefix = 'in_';
- foreach($param[1] as $k => $v)
- {
- $k = ':' . $prefix . $k;
- $key[] = $k;
- $this->value[$k] = $v;
- }
-
- $param[1] = '(' . implode(',', $key) . ')';
- }
- else
- {
- $key = ':' . count($this->value);
- $this->value[$key] = $param[1];
- $param[1] = $key;
- }
- }
- $this->sql->$method($param);
- }
- /**
- * log
- *
- * @return log
- */
- private function log($sql, $value)
- {
- Debug::log(array('sql' => $sql, 'value' => $value));
- }
- }
|