Pdo.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php namespace Dever\Store;
  2. use Dever;
  3. use Dever\Debug;
  4. use Dever\Output;
  5. use Dever\String\Sql;
  6. class Pdo extends Base
  7. {
  8. protected function connect($setting, $partition)
  9. {
  10. if (isset($partition['database'])) {
  11. $setting['name'] .= '_' . $partition['database'];
  12. }
  13. $this->type = $setting['type'];
  14. $handle = false;
  15. if (strpos($setting['host'], ':') !== false) {
  16. list($setting['host'], $setting['port']) = explode(':', $setting['host']);
  17. }
  18. if (empty($setting['pdo_type'])) {
  19. $setting['pdo_type'] = 'mysql';
  20. }
  21. if (empty($setting['charset'])) {
  22. $setting['charset'] = 'utf8mb4';
  23. }
  24. if (empty($setting['collation'])) {
  25. $setting['collation'] = 'utf8mb4_general_ci';
  26. }
  27. $dsn = $setting['pdo_type'] . ':type='.$setting['type'].';host='.$setting['host'].';port='.$setting['port'].';dbname='.$setting['name'].';collation='.$setting['collation'];
  28. try {
  29. if (empty($setting['persistent'])) {
  30. $persistent = false;
  31. }
  32. $handle = new \PDO($dsn, $setting['user'], $setting['pwd'], array(\PDO::ATTR_PERSISTENT => $persistent));
  33. $handle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  34. $handle->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_NATURAL);
  35. $handle->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
  36. //$handle->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
  37. //handle->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
  38. Debug::add('db ' . $setting['host'] . ' connected', $setting['type']);
  39. return $handle;
  40. } catch (\PDOException $e) {
  41. if (strstr($e->getMessage(), 'Unknown database')) {
  42. $this->create($setting);
  43. $this->connect($setting, $partition);
  44. } else {
  45. Output::error($e->getMessage());
  46. }
  47. }
  48. }
  49. private function create($setting)
  50. {
  51. $method = 'mysql';
  52. if (function_exists('mysqli_connect')) {
  53. $method = 'mysqli';
  54. }
  55. $connect = $method . '_connect';
  56. $query = $method . '_query';
  57. $close = $method . '_close';
  58. $link = $connect($setting['host'] . ':' . $setting['port'], $setting['user'], $setting['pwd']);
  59. if ($link) {
  60. $sql = 'CREATE DATABASE `' . $setting['name'] . '` DEFAULT CHARACTER SET ' . $setting['charset'] . ' COLLATE ' . $setting['collation'];
  61. if ($method == 'mysql') {
  62. $query($sql, $link);
  63. } else {
  64. $query($link, $sql);
  65. }
  66. $close($link);
  67. }
  68. }
  69. public function struct($config, $state = 0)
  70. {
  71. if ($state) {
  72. $this->query(Sql::alter($config['table'], $config['struct'], $this->query(Sql::desc($config['table']))));
  73. } else {
  74. $this->query(Sql::create($config));
  75. }
  76. if (isset($config['default']) && $config['default']) {
  77. $count = $this->count($config['table'], array());
  78. if (!$count) {
  79. $this->query(Sql::inserts($config['table'], $config['default']));
  80. }
  81. }
  82. }
  83. public function index($config, $state = 0)
  84. {
  85. $this->query(Sql::index($config['table'], $config['index'], $this->query(Sql::showIndex($config['table']))));
  86. }
  87. public function partition($config, $partition)
  88. {
  89. $this->query(Sql::partition($config['table'], $partition, $this->query(Sql::showIndex($config['table']))));
  90. }
  91. public function query($sql, $bind = array(), $method = 'read')
  92. {
  93. try {
  94. if ($bind) {
  95. $handle = $this->$method->prepare($sql);
  96. $handle->execute($bind);
  97. } else {
  98. $handle = $this->$method->query($sql);
  99. }
  100. } catch (\PDOException $exception) {
  101. $this->error($exception->getMessage(), $sql);
  102. }
  103. if (Debug::$shell) {
  104. $this->sql($sql, $bind);
  105. $this->log(array('sql' => $sql, 'count' => $handle->rowCount()));
  106. }
  107. return $handle;
  108. }
  109. public function select($table, $param, $set, $field)
  110. {
  111. $bind = array();
  112. $sql = Sql::select($table, $param, $bind, $set, $field);
  113. return $this->query($sql, $bind);
  114. }
  115. public function count($table, $param, $field)
  116. {
  117. return $this->select($table, $param, array('col'=>'count(*)'), $field)->fetch(\PDO::FETCH_NUM)[0];
  118. }
  119. public function insert($table, $data, $field)
  120. {
  121. $bind = array();
  122. $sql = Sql::insert($table, $data, $bind, $field);
  123. $this->query($sql, $bind, 'update');
  124. return $this->update->lastInsertId();
  125. }
  126. public function update($table, $param, $data, $field)
  127. {
  128. $bind = array();
  129. $sql = Sql::update($table, $param, $data, $bind, $field);
  130. return $this->query($sql, $bind, 'update')->rowCount();
  131. }
  132. public function delete($table, $param, $field)
  133. {
  134. $bind = array();
  135. $sql = Sql::delete($table, $param, $bind, $field);
  136. return $this->query($sql, $bind, 'update')->rowCount();
  137. }
  138. public function begin()
  139. {
  140. $this->update->beginTransaction();
  141. }
  142. public function commit()
  143. {
  144. $this->update->commit();
  145. }
  146. public function rollback()
  147. {
  148. $this->update->rollback();
  149. }
  150. }