DbSqlAdapterSqlite.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. class LtDbSqlAdapterSqlite implements LtDbSqlAdapter
  3. {
  4. public function setCharset($charset)
  5. {
  6. // return 'PRAGMA encoding = "' . $charset . '"';
  7. return '';
  8. }
  9. public function setSchema($schema)
  10. {
  11. return '';
  12. }
  13. public function beginTransaction()
  14. {
  15. return 'BEGIN TRANSACTION';
  16. }
  17. public function commit()
  18. {
  19. return 'COMMIT TRANSACTION';
  20. }
  21. public function rollBack()
  22. {
  23. return 'ROLLBACK TRANSACTION';
  24. }
  25. public function showSchemas($database)
  26. {
  27. //return "SHOW DATABASES";
  28. return '';
  29. }
  30. public function showTables($schema)
  31. {
  32. // 临时表及其索引不在 SQLITE_MASTER 表中而在 SQLITE_TEMP_MASTER 中出现
  33. return "SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table' ORDER BY name";
  34. }
  35. public function showFields($table)
  36. {
  37. return "PRAGMA table_info('" . $table . "')";
  38. }
  39. public function limit($limit, $offset)
  40. {
  41. return " LIMIT $limit OFFSET $offset";
  42. }
  43. public function getSchemas($queryResult)
  44. {
  45. }
  46. public function getTables($queryResult)
  47. {
  48. return $queryResult;
  49. }
  50. public function getFields($queryResult)
  51. {
  52. $fields = array();
  53. foreach ($queryResult as $key => $value)
  54. {
  55. // 字段名
  56. $fields[$value['name']]['name'] = $value['name'];
  57. // 字段类型
  58. $fulltype = $value['type'];
  59. $size = null;
  60. $precision = null;
  61. $scale = null;
  62. if (preg_match('/^([^\(]+)\(\s*(\d+)\s*,\s*(\d+)\s*\)$/',$fulltype, $matches))
  63. {
  64. $type = $matches[1];
  65. $precision = $matches[2];
  66. $scale = $matches[3]; // aka precision
  67. }
  68. elseif (preg_match('/^([^\(]+)\(\s*(\d+)\s*\)$/',$fulltype, $matches))
  69. {
  70. $type = $matches[1];
  71. $size = $matches[2];
  72. }
  73. else
  74. {
  75. $type = $fulltype;
  76. }
  77. $fields[$value['name']]['type'] = $type;
  78. /**
  79. * not null is 99, null is 0
  80. */
  81. $fields[$value['name']]['notnull'] = (bool) ($value['notnull'] != 0);
  82. $fields[$value['name']]['default'] = $value['dflt_value'];
  83. $fields[$value['name']]['primary'] = (bool) ($value['pk'] == 1 && strtoupper($fulltype) == 'INTEGER');
  84. }
  85. return $fields;
  86. }
  87. public function detectQueryType($sql)
  88. {
  89. if (preg_match("/^\s*SELECT|^\s*PRAGMA/i", $sql))
  90. {
  91. $ret = 'SELECT';
  92. }
  93. else if (preg_match("/^\s*INSERT/i", $sql))
  94. {
  95. $ret = 'INSERT';
  96. }
  97. else if (preg_match("/^\s*UPDATE|^\s*DELETE|^\s*REPLACE/i", $sql))
  98. {
  99. $ret = 'CHANGE_ROWS';
  100. }
  101. else if (preg_match("/^\s*USE|^\s*SET/i", $sql))
  102. {
  103. $ret = 'SET_SESSION_VAR';
  104. }
  105. else
  106. {
  107. $ret = 'OTHER';
  108. }
  109. return $ret;
  110. }
  111. }