|
@@ -5,24 +5,33 @@ class Sql
|
|
|
{
|
|
|
$this->colOrder = $order;
|
|
|
}
|
|
|
+ public static function desc($table)
|
|
|
+ {
|
|
|
+ return 'DESC ' . $table;
|
|
|
+ }
|
|
|
+ public static function truncate($table)
|
|
|
+ {
|
|
|
+ return 'TRUNCATE TABLE `' . $table . '`';
|
|
|
+ }
|
|
|
+ public static function opt($table)
|
|
|
+ {
|
|
|
+ return 'OPTIMIZE TABLE `' . $table . '`';
|
|
|
+ }
|
|
|
+ public static function explain($sql)
|
|
|
+ {
|
|
|
+ return 'EXPLAIN ' . $sql;
|
|
|
+ }
|
|
|
+ public static function showIndex($table)
|
|
|
+ {
|
|
|
+ return 'SHOW INDEX FROM ' . $table;
|
|
|
+ }
|
|
|
public static function create($config)
|
|
|
{
|
|
|
$sql = 'DROP TABLE IF EXISTS `' . $config['name'] . '`;CREATE TABLE IF NOT EXISTS `' . $config['name'] . '`(';
|
|
|
$struct = array('id' => array('name' => 'ID', 'type' => 'int(11)'));
|
|
|
$struct = array_merge($struct, $config['struct']);
|
|
|
foreach ($struct as $k => $v) {
|
|
|
- $sql .= '`' . $k . '` ' . strtoupper($v['type']);
|
|
|
- if ($k == 'id') {
|
|
|
- $sql .= 'UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL';
|
|
|
- } elseif (isset($v['default']) && $v['default']) {
|
|
|
- $sql .= ' NOT NULL DEFAULT "' . $v['default'] . '"';
|
|
|
- } else {
|
|
|
- $sql .= ' NULL';
|
|
|
- }
|
|
|
- if (isset($v['name'])) {
|
|
|
- $sql .= ' COMMENT \'' . $v['name'] . '\'';
|
|
|
- }
|
|
|
- $sql .= ',';
|
|
|
+ $sql .= self::createField($k, $v) . ',';
|
|
|
}
|
|
|
$sql = rtrim($sql, ',') . ')';
|
|
|
if (isset($config['auto'])) {
|
|
@@ -46,93 +55,170 @@ class Sql
|
|
|
}
|
|
|
return $sql;
|
|
|
}
|
|
|
- public static function desc($table)
|
|
|
+ public static function createField($name, $set)
|
|
|
{
|
|
|
- return 'DESC ' . $table;
|
|
|
+ $field = '`' . $name . '` ' . strtoupper($set['type']);
|
|
|
+ if ($name == 'id') {
|
|
|
+ $field .= 'UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL';
|
|
|
+ } elseif (isset($set['default']) && $set['default']) {
|
|
|
+ $field .= ' NOT NULL DEFAULT "' . $set['default'] . '"';
|
|
|
+ } else {
|
|
|
+ $field .= ' NULL';
|
|
|
+ }
|
|
|
+ if (isset($set['name'])) {
|
|
|
+ $field .= ' COMMENT \'' . $set['name'] . '\'';
|
|
|
+ }
|
|
|
+ return $field;
|
|
|
}
|
|
|
public static function alter($table, $struct, $data)
|
|
|
{
|
|
|
- print_r($struct);
|
|
|
- print_r($data);
|
|
|
-
|
|
|
- $drop = $change = array();
|
|
|
+ $sql = array();
|
|
|
+ $alter = 'ALTER TABLE `' . $table . '` ';
|
|
|
foreach ($data as $v) {
|
|
|
- if ($v['Field'] != 'id') {
|
|
|
- if (!isset($struct[$v['Field']])) {
|
|
|
- $drop[] = $v['Field'];
|
|
|
- } elseif (isset($struct[$v['Field']])) {
|
|
|
- if ($struct[$v['Field']]['type'] != $v['Type'] || (isset($struct[$v['Field']]['default']) && $struct[$v['Field']]['default'] != $v['Default'])) {
|
|
|
- $change[] = $struct[$v['Field']];
|
|
|
+ $field = $v['Field'];
|
|
|
+ if ($field != 'id') {
|
|
|
+ $set = $struct[$field] ?? false;
|
|
|
+ if ($set) {
|
|
|
+ if ($set['type'] != $v['Type'] || (isset($set['default']) && $set['default'] != $v['Default'])) {
|
|
|
+ $sql[] = $alter . ' CHANGE `' . $field . '` `' . $field . '` ' . self::createField($field, $set);
|
|
|
} else {
|
|
|
- unset($struct[$v['Field']]);
|
|
|
+ unset($struct[$field]);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ $sql[] = $alter . ' DROP `' . $field . '`';
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- print_r($drop);
|
|
|
- print_r($change);
|
|
|
- print_r($struct);
|
|
|
- die;
|
|
|
-
|
|
|
- $alter = 'ALTER TABLE `' . $table;
|
|
|
-
|
|
|
- if ($drop) {
|
|
|
-
|
|
|
+ if ($struct) {
|
|
|
+ foreach ($struct as $k => $v) {
|
|
|
+ $sql[] = $alter . ' ADD ' . self::createField($k, $v);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+ $sql = implode(';', $sql);
|
|
|
+ return $sql;
|
|
|
+ }
|
|
|
+ public static function index($table, $index, $del = array())
|
|
|
+ {
|
|
|
$sql = array();
|
|
|
- foreach ($config as $k => $v) {
|
|
|
- if (!isset($v['default'])) {
|
|
|
- $v['default'] = '';
|
|
|
+ $alter = 'ALTER TABLE `' . $table . '` ';
|
|
|
+ if ($del) {
|
|
|
+ foreach ($del as $v) {
|
|
|
+ if ($v['Key_name'] != 'PRIMARY') {
|
|
|
+ $sql[] = $alter . ' DROP INDEX ' . $v['Key_name'];
|
|
|
+ }
|
|
|
}
|
|
|
- if (isset($v['type'])) {
|
|
|
- $v = array
|
|
|
- (
|
|
|
- 'add', $k, $k, $v['type'] . ' ' . $v['default'] . ' ' . $v['name'],
|
|
|
- );
|
|
|
+ }
|
|
|
+ if ($index) {
|
|
|
+ foreach ($index as $k => $v) {
|
|
|
+ $t = 'INDEX';
|
|
|
+ if (strpos($v, '.')) {
|
|
|
+ list($v, $t) = explode('.', $v);
|
|
|
+ }
|
|
|
+ $sql[] = $alter . ' ADD ' . strtoupper($t) . ' ' . $k . ' (' . $v . ')';
|
|
|
}
|
|
|
-
|
|
|
- if (empty($v[3])) {
|
|
|
- continue;
|
|
|
+ }
|
|
|
+ $sql = implode(';', $sql);
|
|
|
+ return $sql;
|
|
|
+ }
|
|
|
+ public static function select($table, $param, &$bind, $set = array())
|
|
|
+ {
|
|
|
+ $col = '*';
|
|
|
+ $rule = '';
|
|
|
+ if (isset($set['col'])) {
|
|
|
+ $col = $set['col'];
|
|
|
+ }
|
|
|
+ if (isset($set['group'])) {
|
|
|
+ $rule .= ' GROUP BY ' . $set['group'];
|
|
|
+ }
|
|
|
+ if (isset($set['order'])) {
|
|
|
+ $rule .= ' ORDER BY ' . $set['order'];
|
|
|
+ }
|
|
|
+ if (isset($set['limit'])) {
|
|
|
+ $rule .= ' LIMIT ' . $set['limit'];
|
|
|
+ }
|
|
|
+ if (isset($set['join'])) {
|
|
|
+ $temp = explode('_', $table);
|
|
|
+ $prefix = $temp[0] . '_' . $temp[1] . '_';
|
|
|
+ $table .= ' AS ' . $temp[2];
|
|
|
+ foreach ($set['join'] as $k => $v) {
|
|
|
+ $table .= ' ' . $v['type'] . ' ' . $prefix . $v['table'] . ' AS ' . $v['table'] . ' ON ' . $v['on'];
|
|
|
}
|
|
|
- $sql[$k] = '';
|
|
|
- if (isset($v[3]) && strpos($v[3], ' ') !== false) {
|
|
|
- $com = explode(' ', $v[3]);
|
|
|
- if (strstr($com[0], '-')) {
|
|
|
- $sql[$k] = str_replace('-', '(', $com[0]) . ') ';
|
|
|
- } else {
|
|
|
- $sql[$k] = $com[0] . ' ';
|
|
|
+ }
|
|
|
+ if ($param) {
|
|
|
+ $where = ' WHERE';
|
|
|
+ $i = 0;
|
|
|
+ foreach ($param as $k => $v) {
|
|
|
+ if (strpos($k, '#')) {
|
|
|
+ $k = trim($k, '#');
|
|
|
}
|
|
|
-
|
|
|
- if (isset($com[1]) && $com[1] != '') {
|
|
|
- $sql[$k] .= 'not null default \'' . $com[1] . '\'';
|
|
|
+ if ($k == 'or' || $k == 'and') {
|
|
|
+ $link = '';
|
|
|
+ foreach ($v as $k1 => $v1) {
|
|
|
+ self::where($link, $bind, $i, $k1, $v1);
|
|
|
+ }
|
|
|
+ $link = rtrim($link, 'and');
|
|
|
+ $where = rtrim($where, 'and');
|
|
|
+ $where .= $k.' (' . $link . ') and';
|
|
|
} else {
|
|
|
- $sql[$k] .= 'not null';
|
|
|
+ self::where($where, $bind, $i, $k, $v);
|
|
|
}
|
|
|
+ }
|
|
|
+ $where = rtrim($where, 'and');
|
|
|
+ }
|
|
|
+ return 'SELECT ' . $col . ' FROM ' . $table . $where . $rule;
|
|
|
+ }
|
|
|
|
|
|
- if (!empty($com[2])) {
|
|
|
- $sql[$k] .= ' comment \'' . $com[2] . '\'';
|
|
|
+
|
|
|
+ private static function where(&$where, &$bind, &$i, $k, $v)
|
|
|
+ {
|
|
|
+ $j = ':' . $k . $i;
|
|
|
+ $k = ' `' . $k . '` ';
|
|
|
+ if (is_array($v)) {
|
|
|
+ if (empty($v[0])) {
|
|
|
+ foreach ($v as $k1 => $v1) {
|
|
|
+ self::where($where, $bind, $i, $k1, $v1);
|
|
|
}
|
|
|
- $sql[$k] = strtoupper($sql[$k]);
|
|
|
+ return;
|
|
|
}
|
|
|
-
|
|
|
- if ($v[0] == 'add') {
|
|
|
-
|
|
|
- $sql[$k] = $alter . '` ADD `' . $v[1] . '` ' . $sql[$k];
|
|
|
- } elseif ($v[0] == 'delete') {
|
|
|
-
|
|
|
- $sql[$k] = $alter . '` DROP `' . $v[1] . '`';
|
|
|
+ if (empty($v[1])) {
|
|
|
+ $v[1] = '=';
|
|
|
+ }
|
|
|
+ if (empty($v[2])) {
|
|
|
+ $v[2] = 'and';
|
|
|
+ }
|
|
|
+ if ($v[1] == 'between') {
|
|
|
+ $jd = $j.'d';
|
|
|
+ if (is_string($v[0])) {
|
|
|
+ $v[0] = explode(',', $v[0]);
|
|
|
+ }
|
|
|
+ $bind[$j] = $v[0][0];
|
|
|
+ $bind[$jd] = $v[0][1];
|
|
|
+ $where .= $k . 'between ' . $j . ' and ' . $jd;
|
|
|
+ } elseif ($v[1] == 'in') {
|
|
|
+ $where .= $k . 'in('.$j.')';
|
|
|
+ } elseif ($v[1] == 'like') {
|
|
|
+ if (strstr($v[0], ',')) {
|
|
|
+ $concat = 'concat(",", ' . $j . ',",")';
|
|
|
+ } else {
|
|
|
+ $concat = $j;
|
|
|
+ }
|
|
|
+ $instr = 'instr('.$concat.', ' . $k . ')';
|
|
|
+ $where .= $k . ' ' . $instr . ' > 0';
|
|
|
} else {
|
|
|
-
|
|
|
- $sql[$k] = $alter . '` CHANGE `' . $v[1] . '` `' . $v[2] . '` ' . $sql[$k];
|
|
|
+ $where .= $k . $v[1] . $j;
|
|
|
}
|
|
|
+ $where .= ' ' . $v[2];
|
|
|
+ $v = $v[0];
|
|
|
+ } else {
|
|
|
+ $where .= $k . '=' . $j . ' and';
|
|
|
+ }
|
|
|
+ if (empty($bind[$j])) {
|
|
|
+ $bind[$j] = $v;
|
|
|
}
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
|
|
|
- $sql = implode(';', $sql);
|
|
|
|
|
|
- return $sql;
|
|
|
- }
|
|
|
private function col($col)
|
|
|
{
|
|
|
$result = '';
|
|
@@ -175,42 +261,6 @@ class Sql
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
|
- public static function select($table, $col = '')
|
|
|
- {
|
|
|
- $this->table = $table;
|
|
|
- $where = $this->createWhere();
|
|
|
-
|
|
|
- $join = isset($this->join) ? implode(' ', $this->join) : '';
|
|
|
-
|
|
|
- if (isset($this->orderBy) && $this->orderBy) {
|
|
|
- $order = $this->orderBy;
|
|
|
- } else {
|
|
|
- $order = $this->order;
|
|
|
- }
|
|
|
- $sql = 'SELECT ' . $this->col($col) . ' FROM `' . $table . '` ' . $join . $where . ' ' . $this->group . ' ' . $order . ' ' . $this->limit;
|
|
|
-
|
|
|
- $this->init();
|
|
|
-
|
|
|
- return $sql;
|
|
|
- }
|
|
|
- public static function createWhere()
|
|
|
- {
|
|
|
- $where = '';
|
|
|
- if ($this->where) {
|
|
|
- if (isset($this->colOrder)) {
|
|
|
- ksort($this->where);
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($this->between)) {
|
|
|
- $where = 'WHERE ' . $this->between . ' ' . implode(' ', $this->where);
|
|
|
- $this->limit = '';
|
|
|
- } else {
|
|
|
- $where = 'WHERE ' . ltrim(implode(' ', $this->where), ' and');
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $where;
|
|
|
- }
|
|
|
|
|
|
|
|
|
* count
|
|
@@ -250,48 +300,6 @@ class Sql
|
|
|
|
|
|
return $sql;
|
|
|
}
|
|
|
- public static function showIndex($table)
|
|
|
- {
|
|
|
- return 'SHOW INDEX FROM ' . $table . ' ';
|
|
|
- }
|
|
|
- public static function dropIndex($table, $name)
|
|
|
- {
|
|
|
- return 'ALTER TABLE ' . $table . ' DROP INDEX ' . $name;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * index
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function index($table, $value)
|
|
|
- {
|
|
|
- $sql = 'ALTER TABLE ' . $table . ' ';
|
|
|
-
|
|
|
- $max = count($value) - 1;
|
|
|
-
|
|
|
- $i = 0;
|
|
|
-
|
|
|
- foreach ($value as $k => $v) {
|
|
|
- $type = 'INDEX';
|
|
|
- if (strpos($v, '.')) {
|
|
|
- $t = explode('.', $v);
|
|
|
- $v = $t[0];
|
|
|
- $type = ucwords($t[1]);
|
|
|
- }
|
|
|
- $sql .= 'ADD ' . $type . ' ' . $k . ' (' . $v . ')';
|
|
|
-
|
|
|
- if ($i >= $max) {
|
|
|
- $sql .= '';
|
|
|
- } else {
|
|
|
- $sql .= ',';
|
|
|
- }
|
|
|
-
|
|
|
- $i++;
|
|
|
- }
|
|
|
-
|
|
|
- return $sql;
|
|
|
- }
|
|
|
|
|
|
|
|
|
* insert
|
|
@@ -353,18 +361,6 @@ class Sql
|
|
|
return $sql;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- * explain
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function explain($sql)
|
|
|
- {
|
|
|
- $sql = 'EXPLAIN ' . $sql . ' ';
|
|
|
-
|
|
|
- return $sql;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
* update
|
|
|
*
|
|
@@ -410,334 +406,4 @@ class Sql
|
|
|
|
|
|
return $sql;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- * truncate
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function truncate($table)
|
|
|
- {
|
|
|
- $sql = 'TRUNCATE TABLE `' . $table . '`';
|
|
|
-
|
|
|
- return $sql;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * opt
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function opt($table)
|
|
|
- {
|
|
|
- $sql = 'OPTIMIZE TABLE `' . $table . '`';
|
|
|
-
|
|
|
- return $sql;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * sql
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function sql($sql)
|
|
|
- {
|
|
|
- return $sql;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * init
|
|
|
- *
|
|
|
- * @return object
|
|
|
- */
|
|
|
- public static function init()
|
|
|
- {
|
|
|
- $this->where = $this->value = $this->col = $this->join = $this->score = $this->as = array();
|
|
|
- $this->order = '';
|
|
|
- $this->orderBy = '';
|
|
|
- $this->group = '';
|
|
|
- $this->limit = '';
|
|
|
- $this->prefix = '';
|
|
|
-
|
|
|
- return $this;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * where
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function where($param)
|
|
|
- {
|
|
|
- $where = '';
|
|
|
- if (empty($param[2])) {
|
|
|
- $param[2] = '=';
|
|
|
- }
|
|
|
-
|
|
|
- $value = 1;
|
|
|
- if (strpos($param[2], '|') !== false) {
|
|
|
- $temp = explode('|', $param[2]);
|
|
|
- $param[2] = $temp[0];
|
|
|
- $value = $temp[1];
|
|
|
- }
|
|
|
-
|
|
|
- if (empty($param[3])) {
|
|
|
- $param[3] = 'and';
|
|
|
- }
|
|
|
-
|
|
|
- if (strpos($param[3], '))') !== false) {
|
|
|
- $temp = explode('))', $param[3]);
|
|
|
- $param[3] = $temp[0];
|
|
|
- $where = ' ))';
|
|
|
- } elseif (strpos($param[3], ')') !== false) {
|
|
|
- $temp = explode(')', $param[3]);
|
|
|
- $param[3] = $temp[0];
|
|
|
- $where = ' )';
|
|
|
- }
|
|
|
-
|
|
|
- $col = '`s_' . $param[0] . '`';
|
|
|
-
|
|
|
-
|
|
|
- if (!strpos($param[0], '.')) {
|
|
|
- $param[0] = $this->prefix . '`' . $param[0] . '`';
|
|
|
- }*/
|
|
|
-
|
|
|
- if (!$this->prefix && !strstr($param[0], ',')) {
|
|
|
- $param[0] = '`' . $param[0] . '`';
|
|
|
- }
|
|
|
- if (strstr($param[0], ',')) {
|
|
|
- $temp = explode(',', $param[0]);
|
|
|
- $w = array();
|
|
|
- if (strstr($param[2], 'like')) {
|
|
|
- $content = explode('^', $param[2]);
|
|
|
- $param[2] = $content[0];
|
|
|
-
|
|
|
- foreach ($temp as $v) {
|
|
|
- if (isset($content[1]) && strstr($content[1], ',')) {
|
|
|
- $concat = 'concat(",", ' . $v . ',",")';
|
|
|
- } else {
|
|
|
- $concat = $v;
|
|
|
- }
|
|
|
- $instr = 'instr('.$concat.', ' . $param[1] . ')';
|
|
|
- $w[] = ' ' . $instr . ' > 0 ';
|
|
|
- }
|
|
|
- } else {
|
|
|
- foreach ($temp as $v) {
|
|
|
- $w[] = '`' . $v . '` '. $param[2] . ' ' . $param[1];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $where = ' and (' . implode(' or ', $w) . ') ' . $where;
|
|
|
- } elseif (strstr($param[2], 'like')) {
|
|
|
- $content = explode('^', $param[2]);
|
|
|
- $param[2] = $content[0];
|
|
|
-
|
|
|
- if (isset($content[1]) && strstr($content[1], ',')) {
|
|
|
-
|
|
|
- $len = strlen($content[1]);
|
|
|
- if (isset($content[1]) && strstr($content[1], ',')) {
|
|
|
- $concat = 'concat(' . $param[0] . ',",")';
|
|
|
- } else {
|
|
|
- $concat = $param[0];
|
|
|
- }
|
|
|
- $instr = 'left('.$concat.', '.$len.')=' . $param[1];
|
|
|
- $where = $param[3] . ' ' . $instr . $where;
|
|
|
- } else {
|
|
|
- if ((isset($content[1]) && strstr($content[1], ',')) || $param[2] == 'like_num') {
|
|
|
- $concat = 'concat(",", ' . $param[0] . ',",")';
|
|
|
- } else {
|
|
|
- $concat = $param[0];
|
|
|
- }
|
|
|
- $instr = 'instr('.$concat.', ' . $param[1] . ')';
|
|
|
- $where = $param[3] . ' ' . $instr . ' > 0' . $where;
|
|
|
- if ($param[2] == 'like_score') {
|
|
|
- $this->orderBy = 'order by col_score desc';
|
|
|
- $this->score[] = 'IF('.$instr.', IF(' . $param[0] . '=' . $param[1] . ', 1000*'.$value.', (100-'.$instr.')*'.$value.'), 0)';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- } else {
|
|
|
- $where = $param[3] . ' ' . $param[0] . ' ' . $param[2] . ' ' . $param[1] . $where;
|
|
|
- }
|
|
|
-
|
|
|
- if ($param[2] == 'in' && isset($param[4]) && $param[4] == 'order') {
|
|
|
- $param[1] = str_replace(array('(',')'), '', $param[1]);
|
|
|
- $this->orderBy = 'order by field(' . $param[0] . ', ' . $param[1] . ')';
|
|
|
- }
|
|
|
-
|
|
|
- if (isset($this->colOrder)) {
|
|
|
- $param[0] = trim($param[0], '`');
|
|
|
- if (isset($this->colOrder[$param[0]])) {
|
|
|
- $this->where[$this->colOrder[$param[0]]] = $where;
|
|
|
- } else {
|
|
|
- $num = count($this->where) + 100;
|
|
|
- $this->where[$num] = $where;
|
|
|
- }
|
|
|
- } else {
|
|
|
- $this->where[] = $where;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * order
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function order($param)
|
|
|
- {
|
|
|
- if (is_array($param[0])) {
|
|
|
- $this->order = 'order by ';
|
|
|
- foreach ($param[0] as $k => $v) {
|
|
|
- $k1 = '';
|
|
|
- if (strpos($k, '.')) {
|
|
|
- $t = explode('.', $k);
|
|
|
- $k = $t[1];
|
|
|
- $k1 = $t[0] . '.';
|
|
|
- }
|
|
|
- $order[] = $k1 . '`' . $k . '` ' . $v;
|
|
|
- }
|
|
|
-
|
|
|
- $this->order .= implode(',', $order);
|
|
|
-
|
|
|
-
|
|
|
- } else {
|
|
|
- if (empty($param[1])) {
|
|
|
- $param[1] = 'desc';
|
|
|
- }
|
|
|
-
|
|
|
- if (!strstr($param[0], '()') && !strstr($param[0], 't_')) {
|
|
|
- $param[0] = '`'.$param[0].'`';
|
|
|
- }
|
|
|
-
|
|
|
- $this->order = 'order by ' . $param[0] . ' ' . $param[1];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * group
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function group($param)
|
|
|
- {
|
|
|
- if (is_array($param) && isset($param[0])) {
|
|
|
-
|
|
|
- $param = $param[0];
|
|
|
-
|
|
|
- $this->group = ' group by ' . $param;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (is_string($param) && $param != 'id') {
|
|
|
- if ($param == 'day') {
|
|
|
- $this->col = 'FROM_UNIXTIME(day, "%Y-%m-%d") as day';
|
|
|
- } elseif ($param == 'month') {
|
|
|
- $this->col = 'FROM_UNIXTIME(day, "%Y-%m") as month';
|
|
|
- } elseif ($param == 'year') {
|
|
|
- $this->col = 'FROM_UNIXTIME(day, "%Y") as year';
|
|
|
- } elseif (strpos($param, ',') === false) {
|
|
|
- $this->col = $param;
|
|
|
- }
|
|
|
-
|
|
|
- $this->group = ' group by ' . $param;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * join 为临时解决方案,不建议用join
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function join($param)
|
|
|
- {
|
|
|
- $this->prefix = 't_1.';
|
|
|
- $this->join[] = 'as t_1';
|
|
|
- $num = 2;
|
|
|
- foreach ($param as $k => $v) {
|
|
|
- if ($v) {
|
|
|
- $table = 't_' . $num;
|
|
|
- if (strpos($v['on'][0], '.') === false) {
|
|
|
- $v['on'][0] = 't_1.`' . $v['on'][0] . '`';
|
|
|
- } else {
|
|
|
- $v['on'][0] = 't_' . $v['on'][0];
|
|
|
- }
|
|
|
-
|
|
|
- $v['on'][1] = $table . '.`' . $v['on'][1] . '`';
|
|
|
-
|
|
|
- $v['table'] = str_replace('/', '_', $v['table']);
|
|
|
- if (DEVER_PROJECT != 'default') {
|
|
|
- $v['table'] = DEVER_PROJECT . '_' . $v['table'];
|
|
|
- }
|
|
|
-
|
|
|
- $this->join[] = ucwords($v['type']) . ' `' . $v['table'] . '` AS ' . $table . ' ON ' . $v['on'][0] . '=' . $v['on'][1] . ' ';
|
|
|
-
|
|
|
- if (isset($v['col'])) {
|
|
|
- $this->col = $v['col'];
|
|
|
- }
|
|
|
-
|
|
|
- $num++;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * limit
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function limit($param)
|
|
|
- {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- $this->limit = 'limit ' . $param[0];
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * reset limit
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function reset($param)
|
|
|
- {
|
|
|
- $this->{$param[0]} = '';
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * add
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static function add($param)
|
|
|
- {
|
|
|
- $this->col[] = '`' . $param[0] . '`';
|
|
|
- $this->value[] = $param[1];
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * set
|
|
|
- *
|
|
|
- * @return string
|
|
|
- */
|
|
|
- public static 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];
|
|
|
- }
|
|
|
}
|