123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- <?php namespace Maze\Data;
- class Sql
- {
- /**
- * instance
- *
- * @var string
- */
- static protected $instance;
- /**
- * getInstance
- *
- * @return Maze\Data\Sql;
- */
- static public function getInstance()
- {
- if(empty(self::$instance))
- {
- self::$instance = new self();
- }
- return self::$instance->init();
- }
- /**
- * create
- *
- * @return string
- */
- public function create($table, $struct)
- {
- $create = $primary = array();
- foreach($struct as $k => $v)
- {
- if(isset($v['table']) && $v['table'] != $table)
- {
- continue;
- }
- $primary[$k] = '';
- if(is_array($v))
- {
- if(!isset($v['type']))
- {
- continue;
- }
- $com = $v;
- $v = $com['type'];
- if(strpos($com['type'], 'text') !== false)
- {
- $primary[$k] .= '';
- }
- elseif(strpos($com['type'], 'int') === false)
- {
- $primary[$k] .= 'not null default \'\'';
- }
- elseif(!empty($com['default']))
- {
- $primary[$k] .= 'not null default \'' . $com['default'] . '\'';
- }
- elseif($k != 'id' && strpos($com['type'], 'int') !== false)
- {
- $primary[$k] .= 'not null default 0';
- }
- else
- {
- $primary[$k] .= 'not null';
- }
- if(!empty($com['name']))
- {
- $primary[$k] .= ' comment \'' . $com['name'] . '\'';
- }
- }
- elseif(is_string($v) && strpos($v,' ') !== false)
- {
- $com = explode(' ',$v);
- $v = $com[0];
- if(!empty($com[1]))
- {
- $primary[$k] .= 'not null default \'' . $com[1] . '\'';
- }
- else
- {
- $primary[$k] .= 'not null';
- }
- if(!empty($com[2]))
- {
- $primary[$k] .= ' comment \'' . $com[2] . '\'';
- }
- }
- if($k == 'id')
- {
- $primary[$k] = 'unsigned auto_increment primary key ' . $primary[$k];
- }
- $create[] = '`' . $k . '` ' . strtoupper(str_replace('-','(',$v) . ') ' . $primary[$k] . '');// not null
- }
- $sql = 'DROP TABLE IF EXISTS `' . $table . '`;CREATE TABLE `' . $table . '`(' . implode(',', $create) . ')';
- //echo $sql;die;
- return $sql;
- }
- /**
- * alter
- *
- * @return string
- */
- public function alter($table, $config)
- {
- $create = $primary = array();
- $alter = 'ALTER TABLE `' . $table;
- $sql = array();
- foreach($config as $k => $v)
- {
- if(isset($v['type']))
- {
- $v = array
- (
- 'add', $k, $k, $v['type'] . ' ' . $v['default'] . ' ' . $v['name'],
- );
- }
- $sql[$k] = '';
- if(isset($v[3]) && strpos($v[3],' ') !== false)
- {
- $com = explode(' ',$v[3]);
- $sql[$k] = str_replace('-','(',$com[0]) . ') ';
- if(isset($com[1]) && $com[1] != '')
- {
- $sql[$k] .= 'not null default \'' . $com[1] . '\'';
- }
- else
- {
- $sql[$k] .= 'not null';
- }
- if(!empty($com[2]))
- {
- $sql[$k] .= ' comment \'' . $com[2] . '\'';
- }
- $sql[$k] = strtoupper($sql[$k]);
- }
- if($v[0] == 'add')
- {
- # 新增字段
- $sql[$k] = $alter . '` ADD `' . $v[1] . '` ' . $sql[$k];
- }
- elseif($v[0] == 'delete')
- {
- # 删除字段
- $sql[$k] = $alter . '` DROP `' . $v[1] . '`';
- }
- else
- {
- # 修改字段
- $sql[$k] = $alter . '` CHANGE `' . $v[1] . '` `' . $v[2] . '` ' . $sql[$k];
- }
- }
- $sql = implode(';', $sql);
-
- return $sql;
- }
- /**
- * col
- *
- * @return string
- */
- private function col($col)
- {
- $result = '';
- if(is_array($col))
- {
- $array = array();
- foreach($col as $k => $v)
- {
- if(!is_numeric($k))
- {
- $array[] = $k . ' AS ' . $v;
- }
- else
- {
- $array[] = $v;
- }
- }
- $result = implode(' ', $array);
- }
- else
- {
- $result = $col ? $col : '*';
- }
- if(isset($this->col) && $this->col)
- {
- $result .= ',' . $this->col;
- }
- return $result;
- }
- /**
- * select
- *
- * @return string
- */
- public function select($table, $col = '')
- {
- $where = '';
- if($this->where)
- {
- if(isset($this->between))
- {
- $where = 'WHERE ' . $this->between . ' AND ' . implode(' ', $this->where);
- $this->limit = '';
- }
- else
- {
- $where = 'WHERE ' . implode(' ', $this->where);
- }
- }
- $sql = 'SELECT ' . $this->col($col) . ' FROM `' . $table . '` ' . $where . ' ' . $this->group . ' ' . $this->order . ' ' . $this->limit;
- $this->init();
- return $sql;
- }
- /**
- * count
- *
- * @return string
- */
- public function count($table, $col = '')
- {
- $where = '';
- if($this->where)
- {
- $where = 'WHERE ' . implode(' ', $this->where);
- }
- $state = 1;
- if($col == 'clear')
- {
- $col = '';
- $state = 2;
- }
- if(!$col)
- {
- $col = 'count(*) as total';
- }
- $sql = 'SELECT ' . $col . ' FROM `' . $table . '` ' . $where . ' ' . $this->group . ' ';
- if($state == 1)
- {
- $this->init();
- }
- return $sql;
- }
-
- /**
- * showIndex
- *
- * @return string
- */
- public function showIndex($table)
- {
- $sql = 'SHOW INDEX FROM `' . $table . '` ';
-
- return $sql;
- }
-
- /**
- * dropIndex
- *
- * @return string
- */
- public function dropIndex($table, $name)
- {
- $sql = 'ALTER TABLE `' . $table . '` DROP INDEX ' . $name;
-
- return $sql;
- }
- /**
- * index
- *
- * @return string
- */
- public function index($table, $value)
- {
- $sql = 'ALTER TABLE `' . $table . '` ADD INDEX ';
- $max = count($value)-1;
- $i = 0;
-
- foreach($value as $k => $v)
- {
- $sql .= ' ' . $k . ' (' . $v . ')';
- if($i >= $max)
- {
- $sql .= '';
- }
- else
- {
- $sql .= ',';
- }
- $i++;
- }
- return $sql;
- }
- /**
- * insert
- *
- * @return string
- */
- public function insert($table)
- {
- $sql = 'INSERT INTO `' . $table . '` (' . implode(',', $this->col) . ') VALUES (' . implode(',', $this->value) . ')';
- $this->init();
- return $sql;
- }
-
- /**
- * inserts
- *
- * @return string
- */
- public function inserts($table, $col, $value)
- {
- $sql = 'INSERT INTO `' . $table . '` (' . $col . ') VALUES ';
- $max = count($value)-1;
- foreach($value as $k => $v)
- {
- $sql .= '(' . $v . ')';
- if($k >= $max)
- {
- $sql .= '';
- }
- else
- {
- $sql .= ',';
- }
- }
- return $sql;
- }
- /**
- * update
- *
- * @return string
- */
- public function update($table)
- {
- $where = '';
- if(!$this->where)
- {
- return false;
- }
- else
- {
- $where = 'WHERE ' . implode(' ', $this->where);
- }
- $sql = 'UPDATE `' . $table . '` SET ' . implode(',', $this->value) . ' ' . $where;
- $this->init();
- return $sql;
- }
- /**
- * delete
- *
- * @return string
- */
- public function delete($table)
- {
- $where = '';
- if(!$this->where)
- {
- return false;
- }
- else
- {
- $where = 'WHERE ' . implode(' ', $this->where);
- }
- $sql = 'DELETE FROM `' . $table . '` ' . $where;
- $this->init();
- return $sql;
- }
-
- /**
- * truncate
- *
- * @return string
- */
- public function truncate($table)
- {
- $sql = 'TRUNCATE TABLE `' . $table . '`';
-
- return $sql;
- }
-
- /**
- * sql
- *
- * @return string
- */
- public function sql($sql)
- {
- return $sql;
- }
- /**
- * init
- *
- * @return object
- */
- public function init()
- {
- $this->where = $this->value = $this->col = array();
- $this->order = '';
- $this->group = '';
- $this->limit = '';
- return $this;
- }
- /**
- * where
- *
- * @return string
- */
- public function where($param)
- {
- if(empty($param[2])) $param[2] = '=';
- if(empty($param[3])) $param[3] = 'and';
- $where = '`' . $param[0] . '` ' . $param[2] . ' ' . $param[1];
- if(!$this->where)
- {
- $this->where[] = $where;
- }
- else
- {
- $this->where[] = $param[3] . ' ' . $where;
- }
- }
- /**
- * order
- *
- * @return string
- */
- public function order($param)
- {
- if(is_array($param[0]))
- {
- $this->order = 'order by ';
- foreach($param[0] as $k => $v)
- {
- $order[] = '`' . $k . '` ' . $v;
- }
-
- $this->order .= implode(',', $order);
-
- //echo $this->order;
- }
- else
- {
- if(empty($param[1])) $param[1] = 'desc';
- $this->order = 'order by `' . $param[0] . '` ' . $param[1];
- }
- }
- /**
- * group
- *
- * @return string
- */
- public function group($param)
- {
- if(is_array($param))
- {
- $param = $param[0];
- }
- # 去掉id的分组,没用
- if(isset($param) && $param != 'id')
- {
- if($param == 'day')
- {
- $this->col = 'FROM_UNIXTIME(cdate, "%Y-%m-%d") as day';
- }
- elseif($param == 'month')
- {
- $this->col = 'FROM_UNIXTIME(cdate, "%Y-%m") as month';
- }
- elseif($param == 'year')
- {
- $this->col = 'FROM_UNIXTIME(cdate, "%Y") as year';
- }
- else
- {
- $this->col = $param;
- }
- $this->group = 'group by `' . $param . '`';
- }
- }
- /**
- * limit
- *
- * @return string
- */
- public function limit($param)
- {
- if(empty($param[1])) $param[1] = 0;
- $this->limit = 'limit ' . $param[1] . ',' . $param[0];
-
- //$this->between = ' `id` BETWEEN ' . $param[1] . ' AND ' . ($param[1] + $param[0]);
- }
- /**
- * reset limit
- *
- * @return string
- */
- public function reset($param)
- {
- $this->{$param[0]} = '';
- }
- /**
- * add
- *
- * @return string
- */
- public function add($param)
- {
- $this->col[] = '`' . $param[0] . '`';
- $this->value[] = $param[1];
- }
- /**
- * set
- *
- * @return string
- */
- public function set($param)
- {
- if(empty($param[2])) $param[2] = '=';
- $param[0] = '`' . $param[0] . '`';
- if(strpos($param[2], '+=') !== false)
- {
- $param[2] = '=' . $param[0] . '+';
- }
- if(strpos($param[2], '-=') !== false)
- {
- $param[2] = '=' . $param[0] . '-';
- }
- $this->value[] = $param[0] . $param[2] . $param[1];
- }
- }
|