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.

160 lines
4.0 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 $data
  33. * @param mixed $on_duplicate
  34. * @return int Affected rows count
  35. */
  36. public function insert($table, $data, $on_duplicate = array())
  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 $data
  50. * @param mixed $condition
  51. * @return int Number of updated rows
  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 $condition
  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. foreach ($ident as &$segment) {
  100. $segment = $this->identifier_quote . $segment . $this->identifier_quote;
  101. }
  102. return implode('.', $ident);
  103. }
  104. public function quoteInto($text, $value)
  105. {
  106. $pos = mb_strpos($text, '?');
  107. if ($pos === false) {
  108. return $text;
  109. }
  110. return mb_substr($text, 0, $pos) . $this->quote($value) . mb_substr($text, $pos + 1);
  111. }
  112. /**
  113. * @param mixed $where
  114. * @return string
  115. */
  116. protected function whereExpr($where)
  117. {
  118. if (empty($where)) {
  119. return $where;
  120. }
  121. if (!is_array($where)) {
  122. $where = array($where);
  123. }
  124. foreach ($where as $cond => &$term) {
  125. if (is_int($cond)) {
  126. if ($term instanceof DbExpr) {
  127. $term = (string) $term;
  128. }
  129. } else {
  130. $term = $this->quoteInto($cond, $term);
  131. }
  132. }
  133. return implode(' AND ', $where);
  134. }
  135. abstract protected function driverQuote($value);
  136. abstract protected function driverBeginTransaction();
  137. abstract protected function driverCommitTransaction();
  138. abstract protected function driverRollbackTransaction();
  139. }