PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/sql.php

https://github.com/Jakobo/scar
PHP | 185 lines | 152 code | 31 blank | 2 comment | 29 complexity | eaca1f216ad3ef7744325dcd3824faea MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. class SCAR_SQL {
  3. public $table;
  4. public $columns;
  5. public $where;
  6. public $set;
  7. const ANSI_SELECT = 'SELECT %s FROM %s %s %s %s';
  8. const ANSI_UPDATE = 'UPDATE %s SET %s %s %s';
  9. const ANSI_INSERT = 'INSERT INTO %s (%s) VALUES (%s)';
  10. protected $mode;
  11. protected $SCAR;
  12. public function __construct($mode, $dsn, $SCAR = 'SCAR') {
  13. $this->mode = $mode;
  14. $this->dsn = $dsn;
  15. $this->SCAR = $SCAR;
  16. }
  17. public function render() {
  18. $table = '`'.$this->table.'`';
  19. if ($this->mode == 'update') {
  20. return trim(sprintf(self::ANSI_UPDATE, $table, $this->renderSet(), $this->renderWhere(), $this->renderLimit()));
  21. }
  22. if ($this->mode == 'insert') {
  23. return trim(sprintf(self::ANSI_INSERT, $table, $this->renderInsertSetColumns(), $this->renderInsertSetValues()));
  24. }
  25. return trim(sprintf(self::ANSI_SELECT, $this->renderColumns(), $table, $this->renderWhere(), $this->renderOrder(), $this->renderLimit()));
  26. }
  27. protected function renderColumns() {
  28. if (!isset($this->columns) || !$this->columns) {
  29. return '*';
  30. }
  31. if (is_array($this->columns)) {
  32. return '`'.implode('`, `', $this->columns).'`';
  33. }
  34. elseif ($this->columns == '*') {
  35. return '*';
  36. }
  37. else {
  38. return '`'.$this->columns.'`';
  39. }
  40. }
  41. protected function renderWhere() {
  42. if (!isset($this->where) || !$this->where) {
  43. return '';
  44. }
  45. // where
  46. $where = '';
  47. foreach ($this->where as $where_piece) {
  48. $where .= '`';
  49. $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $where_piece['col']));
  50. $where .= '`';
  51. $where .= ' '.$where_piece['oper'].' ';
  52. if (is_array($where_piece['value'])) {
  53. $where .= '(';
  54. foreach ($where_piece['value'] as $value) {
  55. $where .= '\'';
  56. $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $value));
  57. $where .= '\', ';
  58. }
  59. $where = substr($where, 0, -2);
  60. $where .= ')';
  61. }
  62. else {
  63. $where .= '\'';
  64. $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $where_piece['value']));
  65. $where .= '\'';
  66. }
  67. $where .= ' AND ';
  68. }
  69. if (strlen($where) > 0) {
  70. $where = substr($where, 0, -5);
  71. $where = 'WHERE '.$where;
  72. }
  73. return $where;
  74. }
  75. protected function renderOrder() {
  76. if (!isset($this->order) || !$this->order) {
  77. return '';
  78. }
  79. $order = '';
  80. foreach ($this->order as $order_piece) {
  81. $order .= '`';
  82. $order .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $order_piece['col']));
  83. $order .= '` ';
  84. if ($order_piece['order'] === SORT_ASC) {
  85. $order .= 'ASC';
  86. }
  87. else {
  88. $order .= 'DESC';
  89. }
  90. $order .= ', ';
  91. }
  92. if (strlen($order) > 0) {
  93. $order = substr($order, 0, -2);
  94. $order = 'ORDER BY '.$order;
  95. }
  96. return $order;
  97. }
  98. protected function renderLimit() {
  99. if (!isset($this->limit) || !$this->limit) {
  100. return '';
  101. }
  102. if ($this->limit) {
  103. $out = 'LIMIT '.call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $this->limit['start'])).', ';
  104. $out .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $this->limit['count']));
  105. return $out;
  106. }
  107. return '';
  108. }
  109. protected function renderSet() {
  110. if (!isset($this->set) || !$this->set) {
  111. return '';
  112. }
  113. // set
  114. $set = '';
  115. foreach ($this->set as $set_piece) {
  116. $set .= '`';
  117. $set .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $set_piece['col']));
  118. $set .= '`';
  119. $set .= ' = ';
  120. $set .= '\'';
  121. $set .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $set_piece['value']));
  122. $set .= '\'';
  123. $set .= ', ';
  124. }
  125. if (strlen($set) > 0) {
  126. $set = substr($set, 0, -2);
  127. }
  128. return $set;
  129. }
  130. protected function renderInsertSetColumns() {
  131. if (!isset($this->set) || !$this->set) {
  132. return '';
  133. }
  134. $set = '';
  135. foreach ($this->set as $set_piece) {
  136. $set .= '`'.$set_piece['col'].'`';
  137. $set .= ', ';
  138. }
  139. $set = substr($set, 0, -2);
  140. return $set;
  141. }
  142. protected function renderInsertSetValues() {
  143. if (!isset($this->set) || !$this->set) {
  144. return '';
  145. }
  146. $set = '';
  147. foreach ($this->set as $set_piece) {
  148. $set .= '\''.$set_piece['value'].'\'';
  149. $set .= ', ';
  150. }
  151. $set = substr($set, 0, -2);
  152. return $set;
  153. }
  154. }