1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447 |
- <?php
- namespace core\basic;
- use core\basic\Config;
- use core\view\Paging;
- use core\database\Mysqli;
- use core\database\Sqlite;
- use core\database\Pdo;
- class Model
- {
-
- public $table;
-
- public $pk = 'id';
-
- public $autoTimestamp = false;
-
- public $intTimeFormat = false;
-
- public $updateTimeField = 'update_time';
-
- public $createTimeField = 'create_time';
-
- private $exeSql = array();
-
- private $decode = false;
-
- private $sql = array();
-
- private $showSql = false;
-
- private $showRs = false;
-
- private $dbDriver;
-
- private $selectSql = "SELECT %distinct% %field% FROM %table% %join% %where% %group% %having% %order% %union% %limit%";
-
- private $countSql = "SELECT COUNT(*) AS sum FROM %table% %join% %where% %group% %having% %union%";
- private $countSql2 = "SELECT %distinct% %field% FROM %table% %join% %where% %group% %having% %union%";
-
- private $insertSql = "INSERT INTO %table% %field% VALUES %value%";
-
- private $insertMultSql = "INSERT INTO %table% %field% %value%";
-
- private $insertFromSql = "INSERT INTO %table% %field% %from%";
-
- private $deleteSql = "DELETE FROM %table% %join% %where%";
-
- private $updateSql = "UPDATE %table% SET %value% %join% %where%";
-
- private $checkIndex = "show index from %table%";
-
- public function __construct()
- {
- if (! $this->table) {
- $table_name = Config::get('database.prefix') . hump_to_underline(str_replace('Model', '', basename(get_called_class())));
- $this->table = $table_name;
- }
- }
-
- public function __call($methed, $args)
- {
- if (method_exists($this->getDb(), $methed)) {
- $result = call_user_func_array(array(
- $this->getDb(),
- $methed
- ), $args);
- return $result;
- } else {
- error('不存在数据库操作方法“' . $methed . '”,请核对再试!');
- }
- }
-
- private function getDb()
- {
- if (! $this->dbDriver) {
- $type = Config::get('database.type');
- switch ($type) {
- case 'mysqli':
- $this->dbDriver = Mysqli::getInstance();
- break;
- case 'sqlite':
- $this->dbDriver = Sqlite::getInstance();
- break;
- default:
- $this->dbDriver = Pdo::getInstance();
- }
- }
- return $this->dbDriver;
- }
-
- private function buildSql($sql, $clear = true)
- {
- preg_match_all('/\%([\w]+)\%/', $sql, $matches);
- foreach ($matches[1] as $key => $value) {
- if (isset($this->sql[$value]) && $this->sql[$value]) {
- $sql = str_replace("%$value%", $this->sql[$value], $sql);
- } else {
- if ($value == 'table') {
- $sql = str_replace("%$value%", $this->table, $sql);
- } else {
- $sql = str_replace("%$value%", '', $sql);
- }
- }
- }
-
- $this->exeSql[] = $sql;
- if ($clear) {
- $this->pk = 'id';
- $this->autoTimestamp = false;
- $this->intTimeFormat = false;
- $this->updateTimeField = 'update_time';
- $this->createTimeField = 'create_time';
- $this->sql = array();
- }
-
- if ($this->showSql && $clear) {
- exit($sql);
- } else {
- return $sql;
- }
- }
-
- final public function begin()
- {
- $this->getDb()->begin();
- }
-
- final public function commit()
- {
- $this->getDb()->commit();
- }
-
- final protected function outData($result)
- {
- if ($this->decode) {
- $result = decode_string($result);
- $this->decode = false;
- } else {
- $result = decode_slashes($result);
- }
- if ($this->showRs) {
- print_r($result);
- exit();
- } else {
- return $result;
- }
- }
-
- final public function decode($flag = true)
- {
- if ($flag === true)
- $this->decode = true;
- return $this;
- }
-
- final public function showSql($flag = true)
- {
- if ($flag === true)
- $this->showSql = true;
- return $this;
- }
-
- final public function showRs($flag = true)
- {
- if ($flag === true)
- $this->showRs = true;
- return $this;
- }
-
- final public function autoTime($flag = true)
- {
- if ($flag === true)
- $this->sql['auto_time'] = true;
- return $this;
- }
-
- final public function table($table)
- {
- if (is_array($table)) {
- $table_string = '';
- foreach ($table as $key => $value) {
- if (is_int($key)) {
- $table_string .= '`' . $value . '`,';
- } else {
- $table_string .= '`' . $key . '` AS ' . $value . ',';
- }
- }
- $this->sql['table'] = substr($table_string, 0, - 1);
- } else {
- $this->sql['table'] = $table;
- }
- return $this;
- }
-
- final public function name($table)
- {
- $prefix = Config::get('database.prefix');
- if (is_array($table)) {
- $table_string = '';
- foreach ($table as $key => $value) {
- if (is_int($key)) {
- $table_string .= '`' . $prefix . $value . '`,';
- } else {
- $table_string .= '`' . $prefix . $key . '` AS ' . $value . ',';
- }
- }
- $this->table = substr($table_string, 0, - 1);
- } else {
- $this->table = $prefix . $table;
- }
- return $this;
- }
-
- final public function alias($alias)
- {
- if ($alias) {
- if (! isset($this->table))
- error('调用alias之前必须先设置table');
- if (strpos($this->table, ' AS ') === false) {
- $this->table = $this->table . ' AS ' . $alias;
- }
- }
- return $this;
- }
-
- final public function distinct($flag = true)
- {
- if ($flag === true)
- $this->sql['distinct'] = 'DISTINCT';
- return $this;
- }
-
- final public function field($field)
- {
- if (is_array($field)) {
- $field_string = '';
- foreach ($field as $key => $value) {
- if (is_int($key)) {
- $field_string .= $value . ',';
- } else {
- $field_string .= $key . ' AS ' . $value . ',';
- }
- }
- $this->sql['field'] = substr($field_string, 0, - 1);
- } elseif ($field) {
- $this->sql['field'] = $field;
- }
- return $this;
- }
-
-
-
- final public function where($where, $inConnect = 'AND', $outConnect = 'AND', $fuzzy = false)
- {
- if (! $where) {
- return $this;
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= ' ' . $outConnect . '(';
- } else {
- $this->sql['where'] = 'WHERE(';
- }
- if (is_array($where)) {
- $where_string = '';
- $flag = false;
- foreach ($where as $key => $value) {
- if ($flag) {
- $where_string .= ' ' . $inConnect . ' ';
- } else {
- $flag = true;
- }
- if (! is_int($key)) {
- if ($fuzzy) {
- $where_string .= $key . " like '%" . $value . "%' ";
- } else {
- $where_string .= $key . "='" . $value . "' ";
- }
- } else {
- $where_string .= $value;
- }
- }
- $this->sql['where'] .= $where_string . ')';
- } else {
- $this->sql['where'] .= $where . ')';
- }
- return $this;
- }
-
- final public function exists($subSql)
- {
- if (is_callable($subSql)) {
- $subSql = $subSql(new self());
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND EXISTS ($subSql)";
- } else {
- $this->sql['where'] = "WHERE EXISTS ($subSql)";
- }
- return $this;
- }
-
- final public function notExists($subSql)
- {
- if (is_callable($subSql)) {
- $subSql = $subSql(new self());
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND NOT EXISTS ($subSql)";
- } else {
- $this->sql['where'] = "WHERE NOT EXISTS ($subSql)";
- }
- return $this;
- }
-
- final public function in($field, $range)
- {
- if (! $field)
- return $this;
- if (is_array($range)) {
- if (count($range) == 1) {
- $in_string = "$field='$range[0]'";
- } else {
- $in_string = "$field IN (" . implode_quot(',', $range) . ")";
- }
- } else {
- if (preg_match('/,/', $range)) {
- $in_string = "$field IN (" . implode_quot(',', explode(',', $range)) . ")";
- } else {
- $in_string = "$field = '$range'";
- }
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND $in_string";
- } else {
- $this->sql['where'] = "WHERE $in_string";
- }
- return $this;
- }
-
- final public function notIn($field, $range)
- {
- if (! $field)
- return $this;
- if (is_array($range)) {
- $in_string = implode_quot(',', $range);
- } else {
- if (preg_match('/,/', $range)) {
- $in_string = implode_quot(',', explode(',', $range));
- } else {
- $in_string = "'$range'";
- }
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND $field NOT IN ($in_string)";
- } else {
- $this->sql['where'] = "WHERE $field NOT IN ($in_string)";
- }
- return $this;
- }
-
- final public function like($field, $keyword, $matchType = "all")
- {
- if (! $field)
- return $this;
- switch ($matchType) {
- case 'left':
- $keyword = "$keyword%";
- break;
- case 'right':
- $keyword = "%$keyword";
- break;
- case 'equal':
- case '==':
- $keyword = "$keyword";
- break;
- default:
- $keyword = "%$keyword%";
- }
- if (is_array($field) || preg_match('/,/', $field)) {
- if (! is_array($field)) {
- $field = explode(',', $field);
- }
- foreach ($field as $value) {
- if (isset($sqlStr)) {
- $sqlStr .= " OR $value LIKE '$keyword'";
- } else {
- $sqlStr = "$value LIKE '$keyword'";
- }
- }
- } else {
- $sqlStr = "$field LIKE '$keyword'";
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND ($sqlStr)";
- } else {
- $this->sql['where'] = "WHERE ($sqlStr)";
- }
- return $this;
- }
-
- final public function notLike($field, $keyword, $matchType = "all")
- {
- if (! $field)
- return $this;
- switch ($matchType) {
- case 'left':
- $keyword = "$keyword%";
- break;
- case 'right':
- $keyword = "%$keyword";
- break;
- case 'equal':
- case '==':
- $keyword = "$keyword";
- break;
- default:
- $keyword = "%$keyword%";
- }
- if (is_array($field) || preg_match('/,/', $field)) {
- if (! is_array($field)) {
- $field = explode(',', $field);
- }
- foreach ($field as $value) {
- if (isset($sqlStr)) {
- $sqlStr .= " AND $value NOT LIKE '$keyword'";
- } else {
- $sqlStr = "$value NOT LIKE '$keyword'";
- }
- }
- } else {
- $sqlStr = "$field NOT LIKE '$keyword'";
- }
- if (isset($this->sql['where']) && $this->sql['where']) {
- $this->sql['where'] .= " AND ($sqlStr)";
- } else {
- $this->sql['where'] = "WHERE ($sqlStr)";
- }
- return $this;
- }
-
- final public function order($order)
- {
- if (is_array($order)) {
- $order_string = 'ORDER BY ';
- foreach ($order as $key => $value) {
- if (is_int($key)) {
- $order_string .= $value . ',';
- } else {
- $order_string .= $key . ' ' . $value . ',';
- }
- }
- $this->sql['order'] = substr($order_string, 0, - 1);
- } else {
- $this->sql['order'] = 'ORDER BY ' . $order;
- }
- return $this;
- }
-
- final public function limit($limit)
- {
- $var_num = func_num_args();
- if ($var_num > 1 || preg_match('/,/', $limit)) {
- if ($var_num > 1) {
- $var_arr = func_get_args();
- } else {
- $var_arr = explode(',', $limit);
- }
- switch (get_db_type()) {
- case 'mysql':
- $this->sql['limit'] = 'LIMIT ' . $var_arr[0] . ',' . $var_arr[1];
- break;
- case 'sqlite':
- $this->sql['limit'] = 'LIMIT ' . $var_arr[1] . ' OFFSET ' . $var_arr[0];
- break;
- case 'pgsql':
- $this->sql['limit'] = 'LIMIT ' . $var_arr[1] . ' OFFSET ' . $var_arr[0];
- break;
- }
- } else {
- $this->sql['limit'] = 'LIMIT ' . $limit;
- }
- return $this;
- }
-
- final public function group($group)
- {
- if (is_array($group)) {
- $group_string = 'GROUP BY ';
- foreach ($group as $key => $value) {
- $group_string .= $value . ',';
- }
- $this->sql['group'] = substr($group_string, 0, - 1);
- } else {
- $this->sql['group'] = 'GROUP BY ' . $group;
- }
- return $this;
- }
-
- final public function having($having, $inConnect = 'AND', $outConnect = 'AND')
- {
- if (isset($this->sql['having']) && $this->sql['having']) {
- $this->sql['having'] .= ' ' . $outConnect . '(';
- } else {
- $this->sql['having'] = 'HAVING(';
- }
- if (is_array($having)) {
- $having_string = '';
- $flag = false;
- foreach ($having as $key => $value) {
- if ($flag) {
- $having_string .= ' ' . $inConnect . ' ';
- } else {
- $flag = true;
- }
- if (! is_int($key)) {
- $having_string .= $key . "='" . $value . "' ";
- } else {
- $having_string .= $value;
- }
- }
- $this->sql['having'] .= $having_string . ')';
- } else {
- $this->sql['having'] .= $having . ')';
- }
- return $this;
- }
-
- final public function join(array $join)
- {
- if (count($join) == count($join, 1)) {
- $join_string = '';
- if (isset($join[2])) {
- $join_string .= ' ' . $join[2] . ' JOIN ';
- } else {
- $join_string .= ' LEFT JOIN ';
- }
- $join_string .= $join[0] . ' ON ' . $join[1];
- if (isset($this->sql['join'])) {
- $this->sql['join'] .= $join_string;
- } else {
- $this->sql['join'] = $join_string;
- }
- } else {
- foreach ($join as $key => $value) {
- $this->join($value);
- }
- }
- return $this;
- }
-
- final public function union($subSql, $isAll = false)
- {
- if (! isset($this->sql['union'])) {
- $this->sql['union'] = '';
- }
- if (is_callable($subSql)) {
- $subSql = $subSql(new self());
- }
- if (is_array($subSql)) {
- foreach ($subSql as $key => $value) {
- $this->union($value, $isAll);
- }
- } else {
- if ($isAll) {
- $this->sql['union'] .= " UNION ALL($subSql)";
- } else {
- $this->sql['union'] .= " UNION ($subSql)";
- }
- }
- return $this;
- }
-
- final public function page($args = 1)
- {
- if ($args === false) {
- $this->sql['paging'] = false;
- return $this;
- }
- $this->sql['paging'] = true;
- $paging = Paging::getInstance();
- $var_num = func_num_args();
- if ($var_num > 1 || preg_match('/,/', $args)) {
- if ($var_num > 1) {
- $var_arr = func_get_args();
- } else {
- $var_arr = explode(',', $args);
- }
- $paging->page = $var_arr[0];
- $paging->pageSize = $var_arr[1];
- if (isset($var_arr[2])) {
- $paging->start = $var_arr[2];
- }
- } else {
- $paging->page = $args;
- }
- return $this;
- }
-
- final public function data($data)
- {
- $this->sql['data'] = $data;
- return $this;
- }
-
- final public function relation($field, array $array)
- {
- if ($array) {
- foreach ($array as $value) {
- $data[] = array(
- $field,
- $value
- );
- }
- $this->sql['data'] = $data;
- }
- return $this;
- }
-
- final public function from($subSql)
- {
- if (is_callable($subSql)) {
- $subSql = $subSql(new self());
- }
- $this->sql['from'] = $subSql;
- return $this;
- }
-
-
- final public function select($type = null)
- {
-
- if (is_callable($type)) {
- $type($this);
- return $this->select();
- }
-
- if (! isset($this->sql['field']) || ! $this->sql['field'])
- $this->sql['field'] = '*';
-
-
- if (isset($this->sql['paging']) && $this->sql['paging']) {
- if ($this->sql['group'] || $this->sql['distinct']) {
- if (get_db_type() == 'mysql') {
- $this->limit(Paging::getInstance()->quikLimit());
- $this->sql['field'] = 'SQL_CALC_FOUND_ROWS ' . $this->sql['field'];
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->all($sql, $type);
- $count_sql = "select FOUND_ROWS() as sum";
- if (! ! $rs = $this->getDb()->one($count_sql)) {
- $total = $rs->sum;
-
- $limit = Paging::getInstance()->limit($total, true);
- }
- } else {
- $count_sql = $this->buildSql($this->countSql2, false);
-
- if (! ! $rs = $this->getDb()->all($count_sql)) {
- $total = count($rs);
-
- $limit = Paging::getInstance()->limit($total, true);
-
- $this->limit($limit);
- }
- }
- } else {
-
- $count_sql = $this->buildSql($this->countSql, false);
-
- if (! ! $rs = $this->getDb()->one($count_sql)) {
- $total = $rs->sum;
-
- $limit = Paging::getInstance()->limit($total, true);
-
- $this->limit($limit);
- }
- }
- }
-
- if ($type === false) {
- return $this->buildSql($this->selectSql,false);
- } else {
- $sql = $this->buildSql($this->selectSql);
- }
- if (! isset($result)) {
- $result = $this->getDb()->all($sql, $type);
- }
- return $this->outData($result);
- }
-
- final public function find($type = null)
- {
-
- if (is_callable($type)) {
- $type($this);
- return $this->find();
- }
- if (! isset($this->sql['field']))
- $this->sql['field'] = '*';
- $this->limit(1);
- $sql = $this->buildSql($this->selectSql);
-
- if ($type === false) {
- return $sql;
- }
- $result = $this->getDb()->one($sql, $type);
- return $this->outData($result);
- }
-
- final public function column($fields, $key = null)
- {
-
- if (is_array($fields)) {
- $fields = implode(',', $fields);
- }
-
-
- if ($key && ! preg_match('/(.*,|^)(' . $key . ')(,.*|$)/', $fields)) {
- $this->sql['field'] = $key . ',' . $fields;
- } else {
- $this->sql['field'] = $fields;
- }
-
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->all($sql, 1);
- $data = array();
- foreach ($result as $vkey => $value) {
- if ($key) {
- $key_value = $value[$key];
- if (is_array($value) && count($value) == 2) {
- unset($value[$key]);
- $data[$key_value] = current($value);
- } else {
- $data[$key_value] = $value;
- }
- } else {
- if (is_array($value) && count($value) == 1) {
- $data[] = current($value);
- } else {
- $data[] = $value;
- }
- }
- }
- return $this->outData($data);
- }
-
- final public function value($field)
- {
- $this->sql['field'] = $field;
- $this->limit(1);
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- if (isset($result[0])) {
- return $this->outData($result[0]);
- } else {
- return null;
- }
- }
-
- final public function max($field)
- {
- $this->sql['field'] = "MAX(`$field`)";
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- return $this->outData($result[0]);
- }
-
- final public function min($field)
- {
- $this->sql['field'] = "MIN(`$field`)";
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- return $this->outData($result[0]);
- }
-
- final public function avg($field)
- {
- $this->sql['field'] = "AVG(`$field`)";
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- return $this->outData($result[0]);
- }
-
- final public function sum($field)
- {
- $this->sql['field'] = "SUM(`$field`)";
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- if ($result[0]) {
- return $this->outData($result[0]);
- } else {
- return 0;
- }
- }
-
- final public function count()
- {
- $this->sql['field'] = "COUNT(*)";
- $sql = $this->buildSql($this->selectSql);
- $result = $this->getDb()->one($sql, 2);
- if ($result[0]) {
- return $this->outData($result[0]);
- } else {
- return 0;
- }
- }
-
-
-
- final public function insert(array $data = array(), $batch = true)
- {
-
- if (! $data && isset($this->sql['data'])) {
- return $this->insert($this->sql['data']);
- }
- if (is_array($data)) {
-
- if (! $data)
- return;
- if (count($data) == count($data, 1)) {
- $keys = '';
- $values = '';
- foreach ($data as $key => $value) {
- $this->checkKey($key);
- if (! is_numeric($key)) {
- $keys .= "`" . $key . "`,";
- $values .= "'" . $value . "',";
- }
- }
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- $keys .= "`" . $this->createTimeField . "`,`" . $this->updateTimeField . "`,";
- if ($this->intTimeFormat) {
- $values .= "'" . time() . "','" . time() . "',";
- } else {
- $values .= "'" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "',";
- }
- }
- if ($keys) {
- $this->sql['field'] = '(' . substr($keys, 0, - 1) . ')';
- } elseif (isset($this->sql['field']) && $this->sql['field']) {
- $this->sql['field'] = "({$this->sql['field']})";
- }
- $this->sql['value'] = "(" . substr($values, 0, - 1) . ")";
- $sql = $this->buildSql($this->insertSql);
- } else {
- if ($batch) {
- $key_string = '';
- $value_string = '';
- $flag = false;
- foreach ($data as $keys => $value) {
- if (! $flag) {
- $value_string .= ' SELECT ';
- } else {
- $value_string .= ' UNION All SELECT ';
- }
- foreach ($value as $key2 => $value2) {
-
- if (! $flag && ! is_numeric($key2)) {
- $this->checkKey($key2);
- $key_string .= "`" . $key2 . "`,";
- }
- $value_string .= "'" . $value2 . "',";
- }
- $flag = true;
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- if ($this->intTimeFormat) {
- $value_string .= "'" . time() . "','" . time() . "',";
- } else {
- $value_string .= "'" . date('Y-m-d H:i:s') . "','" . date('Y-m-d H:i:s') . "',";
- }
- }
- $value_string = substr($value_string, 0, - 1);
- }
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- $key_string .= "`" . $this->createTimeField . "`,`" . $this->updateTimeField . "`,";
- }
- if ($key_string) {
- $this->sql['field'] = '(' . substr($key_string, 0, - 1) . ')';
- } elseif (isset($this->sql['field']) && $this->sql['field']) {
- $this->sql['field'] = "({$this->sql['field']})";
- }
- $this->sql['value'] = $value_string;
- $sql = $this->buildSql($this->insertMultSql);
-
- if (get_db_type() == 'mysql') {
- $max_allowed_packet = $this->getDb()->one('SELECT @@global.max_allowed_packet', 2);
- } else {
- $max_allowed_packet = 1 * 1024 * 1024;
- }
- if (strlen($sql) > $max_allowed_packet) {
- return $this->insert($data, false);
- }
- } else {
- foreach ($data as $keys => $value) {
- $result = $this->insert($value);
- }
- return $result;
- }
- }
- } elseif ($this->sql['from']) {
- if (isset($this->sql['field']) && $this->sql['field']) {
- $this->sql['field'] = "({$this->sql['field']})";
- }
- $sql = $this->buildSql($this->insertFromSql);
- } else {
- return;
- }
-
- $sql = preg_replace_r('/pboot:if/i', 'pboot@if', $sql);
- $sql = preg_replace_r('/pboot:sql/i', 'pboot@sql', $sql);
- return $this->getDb()->amd($sql);
- }
-
- final public function insertGetId(array $data = null, $batch = true)
- {
- if ($this->insert($data, $batch)) {
- return $this->getDb()->insertId();
- } else {
- return false;
- }
- }
-
-
- final public function update($data = null)
- {
-
- if (! $data && $this->sql['data']) {
- return $this->update($this->sql['data']);
- }
- $update_string = '';
- if (is_array($data)) {
- if (! $data)
- return;
- foreach ($data as $key => $value) {
- $this->checkKey($key);
- $temp_v_start = substr($value, 0, 2);
- $temp_v_end = substr($value, 2);
- if (is_numeric($temp_v_end) && $temp_v_start == "-=") {
- $update_string .= "`$key`= $key-$temp_v_end,";
- } elseif (is_numeric($temp_v_end) && $temp_v_start == "+=") {
- $update_string .= "`$key`= $key+$temp_v_end,";
- } else {
- $update_string .= "`$key`='$value',";
- }
- }
- $update_string = substr($update_string, 0, - 1);
- } else {
- $update_string = $data;
- }
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- if ($this->intTimeFormat) {
- $update_string .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
- } else {
- $update_string .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
- }
- }
- $this->sql['value'] = $update_string;
- $sql = $this->buildSql($this->updateSql);
- $sql = preg_replace_r('/pboot:if/i', 'pboot@if', $sql);
- $sql = preg_replace_r('/pboot:sql/i', 'pboot@sql', $sql);
- return $this->getDb()->amd($sql);
- }
-
- final public function setField($field, $value)
- {
- $this->checkKey($field);
- $this->sql['value'] = "`$field`='$value'";
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- if ($this->intTimeFormat) {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
- } else {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
- }
- }
- $sql = $this->buildSql($this->updateSql);
- return $this->getDb()->amd($sql);
- }
-
- final public function setInc($field, $value = 1)
- {
- $this->checkKey($field);
- $this->sql['value'] = " `$field`= $field+$value";
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- if ($this->intTimeFormat) {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
- } else {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
- }
- }
- $sql = $this->buildSql($this->updateSql);
- return $this->getDb()->amd($sql);
- }
-
- final public function setDec($field, $value = 1)
- {
- $this->checkKey($field);
- $this->sql['value'] = " `$field`= $field-$value";
- if ($this->autoTimestamp || (isset($this->sql['auto_time']) && $this->sql['auto_time'] == true)) {
- if ($this->intTimeFormat) {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . time() . "'";
- } else {
- $this->sql['value'] .= ",`" . $this->updateTimeField . "`=' " . date('Y-m-d H:i:s') . "'";
- }
- }
- $sql = $this->buildSql($this->updateSql);
- return $this->getDb()->amd($sql);
- }
-
-
- final public function delete($data = null, $key = null)
- {
- if ($data) {
- if (! $key)
- $key = $this->pk;
- $this->checkKey($key);
- if (is_array($data) || preg_match('/,/', $data)) {
- $this->in($key, $data);
- } else {
- $this->where("$key='$data'");
- }
- }
- $sql = $this->buildSql($this->deleteSql);
- return $this->getDb()->amd($sql);
- }
-
- private function checkKey($key)
- {
- if (! $key)
- return;
- if (! preg_match('/^[\w\.\-]+$/', $key)) {
- error('传递的SQL数据中含有非法字符:' . $key);
- }
- }
-
- public function checkIndexSql(): array
- {
- $sql = $this->buildSql($this->checkIndex);
- $result = $this->getDb()->query($sql);
- return $this->getDb()->fetchQuery($result);
- }
- }
|