Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Database/Query.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan\Database;
 12: 
 13: use Kotchasan\ArrayTool;
 14: 
 15: /**
 16:  * Database Query (base class).
 17:  *
 18:  * @author Goragod Wiriya <admin@goragod.com>
 19:  *
 20:  * @since 1.0
 21:  */
 22: abstract class Query extends \Kotchasan\Database\Db
 23: {
 24:     /**
 25:      * true แสดง Query ออกทางหน่าจอก่อนการ execute.
 26:      *
 27:      * @var bool
 28:      */
 29:     protected $debugger = false;
 30:     /**
 31:      * ตัวแปรเก็บคำสั่ง SQL.
 32:      *
 33:      * @var array
 34:      */
 35:     protected $sqls;
 36: 
 37:     /**
 38:      * คำสั่งสำหรับแสดง Query ออกทางหน้าจอ
 39:      * ใช้ในการ debug Query.
 40:      */
 41:     public function debug()
 42:     {
 43:         $this->debugger = true;
 44: 
 45:         return $this;
 46:     }
 47: 
 48:     /**
 49:      * ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล
 50:      * คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table ครอบชื่อตารางด้วย ``.
 51:      *
 52:      * @param string $table ชื่อตารางตามที่กำหนดใน settings/datasbase.php
 53:      *
 54:      * @return string
 55:      */
 56:     public function getFullTableName($table)
 57:     {
 58:         $dbname = empty($this->db->settings->dbname) ? '' : '`'.$this->db->settings->dbname.'`.';
 59: 
 60:         return $dbname.'`'.$this->getTableName($table).'`';
 61:     }
 62: 
 63:     /**
 64:      * ฟังก์ชั่นอ่านชื่อตารางจากการตั้งค่าฐานข้อมุล
 65:      * คืนค่า ชื่อตารางรวม prefix ถ้าไม่มีชื่อกำหนดไว้ จะคืนค่า $table.
 66:      *
 67:      * @param string $table ชื่อตารางตามที่กำหนดใน settings/datasbase.php
 68:      *
 69:      * @return string
 70:      */
 71:     public function getTableName($table)
 72:     {
 73:         $prefix = empty($this->db->settings->prefix) ? '' : $this->db->settings->prefix.'_';
 74: 
 75:         return $prefix.(isset($this->db->tables->$table) ? $this->db->tables->$table : $table);
 76:     }
 77: 
 78:     /**
 79:      * ฟังก์ชั่นสร้างคำสั่ง SQL เป็นข้อความ
 80:      *
 81:      * @return string
 82:      */
 83:     public function text()
 84:     {
 85:         $sql = '';
 86:         if (!empty($this->sqls)) {
 87:             $sql = $this->db->makeQuery($this->sqls);
 88:             foreach (array_reverse($this->getValues()) as $key => $value) {
 89:                 $sql = str_replace($key, (is_string($value) ? "'$value'" : $value), $sql);
 90:             }
 91:         }
 92: 
 93:         return $sql;
 94:     }
 95: 
 96:     /**
 97:      * ฟังก์ชั่นสร้างคีย์ สำหรับการ execute.
 98:      *
 99:      * @param string $name   ชื่อฟิลด์
100:      * @param string $prefix คำนำหน้าชื่อฟิลด์ ใช้เพื่อป้องกันการใช้ตัวแปรซ้ำ
101:      *
102:      * @return string
103:      */
104:     protected function aliasName($name, $prefix = '')
105:     {
106:         return ':'.$prefix.trim(preg_replace('/[`\._\-]/', '', $name));
107:     }
108: 
109:     /**
110:      * สร้าง query สำหรับ GROUP BY.
111:      *
112:      * @param array|string $fields array('U.id', 'U.username') หรือ string U.id
113:      *
114:      * @return string
115:      */
116:     protected function buildGroup($fields)
117:     {
118:         $sqls = array();
119:         foreach ((array) $fields as $item) {
120:             $sqls[] = $this->fieldName($item);
121:         }
122: 
123:         return empty($sqls) ? '' : implode(', ', $sqls);
124:     }
125: 
126:     /**
127:      * สร้างคำสั่ง JOIN
128:      * ถ้าไม่มี alias คืนค่าว่าง.
129:      *
130:      * @param string|array $table ชื่อตารางต้องมี alias ด้วย หรือ (QueryBuilder, alias)
131:      * @param string       $type  เข่น INNER OUTER LEFT RIGHT
132:      * @param mixed        $on    query string หรือ array
133:      *
134:      * @return string
135:      */
136:     protected function buildJoin($table, $type, $on)
137:     {
138:         $ret = $this->buildWhere($on);
139:         $sql = is_array($ret) ? $ret[0] : $ret;
140:         if (is_array($table)) {
141:             $sql = ' '.$type.' JOIN ('.$table[0]->text().') AS '.$table[1].' ON '.$sql;
142:         } elseif (preg_match('/^([a-zA-Z0-9_]+)([\s]+(as|AS))?[\s]+([A-Z0-9]{1,2})$/', $table, $match)) {
143:             $sql = ' '.$type.' JOIN '.$this->getFullTableName($match[1]).' AS '.$match[4].' ON '.$sql;
144:         } elseif (preg_match('/^([a-z0-9_]+)([\s]+(as|AS))?[\s]+([a-z0-9_]+)$/', $table, $match)) {
145:             $sql = ' '.$type.' JOIN '.$this->getFullTableName($match[1]).' AS `'.$match[4].'` ON '.$sql;
146:         } else {
147:             $sql = ' '.$type.' JOIN '.$table.' ON '.$sql;
148:         }
149:         if (is_array($ret)) {
150:             return array($sql, $ret[1]);
151:         } else {
152:             return $sql;
153:         }
154:     }
155: 
156:     /**
157:      * สร้าง query เรียงลำดับ
158:      *
159:      * @param array|string $fields array('field ASC','field DESC') หรือ 'field ASC', 'field DESC', ....
160:      *
161:      * @return string
162:      */
163:     protected function buildOrder($fields)
164:     {
165:         $sqls = array();
166:         foreach ((array) $fields as $item) {
167:             if (preg_match('/^([A-Z]{1,1}[0-9]{0,1}\.)([a-zA-Z0-9_]+)([\s]{1,}(ASC|DESC|asc|desc))?$/', $item, $match)) {
168:                 // U.id DESC
169:                 $sqls[] = $match[1].'`'.$match[2].'`'.(isset($match[4]) ? " $match[4]" : '');
170:             } elseif (preg_match('/^([a-zA-Z0-9_]+)(\.([a-zA-Z0-9_]+))?(([\s]+)?(ASC|DESC|asc|desc))?$/', $item, $match)) {
171:                 // field.id DESC
172:                 $sqls[] = '`'.$match[1].'`'.(empty($match[3]) ? '' : '.`'.$match[3].'`').(isset($match[6]) ? " $match[6]" : '');
173:             } elseif (strtoupper($item) === 'RAND()') {
174:                 // RAND()
175:                 $sqls[] = 'RAND()';
176:             }
177:         }
178: 
179:         return implode(', ', $sqls);
180:     }
181: 
182:     /**
183:      * ฟังก์ชั่นสร้าง query string สำหรับคำสั่ง SELECT.
184:      *
185:      * @param string|array|QueryBuilder $fields
186:      *
187:      * @return string
188:      */
189:     protected function buildSelect($fields)
190:     {
191:         if (is_array($fields)) {
192:             if ($fields[0] instanceof QueryBuilder) {
193:                 // QueryBuilder
194:                 $ret = '('.$fields[0]->text().') AS `'.$fields[1].'`';
195:             } elseif (is_string($fields[0]) && preg_match('/^([a-zA-Z0-9\\\]+)::([a-zA-Z0-9]+)$/', $fields[0], $match)) {
196:                 // Recordset
197:                 $ret = '\''.addslashes($fields[0]).'\' AS `'.$fields[1].'`';
198:             } else {
199:                 // multiples
200:                 $rets = array();
201:                 foreach ($fields as $item) {
202:                     $rets[] = $this->buildSelect($item);
203:                 }
204:                 $ret = implode(',', $rets);
205:             }
206:         } elseif ($fields instanceof QueryBuilder) {
207:             // QueryBuilder
208:             $ret = '('.$fields->text().')';
209:         } elseif ($fields instanceof Sql) {
210:             // Sql
211:             $ret = $fields->text();
212:         } elseif ($fields == '*') {
213:             $ret = '*';
214:         } elseif (preg_match('/^(NULL|[0-9]+)([\s]+as)?[\s]+`?([^`]+)`?$/i', $fields, $match)) {
215:             // 0 as alias, NULL as alias
216:             $ret = $match[1].' AS `'.$match[3].'`';
217:         } elseif (preg_match('/^([\'"])(.*)\\1([\s]+as)?[\s]+`?([^`]+)`?$/i', $fields, $match)) {
218:             // 'string' as alias
219:             $ret = "'$match[2]' AS `$match[4]`";
220:         } elseif (preg_match('/^([A-Z]{1,1}[0-9]{0,1})\.`?([\*a-zA-Z0-9_]+)`?(([\s]+(as|AS))?[\s]+`?([^`]+)`?)?$/', $fields, $match)) {
221:             // U.id alias U.* AS alias
222:             $ret = $match[1].'.'.($match[2] == '*' ? '*' : '`'.$match[2].'`').(isset($match[6]) ? ' AS `'.$match[6].'`' : '');
223:         } elseif (preg_match('/^`?([a-z0-9_]+)`?\.`?([\*a-z0-9_]+)`?(([\s]+as)?[\s]+`?([^`]+)`?)?$/i', $fields, $match)) {
224:             // table.field alias
225:             $ret = '`'.$match[1].'`.'.($match[2] == '*' ? '*' : '`'.$match[2].'`').(isset($match[5]) ? ' AS `'.$match[5].'`' : '');
226:         } elseif (preg_match('/^`?([a-z0-9_]+)`?([\s]+as)?[\s]+`?([^`]+)`?$/i', $fields, $match)) {
227:             // table.field
228:             $ret = '`'.$match[1].'` AS `'.$match[3].'`';
229:         } elseif (preg_match('/([a-z0-9_]+)/i', $fields, $match)) {
230:             // field name เช่น id
231:             $ret = '`'.$fields.'`';
232:         }
233: 
234:         return isset($ret) ? $ret : '';
235:     }
236: 
237:     /**
238:      * แปลงข้อมูลรูปแบบ SQL
239:      * รูปแบบ array('field1', 'condition', 'field2')
240:      * ไม่ระบุ condition หมายถึง = หรือ IN.
241:      *
242:      * @param array $params
243:      *
244:      * @return string
245:      */
246:     protected function buildValue($params)
247:     {
248:         if (is_array($params)) {
249:             if (count($params) == 2) {
250:                 $params = array($params[0], '=', $params[1]);
251:             } else {
252:                 $params = array($params[0], trim($params[1]), $params[2]);
253:             }
254:             $key = $this->fieldName($params[0]);
255:             if (is_numeric($params[2]) || is_bool($params[2])) {
256:                 // value เป็นตัวเลข หรือ boolean
257:                 $value = $params[2];
258:             } elseif (is_array($params[2])) {
259:                 // value เป็น array
260:                 if ($params[1] == '=') {
261:                     $params[1] = 'IN';
262:                 }
263:                 $qs = array();
264:                 foreach ($params[2] as $item) {
265:                     if (is_numeric($item) || is_bool($item)) {
266:                         $qs[] = $item;
267:                     } else {
268:                         $qs[] = "'$item'";
269:                     }
270:                 }
271:                 $value = '('.implode(', ', $qs).')';
272:             } elseif (preg_match('/^\((.*)\)([\s]+as)?[\s]+([a-z0-9_]+)$/i', $params[2], $match)) {
273:                 // value เป็น query string
274:                 $value = "($match[1]) AS `$match[3]`";
275:             } elseif (preg_match('/^([A-Z]{1,1}[0-9]{0,1})\.([a-zA-Z0-9_]+)$/', $params[2], $match)) {
276:                 // U.id
277:                 $value = $match[1].'.`'.$match[2].'`';
278:             } elseif (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)$/i', $params[2], $match)) {
279:                 // value เป็น table.field
280:                 $value = '`'.$match[1].'`.`'.$match[2].'`';
281:             } else {
282:                 // value เป็น string
283:                 $value = "'".$params[2]."'";
284:             }
285:             $params = $key.' '.$params[1].' '.$value;
286:         }
287: 
288:         return $params;
289:     }
290: 
291:     /**
292:      * ฟังก์ชั่นสร้างคำสั่ง WHERE.
293:      * คืนค่า string สำหรับคำสั่ง WHERE หรือคืนค่า array(where, values) สำหรับใช้กับการ bind.
294:      *
295:      * @param mixed  $condition
296:      * @param string $operator  (optional) เช่น AND หรือ OR
297:      * @param string $id        (optional )ชื่อฟิลด์ที่เป็น key
298:      *
299:      * @return string|array
300:      */
301:     protected function buildWhere($condition, $operator = 'AND', $id = 'id')
302:     {
303:         if (is_array($condition)) {
304:             if (is_array($condition[0])) {
305:                 $qs = array();
306:                 $ps = array();
307:                 foreach ($condition as $i => $item) {
308:                     $ret = $this->whereValue($item, $i);
309:                     if (is_array($ret)) {
310:                         $qs[] = $ret[0];
311:                         $ps += $ret[1];
312:                     } else {
313:                         $qs[] = $ret;
314:                     }
315:                 }
316:                 $ret = implode(' '.$operator.' ', $qs);
317:                 if (!empty($ps)) {
318:                     $ret = array($ret, $ps);
319:                 }
320:             } elseif ($condition[0] instanceof Sql) {
321:                 $qs = array();
322:                 $ps = array();
323:                 foreach ($condition as $i => $item) {
324:                     $qs[] = $item->text();
325:                     $ps += $item->getValues();
326:                 }
327:                 $ret = implode(' '.$operator.' ', $qs);
328:                 if (!empty($ps)) {
329:                     $ret = array($ret, $ps);
330:                 }
331:             } else {
332:                 $ret = $this->whereValue($condition);
333:             }
334:         } elseif ($condition instanceof Sql) {
335:             $values = $condition->getValues();
336:             if (empty($values)) {
337:                 $ret = $condition->text();
338:             } else {
339:                 $ret = array($condition->text(), $values);
340:             }
341:         } elseif (preg_match('/^[0-9]+$/', $condition)) {
342:             // primaryKey
343:             $ret = $this->fieldName($id).' = '.$condition;
344:         } else {
345:             // พารามิเตอร์ ไม่ถูกต้อง
346:             trigger_error('Invalid arguments in buildWhere('.var_export($condition, true).')', E_USER_ERROR);
347:         }
348: 
349:         return $ret;
350:     }
351: 
352:     /**
353:      * ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์
354:      * คืนค่า ($condition, $values).
355:      *
356:      * @param mixed  $condition
357:      * @param string $operator  (optional) เช่น AND หรือ OR
358:      * @param string $id        (optional )ชื่อฟิลด์ที่เป็น key
359:      *
360:      * @return array
361:      */
362:     protected function buildWhereValues($condition, $operator = 'AND', $id = 'id')
363:     {
364:         if (is_array($condition)) {
365:             $values = array();
366:             $qs = array();
367:             if (is_array($condition[0])) {
368:                 foreach ($condition as $item) {
369:                     $ret = $this->buildWhereValues($item, $operator, $id);
370:                     $qs[] = $ret[0];
371:                     $values = ArrayTool::replace($values, $ret[1]);
372:                 }
373:                 $condition = implode(' '.$operator.' ', $qs);
374:             } elseif (strpos($condition[0], '(') !== false) {
375:                 $condition = $condition[0];
376:             } else {
377:                 if (count($condition) == 2) {
378:                     $condition = array($condition[0], '=', $condition[1]);
379:                 } else {
380:                     $condition[1] = strtoupper(trim($condition[1]));
381:                 }
382:                 if (is_array($condition[2])) {
383:                     $operator = $condition[1] == '=' ? 'IN' : $condition[1];
384:                     $qs = array();
385:                     foreach ($condition[2] as $k => $v) {
386:                         $qs[] = ":$condition[0]$k";
387:                         $values[":$condition[0]$k"] = $v;
388:                     }
389:                     $condition = $this->fieldName($condition[0]).' '.$operator.' ('.implode(',', $qs).')';
390:                 } else {
391:                     $values[":$condition[0]"] = $condition[2];
392:                     $condition = $this->fieldName($condition[0]).' '.$condition[1].' :'.$condition[0];
393:                 }
394:             }
395:         } elseif (is_numeric($condition)) {
396:             // primaryKey
397:             $values = array(":$id" => $condition);
398:             $condition = "`$id` = :$id";
399:         } else {
400:             $values = array();
401:         }
402: 
403:         return array($condition, $values);
404:     }
405: 
406:     /**
407:      * แปลงข้อความสำหรับชื่อฟิลด์หรือชื่อตาราง
408:      *
409:      * @param string $name
410:      *
411:      * @return string
412:      */
413:     protected function fieldName($name)
414:     {
415:         if (is_array($name)) {
416:             if ($name[0] instanceof QueryBuilder) {
417:                 $ret = '('.$name[0]->text().') AS `'.$name[1].'`';
418:             } else {
419:                 $rets = array();
420:                 foreach ($name as $item) {
421:                     $rets[] = $this->fieldName($item);
422:                 }
423:                 $ret = implode(', ', $rets);
424:             }
425:         } elseif (is_numeric($name)) {
426:             $ret = $name;
427:         } elseif (is_string($name)) {
428:             $name = trim($name);
429:             if (strpos($name, '(') !== false && preg_match('/^(.*?)(\s{0,}(as)?\s{0,}`?([a-z0-9_]+)`?)?$/i', $name, $match)) {
430:                 // (...) as pos
431:                 $ret = $match[1].(isset($match[4]) ? " AS `$match[4]`" : '');
432:             } elseif (preg_match('/^([A-Z]{1,1}[0-9]{0,1})\.([\*a-zA-Z0-9_]+)((\s+(as|AS))?\s+([a-zA-Z0-9_]+))?$/', $name, $match)) {
433:                 // U.id as user_id U.*
434:                 $ret = $match[1].'.'.($match[2] == '*' ? '*' : '`'.$match[2].'`').(isset($match[6]) ? ' AS `'.$match[6].'`' : '');
435:             } elseif (preg_match('/^`?([a-z0-9_]+)`?\.([\*a-z0-9_]+)(([\s]+as)?[\s]+([a-z0-9_]+))?$/i', $name, $match)) {
436:                 // `user`.id, user.id as user_id
437:                 $ret = '`'.$match[1].'`.'.($match[2] == '*' ? '*' : '`'.$match[2].'`').(isset($match[5]) ? ' AS `'.$match[5].'`' : '');
438:             } elseif (preg_match('/^([a-z0-9_]+)(([\s]+as)?[\s]+([a-z0-9_]+))?$/i', $name, $match)) {
439:                 // user as user_id
440:                 $ret = '`'.$match[1].'`'.(isset($match[4]) ? ' AS `'.$match[4].'`' : '');
441:             } else {
442:                 $ret = $name == '*' ? '*' : '`'.$name.'`';
443:             }
444:         } elseif ($name instanceof Sql) {
445:             $ret = $name->text();
446:         } elseif ($name instanceof QueryBuilder) {
447:             $ret = '('.$name->text().')';
448:         } else {
449:             // พารามิเตอร์ ไม่ถูกต้อง
450:             trigger_error('Invalid arguments in fieldName('.var_export($name, true).')', E_USER_ERROR);
451:         }
452: 
453:         return $ret;
454:     }
455: 
456:     /**
457:      * แปลงข้อความสำหรับ value.
458:      *
459:      * @param string $value
460:      *
461:      * @return string
462:      */
463:     protected function fieldValue($value)
464:     {
465:         if (is_array($value)) {
466:             $rets = array();
467:             foreach ($value as $item) {
468:                 $rets[] = $this->fieldValue($item);
469:             }
470:             $ret = '('.implode(', ', $rets).')';
471:         } elseif (is_numeric($value)) {
472:             $ret = $value;
473:         } elseif (preg_match('/^([a-z0-9_]+)\.([a-z0-9_]+)(([\s]+as)?[\s]+([a-z0-9]+))?$/i', $value, $match)) {
474:             $ret = "`$match[1]`.`$match[2]`".(isset($match[5]) ? ' AS `'.$match[5].'`' : '');
475:         } else {
476:             $ret = '\''.$value.'\'';
477:         }
478: 
479:         return $ret;
480:     }
481: 
482:     /**
483:      * ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย AND.
484:      *
485:      * @param array $params คำสั่ง รูปแบบ array('field1', 'condition', 'field2')
486:      *
487:      * @return \Sql
488:      */
489:     protected function groupAnd($params)
490:     {
491:         if (func_num_args() > 1) {
492:             $params = func_get_args();
493:         }
494:         $sqls = array();
495:         foreach ($params as $i => $item) {
496:             $sqls[] = $this->buildValue($item);
497:         }
498: 
499:         return Sql::create('('.implode(' AND ', $sqls).')');
500:     }
501: 
502:     /**
503:      * ฟังก์ชั่นสำหรับจัดกลุ่มคำสั่ง และ เชื่อมแต่ละกลุ่มด้วย OR.
504:      *
505:      * @param array $params คำสั่ง รูปแบบ array('field1', 'condition', 'field2')
506:      *
507:      * @return \Sql
508:      */
509:     protected function groupOr($params)
510:     {
511:         if (func_num_args() > 1) {
512:             $params = func_get_args();
513:         }
514:         $sqls = array();
515:         foreach ($params as $i => $item) {
516:             $sqls[] = $this->buildValue($item);
517:         }
518: 
519:         return Sql::create('('.implode(' OR ', $sqls).')');
520:     }
521: 
522:     /**
523:      * ฟังก์ชั่นอ่านชื่อตารางและชื่อรอง และใส่ ` ครอบชื่อตารางด้วย.
524:      *
525:      * @param string $table ชื่อตารางตามที่กำหนดใน settings/datasbase.php
526:      *
527:      * @return string
528:      */
529:     protected function quoteTableName($table)
530:     {
531:         if (is_array($table)) {
532:             if ($table[0] instanceof QueryBuilder) {
533:                 $table = '('.$table[0]->text().') AS '.$table[1];
534:             } else {
535:                 $table = '('.$table[0].') AS '.$table[1];
536:             }
537:         } elseif (preg_match('/^([a-zA-Z0-9_]+)(\s+(as|AS))?[\s]+([A-Z0-9]{1,2})$/', $table, $match)) {
538:             $table = $this->getFullTableName($match[1]).' AS '.$match[4];
539:         } elseif (preg_match('/^([a-zA-Z0-9_]+)(\s+(as|AS))?[\s]+([a-zA-Z0-9]+)$/', $table, $match)) {
540:             $table = $this->getFullTableName($match[1]).' AS `'.$match[4].'`';
541:         } else {
542:             $table = $this->getFullTableName($table);
543:         }
544: 
545:         return $table;
546:     }
547: 
548:     /**
549:      * สร้างคำสั่ง WHERE.
550:      *
551:      * @param array    $params
552:      * @param int|null $i
553:      *
554:      * @return array|string
555:      */
556:     private function whereValue($params, $i = null)
557:     {
558:         $result = array();
559:         if (is_array($params)) {
560:             if (count($params) == 2) {
561:                 $operator = '=';
562:                 $value = $params[1];
563:             } else {
564:                 $operator = trim($params[1]);
565:                 $value = $params[2];
566:             }
567:             $key = $this->fieldName($params[0]);
568:             if ($value instanceof QueryBuilder) {
569:                 $values = $value->getValues();
570:                 if (empty($values)) {
571:                     $result = $key.' '.$operator.' ('.$value->text().')';
572:                 } else {
573:                     $result = array($key.' '.$operator.' ('.$value->text().')', $values);
574:                 }
575:             } elseif ($value instanceof Sql) {
576:                 $values = $value->getValues();
577:                 if (empty($values)) {
578:                     $result = $key.' '.$operator.' '.$value->text();
579:                 } else {
580:                     $result = array($key.' '.$operator.' '.$value->text(), $values);
581:                 }
582:             } elseif (is_array($value)) {
583:                 if ($operator == '=') {
584:                     $operator = 'IN';
585:                 }
586:                 $q = $this->aliasName($key);
587:                 $qs = array();
588:                 $vs = array();
589:                 foreach ($value as $a => $item) {
590:                     if (empty($item)) {
591:                         $qs[] = is_string($item) ? "'$item'" : $item;
592:                     } elseif (is_string($item)) {
593:                         if (preg_match('/^([A-Z]{1,1}[0-9]{0,1})\.`?([a-zA-Z0-9_\-]+)`?$/', $item, $match)) {
594:                             $qs[] = "$match[1].`$match[2]`";
595:                         } elseif (preg_match('/^`([a-zA-Z0-9_\-]+)`$/', $item, $match)) {
596:                             $qs[] = "`$match[1]`";
597:                         } else {
598:                             $k = $q.($i === null ? '' : $i).$a;
599:                             $qs[] = $k;
600:                             $vs[$k] = $item;
601:                         }
602:                     } else {
603:                         $k = $q.($i === null ? '' : $i).$a;
604:                         $qs[] = $k;
605:                         $vs[$k] = $item;
606:                     }
607:                 }
608:                 $result = array($key.' '.$operator.' ('.implode(', ', $qs).')', $vs);
609:             } elseif (empty($value)) {
610:                 // value เป็น string ว่าง, 0, null
611:                 $result = $key.' '.$operator.' '.(is_string($value) ? "'$value'" : $value);
612:             } elseif (preg_match('/^(\-?[0-9\s\.]+|true|false)$/i', $value)) {
613:                 // value เป็น ตัวเลข จุดทศนิยม เครื่องหมาย - / , และ true, false
614:                 // เช่น ตัวเลข, จำนวนเงิน, boolean
615:                 $result = "$key $operator ".(is_string($value) ? "'$value'" : $value);
616:             } elseif (preg_match('/^[0-9\s\-:]+$/', $value)) {
617:                 // วันที่
618:                 $result = "$key $operator '$value'";
619:             } elseif (preg_match('/^([A-Z]{1,1}[0-9]{0,1})\.([a-zA-Z0-9_\-]+)$/', $value, $match)) {
620:                 // U.id
621:                 if ($operator == 'IN' || $operator == 'NOT IN') {
622:                     $result = "$key $operator ($match[1].`$match[2]`)";
623:                 } else {
624:                     $result = "$key $operator $match[1].`$match[2]`";
625:                 }
626:             } elseif (preg_match('/^`([a-zA-Z0-9_\-]+)`$/', $value, $match)) {
627:                 // `id`
628:                 if ($operator == 'IN' || $operator == 'NOT IN') {
629:                     $result = "$key $operator (`$match[1]`)";
630:                 } else {
631:                     $result = "$key $operator `$match[1]`";
632:                 }
633:             } else {
634:                 // value เป็น string
635:                 $q = ':'.preg_replace('/[\.`]/', '', strtolower($key)).($i === null ? '' : $i);
636:                 $result = array($key.' '.$operator.' '.$q, array($q => $value));
637:             }
638:         } elseif ($params instanceof QueryBuilder) {
639:             $values = $params->getValues();
640:             if (empty($values)) {
641:                 $result = $key.' '.$operator.' ('.$params->text().')';
642:             } else {
643:                 $result = array($key.' '.$operator.' ('.$params->text().')', $values);
644:             }
645:         } elseif ($params instanceof Sql) {
646:             $result = $params->text();
647:         } else {
648:             $result = $params;
649:         }
650: 
651:         return $result;
652:     }
653: }
654: 
Kotchasan API documentation generated by ApiGen