Pgsql.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * Pgsql数据库驱动
  16. */
  17. class Pgsql extends Builder
  18. {
  19. protected $insertSql = 'INSERT INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
  20. protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
  21. /**
  22. * limit分析
  23. * @access protected
  24. * @param Query $query 查询对象
  25. * @param mixed $limit
  26. * @return string
  27. */
  28. public function parseLimit(Query $query, $limit)
  29. {
  30. $limitStr = '';
  31. if (!empty($limit)) {
  32. $limit = explode(',', $limit);
  33. if (count($limit) > 1) {
  34. $limitStr .= ' LIMIT ' . $limit[1] . ' OFFSET ' . $limit[0] . ' ';
  35. } else {
  36. $limitStr .= ' LIMIT ' . $limit[0] . ' ';
  37. }
  38. }
  39. return $limitStr;
  40. }
  41. /**
  42. * 字段和表名处理
  43. * @access public
  44. * @param Query $query 查询对象
  45. * @param mixed $key 字段名
  46. * @param bool $strict 严格检测
  47. * @return string
  48. */
  49. public function parseKey(Query $query, $key, $strict = false)
  50. {
  51. if (is_numeric($key)) {
  52. return $key;
  53. } elseif ($key instanceof Expression) {
  54. return $key->getValue();
  55. }
  56. $key = trim($key);
  57. if (strpos($key, '->') && false === strpos($key, '(')) {
  58. // JSON字段支持
  59. list($field, $name) = explode('->', $key);
  60. $key = $field . '->>\'' . $name . '\'';
  61. } elseif (strpos($key, '.')) {
  62. list($table, $key) = explode('.', $key, 2);
  63. $alias = $query->getOptions('alias');
  64. if ('__TABLE__' == $table) {
  65. $table = $query->getOptions('table');
  66. $table = is_array($table) ? array_shift($table) : $table;
  67. }
  68. if (isset($alias[$table])) {
  69. $table = $alias[$table];
  70. }
  71. }
  72. if (isset($table)) {
  73. $key = $table . '.' . $key;
  74. }
  75. return $key;
  76. }
  77. /**
  78. * 随机排序
  79. * @access protected
  80. * @param Query $query 查询对象
  81. * @return string
  82. */
  83. protected function parseRand(Query $query)
  84. {
  85. return 'RANDOM()';
  86. }
  87. }