/lib/sql.php
PHP | 185 lines | 152 code | 31 blank | 2 comment | 29 complexity | eaca1f216ad3ef7744325dcd3824faea MD5 | raw file
Possible License(s): JSON
- <?php
- class SCAR_SQL {
- public $table;
- public $columns;
- public $where;
- public $set;
-
- const ANSI_SELECT = 'SELECT %s FROM %s %s %s %s';
- const ANSI_UPDATE = 'UPDATE %s SET %s %s %s';
- const ANSI_INSERT = 'INSERT INTO %s (%s) VALUES (%s)';
-
- protected $mode;
- protected $SCAR;
-
- public function __construct($mode, $dsn, $SCAR = 'SCAR') {
- $this->mode = $mode;
- $this->dsn = $dsn;
- $this->SCAR = $SCAR;
- }
-
- public function render() {
- $table = '`'.$this->table.'`';
-
- if ($this->mode == 'update') {
- return trim(sprintf(self::ANSI_UPDATE, $table, $this->renderSet(), $this->renderWhere(), $this->renderLimit()));
- }
-
- if ($this->mode == 'insert') {
- return trim(sprintf(self::ANSI_INSERT, $table, $this->renderInsertSetColumns(), $this->renderInsertSetValues()));
- }
-
- return trim(sprintf(self::ANSI_SELECT, $this->renderColumns(), $table, $this->renderWhere(), $this->renderOrder(), $this->renderLimit()));
- }
-
- protected function renderColumns() {
- if (!isset($this->columns) || !$this->columns) {
- return '*';
- }
-
- if (is_array($this->columns)) {
- return '`'.implode('`, `', $this->columns).'`';
- }
- elseif ($this->columns == '*') {
- return '*';
- }
- else {
- return '`'.$this->columns.'`';
- }
- }
-
- protected function renderWhere() {
- if (!isset($this->where) || !$this->where) {
- return '';
- }
-
- // where
- $where = '';
- foreach ($this->where as $where_piece) {
- $where .= '`';
- $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $where_piece['col']));
- $where .= '`';
-
- $where .= ' '.$where_piece['oper'].' ';
-
- if (is_array($where_piece['value'])) {
- $where .= '(';
- foreach ($where_piece['value'] as $value) {
- $where .= '\'';
- $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $value));
- $where .= '\', ';
- }
- $where = substr($where, 0, -2);
- $where .= ')';
- }
- else {
- $where .= '\'';
- $where .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $where_piece['value']));
- $where .= '\'';
- }
-
- $where .= ' AND ';
- }
-
- if (strlen($where) > 0) {
- $where = substr($where, 0, -5);
- $where = 'WHERE '.$where;
- }
- return $where;
- }
-
- protected function renderOrder() {
- if (!isset($this->order) || !$this->order) {
- return '';
- }
-
- $order = '';
- foreach ($this->order as $order_piece) {
- $order .= '`';
- $order .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $order_piece['col']));
- $order .= '` ';
-
- if ($order_piece['order'] === SORT_ASC) {
- $order .= 'ASC';
- }
- else {
- $order .= 'DESC';
- }
-
- $order .= ', ';
- }
-
- if (strlen($order) > 0) {
- $order = substr($order, 0, -2);
- $order = 'ORDER BY '.$order;
- }
-
- return $order;
- }
-
- protected function renderLimit() {
- if (!isset($this->limit) || !$this->limit) {
- return '';
- }
-
- if ($this->limit) {
- $out = 'LIMIT '.call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $this->limit['start'])).', ';
- $out .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $this->limit['count']));
- return $out;
- }
- return '';
- }
-
- protected function renderSet() {
- if (!isset($this->set) || !$this->set) {
- return '';
- }
-
- // set
- $set = '';
- foreach ($this->set as $set_piece) {
- $set .= '`';
- $set .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $set_piece['col']));
- $set .= '`';
-
- $set .= ' = ';
-
- $set .= '\'';
- $set .= call_user_func_array(array($this->SCAR, 'escape'), array($this->dsn, $set_piece['value']));
- $set .= '\'';
-
- $set .= ', ';
- }
-
- if (strlen($set) > 0) {
- $set = substr($set, 0, -2);
- }
- return $set;
- }
-
- protected function renderInsertSetColumns() {
- if (!isset($this->set) || !$this->set) {
- return '';
- }
-
- $set = '';
- foreach ($this->set as $set_piece) {
- $set .= '`'.$set_piece['col'].'`';
- $set .= ', ';
- }
- $set = substr($set, 0, -2);
- return $set;
- }
- protected function renderInsertSetValues() {
- if (!isset($this->set) || !$this->set) {
- return '';
- }
- $set = '';
- foreach ($this->set as $set_piece) {
- $set .= '\''.$set_piece['value'].'\'';
- $set .= ', ';
- }
- $set = substr($set, 0, -2);
- return $set;
- }
- }