| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127 |
- <?php
- /**
- * ZenTaoPHP的dao和sql类。
- * The dao and sql class file of ZenTaoPHP framework.
- *
- * The author disclaims copyright to this source code. In place of
- * a legal notice, here is a blessing:
- *
- * May you do good and not evil.
- * May you find forgiveness for yourself and forgive others.
- * May you share freely, never taking more than you give.
- */
- /**
- * DAO类。
- * DAO, data access object.
- *
- * @package framework
- */
- class baseDAO
- {
- /* Use these strang strings to avoid conflicting with these keywords in the sql body. */
- const WHERE = 'wHeRe';
- const GROUPBY = 'gRoUp bY';
- const HAVING = 'hAvInG';
- const ORDERBY = 'oRdEr bY';
- const LIMIT = 'lImiT';
- /**
- * 缓存未命中标识。
- * The cache miss flag.
- *
- * @var string
- * @access public
- */
- const CACHE_MISS = 'DAO_CAHCE_MISS';
- /**
- * 全局对象$app
- * The global app object.
- *
- * @var object
- * @access public
- */
- public $app;
- /**
- * 全局对象$config
- * The global config object.
- *
- * @var object
- * @access public
- */
- public $config;
- /**
- * 数据库类型。
- * The database type.
- *
- * @var bool
- * @access public
- */
- public $driver = 'mysql';
- /**
- * 全局对象$lang
- * The global lang object.
- *
- * @var object
- * @access public
- */
- public $lang;
- /**
- * 全局对象$dbh
- * The global dbh(database handler) object.
- *
- * @var object
- * @access public
- */
- public $dbh;
- /**
- * 全局对象$slaveDBH。
- * The global slaveDBH(database handler) object.
- *
- * @var object
- * @access public
- */
- public $slaveDBH;
- /**
- * 全局对象$cache。
- * The global cache object.
- *
- * @var object
- * @access public
- */
- public $cache = null;
- /**
- * sql对象,用于生成sql语句。
- * The sql object, used to create the query sql.
- *
- * @var object
- * @access public
- */
- public $sqlobj;
- /**
- * 正在使用的表。
- * The table of current query.
- *
- * @var string
- * @access public
- */
- public $table;
- /**
- * $this->table的别名。
- * The alias of $this->table.
- *
- * @var string
- * @access public
- */
- public $alias;
- /**
- * 查询的字段。
- * The fields will be returned.
- *
- * @var string
- * @access public
- */
- public $fields;
- /**
- * 查询模式,raw模式用于正常的select update等sql拼接操作,magic模式用于findByXXX等魔术方法。
- * The query mode, raw or magic.
- *
- * This var is used to diff dao::from() with sql::from().
- *
- * @var string
- * @access public
- */
- public $mode;
- /**
- * 执行方式:insert, select, update, delete, replace。
- * The query method: insert, select, update, delete, replace.
- *
- * @var string
- * @access public
- */
- public $method;
- /**
- * 是否自动增加lang条件。
- * If auto add lang statement.
- *
- * @var bool
- * @access public
- */
- public $autoLang;
- /**
- * 是否自动过滤模板数据。
- * If auto filter template data.
- *
- * @var string skip(本次不过滤)|always(总是过滤)|never(从不过滤)
- * @access public
- */
- public static $filterTpl = 'always';
- /**
- * 上一次插入的数据id。
- * Last insert id.
- *
- * @var int
- * @access private
- */
- protected $_lastInsertID = false;
- /**
- * 执行的请求,所有的查询都保存在该数组。
- * The queries executed. Every query will be saved in this array.
- *
- * @var array
- * @access public
- */
- public static $querys = array();
- /**
- * 执行fetchAll是否跳过text类型字段。
- * Exclude text fields when fetchAll.
- *
- * @var bool
- * @access public
- */
- public static $autoExclude = false;
- /**
- * 存放错误的数组。
- * The errors.
- *
- * @var array
- * @access public
- */
- public static $errors = array();
- /**
- * 实时记录日志设置,并设置记录文件。
- * Open real time log and set real time file.
- *
- * @var array
- * @access public
- */
- public static $realTimeLog = false;
- public static $realTimeFile = '';
- /**
- * 缓存已经查询过的表结构。
- * Cache desc tables.
- *
- * @var array
- * @access public
- */
- public static $tablesDesc = array();
- /**
- * 缓存已经查询过的唯一索引。
- * Cache unique indexes.
- *
- * @var array
- * @access private
- */
- protected static $uniqueIndexes = [];
- /**
- * 构造方法。
- * The construct method.
- *
- * @param object $app
- * @access public
- * @return void
- */
- public function __construct($app)
- {
- global $config, $lang, $dbh, $slaveDBH;
- $this->app = $app;
- $this->config = $config;
- $this->lang = $lang;
- $this->dbh = $dbh;
- $this->cache = $app->cache;
- $this->slaveDBH = $slaveDBH ? $slaveDBH : false;
- $this->reset();
- }
- /**
- * 设置$table属性。
- * Set the $table property.
- *
- * @param string $table
- * @access public
- * @return void
- */
- public function setTable($table)
- {
- $this->table = ($table && strpos($table, '`') === false) ? "`{$table}`" : $table;
- }
- /**
- * 设置$alias属性。
- * Set the $alias property.
- *
- * @param string $alias
- * @access public
- * @return void
- */
- public function setAlias($alias)
- {
- $this->alias = $alias;
- }
- /**
- * 设置$fields属性。
- * Set the $fields property.
- *
- * @param string $fields
- * @access public
- * @return void
- */
- public function setFields($fields)
- {
- $this->fields = $fields;
- }
- /**
- * 设置autoLang项。
- * Set autoLang item.
- *
- * @param bool $autoLang
- * @access public
- * @return void
- */
- public function setAutoLang($autoLang)
- {
- $this->autoLang = $autoLang;
- return $this;
- }
- /**
- * 设置过滤模板数据的方式。
- * Set the way to filter template data.
- *
- * @param string $method skip(本次不过滤)|always(总是过滤)|never(从不过滤)
- * @access public
- * @return void
- */
- public function filterTpl($method = 'always')
- {
- if($method == 'skip' && dao::$filterTpl == 'never') return $this;
- dao::$filterTpl = $method;
- return $this;
- }
- /**
- * 重置属性。
- * Reset the vars.
- *
- * @access public
- * @return void
- */
- public function reset()
- {
- $this->setFields('');
- $this->setTable('');
- $this->setAlias('');
- $this->setMode('');
- $this->setMethod('');
- $this->setAutoLang(isset($this->config->framework->autoLang) and $this->config->framework->autoLang);
- }
- //-----根据请求的方式,调用sql类相应的方法(Call according method of sql class by query method. -----//
- /**
- * 设置请求模式。像findByxxx之类的方法,使用的是magic模式;其他方法使用的是raw模式。
- * Set the query mode. If the method if like findByxxx, the mode is magic. Else, the mode is raw.
- *
- * @param string $mode magic|raw
- * @access public
- * @return void
- */
- public function setMode($mode = '')
- {
- $this->mode = $mode;
- }
- /**
- * 设置请求方法:select|update|insert|delete|replace 。
- * Set the query method: select|update|insert|delete|replace
- *
- * @param string $method
- * @access public
- * @return void
- */
- public function setMethod($method = '')
- {
- $this->method = $method;
- }
- /**
- * 生成缓存的 key。
- * Create the cache key.
- *
- * @param mixed $args
- * @access private
- * @return string
- */
- private function createCacheKey(...$args)
- {
- if(empty($this->cache)) return implode('-', $args);
- return $this->cache->createKey('dao', ...$args);
- }
- /**
- * 获取缓存。
- * Get the cache.
- *
- * @param string $key
- * @access public
- * @return mixed
- */
- public function getCache($key)
- {
- if(!$this->app->isServing() || empty($this->cache)) return self::CACHE_MISS;
- $cache = $this->cache->getByKey($key);
- if($cache === null) return self::CACHE_MISS;
- if(count($cache) < 3) return self::CACHE_MISS;
- /* 解析缓存的更新时间和值到变量中。 */
- /* Parse the cache time and value to variables. */
- list($cachedTime, $cachedSQL, $cachedValue) = $cache;
- /* 查找 sql 语句中包含的表名。*/
- /* Find the table names in the sql. */
- preg_match_all("/({$this->config->db->prefix}\w+)[`\" ]/", $cachedSQL, $tables);
- if(!isset($tables[1])) return self::CACHE_MISS;
- /* 检查 sql 语句中包含的表的更新时间是否大于缓存的更新时间,如果大于则不使用缓存。*/
- /* Check if the update time of the tables in the sql is greater than the cache time, if greater, don't use the cache. */
- foreach($tables[1] as $table)
- {
- if(strpos($table, 'boardlayer') !== false) return self::CACHE_MISS;
- $tableKey = $this->createCacheKey('table', $table);
- $tableCache = $this->cache->getByKey($tableKey);
- if($tableCache === null) continue;
- if($tableCache[0] > $cachedTime) return self::CACHE_MISS;
- }
- /* 检查是否可以使用客户端缓存。*/
- /* Check if can use the client cache. */
- $this->app->useClientCache = $this->app->clientCacheTime > $cachedTime;
- return $cachedValue ?: self::CACHE_MISS;
- }
- /**
- * 设置缓存。
- * Set the cache.
- *
- * @param string $key
- * @param string $sql
- * @param mixed $value
- * @param int $ttl
- * @access public
- * @return void
- */
- public function setCache($key, $sql = '', $value = null, $ttl = null)
- {
- if(!$this->app->isServing() || empty($this->cache)) return false;
- $this->app->useClientCache = false;
- $this->cache->saveByKey($key, array(microtime(true), $sql, $value), $ttl ?? $this->config->cache->dao->lifetime);
- }
- /**
- * 检查 sql 语句中是否包含表名,如果包含则设置表的缓存时间。
- * Check if the sql contains the table name, if contains, set the table cache time.
- *
- * @param string $sql
- * @access public
- * @return void
- */
- public function setTableCache($sql)
- {
- /* 查找 sql 语句中包含的表名。*/
- /* Find the table names in the sql. */
- preg_match_all("/({$this->config->db->prefix}\w+)[`\" ]/", $sql, $tables);
- if(!isset($tables[1])) return;
- foreach($tables[1] as $table)
- {
- /* 更新表的缓存时间。*/
- /* Update the table cache time. */
- $table = str_replace(array('`', '"'), '', $table);
- $key = $this->createCacheKey('table', $table);
- $this->setCache($key, '', $table, 0);
- }
- }
- /**
- * 清除缓存。
- * Clear the cache.
- *
- * @access public
- * @return void
- */
- public function clearCache()
- {
- if(!empty($this->cache)) $this->cache->clear();
- }
- /**
- * 开始事务。
- * Begin Transaction
- *
- * @access public
- * @return bool
- */
- public function begin()
- {
- return $this->dbh->beginTransaction();
- }
- /**
- * 检查是否在事务内。
- * Check in transaction.
- *
- * @access public
- * @return bool
- */
- public function inTransaction()
- {
- return $this->dbh->inTransaction();
- }
- /**
- * 事务回滚。
- * Roll back
- *
- * @access public
- * @return bool
- */
- public function rollBack()
- {
- return $this->dbh->rollBack();
- }
- /**
- * 提交事务。
- * Commits a transaction.
- *
- * @access public
- * @return bool
- */
- public function commit()
- {
- return $this->dbh->commit();
- }
- /**
- * Show tables.
- *
- * @access public
- * @return array
- */
- public function showTables()
- {
- return $this->query("SHOW TABLES")->fetchAll(PDO::FETCH_ASSOC);
- }
- /**
- * Get table engines.
- *
- * @access public
- * @return array
- */
- public function getTableEngines()
- {
- $tables = $this->query("SHOW TABLE STATUS WHERE `Engine` is not null")->fetchAll();
- $tableEngines = array();
- foreach($tables as $table) $tableEngines[$table->Name] = $table->Engine;
- return $tableEngines;
- }
- /**
- * Clear the cache of tables desc fields.
- *
- * @access public
- * @return void
- */
- public function clearTablesDescCache()
- {
- dao::$tablesDesc = array();
- }
- /**
- * Desc table, show fields.
- *
- * @param string $tableName
- * @access public
- * @return array
- */
- public function descTable($tableName)
- {
- if(isset(dao::$tablesDesc[$tableName])) return dao::$tablesDesc[$tableName];
- $dbh = $this->slaveDBH ? $this->slaveDBH : $this->dbh;
- $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
- $fields = array();
- $stmt = $dbh->rawQuery("DESC $tableName");
- while($field = $stmt->fetch()) $fields[$field->field] = $field;
- dao::$tablesDesc[$tableName] = $fields;
- $dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
- return $fields;
- }
- /**
- * select方法,调用sql::select()。
- * The select method, call sql::select().
- *
- * @param string $fields
- * @access public
- * @return static|sql|baseDAO the dao object self.
- */
- public function select($fields = '*')
- {
- $this->setMode('raw');
- $this->setMethod('select');
- $this->sqlobj = sql::select($fields);
- return $this;
- }
- /**
- * 获取查询记录条数。
- * The count method, call sql::select() and from().
- * use as $this->dao->select()->from(TABLE_BUG)->where()->count();
- *
- * @param string $distinctField
- * @access public
- * @return int
- */
- public function count($distinctField = '')
- {
- /* 获得SELECT,FROM的位置,使用count(*)替换其字段。 */
- /* Get the SELECT, FROM position, thus get the fields, replace it by count(*). */
- $sql = $this->get();
- $selectPOS = strpos($sql, 'SELECT') + strlen('SELECT');
- $fromPOS = strpos($sql, 'FROM');
- $fields = substr($sql, $selectPOS, $fromPOS - $selectPOS);
- $countField = $distinctField ? 'distinct ' . $distinctField : '*';
- $sql = str_replace($fields, " COUNT($countField) AS `recTotal` ", substr($sql, 0, $fromPOS)) . substr($sql, $fromPOS);
- /*
- * 去掉SQL语句中order和limit之后的部分。
- * Remove the part after order and limit.
- **/
- $subLength = strlen($sql);
- $lastRight = strrpos($sql, ')') > 0 ? strrpos($sql, ')') : 0;
- $groupPOS = strripos($sql, 'group by', $lastRight);
- $orderPOS = strripos($sql, 'order by', $lastRight);
- $limitPOS = strripos($sql, 'limit', $lastRight);
- if($limitPOS) $subLength = $limitPOS;
- if($orderPOS) $subLength = $orderPOS;
- if($groupPOS) $subLength = $groupPOS;
- $sql = substr($sql, 0, $subLength);
- /*
- * 获取记录数。
- * Get the records count.
- **/
- try
- {
- $dbh = $this->slaveDBH ? $this->slaveDBH : $this->dbh;
- $row = $dbh->query($sql)->fetch(PDO::FETCH_OBJ);
- }
- catch (PDOException $e)
- {
- $this->sqlError($e);
- }
- return is_object($row) ? $row->recTotal : 0;
- }
- /**
- * update方法,调用sql::update()。
- * The update method, call sql::update().
- *
- * @param string $table
- * @access public
- * @return static|sql the dao object self.
- */
- public function update($table)
- {
- $this->setMode('raw');
- $this->setMethod('update');
- $this->sqlobj = sql::update($table);
- $this->setTable($table);
- return $this;
- }
- /**
- * delete方法,调用sql::delete()。
- * The delete method, call sql::delete().
- *
- * @access public
- * @return static|sql the dao object self.
- */
- public function delete()
- {
- $this->setMode('raw');
- $this->setMethod('delete');
- $this->sqlobj = sql::delete();
- return $this;
- }
- /**
- * insert方法,调用sql::insert()。
- * The insert method, call sql::insert().
- *
- * @param string $table
- * @access public
- * @return static|sql the dao object self.
- */
- public function insert($table)
- {
- $this->setMode('raw');
- $this->setMethod('insert');
- $this->sqlobj = sql::insert($table);
- $this->setTable($table);
- return $this;
- }
- /**
- * replace方法,调用sql::replace()。
- * The replace method, call sql::replace().
- *
- * @param string $table
- * @access public
- * @return static|sql the dao object self.
- */
- public function replace($table)
- {
- $this->setMode('raw');
- $this->setMethod('replace');
- $this->sqlobj = sql::replace($table);
- $this->setTable($table);
- return $this;
- }
- /**
- * 设置要操作的表。
- * Set the from table.
- *
- * @param string $table
- * @access public
- * @return static|sql the dao object self.
- */
- public function from($table)
- {
- $this->setTable($table);
- if($this->mode == 'raw') $this->sqlobj->from($table);
- return $this;
- }
- /**
- * 设置字段。
- * Set the fields.
- *
- * @param string $fields
- * @access public
- * @return static|sql the dao object self.
- */
- public function fields($fields)
- {
- $this->setFields($fields);
- return $this;
- }
- /**
- * 表别名,相当于sql里的AS。(as是php的关键词,使用alias代替)
- * Alias a table, equal the AS keyword. (Don't use AS, because it's a php keyword.)
- *
- * @param string $alias
- * @access public
- * @return static|sql the dao object self.
- */
- public function alias($alias)
- {
- if(empty($this->alias)) $this->setAlias($alias);
- $this->sqlobj->alias($alias);
- return $this;
- }
- /**
- * 设置需要更新或插入的数据。
- * Set the data to update or insert.
- *
- * @param object $data the data object or array
- * @param string $skipFields the skip fields
- * @access public
- * @return static|sql the dao object self.
- */
- public function data($data, $skipFields = '')
- {
- if(!is_object($data)) $data = (object)$data;
- if($this->autoLang and !isset($data->lang))
- {
- $data->lang = $this->app->getClientLang();
- if(isset($this->app->config->cn2tw) and $this->app->config->cn2tw and $data->lang == 'zh-tw') $data->lang = 'zh-cn';
- if(defined('RUN_MODE') and RUN_MODE == 'front' and !empty($this->app->config->cn2tw)) $data->lang = str_replace('zh-tw', 'zh-cn', $data->lang);
- }
- $this->sqlobj->data($data, $skipFields);
- return $this;
- }
- //-------------------- sql相关的方法(The sql related method) --------------------//
- /**
- * 获取sql字符串。
- * Get the sql string.
- *
- * @access public
- * @return string the sql string after process.
- */
- public function get()
- {
- return self::processKeywords($this->processSQL());
- }
- /**
- * 打印sql字符串。
- * Print the sql string.
- *
- * @access public
- * @return void
- */
- public function printSQL()
- {
- echo $this->processSQL();
- }
- /**
- * 解析SQL语句。
- * Explain sql.
- *
- * @param string $sql
- * @access public
- * @return array|void
- */
- public function explain($sql = '', $exit = true)
- {
- $sql = empty($sql) ? $this->processSQL() : $sql;
- $result = $this->dbh->rawQuery('explain ' . $sql)->fetchAll();
- if($exit) a($result);
- return $result;
- }
- /**
- * 处理sql语句,替换表和字段。
- * Process the sql, replace the table, fields.
- *
- * @param string $setIsTpl
- * @access public
- * @return string the sql string after process.
- */
- public function processSQL($filterTpl = true)
- {
- $sql = $this->sqlobj->get();
- $needFilterTpl = $filterTpl && empty($this->app->installing) && empty($this->app->upgrading);
- /* INSERT INTO table VALUES(...) */
- if($this->method == 'insert' and !empty($this->sqlobj->data))
- {
- $desc = $this->descTable($this->table);
- $skipFields = $this->sqlobj->skipFields;
- $values = array();
- foreach($this->sqlobj->data as $field => $value)
- {
- if(strpos($skipFields, ",$field,") !== false) continue;
- $values[$field] = $this->sqlobj->quote($value);
- unset($desc[$field]);
- }
- /* If field can not null, add this field use default value. */
- foreach($desc as $field)
- {
- if(strtolower($field->null) == 'yes') continue;
- if($field->field == 'id') continue;
- if($field->default !== '') continue;
- $values[$field->field] = "''";
- if(strpos($field->type, 'date') !== false) $values[$field->field] = "'0000-00-00'";
- if(strpos($field->type, 'int') !== false) $values[$field->field] = "0";
- if(strpos($field->type, 'float') !== false) $values[$field->field] = "0";
- if(strpos($field->type, 'decimal') !== false) $values[$field->field] = "0";
- if(strpos($field->type, 'double') !== false) $values[$field->field] = "0";
- }
- $sql .= '(`' . implode('`,`', array_keys($values)) . '`)' . ' VALUES(' . implode(',', $values) . ')';
- }
- elseif($this->method == 'select' && dao::$filterTpl == 'always' && $needFilterTpl)
- {
- /* 过滤模板类型的数据 */
- foreach(array('project', 'task') as $table)
- {
- $table = $this->config->db->prefix . $table;
- if(strpos($sql, "`$table`") === false) continue;
- if(preg_match("/`isTpl`\s*=\s*('1'|1)/", $sql) || preg_match("/isTpl\s*=\s*('1'|1)/", $sql)) continue; // 指定查询模板类型的数据则不过滤
- preg_match_all('/\(\s*(SELECT\b.*?\bFROM\b.*?)(?=\)\s*(AND|\)|$))/is', $sql, $matches); // 匹配子查询
- if(!$matches[1])
- {
- $alias = preg_match("/`$table`\s+as\s+(\w+)/i", $sql, $matches) ? $matches[1] : '';
- $replace = $alias ? "wHeRe ($alias.`isTpl` = '0' OR $alias.`isTpl` IS NULL) AND" : "wHeRe `isTpl` = '0' AND";
- $sql = preg_replace("/wHeRE/i", $replace, $sql, 1);
- }
- else
- {
- foreach($matches[1] as $index => $subSQL)
- {
- $sql = str_ireplace($subSQL, "$$index", $sql);
- }
- if(strpos($sql, "`$table`") !== false)
- {
- $alias = preg_match("/`$table`\s+as\s+(\w+)/i", $sql, $mainMatches) ? $mainMatches[1] : '';
- $replace = $alias ? "wHeRe ($alias.`isTpl` = '0' OR $alias.`isTpl` IS NULL) AND" : "wHeRe `isTpl` = '0' AND";
- $sql = preg_replace("/wHeRE/i", $replace, $sql, 1);
- }
- foreach($matches[1] as $index => $subSQL)
- {
- if(strpos($sql, "`$table`") !== false && !preg_match("/`isTpl`\s*=\s*('1'|1)/", $subSQL))
- {
- $alias = preg_match("/`$table`\s+as\s+(\w+)/i", $subSQL, $subMatches) ? $subMatches[1] : '';
- $replace = $alias ? "wHeRe ($alias.`isTpl` = '0' OR $alias.`isTpl` IS NULL) AND" : "wHeRe `isTpl` = '0' AND";
- $subSQL = preg_replace("/wHeRE/i", $replace, $subSQL, 1);
- }
- $sql = str_ireplace("$$index", $subSQL, $sql);
- }
- }
- }
- }
- if(dao::$filterTpl == 'skip') dao::$filterTpl = 'always';
- /**
- * 如果是magic模式,处理表和字段。
- * If the mode is magic, process the $fields and $table.
- **/
- if($this->mode == 'magic')
- {
- if($this->fields == '') $this->fields = '*';
- if($this->table == '') $this->app->triggerError('Must set the table name', __FILE__, __LINE__, $exit = true);
- $sql = sprintf($this->sqlobj->get(), $this->fields, $this->table);
- }
- /* If the method is select, update or delete, set the lang condition. */
- if($this->autoLang and $this->table != '' and $this->method != 'insert' and $this->method != 'replace')
- {
- $lang = $this->app->getClientLang();
- /* Get the position to insert lang = ?. */
- $wherePOS = strrpos($sql, DAO::WHERE); // The position of WHERE keyword.
- $groupPOS = strrpos($sql, DAO::GROUPBY); // The position of GROUP BY keyword.
- $havingPOS = strrpos($sql, DAO::HAVING); // The position of HAVING keyword.
- $orderPOS = strrpos($sql, DAO::ORDERBY); // The position of ORDERBY keyword.
- $limitPOS = strrpos($sql, DAO::LIMIT); // The position of LIMIT keyword.
- $splitPOS = $orderPOS ? $orderPOS : $limitPOS; // If $orderPOS, use it instead of $limitPOS.
- $splitPOS = $havingPOS ? $havingPOS : $splitPOS; // If $havingPOS, use it instead of $orderPOS.
- $splitPOS = $groupPOS ? $groupPOS : $splitPOS; // If $groupPOS, use it instead of $havingPOS.
- /* Set the condition to be appended. */
- $tableName = !empty($this->alias) ? $this->alias : $this->table;
- if(!empty($this->app->config->cn2tw)) $lang = str_replace('zh-tw', 'zh-cn', $lang);
- $langCondition = " $tableName.lang in('{$lang}', 'all') ";
- /* If $splitPOS > 0, split the sql at $splitPOS. */
- if($splitPOS)
- {
- $firstPart = substr($sql, 0, $splitPOS);
- $lastPart = substr($sql, $splitPOS);
- if($wherePOS)
- {
- $sql = $firstPart . " AND $langCondition " . $lastPart;
- }
- else
- {
- $sql = $firstPart . " WHERE $langCondition " . $lastPart;
- }
- }
- else
- {
- $sql .= $wherePOS ? " AND $langCondition" : " WHERE $langCondition";
- }
- }
- return $sql;
- }
- /**
- * 替换sql常量关键字。
- * Process the sql keywords, replace the constants to normal.
- *
- * @param string $sql
- * @access public
- * @return string the sql string.
- */
- public static function processKeywords($sql)
- {
- return str_replace(array(DAO::WHERE, DAO::GROUPBY, DAO::HAVING, DAO::ORDERBY, DAO::LIMIT), array('WHERE', 'GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT'), $sql);
- }
- //-------------------- 查询相关方法(Query related methods) --------------------//
- /**
- * 设置$dbh,数据库连接句柄。
- * Set the dbh.
- *
- * You can use like this: $this->dao->dbh($dbh), thus you can handle two database.
- *
- * @param object $dbh
- * @access public
- * @return static|sql the dao object self.
- */
- public function dbh($dbh)
- {
- $this->dbh = $dbh;
- return $this;
- }
- /**
- * 执行SQL语句,返回PDOStatement结果集。
- * Query the sql, return the statement object.
- *
- * @access public
- * @return static|sql the PDOStatement object.
- */
- public function query($sql = '')
- {
- if($sql)
- {
- $sql = $this->dbh->formatSQL($sql);
- $sqlMethod = strtolower(substr($sql, 0, strpos($sql, ' ')));
- $this->setMethod($sqlMethod);
- $this->sqlobj = new sql();
- $this->sqlobj->sql = $sql;
- }
- else
- {
- $sql = $this->dbh->formatSQL($this->processSQL());
- }
- try
- {
- /* Real-time save log. */
- if(dao::$realTimeLog && dao::$realTimeFile) file_put_contents(dao::$realTimeFile, $sql . "\n", FILE_APPEND);
- $method = $this->method;
- $this->reset();
- if($this->slaveDBH and in_array($method, array('select', 'desc')))
- {
- return $this->slaveDBH->rawQuery($sql);
- }
- else
- {
- /* Force to query from master db, if db has been changed. */
- $this->slaveDBH = false;
- return $this->driver == 'sqlite' ? $this->dbh->query($sql) : $this->dbh->rawQuery($sql);
- }
- }
- catch (PDOException $e)
- {
- $this->sqlError($e);
- }
- }
- /**
- * 返回SQL结果的所有字段信息。
- * Return the fields meta of PDOStatement.
- *
- * @param string|PDOStatement $stmt
- * @access public
- * @return array
- */
- public function getColumns($stmt)
- {
- /* 如果$stmt是SQL查询语句,先执行查询获得 PDO stmt. */
- /* If $stmt is a SQL string, query to get PDO stmt. */
- if(is_string($stmt)) $stmt = $this->query($stmt);
- try
- {
- $columns = array();
- for($columnIndex = 0; $columnIndex < $stmt->columnCount(); $columnIndex++)
- {
- $columns[] = $stmt->getColumnMeta($columnIndex);
- }
- return $columns;
- }
- catch (PDOException $e)
- {
- $this->sqlError($e);
- }
- }
- /**
- * 将记录进行分页,自动设置limit语句。
- * Page the records, set the limit part auto.
- *
- * @param object $pager
- * @param string $distinctField
- * @access public
- * @return static|sql the dao object self.
- */
- public function page($pager, $distinctField = '')
- {
- if(!is_object($pager)) return $this;
- /*
- * 重新计算分页数据,并判断是否需要返回上一页。
- * Calculate pagination to determine whether to return to the previous page.
- */
- $originalPageID = $pager->pageID;
- $recTotal = $this->count($distinctField);
- $pager->setRecTotal($recTotal);
- $pager->setPageTotal();
- if($originalPageID > $pager->pageTotal) $pager->setPageID($pager->pageTotal);
- $this->sqlobj->limit($pager->limit());
- return $this;
- }
- /**
- * 获取唯一索引。
- * Get unique indexes.
- *
- * @param string $table
- * @access public
- * @return array
- */
- protected function getUniqueIndexes($table)
- {
- if(isset(dao::$uniqueIndexes[$table])) return dao::$uniqueIndexes[$table];
- $indexes = [];
- $table = trim($table, '`');
- $rows = $this->select('INDEX_NAME, COLUMN_NAME')->from('INFORMATION_SCHEMA.STATISTICS')->where('TABLE_SCHEMA')->eq($this->config->db->name)->andWhere('TABLE_NAME')->eq($table)->andWhere('NON_UNIQUE')->eq(0)->andWhere('INDEX_NAME')->ne('PRIMARY')->query()->fetchAll();
- foreach($rows as $row) $indexes[$row->INDEX_NAME][] = $row->COLUMN_NAME;
- dao::$uniqueIndexes[$table] = $indexes;
- return $indexes;
- }
- /**
- * 把 replace 转换为 delete 和 insert。
- * Convert replace to delete and insert.
- *
- * @param string $table
- * @access private
- * @return int
- */
- protected function convertReplaceToInsert($table)
- {
- $processedData = new stdclass();
- foreach($this->sqlobj->data as $field => $value)
- {
- $field = trim($field, '`');
- $processedData->{$field} = $value;
- }
- $indexes = $this->getUniqueIndexes($table);
- if(!$indexes)
- {
- dao::$errors[] = "The table {$table} has no unique indexes.";
- return 0;
- }
- $this->begin();
- foreach($indexes as $fields)
- {
- $this->delete()->from($table)->where('1=1');
- foreach($fields as $field)
- {
- if(!isset($processedData->$field))
- {
- dao::$errors[] = "The field $field of table {$table} is required.";
- return 0;
- }
- $this->andWhere("`{$field}`")->eq($processedData->$field);
- }
- $this->exec();
- }
- $result = $this->insert($table)->data($processedData)->exec();
- if(!$result) $this->rollback();
- $this->commit();
- return $result;
- }
- /**
- * 执行SQL。query()会返回stmt对象,该方法只返回更改或删除的记录数。
- * Execute the sql. It's different with query(), which return the stmt object. But this not.
- *
- * @param string $sql
- * @access public
- * @return int the modified or deleted records. 更改或删除的记录数。
- */
- public function exec($sql = '')
- {
- if(dao::isError()) return 0;
- if($this->method == 'replace' && !empty($this->sqlobj->data))
- {
- $table = $this->table;
- if(strpos($table, '`') === false) $table = "`{$table}`";
- if(isset($this->config->cache->raw[$table])) return $this->convertReplaceToInsert($table);
- }
- if($sql)
- {
- $this->sqlobj = new sql();
- }
- else
- {
- $sql = $this->processSQL();
- }
- /* Assign the $sql to $this->sqlobj, so sqlError() can print the full sql statement if any exception occurs. */
- $this->sqlobj->sql = $sql;
- try
- {
- /* Real-time save log. */
- if(dao::$realTimeLog && dao::$realTimeFile) file_put_contents(dao::$realTimeFile, $sql . "\n", FILE_APPEND);
- $table = $this->table;
- $method = $this->method;
- $this->reset();
- /* Force to query from master db, if db has been changed. */
- $this->slaveDBH = false;
- if($this->cache) $this->cache->prepare($table, $method, $sql);
- $result = $this->dbh->exec($sql);
- /* See: https://www.php.net/manual/en/pdo.lastinsertid.php .*/
- if($method == 'insert') $this->_lastInsertID = $this->dbh->lastInsertID();
- if($this->cache)
- {
- if($result)
- {
- $this->setTableCache($sql);
- $this->cache->sync();
- }
- else
- {
- $this->cache->reset();
- }
- }
- if(in_array($table, $this->config->userview->relatedTables))
- {
- $this->dbh->exec('UPDATE ' . TABLE_CONFIG . " SET `value` = '" . time() . "' WHERE `owner` = 'system' AND `module` = 'common' AND `section` = 'userview' AND `key` = 'relatedTablesUpdateTime'");
- }
- if($this->config->enableDuckdb)
- {
- $queueTable = TABLE_DUCKDBQUEUE;
- if(!empty($table) && $table != $queueTable)
- {
- $now = helper::now();
- $object = trim($table, '`');
- $this->dbh->exec("UPDATE {$queueTable} SET updatedTime = '$now' WHERE object = '$object'");
- $this->dbh->exec("INSERT INTO {$queueTable} (`object`, `updatedTime`, `syncTime`) SELECT '$object', '$now', NULL WHERE NOT EXISTS (SELECT 1 FROM {$queueTable} WHERE `object` = '$object' );");
- }
- }
- return $result;
- }
- catch (PDOException $e)
- {
- $this->sqlError($e);
- }
- }
- //-------------------- Fetch相关方法(Fetch related methods) -------------------//
- /**
- * 获取一个记录。
- * Fetch one record.
- *
- * @param string $field 如果已经设置获取的字段,则只返回这个字段的值,否则返回这个记录。
- * if the field is set, only return the value of this field, else return this record
- * @access public
- * @return object|mixed
- */
- public function fetch($field = '')
- {
- $sql = $this->processSQL(false);
- $key = $this->createCacheKey('fetch', md5($sql));
- $result = $this->getCache($key);
- if($result === self::CACHE_MISS)
- {
- $table = $this->table;
- $result = $this->query($sql)->fetch(PDO::FETCH_OBJ);
- $result = helper::decodeHtmlSpecialChars($table, $result);
- $this->setCache($key, $sql, $result);
- }
- if(empty($field)) return $result;
- return $result ? $result->$field : '';
- }
- /**
- * 匹配SQL语句中的表别名,返回['t1.*' => 't1', '*' => '']
- * Match table alias name.
- *
- * @param string $sql
- * @access private
- * @return array
- */
- private function matchTableAlias($sql)
- {
- $pattern = '/SELECT\s+((\w+\.\*,?\s*)+|\*)/i';
- if(preg_match($pattern, $sql, $matches))
- {
- if (trim($matches[1]) === '*') return ['*' => ''];
- /* Get table alias name */
- preg_match_all('/(\w+)\.\*/', $matches[1], $aliasName);
- return !empty($aliasName[1]) ? [$aliasName[0][0] => $aliasName[1][0]] : [];
- }
- return [];
- }
- /**
- * 将SQL语句中的*展开为具体的字段。
- * Extract fields in SQL.
- *
- * @param string $sql
- * @access private
- * @return string
- */
- private function extractSQLFields($sql)
- {
- $aliasList = $this->matchTableAlias($sql);
- foreach($aliasList as $selectStr => $tableAlias)
- {
- /* Get fields for selectStr. */
- $tableName = $this->sqlobj->tableAlias[$tableAlias];
- $fields = $this->descTable($tableName);
- /* 使用具体的字段替换星号。 Replace selectStr with fields. */
- $tableFields = [];
- foreach($fields as $field)
- {
- if(strpos($field->type, 'text') !== false || strpos($field->type, 'blob') !== false) continue;
- $tableFields[] = ($tableAlias ? $tableAlias . '.`' : '`') . $field->field . '`';
- }
- $sql = str_replace($selectStr, implode(',', $tableFields), $sql);
- }
- return $sql;
- }
- /**
- * 获取所有记录。
- * Fetch all records.
- *
- * @param string $keyField 返回以该字段做键的记录
- * the key field, thus the return records is keyed by this field
- * @param bool $autoExclude 是否排除text类型字段 exclude field type of text
- * @access public
- * @return array the records
- */
- public function fetchAll($keyField = '', $autoExclude = true)
- {
- $sql = $this->processSQL();
- if(self::$autoExclude && $autoExclude) $sql = $this->extractSQLFields($sql);
- $key = $this->createCacheKey('fetchAll', md5($sql));
- $rows = $this->getCache($key);
- if($rows === self::CACHE_MISS)
- {
- $table = $this->table;
- $rows = $this->query($sql)->fetchAll();
- $rows = helper::decodeHtmlSpecialChars($table, $rows);
- $this->setCache($key, $sql, $rows);
- }
- if(empty($keyField)) return $rows;
- $result = array();
- foreach($rows as $i => $row) $result[$row->$keyField] = $row;
- return $result;
- }
- /**
- * 获取所有记录并将按照字段分组。
- * Fetch all records and group them by one field.
- *
- * @param string $groupField 分组的字段 the field to group by
- * @param string $keyField 键字段 the field of key
- * @access public
- * @return array the records.
- */
- public function fetchGroup($groupField, $keyField = '')
- {
- $sql = $this->processSQL();
- $key = $this->createCacheKey('fetchAll', md5($sql));
- $rows = $this->getCache($key);
- if($rows === self::CACHE_MISS)
- {
- $table = $this->table;
- $rows = $this->query($sql)->fetchAll();
- $rows = helper::decodeHtmlSpecialChars($table, $rows);
- $this->setCache($key, $sql, $rows);
- }
- $result = array();
- foreach($rows as $i => $row)
- {
- empty($keyField) ? $result[$row->$groupField][] = $row : $result[$row->$groupField][$row->$keyField] = $row;
- }
- return $result;
- }
- /**
- * 获取的记录是以关联数组的形式
- * Fetch array like key=>value.
- *
- * 如果没有设置参数,用首末两键作为参数。
- * If the keyFiled and valueField not set, use the first and last in the record.
- *
- * @param string $keyField
- * @param string $valueField
- * @access public
- * @return array
- */
- public function fetchPairs($keyField = '', $valueField = '')
- {
- $sql = $this->processSQL();
- $key = $this->createCacheKey('fetchAll', md5($sql));
- $rows = $this->getCache($key);
- if($rows === self::CACHE_MISS)
- {
- $table = $this->table;
- $rows = $this->query($sql)->fetchAll();
- $rows = helper::decodeHtmlSpecialChars($table, $rows);
- $this->setCache($key, $sql, $rows);
- }
- $ready = false;
- $keyField = trim($keyField, '`');
- $valueField = trim($valueField, '`');
- $result = array();
- foreach($rows as $row)
- {
- $row = (array)$row;
- if(!$ready)
- {
- if(empty($keyField)) $keyField = key($row);
- if(empty($valueField))
- {
- end($row);
- $valueField = key($row);
- }
- $ready = true;
- }
- $result[$row[$keyField]] = $row[$valueField];
- }
- return $result;
- }
- /**
- * 返回最后插入的ID。
- * Return the last insert ID.
- *
- * @access public
- * @return int|false
- */
- public function lastInsertID()
- {
- return $this->_lastInsertID !== false ? (int)$this->_lastInsertID : false;
- }
- //-------------------- 魔术方法(Magic methods) --------------------//
- /**
- * 解析dao的方法名,处理魔术方法。
- * Use it to do some convenient queries.
- *
- * @param string $funcName the function name to be called
- * @param array $funcArgs the params
- * @access public
- * @return static|sql the dao object self.
- */
- public function __call($funcName, $funcArgs)
- {
- $funcName = strtolower($funcName);
- /*
- * 如果是findByxxx,转换为where条件语句。
- * findByxxx, xxx as will be in the where.
- **/
- if(strpos($funcName, 'findby') !== false)
- {
- $this->setMode('magic');
- $this->setFields('');
- $field = str_replace('findby', '', $funcName);
- if(count($funcArgs) == 1)
- {
- $operator = '=';
- $value = $funcArgs[0];
- }
- else
- {
- $operator = $funcArgs[0];
- $value = $funcArgs[1];
- }
- $this->sqlobj = sql::select('%s')->from('%s')->where($field, $operator, $value);
- return $this;
- }
- /*
- * 获取指定个数的记录:fetch10 获取10条记录。
- * Fetch10.
- **/
- elseif(strpos($funcName, 'fetch') !== false)
- {
- $max = str_replace('fetch', '', $funcName);
- $stmt = $this->query();
- $rows = array();
- $key = isset($funcArgs[0]) ? $funcArgs[0] : '';
- $i = 0;
- while($row = $stmt->fetch())
- {
- $key ? $rows[$row->$key] = $row : $rows[] = $row;
- $i ++;
- if($i == $max) break;
- }
- return $rows;
- }
- /*
- * 其他的方法,转到sqlobj对象执行。
- * Others, call the method in sql class.
- **/
- else
- {
- $this->sqlobj->$funcName(...$funcArgs);
- return $this;
- }
- }
- //-------------------- 条件检查( Data Checking)--------------------//
- /**
- * 检查字段是否满足条件。
- * Check a filed is satisfied with the check rule.
- *
- * @param string $fieldName the field to check
- * @param string $funcName the check rule
- * @param string $condition the condition
- * @access public
- * @return static|sql the dao object self.
- */
- public function check($fieldName, $funcName, $condition = '')
- {
- /*
- * 如果没数据中没有该字段,直接返回。
- * If no this field in the data, return.
- **/
- $settedFields = array_keys(get_object_vars($this->sqlobj->data));
- if(!in_array($fieldName, $settedFields)) return $this;
- /* 设置字段值。 */
- /* Set the field label and value. */
- global $lang, $config;
- if(isset($config->db->prefix))
- {
- $table = strtolower(str_replace(array($config->db->prefix, '`'), '', $this->table));
- }
- elseif(strpos($this->table, '_') !== false)
- {
- $table = strtolower(substr($this->table, strpos($this->table, '_') + 1));
- $table = str_replace('`', '', $table);
- }
- else
- {
- $table = strtolower($this->table);
- }
- $fieldLabel = isset($lang->$table->$fieldName) ? $lang->$table->$fieldName : $fieldName;
- $value = isset($this->sqlobj->data->$fieldName) ? $this->sqlobj->data->$fieldName : null;
- /*
- * 检查唯一性。
- * Check unique.
- **/
- if($funcName == 'unique')
- {
- $args = func_get_args();
- $sql = "SELECT COUNT(1) AS `count` FROM $this->table WHERE `$fieldName` = " . $this->sqlobj->quote($value);
- if($condition) $sql .= ' AND ' . $condition;
- try
- {
- $row = $this->dbh->query($sql)->fetch();
- if($row->count != 0) $this->logError($funcName, $fieldName, $fieldLabel, array($value));
- }
- catch (PDOException $e)
- {
- $this->sqlError($e);
- }
- }
- else
- {
- /*
- * 创建参数。
- * Create the params.
- **/
- $funcArgs = func_get_args();
- unset($funcArgs[0]);
- unset($funcArgs[1]);
- for($i = 0; $i < VALIDATER::MAX_ARGS; $i ++)
- {
- ${"arg$i"} = isset($funcArgs[$i + 2]) ? $funcArgs[$i + 2] : null;
- }
- $checkFunc = 'check' . $funcName;
- if(validater::$checkFunc($value, $arg0, $arg1, $arg2) === false)
- {
- $this->logError($funcName, $fieldName, $fieldLabel, $funcArgs);
- }
- }
- return $this;
- }
- /**
- * 检查一个字段是否满足条件。
- * Check a field, if satisfied with the condition.
- *
- * @param string $condition
- * @param string $fieldName
- * @param string $funcName
- * @access public
- * @return static|sql the dao object self.
- */
- public function checkIF($condition, $fieldName, $funcName)
- {
- if(!$condition) return $this;
- $funcArgs = func_get_args();
- for($i = 0; $i < VALIDATER::MAX_ARGS; $i ++)
- {
- ${"arg$i"} = isset($funcArgs[$i + 3]) ? $funcArgs[$i + 3] : null;
- }
- $this->check($fieldName, $funcName, $arg0, $arg1, $arg2);
- return $this;
- }
- /**
- * 批量检查字段。
- * Batch check some fileds.
- *
- * @param string $fields the fields to check, join with ,
- * @param string $funcName
- * @access public
- * @return static|sql the dao object self.
- */
- public function batchCheck($fields, $funcName)
- {
- $fields = explode(',', str_replace(' ', '', $fields));
- $funcArgs = func_get_args();
- for($i = 0; $i < VALIDATER::MAX_ARGS; $i ++)
- {
- ${"arg$i"} = isset($funcArgs[$i + 2]) ? $funcArgs[$i + 2] : null;
- }
- foreach($fields as $fieldName) $this->check($fieldName, $funcName, $arg0, $arg1, $arg2);
- return $this;
- }
- /**
- * 批量检查字段是否满足条件。
- * Batch check fields on the condition is true.
- *
- * @param string $condition
- * @param string $fields
- * @param string $funcName
- * @access public
- * @return static|sql the dao object self.
- */
- public function batchCheckIF($condition, $fields, $funcName)
- {
- if(!$condition) return $this;
- $fields = explode(',', str_replace(' ', '', $fields));
- $funcArgs = func_get_args();
- for($i = 0; $i < VALIDATER::MAX_ARGS; $i ++)
- {
- ${"arg$i"} = isset($funcArgs[$i + 3]) ? $funcArgs[$i + 3] : null;
- }
- foreach($fields as $fieldName) $this->check($fieldName, $funcName, $arg0, $arg1, $arg2);
- return $this;
- }
- /**
- * 根据数据库结构检查字段。
- * Check the fields according the the database schema.
- *
- * @param string $skipFields fields to skip checking
- * @access public
- * @return static|sql the dao object self.
- */
- public function autoCheck($skipFields = '')
- {
- $fields = $this->getFieldsType();
- $skipFields = ",$skipFields,";
- foreach($fields as $fieldName => $validater)
- {
- if(strpos($skipFields, $fieldName) !== false) continue; // skip it.
- if(!isset($this->sqlobj->data->$fieldName)) continue;
- if($validater['rule'] == 'skip') continue;
- $options = array();
- if(isset($validater['options'])) $options = array_values($validater['options']);
- for($i = 0; $i < VALIDATER::MAX_ARGS; $i ++)
- {
- ${"arg$i"} = isset($options[$i]) ? $options[$i] : null;
- }
- $this->check($fieldName, $validater['rule'], $arg0, $arg1, $arg2);
- }
- return $this;
- }
- /**
- * 记录错误到日志。
- * Log the error.
- *
- * module/common/lang中定义了错误提示信息。
- * For the error notice, see module/common/lang.
- *
- * @param string $checkType the check rule
- * @param string $fieldName the field name
- * @param string $fieldLabel the field label
- * @param array $funcArgs the args
- * @access public
- * @return void
- */
- public function logError($checkType, $fieldName, $fieldLabel, $funcArgs = array())
- {
- global $lang;
- $error = $lang->error->$checkType;
- $replaces = array_merge(array($fieldLabel), $funcArgs); // the replace values.
- /*
- * 如果$error错误信息是一个字符串,进行替换。
- * Just a string, cycle the $replaces.
- **/
- if(!is_array($error))
- {
- foreach($replaces as $replace)
- {
- if(is_array($replace)) $replace = implode(',', $replace);
- $pos = strpos($error, '%s');
- if($pos === false) break;
- $error = substr($error, 0, $pos) . $replace . substr($error, $pos + 2);
- }
- }
- /*
- * 如果error错误信息是一个数组,选择一个%s满足替换个数的进行替换。
- * If the error define is an array, select the one which %s counts match the $replaces.
- **/
- else
- {
- /*
- * 去掉空值项。
- * Remove the empty items.
- **/
- foreach($replaces as $key => $value) if(is_null($value)) unset($replaces[$key]);
- $replacesCount = count($replaces);
- foreach($error as $errorString)
- {
- if(substr_count($errorString, '%s') == $replacesCount)
- {
- $error = vsprintf($errorString, $replaces);
- }
- }
- }
- dao::$errors[$fieldName][] = $error;
- }
- /**
- * 判断是否有错误。
- * Judge any error or not.
- *
- * @access public
- * @return bool
- */
- public static function isError()
- {
- return !empty(dao::$errors);
- }
- /**
- * 获取错误。
- * Get the errors.
- *
- * @access public
- * @return array|string
- */
- public static function getError($join = false)
- {
- $errors = dao::$errors;
- dao::$errors = array(); // 清除dao的错误信息(Must clear errors)
- if(!$join) return $errors;
- if(is_array($errors))
- {
- $message = '';
- foreach($errors as $item)
- {
- is_array($item) ? $message .= implode('\n', $item) . "\n" : $message .= $item . "\n";
- }
- return $message;
- }
- }
- /**
- * 获取表的字段类型。
- * Get the defination of fields of the table.
- *
- * @access public
- * @return array
- */
- public function getFieldsType()
- {
- $fields = array();
- $rawFields = $this->descTable($this->table);
- foreach($rawFields as $rawField)
- {
- $firstPOS = strpos($rawField->type, '(');
- if(!$firstPOS) $firstPOS = strpos($rawField->type, ' ');
- $type = substr($rawField->type, 0, $firstPOS > 0 ? $firstPOS : strlen($rawField->type));
- $type = str_replace(array('big', 'small', 'medium', 'tiny', 'var'), '', $type);
- $field = array();
- if($type == 'enum' or $type == 'set')
- {
- $rangeBegin = $firstPOS + 2; // 移除开始的引用符 Remove the first quote.
- $rangeEnd = strrpos($rawField->type, ')') - 1; // 移除结束的引用符 Remove the last quote.
- $range = substr($rawField->type, $rangeBegin, $rangeEnd - $rangeBegin);
- $field['rule'] = 'reg';
- $field['options']['reg'] = '/' . str_replace("','", '|', $range) . '/';
- }
- elseif($type == 'char')
- {
- $begin = $firstPOS + 1;
- $end = strpos($rawField->type, ')', $begin);
- $length = substr($rawField->type, $begin, $end - $begin);
- $field['rule'] = 'length';
- $field['options']['max'] = $length;
- $field['options']['min'] = 0;
- }
- elseif($type == 'int')
- {
- $field['rule'] = 'int';
- }
- elseif($type == 'float' or $type == 'double')
- {
- $field['rule'] = 'float';
- }
- elseif($type == 'date')
- {
- $field['rule'] = 'date';
- }
- elseif($type == 'datetime')
- {
- $field['rule'] = 'datetime';
- }
- else
- {
- $field['rule'] = 'skip';
- }
- $fields[$rawField->field] = $field;
- }
- return $fields;
- }
- /**
- * Process SQL error by code.
- *
- * @param object $exception
- * @access public
- * @return void
- */
- public function sqlError($exception)
- {
- $message = $exception->getMessage();
- $message .= ' ' . helper::checkDB2Repair($exception);
- $sql = $this->sqlobj->get();
- $message .= "<p>The sql is: $sql</p>";
- /*
- * 如果开启了将sql错误作为异常抛出,那么拦截sql错误,不触发错误。
- * If throwing sql errors as exceptions is enabled, sql errors are intercepted and not triggered.
- */
- if($this->app->throwError)
- {
- throw new Exception($message);
- }
- $this->app->triggerError($message, __FILE__, __LINE__, $exit = true);
- }
- /**
- * 获取本次会话的 SQL 语句和执行时间。
- * Get SQL statements and execution time of current session.
- *
- * @access public
- * @return array
- */
- public function getProfiles()
- {
- $profiles = [];
- $basePath = $this->app->getBasePath();
- $sqlTypes = ['SELECT', 'INSERT', 'UPDATE', 'DELETE', 'REPLACE'];
- foreach(dbh::$queries as $key => $query)
- {
- $profile = new stdClass();
- $profile->Query_ID = $key + 1;
- $profile->Query = $query;
- $profile->Explain = [];
- $profile->Error = '';
- $profile->Duration = dbh::$durations[$key] ?? 0;
- $profile->Code = str_replace($basePath, '', dbh::$traces[$key] ?? '');
- $profiles[] = $profile;
- $allowExplain = false;
- foreach($sqlTypes as $type)
- {
- if(stripos($query, $type) === 0)
- {
- $allowExplain = true;
- break;
- }
- }
- if(!$allowExplain) continue;
- try
- {
- $slow = false;
- $rows = $this->explain($query, false);
- foreach($rows as $row)
- {
- if($row->type === 'ALL'
- || stripos($row->Extra, 'temporary') !== false
- || stripos($row->Extra, 'filesort') !== false
- || stripos($row->Extra, 'join buffer') !== false
- || stripos($row->Extra, 'checked for each record') !== false
- || stripos($row->Extra, 'full scan on null key') !== false
- )
- {
- $slow = true;
- break;
- }
- }
- if($slow) $profile->Explain = $rows;
- }
- catch(PDOException $e)
- {
- $profile->Error = 'Can not explain the sql statement.';
- }
- }
- return $profiles;
- }
- /**
- * 获取数据库版本。
- * Get database version.
- *
- * @access public
- * @return string|void
- */
- public function getVersion()
- {
- return $this->dbh->getVersion();
- }
- /**
- * 创建临时表。
- * Create temporary table.
- *
- * @param int $ids 用于创建临时表的 id 列表,字符串或数组。
- * @param string $tableName 临时表的名称,默认值为空,由程序自动生成。
- * @param bool $filterTable 是否过滤重复的表名,默认值为 true。
- * @param int $limit 用于创建临时表的 id 列表的数量限制,数量小于这个值时不创建临时表,0 表示不限制数量。
- * @access public
- * @return false|string
- */
- public function createTemporaryTable($idList, $tableName = '', $filterTable = true, $limit = 100)
- {
- return $this->sqlobj->createTemporaryTable($idList, $tableName, $filterTable, $limit);
- }
- }
- /**
- * SQL类。
- * The SQL class.
- *
- * @package framework
- */
- class baseSQL
- {
- /**
- * 所有方法的最大参数个数。
- * The max count of params of all methods.
- *
- */
- const MAX_ARGS = 3;
- /**
- * SQL字符串。
- * The sql string.
- *
- * @var string
- * @access public
- */
- public $sql = '';
- /**
- * 全局对象$app
- * The global app object.
- *
- * @var object
- * @access public
- */
- public $app;
- /**
- * 全局变量$dbh。
- * The global $dbh.
- *
- * @var object
- * @access public
- */
- public $dbh;
- /**
- * 更新或插入的数据。
- * The data to update or insert.
- *
- * @var mixed
- * @access public
- */
- public $data;
- /**
- * 不需要拼接SQL的字段
- * skipFields
- *
- * @var mixed
- * @access public
- */
- public $skipFields;
- /**
- * SQL 方法, insert, update, delete ...
- * SQL method, insert, update, delete ...
- *
- * @var mixed
- * @access public
- */
- public $method;
- /**
- * setField
- *
- * @var mixed
- * @access public
- */
- public $setField;
- /**
- * 是否是第一次设置。
- * Is the first time to call set.
- *
- * @var bool
- * @access public
- */
- public $isFirstSet = true;
- /**
- * 是否是在条件语句中。
- * If in the logic of judge condition or not.
- *
- * @var bool
- * @access public
- */
- public $inCondition = false;
- /**
- * 条件是否为真。
- * The condition is true or not.
- *
- * @var bool
- * @access public
- */
- public $conditionIsTrue = false;
- /**
- * 条件结果,beginIF 中表达式的结果会存储到这个数组中。
- * Store the result of the expression.
- *
- * @var bool
- * @access public;
- */
- public $conditionResults = array();
- /**
- * 条件层级。
- * The condition level.
- *
- * @var bool
- * @access public;
- */
- public $conditionLevel = 0;
- /**
- * WHERE条件嵌套小括号标记。
- * If in mark or not.
- *
- * @var bool
- * @access public
- */
- public $inMark = false;
- /**
- * 是否开启特殊字符转义。
- * Magic quote or not.
- *
- * @var bool
- * @access public
- */
- public $magicQuote;
- /**
- * 表别名。
- * Table alias.
- *
- * @var array
- * @access public
- */
- public $tableAlias;
- /**
- * 当前操作的表。
- * Current table.
- *
- * @var array
- * @access public
- */
- public $currentTable;
- /**
- * 已创建的临时表。
- * The temporary tables.
- *
- * @var array
- * @access public
- */
- public $tempTables = [];
- /**
- * 构造方法。
- * The construct function.
- *
- * @access public
- * @return void
- */
- public function __construct($table = '')
- {
- global $app, $dbh;
- $this->app = $app;
- $this->dbh = $dbh;
- $this->data = new stdclass();
- $this->skipFields = '';
- $this->tableAlias = [];
- $this->magicQuote = (version_compare(phpversion(), '5.4', '<') and function_exists('get_magic_quotes_gpc') and get_magic_quotes_gpc());
- }
- /**
- * 工厂方法。
- * The factory method.
- *
- * @param string $table
- * @access public
- * @return object the sql object.
- */
- public static function factory($table = '')
- {
- return new sql($table);
- }
- /**
- * 设置SQL的方法。
- * Set SQL method.
- *
- * @param string $method
- * @access public
- * @return void
- */
- public function setMethod($method = '')
- {
- $this->method = $method;
- }
- /**
- * select语句。
- * The sql is select.
- *
- * @param string $field
- * @access public
- * @return object the sql object.
- */
- public static function select($field = '*')
- {
- $sqlobj = self::factory();
- $sqlobj->setMethod('select');
- $sqlobj->sql = "SELECT $field ";
- return $sqlobj;
- }
- /**
- * update语句。
- * The sql is update.
- *
- * @param string $table
- * @access public
- * @return object the sql object.
- */
- public static function update($table)
- {
- $sqlobj = self::factory();
- $sqlobj->setMethod('update');
- $sqlobj->sql = "UPDATE $table SET ";
- return $sqlobj;
- }
- /**
- * insert语句。
- * The sql is insert.
- *
- * @param string $table
- * @access public
- * @return object the sql object.
- */
- public static function insert($table)
- {
- $sqlobj = self::factory();
- $sqlobj->setMethod('insert');
- $sqlobj->sql = "INSERT INTO $table ";
- return $sqlobj;
- }
- /**
- * replace语句。
- * The sql is replace.
- *
- * @param string $table
- * @access public
- * @return object the sql object.
- */
- public static function replace($table)
- {
- $sqlobj = self::factory();
- $sqlobj->setMethod('replace');
- $sqlobj->sql = "REPLACE INTO $table SET ";
- return $sqlobj;
- }
- /**
- * delete语句。
- * The sql is delete.
- *
- * @access public
- * @return object the sql object.
- */
- public static function delete()
- {
- $sqlobj = self::factory();
- $sqlobj->setMethod('delete');
- $sqlobj->sql = "DELETE ";
- return $sqlobj;
- }
- /**
- * 将关联数组转换为sql语句中 `key` = value 的形式。
- * Join the data items by key = value.
- *
- * @param object $data
- * @param string $skipFields the fields to skip.
- * @access public
- * @return object the sql object.
- */
- public function data($data, $skipFields = '')
- {
- $data = (object) $data;
- if($skipFields) $this->skipFields = ',' . str_replace(' ', '', $skipFields) . ',';
- if($this->method != 'insert')
- {
- foreach($data as $field => $value)
- {
- if(!preg_match('|^\w+$|', $field))
- {
- unset($data->$field);
- continue;
- }
- if(strpos($this->skipFields, ",$field,") !== false) continue;
- if($field == 'id' and $this->method == 'update') continue; // primary key not allowed in dmdb.
- $this->sql .= "`$field` = " . $this->quote($value) . ',';
- }
- }
- $this->data = $data;
- $this->sql = rtrim($this->sql, ','); // Remove the last ','.
- return $this;
- }
- /**
- * 在左边添加'('。
- * Add an '(' at left.
- *
- * @param int $count
- * @access public
- * @return static|sql the sql object.
- */
- public function markLeft($count = 1)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= str_repeat('(', $count);
- $this->inMark = true;
- return $this;
- }
- /**
- * 在右边增加')'。
- * Add an ')' at right.
- *
- * @param int $count
- * @access public
- * @return static|sql the sql object.
- */
- public function markRight($count = 1)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= str_repeat(')', $count);
- $this->inMark = false;
- return $this;
- }
- /**
- * SET部分。
- * The set part.
- *
- * @param string $set
- * @access public
- * @return static|sql the sql object.
- */
- public function set($set)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- /* DMDB replace will use $this->data. */
- if($this->method == 'insert' or $this->method == 'replace')
- {
- $this->setField = $set;
- $this->data->$set = '';
- }
- if($this->method != 'insert')
- {
- /* Add ` to avoid keywords of mysql. */
- if(strpos($set, '=') === false)
- {
- $set = str_replace(',', '', $set);
- $set = $this->dbh->iqchar . str_replace('`', '', $set) . $this->dbh->iqchar;
- }
- else
- {
- $set = str_replace('`', $this->dbh->iqchar, $set);
- }
- $this->sql .= $this->isFirstSet ? " $set" : ", $set";
- if($this->isFirstSet) $this->isFirstSet = false;
- }
- return $this;
- }
- /**
- * 创建From部分。
- * Create the from part.
- *
- * @param string $table
- * @access public
- * @return static|sql the sql object.
- */
- public function from($table)
- {
- $this->sql .= "FROM $table";
- $this->currentTable = $table;
- /* Default table. */
- $this->tableAlias[''] = $table;
- return $this;
- }
- /**
- * 创建Alias部分,Alias转为AS。
- * Create the Alias part.
- *
- * @param string $alias
- * @access public
- * @return static|sql the sql object.
- */
- public function alias($alias)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " AS $alias ";
- $this->tableAlias[$alias] = $this->currentTable;
- return $this;
- }
- /**
- * 创建LEFT JOIN部分。
- * Create the left join part.
- *
- * @param string $table
- * @access public
- * @return static|sql the sql object.
- */
- public function leftJoin($table)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " LEFT JOIN $table";
- $this->currentTable = $table;
- return $this;
- }
- /**
- * 创建ON部分。
- * Create the on part.
- *
- * @param string $condition
- * @access public
- * @return static|sql the sql object.
- */
- public function on($condition)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " ON $condition ";
- return $this;
- }
- /**
- * 开始条件判断。
- * Begin condition judge.
- *
- * @param bool $condition
- * @access public
- * @return static|sql the sql object.
- */
- public function beginIF($condition)
- {
- $this->inCondition = true;
- $this->conditionLevel += 1;
- $this->conditionResults[$this->conditionLevel] = $condition;
- $this->conditionIsTrue = !in_array(false, $this->conditionResults);
- return $this;
- }
- /**
- * 结束条件判断。
- * End the condition judge.
- *
- * @access public
- * @return static|sql the sql object.
- */
- public function fi()
- {
- unset($this->conditionResults[$this->conditionLevel]);
- $this->conditionLevel -= 1;
- if($this->conditionLevel > 0)
- {
- $this->conditionIsTrue = !in_array(false, $this->conditionResults);
- return $this;
- }
- $this->inCondition = false;
- $this->conditionIsTrue = false;
- return $this;
- }
- /**
- * 创建WHERE部分。
- * Create the where part.
- *
- * @param string $arg1 the field name
- * @param string $arg2 the operator
- * @param string $arg3 the value
- * @access public
- * @return static|sql the sql object.
- */
- public function where($arg1 = '', $arg2 = null, $arg3 = null)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(!$arg1)
- {
- $condition = '';
- }
- elseif($arg3 !== null)
- {
- $condition = "`$arg1` $arg2 " . $this->quote($arg3);
- }
- else
- {
- $condition = (is_string($arg1) && ctype_alnum($arg1)) ? '`' . $arg1 . '`' : $arg1;
- }
- if(!$this->inMark) $this->sql .= ' ' . DAO::WHERE ." $condition ";
- if($this->inMark) $this->sql .= " $condition ";
- return $this;
- }
- /**
- * 创建AND部分。
- * Create the AND part.
- *
- * @param string $condition
- * @access public
- * @return static|sql the sql object.
- */
- public function andWhere($condition, $addMark = false)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(is_string($condition) && ctype_alnum($condition)) $condition = '`' . $condition . '`';
- $mark = $addMark ? '(' : '';
- $this->sql .= " AND {$mark} $condition ";
- return $this;
- }
- /**
- * 创建OR部分。
- * Create the OR part.
- *
- * @param string $condition
- * @access public
- * @return static|sql the sql object.
- */
- public function orWhere($condition)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(is_string($condition) && ctype_alnum($condition)) $condition = '`' . $condition . '`';
- $this->sql .= " OR $condition ";
- return $this;
- }
- /**
- * 创建'='部分。
- * Create the '='.
- *
- * @param string $value
- * @access public
- * @return static|sql the sql object.
- */
- public function eq($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if($this->method == 'insert' or $this->method == 'replace')
- {
- $field = $this->setField;
- $this->data->$field = $value;
- }
- if($this->method != 'insert')
- {
- $this->sql .= " = " . $this->quote($value);
- }
- return $this;
- }
- /**
- * 创建'!='。
- * Create '!='.
- *
- * @param string $value
- * @access public
- * @return static|sql the sql object.
- */
- public function ne($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " != " . $this->quote($value);
- return $this;
- }
- /**
- * 创建'>'。
- * Create '>'.
- *
- * @param string $value
- * @access public
- * @return static|sql the sql object.
- */
- public function gt($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " > " . $this->quote($value);
- return $this;
- }
- /**
- * 创建'>='
- * Create '>='.
- *
- * @param string $value
- * @access public
- * @return static|sql the sql object.
- */
- public function ge($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " >= " . $this->quote($value);
- return $this;
- }
- /**
- * 创建'<'。
- * Create '<'.
- *
- * @param mixed $value
- * @access public
- * @return static|sql the sql object.
- */
- public function lt($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " < " . $this->quote($value);
- return $this;
- }
- /**
- * 创建 '<='。
- * Create '<='.
- *
- * @param mixed $value
- * @access public
- * @return static|sql the sql object.
- */
- public function le($value)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " <= " . $this->quote($value);
- return $this;
- }
- /**
- * 创建"between and"。
- * Create "between and"
- *
- * @param string $min
- * @param string $max
- * @access public
- * @return static|sql the sql object.
- */
- public function between($min, $max)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $min = $this->quote($min);
- $max = $this->quote($max);
- $this->sql .= " BETWEEN $min AND $max ";
- return $this;
- }
- /**
- * 创建IN部分。
- * Create in part.
- *
- * @param string|array $ids ','分割的字符串或者数组。List string by ',' or an array.
- * @param bool $useTemporaryTable 是否使用临时表。 Use temporary table or not.
- * @param string $tableName 临时表的名称,默认值为空,由程序自动生成。The name of temporary table, default is empty, generated by program.
- * @param bool $filterTable 是否过滤重复的表名,默认值为 true。Filter duplicate table name or not.
- * @param bool $limit 用于创建临时表的 id 列表的数量限制,数量小于这个值时不创建临时表,0 表示不限制数量。Limit the count of ids to create temporary table, 0 means no limit.
- * @access public
- * @return static|sql the sql object.
- */
- public function in($ids, $useTemporaryTable = false, $tableName = '', $filterTable = true, $limit = 100)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(is_null($ids))
- {
- $this->sql .= ' IS NULL';
- return $this;
- }
- if($useTemporaryTable)
- {
- $tableName = $this->createTemporaryTable($ids, $tableName, $filterTable, $limit, false);
- if($tableName)
- {
- $this->sql .= " IN (SELECT id FROM $tableName)";
- return $this;
- }
- }
- $this->sql .= helper::dbIN($ids);
- return $this;
- }
- /**
- * 创建临时表。
- * Create temporary table.
- *
- * @param int $ids 用于创建临时表的 id 列表,字符串或数组。
- * @param string $tableName 临时表的名称,默认值为空,由程序自动生成。
- * @param bool $filterTable 是否过滤重复的表名,默认值为 true。
- * @param int $limit 用于创建临时表的 id 列表的数量限制,数量小于这个值时不创建临时表,0 表示不限制数量。
- * @param bool $exit 是否停止程序执行,默认值为 true。
- * @access public
- * @return string
- */
- public function createTemporaryTable($ids, $tableName = '', $filterTable = true, $limit = 100, $exit = true)
- {
- if(!is_string($ids) && !is_array($ids))
- {
- $this->app->triggerError('The idList must be a string or an array.', __FILE__, __LINE__, $exit);
- return '';
- }
- if(is_string($ids)) $ids = explode(',', $ids);
- $idList = [];
- foreach($ids as $id)
- {
- if(empty($id)) continue;
- $intID = (int)$id;
- if($intID != $id) continue;
- $idList[$intID] = $intID;
- }
- if($limit && count($idList) < $limit)
- {
- $this->app->triggerError("The idList count must be greater than $limit", __FILE__, __LINE__, $exit);
- return '';
- }
- $rows = '';
- if(empty($tableName))
- {
- if($filterTable)
- {
- /* 如果开启了唯一表名,那么将表名设置为 md5(idList). */
- asort($idList);
- $rows = '(' . implode('),(', $idList) . ')';
- $tableName = "temp_" . md5($rows);
- if(isset($this->tempTables[$tableName])) return $tableName;
- }
- else
- {
- $tableName = 'temp_' . uniqid();
- $rows = '(' . implode('),(', $idList) . ')';
- }
- }
- if(!preg_match('/^temp_\w+$/', $tableName))
- {
- $this->app->triggerError("The table name should be like 'temp_xxx', where xxx is a string only containing letters, numbers and underscores.", __FILE__, __LINE__, $exit);
- return '';
- }
- if($filterTable && isset($this->tempTables[$tableName])) return $tableName;
- try
- {
- $this->dbh->exec("CREATE TEMPORARY TABLE IF NOT EXISTS {$tableName} (id int PRIMARY KEY)");
- $this->dbh->exec("INSERT INTO {$tableName} VALUES {$rows}");
- }
- catch(PDOException $e)
- {
- $message = $e->getMessage() . ' ' . helper::checkDB2Repair($e);
- $this->app->triggerError($message, __FILE__, __LINE__, $exit);
- return '';
- }
- if($filterTable) $this->tempTables[$tableName] = $tableName;
- return $tableName;
- }
- /**
- * 创建'NOT IN'部分。
- * Create not in part.
- *
- * @param string|array $ids list string by ',' or an array
- * @access public
- * @return static|sql the sql object.
- */
- public function notin($ids)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if((is_string($ids) && $ids === '') || (is_array($ids) && empty($ids)))
- {
- $pattern = '/\s+(?:(?:[a-zA-Z0-9]+\.)?|)(?:`([^`]+)`|"([^"]+)"|(\w+))\s*$/i';
- $replacement = ' 1=1 ';
- $this->sql = preg_replace($pattern, $replacement, $this->sql);
- return $this;
- }
- $dbIN = helper::dbIN($ids);
- if(strpos($dbIN, '=') === 0) $this->sql .= ' !' . $dbIN;
- else $this->sql .= ' NOT ' . helper::dbIN($ids);
- return $this;
- }
- /**
- * 创建子查询IN部分。
- * Create subquery in part.
- *
- * @param string|dao
- * @access public
- * @return static|sql the sql object.
- */
- public function subIn($subquery)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(!is_string($subquery)) $subquery = $this->dbh->formatSQL($subquery->processSQL());
- $this->sql .= ' IN (' . $subquery . ')';
- return $this;
- }
- /**
- * 创建子查询'NOT IN'部分。
- * Create subquery not in part.
- *
- * @param string|dao
- * @access public
- * @return static|sql the sql object.
- */
- public function subNotIn($subquery)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(!is_string($subquery)) $subquery = $this->dbh->formatSQL($subquery->processSQL());
- $this->sql .= ' NOT IN (' . $subquery . ')';
- return $this;
- }
- /**
- * 创建LIKE部分。
- * Create the like by part.
- *
- * @param string $string
- * @access public
- * @return static|sql the sql object.
- */
- public function like($string)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " LIKE " . $this->quote($string);
- return $this;
- }
- /**
- * 创建NOT LIKE部分。
- * Create the not like by part.
- *
- * @param string $string
- * @access public
- * @return static|sql the sql object.
- */
- public function notLike($string)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= "NOT LIKE " . $this->quote($string);
- return $this;
- }
- /**
- * 字段为空。
- * Set the field is null statement part.
- *
- * @access public
- * @return static|sql the sql object.
- */
- public function isNULL()
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " IS NULL ";
- return $this;
- }
- /**
- * 字段不为空。
- * Set the field is not null statement part.
- *
- * @access public
- * @return static|sql the sql object.
- */
- public function notNULL()
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " IS NOT NULL ";
- return $this;
- }
- /**
- * 不为空日期
- * Create not zero date.
- *
- * @access public
- * @return static|sql the sql object.
- */
- public function notZeroDate()
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " > '1970-01-01' ";
- return $this;
- }
- /**
- * 不为空时间
- * Create not zero datetime.
- *
- * @access public
- * @return static|sql the sql object.
- */
- public function notZeroDatetime()
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= " > '1970-01-01 00:00:01' ";
- return $this;
- }
- /**
- * 创建ORDER BY部分。
- * Create the order by part.
- *
- * @param string $order
- * @access public
- * @return static|sql the sql object.
- */
- public function orderBy($order)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $order = str_replace(array('|desc', 'desc', '_desc'), ' desc', $order);
- $order = str_replace(array('|asc', 'asc', '_asc'), ' asc', $order);
- /* Add "`" in order string. */
- /* When order has limit string. */
- $pos = stripos($order, 'limit');
- $orders = $pos ? substr($order, 0, $pos) : $order;
- $limit = $pos ? substr($order, $pos) : '';
- if(!empty($limit))
- {
- $trimmedLimit = trim(str_replace('limit', '', $limit));
- if(!preg_match('/^[0-9]+ *(, *[0-9]+)?$/', $trimmedLimit)) helper::end("Limit is bad query, The limit is " . htmlspecialchars($limit));
- }
- $orders = trim($orders);
- if(empty($orders)) return $this;
- if(!preg_match('/^(\w+\.)?(`\w+`|\w+)( +(desc|asc))?( *(, *(\w+\.)?(`\w+`|\w+)( +(desc|asc))?)?)*$/i', $orders)) helper::end("Order is bad request, The order is " . htmlspecialchars($orders));
- $orders = explode(',', $orders);
- foreach($orders as $i => $order)
- {
- $orderParse = explode(' ', trim($order));
- foreach($orderParse as $key => $value)
- {
- $value = trim($value);
- if(empty($value) or strtolower($value) == 'desc' or strtolower($value) == 'asc') continue;
- $field = $value;
- /* such as t1.id field. */
- if(strpos($value, '.') !== false) list($table, $field) = explode('.', $field);
- if(strpos($field, '`') === false) $field = "`$field`";
- $orderParse[$key] = isset($table) ? $table . '.' . $field : $field;
- unset($table);
- }
- $orders[$i] = implode(' ', $orderParse);
- if(empty($orders[$i])) unset($orders[$i]);
- }
- $order = implode(',', $orders) . ' ' . $limit;
- $this->sql .= ' ' . DAO::ORDERBY . " $order";
- return $this;
- }
- /**
- * 创建LIMIT部分。
- * Create the limit part.
- *
- * @param string $limit
- * @access public
- * @return static|sql the sql object.
- */
- public function limit($limit)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- if(empty($limit)) return $this;
- /* filter limit. */
- $limit = trim(str_ireplace('limit', '', $limit));
- if(!preg_match('/^[0-9]+ *(, *[0-9]+)?$/', $limit))
- {
- $limit = htmlspecialchars($limit);
- helper::end("Limit is bad query, The limit is $limit");
- }
- $this->sql .= ' ' . DAO::LIMIT . " $limit ";
- return $this;
- }
- /**
- * 创建GROUP BY部分。
- * Create the groupby part.
- *
- * @param string $groupBy
- * @access public
- * @return static|sql the sql object.
- */
- public function groupBy($groupBy)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- //The dm database cannot use alias for group by
- /*
- if(!preg_match('/^\w+[a-zA-Z0-9_`.]+$/', $groupBy))
- {
- $groupBy = htmlspecialchars($groupBy);
- helper::end("Group is bad query, The group is $groupBy");
- }
- */
- $this->sql .= ' ' . DAO::GROUPBY . " $groupBy";
- return $this;
- }
- /**
- * 创建HAVING部分。
- * Create the having part.
- *
- * @param string $having
- * @access public
- * @return static|sql the sql object.
- */
- public function having($having)
- {
- if($this->inCondition and !$this->conditionIsTrue) return $this;
- $this->sql .= ' ' . DAO::HAVING . " $having";
- return $this;
- }
- /**
- * 获取SQL字符串。
- * Get the sql string.
- *
- * @access public
- * @return static|sql
- */
- public function get()
- {
- return $this->sql;
- }
- /**
- * 对字段加转义。
- * Quote a var.
- *
- * @param mixed $value
- * @access public
- * @return mixed
- */
- public function quote($value)
- {
- if(is_null($value)) return 'NULL';
- if($this->magicQuote) $value = stripslashes($value);
- return $this->dbh->quote((string)$value);
- }
- }
|