Pgsql.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. /**
  14. * Pgsql数据库驱动
  15. */
  16. class Pgsql extends Builder
  17. {
  18. protected $insertSql = 'INSERT INTO %TABLE% (%FIELD%) VALUES (%DATA%) %COMMENT%';
  19. protected $insertAllSql = 'INSERT INTO %TABLE% (%FIELD%) %DATA% %COMMENT%';
  20. /**
  21. * limit分析
  22. * @access protected
  23. * @param mixed $limit
  24. * @return string
  25. */
  26. public function parseLimit($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 mixed $key
  43. * @param array $options
  44. * @return string
  45. */
  46. protected function parseKey($key, $options = [], $strict = false)
  47. {
  48. if (is_numeric($key)) {
  49. return $key;
  50. } elseif ($key instanceof Expression) {
  51. return $key->getValue();
  52. }
  53. $key = trim($key);
  54. if (strpos($key, '$.') && false === strpos($key, '(')) {
  55. // JSON字段支持
  56. list($field, $name) = explode('$.', $key);
  57. $key = $field . '->>\'' . $name . '\'';
  58. } elseif (strpos($key, '.')) {
  59. list($table, $key) = explode('.', $key, 2);
  60. if ('__TABLE__' == $table) {
  61. $table = $this->query->getTable();
  62. }
  63. if (isset($options['alias'][$table])) {
  64. $table = $options['alias'][$table];
  65. }
  66. }
  67. if (isset($table)) {
  68. $key = $table . '.' . $key;
  69. }
  70. return $key;
  71. }
  72. /**
  73. * 随机排序
  74. * @access protected
  75. * @return string
  76. */
  77. protected function parseRand()
  78. {
  79. return 'RANDOM()';
  80. }
  81. }