|
@@ -1,40 +1,41 @@
|
|
|
<?php namespace Dever;
|
|
|
+use Dever;
|
|
|
class Sql
|
|
|
{
|
|
|
- public static function desc($table)
|
|
|
+ public function desc($table)
|
|
|
{
|
|
|
return 'DESC ' . $table;
|
|
|
}
|
|
|
- public static function truncate($table)
|
|
|
+ public function truncate($table)
|
|
|
{
|
|
|
return 'TRUNCATE TABLE `' . $table . '`';
|
|
|
}
|
|
|
- public static function optimize($table)
|
|
|
+ public function optimize($table)
|
|
|
{
|
|
|
return 'OPTIMIZE TABLE `' . $table . '`';
|
|
|
}
|
|
|
- public static function analyze($table)
|
|
|
+ public function analyze($table)
|
|
|
{
|
|
|
return 'ANALYZE TABLE `' . $table . '`';
|
|
|
}
|
|
|
- public static function explain($sql)
|
|
|
+ public function explain($sql)
|
|
|
{
|
|
|
return 'EXPLAIN ' . $sql;
|
|
|
}
|
|
|
- public static function showIndex($table)
|
|
|
+ public function showIndex($table)
|
|
|
{
|
|
|
return 'SHOW INDEX FROM ' . $table;
|
|
|
}
|
|
|
- public static function create($config)
|
|
|
+ public function create($config)
|
|
|
{
|
|
|
- if (isset(Config::get('setting')['database']['create']) && !Config::get('setting')['database']['create']) {
|
|
|
+ if (isset(Dever::config('setting')['database']['create']) && !Dever::config('setting')['database']['create']) {
|
|
|
return;
|
|
|
}
|
|
|
$sql = 'DROP TABLE IF EXISTS `' . $config['table'] . '`;CREATE TABLE IF NOT EXISTS `' . $config['table'] . '`(';
|
|
|
$struct = array('id' => array('name' => 'ID', 'type' => 'int(11)'),'cdate' => array('name' => 'cdate', 'type' => 'int(11)'));
|
|
|
$struct = array_merge($struct, $config['struct']);
|
|
|
foreach ($struct as $k => $v) {
|
|
|
- $sql .= self::createField($k, $v) . ',';
|
|
|
+ $sql .= $this->createField($k, $v) . ',';
|
|
|
}
|
|
|
$sql = rtrim($sql, ',') . ')';
|
|
|
if (isset($config['auto'])) {
|
|
@@ -45,7 +46,7 @@ class Sql
|
|
|
}
|
|
|
return $sql . ' COMMENT="'.$config['name'].'"';
|
|
|
}
|
|
|
- public static function createField($name, $set)
|
|
|
+ public function createField($name, $set)
|
|
|
{
|
|
|
$field = '`' . $name . '` ' . strtoupper($set['type']);
|
|
|
if ($name == 'id') {
|
|
@@ -64,7 +65,7 @@ class Sql
|
|
|
}
|
|
|
return $field;
|
|
|
}
|
|
|
- public static function alter($table, $struct, $data)
|
|
|
+ public function alter($table, $struct, $data)
|
|
|
{
|
|
|
$sql = [];
|
|
|
$alter = 'ALTER TABLE `' . $table . '` ';
|
|
@@ -74,7 +75,7 @@ class Sql
|
|
|
$set = $struct[$field] ?? false;
|
|
|
if ($set) {
|
|
|
if ($set['type'] != $v['Type'] || (isset($set['default']) && $set['default'] != $v['Default'])) {
|
|
|
- $sql[] = $alter . ' CHANGE `' . $field . '` ' . self::createField($field, $set);
|
|
|
+ $sql[] = $alter . ' CHANGE `' . $field . '` ' . $this->createField($field, $set);
|
|
|
} else {
|
|
|
unset($struct[$field]);
|
|
|
}
|
|
@@ -85,12 +86,12 @@ class Sql
|
|
|
}
|
|
|
if ($struct) {
|
|
|
foreach ($struct as $k => $v) {
|
|
|
- $sql[] = $alter . ' ADD ' . self::createField($k, $v);
|
|
|
+ $sql[] = $alter . ' ADD ' . $this->createField($k, $v);
|
|
|
}
|
|
|
}
|
|
|
return implode(';', $sql);
|
|
|
}
|
|
|
- public static function index($table, $index, $del = [])
|
|
|
+ public function index($table, $index, $del = [])
|
|
|
{
|
|
|
$sql = [];
|
|
|
$alter = 'ALTER TABLE `' . $table . '` ';
|
|
@@ -110,7 +111,7 @@ class Sql
|
|
|
}
|
|
|
return implode(';', $sql);
|
|
|
}
|
|
|
- public static function partition($table, $partition, $index)
|
|
|
+ public function partition($table, $partition, $index)
|
|
|
{
|
|
|
$state = true;
|
|
|
foreach ($index as $k => $v) {
|
|
@@ -147,10 +148,10 @@ class Sql
|
|
|
if ($state) {
|
|
|
return $alter . 'PARTITIONS ' . $partition['value'];
|
|
|
}
|
|
|
- return self::desc($table);
|
|
|
+ return $this->desc($table);
|
|
|
}
|
|
|
}
|
|
|
- public static function select($table, $param, &$bind, $set = [], $field = [], $version = false, $type = '')
|
|
|
+ public function select($table, $param, &$bind, $set = [], $field = [], $version = false, $type = '')
|
|
|
{
|
|
|
$col = '*';
|
|
|
$rule = '';
|
|
@@ -174,7 +175,7 @@ class Sql
|
|
|
}
|
|
|
if (isset($set['limit'])) {
|
|
|
if (is_array($set['limit']) && !$type) {
|
|
|
- $table .= ' inner join (select id from ' . $table . self::where($param, $bind, $field) . $rule . ' limit ' . $set['limit'][0].','.$set['limit'][1].') as t on '.$table.'.id=t.id';
|
|
|
+ $table .= ' inner join (select id from ' . $table . $this->where($param, $bind, $field) . $rule . ' limit ' . $set['limit'][0].','.$set['limit'][1].') as t on '.$table.'.id=t.id';
|
|
|
$rule = '';
|
|
|
$param = false;
|
|
|
} else {
|
|
@@ -189,9 +190,9 @@ class Sql
|
|
|
if ($version) {
|
|
|
$rule .= ' FOR UPDATE';
|
|
|
}
|
|
|
- return 'SELECT ' . $col . ' FROM ' . $table . self::where($param, $bind, $field, $type) . $rule;
|
|
|
+ return 'SELECT ' . $col . ' FROM ' . $table . $this->where($param, $bind, $field, $type) . $rule;
|
|
|
}
|
|
|
- public static function where($param, &$bind, $field = [], $type = '')
|
|
|
+ public function where($param, &$bind, $field = [], $type = '')
|
|
|
{
|
|
|
if ($param) {
|
|
|
$first = $second = '';
|
|
@@ -205,21 +206,21 @@ class Sql
|
|
|
$first_link = $second_link = '';
|
|
|
foreach ($v as $k1 => $v1) {
|
|
|
if (is_array($v1)) {
|
|
|
- self::field($second_link, $bind, $i, $k1, $v1[0], $v1[1], $field, $type);
|
|
|
+ $this->field($second_link, $bind, $i, $k1, $v1[0], $v1[1], $field, $type);
|
|
|
} else {
|
|
|
- self::field($first_link, $bind, $i, $k1, '=', $v1, $field, $type);
|
|
|
+ $this->field($first_link, $bind, $i, $k1, '=', $v1, $field, $type);
|
|
|
}
|
|
|
}
|
|
|
- $second .= ' ' . $k . ' (' . self::replace($first_link) . $second_link . ')';
|
|
|
+ $second .= ' ' . $k . ' (' . $this->replace($first_link) . $second_link . ')';
|
|
|
} else {
|
|
|
if (is_array($v)) {
|
|
|
- self::field($second, $bind, $i, $k, $v[0], $v[1], $field, $type);
|
|
|
+ $this->field($second, $bind, $i, $k, $v[0], $v[1], $field, $type);
|
|
|
} else {
|
|
|
- self::field($first, $bind, $i, $k, '=', $v, $field, $type);
|
|
|
+ $this->field($first, $bind, $i, $k, '=', $v, $field, $type);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return ' WHERE ' . self::replace($first . $second);
|
|
|
+ return ' WHERE ' . $this->replace($first . $second);
|
|
|
} elseif (is_numeric($param)) {
|
|
|
if ($type == 'Influxdb') {
|
|
|
return ' WHERE "id" = \'' . $param . '\'';
|
|
@@ -233,7 +234,7 @@ class Sql
|
|
|
return '';
|
|
|
}
|
|
|
}
|
|
|
- private static function field(&$sql, &$bind, &$num, $key, $symbol, $value, $field, $type)
|
|
|
+ private function field(&$sql, &$bind, &$num, $key, $symbol, $value, $field, $type)
|
|
|
{
|
|
|
$prefix = '';
|
|
|
if (strstr($key, '.')) {
|
|
@@ -306,7 +307,7 @@ class Sql
|
|
|
$state && $bind[$index] = $value;
|
|
|
$num++;
|
|
|
}
|
|
|
- public static function insert($table, $data, &$bind, $field)
|
|
|
+ public function insert($table, $data, &$bind, $field)
|
|
|
{
|
|
|
$sql = 'INSERT INTO `' . $table . '` SET ';#IGNORE
|
|
|
foreach ($data as $k => $v) {
|
|
@@ -321,7 +322,7 @@ class Sql
|
|
|
}
|
|
|
return rtrim($sql, ',');
|
|
|
}
|
|
|
- public static function update($table, $param, $data, &$bind, $field)
|
|
|
+ public function update($table, $param, $data, &$bind, $field)
|
|
|
{
|
|
|
$i = 0;
|
|
|
$sql = 'UPDATE `' . $table . '` SET ';
|
|
@@ -345,13 +346,13 @@ class Sql
|
|
|
$sql .= '`' . $k . '`=' . $a . ':' . $k . ',';
|
|
|
$bind[':'.$k] = $v;
|
|
|
}
|
|
|
- return rtrim($sql, ',') . self::where($param, $bind, $field);
|
|
|
+ return rtrim($sql, ',') . $this->where($param, $bind, $field);
|
|
|
}
|
|
|
- public static function delete($table, $param, &$bind, $field)
|
|
|
+ public function delete($table, $param, &$bind, $field)
|
|
|
{
|
|
|
- return 'DELETE FROM `' . $table . '`' . self::where($param, $bind, $field);
|
|
|
+ return 'DELETE FROM `' . $table . '`' . $this->where($param, $bind, $field);
|
|
|
}
|
|
|
- public static function inserts($table, $param)
|
|
|
+ public function inserts($table, $param)
|
|
|
{
|
|
|
$num = $param['num'] ?? 1;
|
|
|
$sql = 'INSERT INTO `' . $table . '` (' . $param['field'] . ') VALUES ';
|
|
@@ -363,11 +364,11 @@ class Sql
|
|
|
$sql .= implode(',', $insert) . ',';
|
|
|
return rtrim($sql, ',');
|
|
|
}
|
|
|
- public static function distance($lng, $lat)
|
|
|
+ public function distance($lng, $lat)
|
|
|
{
|
|
|
return 'round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2) as distance';
|
|
|
}
|
|
|
- private static function replace($string)
|
|
|
+ private function replace($string)
|
|
|
{
|
|
|
if (strpos($string, ' and ') === 0) {
|
|
|
$string = substr($string, 5);
|