You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.1 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage db
  7. * @since 2011-11-11
  8. */
  9. abstract class SqlDbDriver extends DbDriver
  10. {
  11. protected $identifier_quote = '`';
  12. public function beginTransaction()
  13. {
  14. $this->connect();
  15. $this->driverBeginTransaction();
  16. return $this;
  17. }
  18. public function commit()
  19. {
  20. $this->connect();
  21. $this->driverCommitTransaction();
  22. return $this;
  23. }
  24. public function rollback()
  25. {
  26. $this->connect();
  27. $this->driverRollbackTransaction();
  28. return $this;
  29. }
  30. /**
  31. * @param string $table
  32. * @param mixed $bind
  33. * @param mixed $on_duplicate
  34. * @return int Affected rows count
  35. */
  36. public function insert($table, $data)
  37. {
  38. $columns = array();
  39. foreach ($data as $col => $val) {
  40. $columns[] = $this->quoteIdentifier($col);
  41. }
  42. $values = array_values($data);
  43. $sql = 'INSERT INTO ' . $this->quoteIdentifier($table)
  44. . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')';
  45. return $this->query($sql)->affectedRows();
  46. }
  47. /**
  48. * @param string $table
  49. * @param array $bind
  50. * @param mixed $where
  51. * @return int
  52. */
  53. public function update($table, $data, $condition = '')
  54. {
  55. $set = array();
  56. foreach ($data as $col => $val) {
  57. $set[] = $this->quoteIdentifier($col) . '=' . $this->quote($val);
  58. }
  59. $where = $this->whereExpr($condition);
  60. $sql = 'UPDATE ' . $this->quoteIdentifier($table) . ' SET ' . implode(', ', $set)
  61. . (($where) ? (' WHERE ' . $where) : '');
  62. return $this->query($sql)->affectedRows();
  63. }
  64. /**
  65. * @param string $table
  66. * @param mixed $where
  67. * @return int
  68. */
  69. public function delete($table, $condition = '')
  70. {
  71. $where = $this->whereExpr($condition);
  72. $sql = 'DELETE FROM ' . $this->quoteIdentifier($table) . (($where) ? (' WHERE ' . $where) : '');
  73. return $this->query($sql)->affectedRows();
  74. }
  75. /**
  76. * @param mixed $value
  77. * @return string
  78. */
  79. public function quote($value)
  80. {
  81. if ($value instanceof DbExpr) {
  82. return (string) $value;
  83. }
  84. if (is_array($value)) {
  85. foreach ($value as &$val) {
  86. $val = $this->quote($val);
  87. }
  88. return implode(', ', $value);
  89. }
  90. return $this->driverQuote($value);
  91. }
  92. /**
  93. * @param string $ident
  94. * @return string
  95. */
  96. public function quoteIdentifier($ident)
  97. {
  98. $ident = explode('.', $ident);
  99. if (!is_array($ident)) {
  100. $ident = array($ident);
  101. }
  102. foreach ($ident as &$segment) {
  103. $segment = $this->identifier_quote . $segment . $this->identifier_quote;
  104. }
  105. return implode('.', $ident);
  106. }
  107. public function quoteInto($text, $value)
  108. {
  109. $pos = mb_strpos($text, '?');
  110. if ($pos === false) {
  111. return $text;
  112. }
  113. return mb_substr($text, 0, $pos) . $this->quote($value) . mb_substr($text, $pos + 1);
  114. }
  115. /**
  116. * @param mixed $where
  117. * @return string
  118. */
  119. protected function whereExpr($where)
  120. {
  121. if (empty($where)) {
  122. return $where;
  123. }
  124. if (!is_array($where)) {
  125. $where = array($where);
  126. }
  127. foreach ($where as $cond => &$term) {
  128. if (is_int($cond)) {
  129. if ($term instanceof DbExpr) {
  130. $term = (string) $term;
  131. }
  132. } else {
  133. $term = $this->quoteInto($cond, $term);
  134. }
  135. }
  136. return implode(' AND ', $where);
  137. }
  138. /**
  139. * @return DbStatement
  140. */
  141. abstract protected function driverQuote($value);
  142. abstract protected function driverBeginTransaction();
  143. abstract protected function driverCommitTransaction();
  144. abstract protected function driverRollbackTransaction();
  145. }