DbSqlAdapterMysql.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. class LtDbSqlAdapterMysql implements LtDbSqlAdapter
  3. {
  4. public function setCharset($charset)
  5. {
  6. return "SET NAMES " . str_replace('-', '', $charset);
  7. }
  8. public function setSchema($schema)
  9. {
  10. return "USE $schema";
  11. }
  12. public function showSchemas($database)
  13. {
  14. return "SHOW DATABASES";
  15. }
  16. public function showTables($schema)
  17. {
  18. return "SHOW TABLES";
  19. }
  20. public function showFields($table)
  21. {
  22. return "DESCRIBE $table";
  23. }
  24. public function beginTransaction()
  25. {
  26. return "START TRANSACTION";
  27. }
  28. public function commit()
  29. {
  30. return "COMMIT";
  31. }
  32. public function rollBack()
  33. {
  34. return "ROLLBACK";
  35. }
  36. public function limit($limit, $offset)
  37. {
  38. return " LIMIT $limit OFFSET $offset";
  39. }
  40. public function getSchemas($queryResult)
  41. {
  42. }
  43. public function getTables($queryResult)
  44. {
  45. }
  46. public function getFields($queryResult)
  47. {
  48. foreach ($queryResult as $key => $value)
  49. {
  50. $fields[$value['Field']]['name'] = $value['Field'];
  51. $fields[$value['Field']]['type'] = $value['Type'];
  52. /*
  53. * not null is NO or empty, null is YES
  54. */
  55. $fields[$value['Field']]['notnull'] = (bool) ($value['Null'] != 'YES');
  56. $fields[$value['Field']]['default'] = $value['Default'];
  57. $fields[$value['Field']]['primary'] = (strtolower($value['Key']) == 'pri');
  58. }
  59. return $fields;
  60. }
  61. public function detectQueryType($sql)
  62. {
  63. if (preg_match("/^\s*SELECT|^\s*EXPLAIN|^\s*SHOW|^\s*DESCRIBE/i", $sql))
  64. {
  65. $ret = 'SELECT';
  66. }
  67. else if (preg_match("/^\s*INSERT/i", $sql))
  68. {
  69. $ret = 'INSERT';
  70. }
  71. else if (preg_match("/^\s*UPDATE|^\s*DELETE|^\s*REPLACE/i", $sql))
  72. {
  73. $ret = 'CHANGE_ROWS';
  74. }
  75. else if (preg_match("/^\s*USE|^\s*SET/i", $sql))
  76. {
  77. $ret = 'SET_SESSION_VAR';
  78. }
  79. else
  80. {
  81. $ret = 'OTHER';
  82. }
  83. return $ret;
  84. }
  85. }