DbConnectionAdapterPdo.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. class LtDbConnectionAdapterPdo implements LtDbConnectionAdapter
  3. {
  4. public function connect($connConf)
  5. {
  6. // $option = array(PDO::ATTR_PERSISTENT => true);
  7. if (isset($connConf['pconnect']) && true == $connConf['pconnect'])
  8. {
  9. $option[PDO::ATTR_PERSISTENT] = true;
  10. }
  11. else
  12. {
  13. $option[PDO::ATTR_PERSISTENT] = false;
  14. }
  15. switch ($connConf['adapter'])
  16. {
  17. case "pdo_mysql":
  18. $dsn = "mysql:host={$connConf['host']};dbname={$connConf['dbname']}";
  19. break;
  20. case "pdo_sqlite":
  21. $connConf["host"] = rtrim($connConf["host"], '\\/') . DIRECTORY_SEPARATOR;
  22. if (!is_dir($connConf["host"]))
  23. {
  24. if (!@mkdir($connConf["host"], 0777, true))
  25. {
  26. trigger_error("Can not create {$connConf['host']}");
  27. }
  28. }
  29. $dsn = "{$connConf['sqlite_version']}:{$connConf['host']}{$connConf['dbname']}";
  30. break;
  31. case "pdo_pgsql":
  32. $dsn = "pgsql:host={$connConf['host']} port={$connConf['port']} dbname={$connConf['dbname']} user={$connConf['username']} password={$connConf['password']}";
  33. break;
  34. case "odbc":
  35. $dsn = "odbc:" . $connConf["host"];
  36. break;
  37. }
  38. return new PDO($dsn, $connConf['username'], $connConf['password'], $option);
  39. }
  40. public function exec($sql, $connResource)
  41. {
  42. return $connResource->exec($sql);
  43. }
  44. public function query($sql, $connResource)
  45. {
  46. return $connResource->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  47. }
  48. /**
  49. *
  50. * @todo pgsql support
  51. */
  52. public function lastInsertId($connResource)
  53. {
  54. return $connResource->lastInsertId();
  55. }
  56. public function escape($sql, $connResource)
  57. {
  58. // quote返回值带最前面和最后面的单引号, 这里去掉, DbHandler中加
  59. return trim($connResource->quote($sql), "'");
  60. }
  61. }