sql; } } class WpSqlDeleteBuilder extends WpStatementBuilder { public function __construct($table) { $this->sql = 'DELETE FROM ' . $table; } public function where($variable, $value) { $this->sql .= " WHERE $variable = '$value'"; return $this; } } class WpSqlUpdateBuilder extends WpStatementBuilder { public function __construct($table) { $this->sql = 'UPDATE ' . $table; } public function set($fields) { $lastValue = end($fields); $lastCol = key($fields); array_pop($fields); $set = ''; foreach ($fields as $column => $value) { $set .= "$column = '$value', "; } $this->sql .= " SET $set $lastCol = '$lastValue'"; return $this; } public function where($variable, $value) { $this->sql .= " WHERE $variable = '$value'"; return $this; } } class WpSqlInsertBuilder extends WpStatementBuilder { private $values; public function __construct($fields) { $cols = ''; $values = ''; $lastValue = end($fields); $lastCol = key($fields); array_pop($fields); foreach ($fields as $column => $value) { $cols .= "$column, "; $values .= "'$value', "; } $this->values = " ($cols $lastCol) VALUES($values '$lastValue')"; return $this; } public function into($table) { $this->sql = 'INSERT INTO ' . $table . $this->values; return $this; } } class WpSqlSelectBuilder extends WpStatementBuilder { public function __construct($fields) { $this->sql = 'SELECT ' . $fields; } public function from($table, $as = null) { $this->sql .= ' FROM ' .$table . (isset($as) ? ' AS ' . $as : ''); return $this; } public function andFrom($table, $as) { $this->sql .= ', ' .$table . (isset($as) ? ' AS ' . $as : ''); return $this; } public function where($variable, $value, $type = null, $isString = true) { $this->sql .= (isset($type) ? $type : WpSqlBuilder::WHERE) . $variable . ' = ' . (($isString) ? "'$value'" : $value); return $this; } } $fields = array('id' => 1, 'col' => 'etc.'); $table = 'myTable'; $SqlBuilder = new WpSqlBuilder(); $SqlBuilder->deleteFrom($table)->where('id', 5)->execute(); $SqlBuilder->update('myTable')->set($fields)->where('id', 5)->execute(); $SqlBuilder->insert($fields)->into('myTable')->execute(); $SqlBuilder->select('*')->from($table, 'a') ->andFrom($table, 'b') ->where('id', 5, WpSqlBuilder::WHERE, false) ->where('a.id', 'b.id', WpSqlBuilder::AND_WHERE, false) ->execute(); /* DELETE FROM myTable WHERE id = '5' UPDATE myTable SET id = '1', col = 'etc.' WHERE id = '5' INSERT INTO myTable (id, col) VALUES('1', 'etc.') SELECT * FROM myTable AS a, myTable AS b WHERE id = 5 AND a.id = b.id */ ?>