0
0

Sqlite.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\db\builder;
  12. use think\db\Builder;
  13. use think\db\Query;
  14. /**
  15. * Sqlite数据库驱动
  16. */
  17. class Sqlite extends Builder
  18. {
  19. /**
  20. * limit
  21. * @access public
  22. * @param Query $query 查询对象
  23. * @param mixed $limit
  24. * @return string
  25. */
  26. public function parseLimit(Query $query, $limit)
  27. {
  28. $limitStr = '';
  29. if (!empty($limit)) {
  30. $limit = explode(',', $limit);
  31. if (count($limit) > 1) {
  32. $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
  33. } else {
  34. $limitStr .= ' LIMIT ' . $limit[0] . ' ';
  35. }
  36. }
  37. return $limitStr;
  38. }
  39. /**
  40. * 随机排序
  41. * @access protected
  42. * @param Query $query 查询对象
  43. * @return string
  44. */
  45. protected function parseRand(Query $query)
  46. {
  47. return 'RANDOM()';
  48. }
  49. /**
  50. * 字段和表名处理
  51. * @access public
  52. * @param Query $query 查询对象
  53. * @param mixed $key 字段名
  54. * @param bool $strict 严格检测
  55. * @return string
  56. */
  57. public function parseKey(Query $query, $key, $strict = false)
  58. {
  59. if (is_numeric($key)) {
  60. return $key;
  61. } elseif ($key instanceof Expression) {
  62. return $key->getValue();
  63. }
  64. $key = trim($key);
  65. if (strpos($key, '.')) {
  66. list($table, $key) = explode('.', $key, 2);
  67. $alias = $query->getOptions('alias');
  68. if ('__TABLE__' == $table) {
  69. $table = $query->getOptions('table');
  70. $table = is_array($table) ? array_shift($table) : $table;
  71. }
  72. if (isset($alias[$table])) {
  73. $table = $alias[$table];
  74. }
  75. }
  76. if (isset($table)) {
  77. $key = $table . '.' . $key;
  78. }
  79. return $key;
  80. }
  81. }