Permalink
Browse files

Normalise.

  • Loading branch information...
1 parent 8c307bc commit d77d320c07a52faa6099dae3dd39be9fe8fd7f4c @nathggns nathggns committed Feb 21, 2013
@@ -153,9 +153,9 @@ public function set($key, $value) {
/* Utility functions for the subclass */
- protected function extract() {
+ protected function extract_select() {
- $options = call_user_func_array([$this, 'extract_shuffle'], func_get_args());
+ $options = call_user_func_array([$this, 'extract_shuffle_select'], func_get_args());
$args = [];
$required = ['table'];
@@ -189,7 +189,50 @@ protected function extract() {
return $args;
}
- protected function extract_shuffle($table = null, $options = []) {
+ protected function extract_update() {
+
+ $args = $this->extract_shuffle(['table'], ['table', 'data', 'where', 'conds'], func_get_args());
+
+ if (isset($args['conds'])) {
+ $args['where'] = $args['conds'];
+ unset($args['conds']);
+ }
+
+ return $args;
+ }
+
+ protected function extract_delete() {
+ $args = $this->extract_shuffle(['table'], ['table', 'where', 'conds'], func_get_args());
+
+ if (isset($args['conds'])) {
+ $args['where'] = $args['conds'];
+ unset($args['conds']);
+ }
+
+ return $args;
+ }
+
+ protected function extract_shuffle($required, $keys, $options) {
+ if (count($options) === 1 && is_array(reset($options)) && is_hash(reset($options))) {
+ $args = reset($options);
+ } else {
+ $args = [];
+
+ foreach ($keys as $i => $key) {
+ $args[$key] = isset($options[$i]) ? $options[$i] : null;
+ }
+ }
+
+ foreach ($required as $req) {
+ if (!isset($args[$req])) {
+ throw new InvalidArgumentException('Missing ' . $req);
+ }
+ }
+
+ return $args;
+ }
+
+ protected function extract_shuffle_select($table = null, $options = []) {
// Argument shuffling
if (is_array($table)) {
$options = $table;
@@ -8,7 +8,7 @@
interface DatabaseQueryBuilderInterface {
public function select();
public function insert($table, $data);
- public function update($table, $data, $where = []);
+ public function update();
public function structure($table);
- public function delete($table);
+ public function delete();
}
@@ -7,37 +7,25 @@
*/
class DatabaseQueryBuilderSQL extends DatabaseQueryBuilder {
- protected $operators = ['=', '>', '<', '<>', '!='];
+ protected $operators = ['=', '>', '<', '<>', '!='];
protected $joins = ['AND', 'OR'];
protected $default_meta = ['connector' => 'AND', 'operator' => '='];
public function select() {
$args = func_get_args();
- $options = call_user_func_array([$this, 'extract'], $args);
+ $options = call_user_func_array([$this, 'extract_select'], $args);
- if ((count($args) === 1 && is_string(reset($args))) || $this->chained()) {
- return $this->start('select', recursive_overwrite($this->query_opts, $options));
+ if ($this->chained($args)) {
+ return $this->start('select', $options);
}
- $options = call_user_func_array([$this, 'extract'], $args);
extract($options);
if (!is_array($table)) {
$table = [$table];
}
- $parts = [];
- foreach ($table as $key => $val) {
- $col = $this->backtick(is_int($key) ? $val : $key);
-
- if (!is_int($key)) {
- $col .= ' AS ' . $this->backtick($val);
- }
-
- $parts[] = $col;
- }
-
- $table = implode(',', $parts);
+ $table = $this->table($table);
$vals = $this->backtick($vals);
@@ -110,43 +98,27 @@ public function insert($table, $data = null) {
return $query;
}
- public function update($table, $data = null, $where = null) {
-
- $default = [
- 'table' => null,
- 'data' => [],
- 'conds' => []
- ];
-
- $options = [];
-
- if (is_array($table)) {
- $options = $table;
- } else {
- $options['table'] = $table;
- }
+ public function update() {
- if (!is_null($data)) {
- $options['data'] = $data;
- }
+ $args = func_get_args();
+ $options = call_user_func_array([$this, 'extract_update'], $args);
- if (!is_null($where)) {
- $options['conds'] = $where;
+ if ($this->chained($args)) {
+ $this->start('update', $options);
+ return $this;
}
- $options = recursive_overwrite($default, $options);
- list($table, $data, $where) = array_values($options);
-
-
- if ($this->chained(func_get_args())) {
- return $this->start('update', $options);
- }
+ extract($options);
if (count($data) === 0) {
throw new InvalidArgumentException('You must pass data to set');
}
- $table = $this->backtick($table);
+ if (!is_array($table)) {
+ $table = [$table];
+ }
+
+ $table = $this->table($table);
$keys = $this->backtick(array_keys($data));
$data = $this->escape($data);
$query = 'UPDATE ' . $table . ' SET ' . $this->pairs($keys, $data);
@@ -158,33 +130,22 @@ public function update($table, $data = null, $where = null) {
return $query;
}
- public function delete($table, $where = null) {
+ public function delete() {
- $defaults = [
- 'table' => null,
- 'conds' => []
- ];
-
- $options = [];
-
- if (is_array($table)) {
- $options = $table;
- } else {
- $options['table'] = $table;
- }
+ $args = func_get_args();
+ $options = call_user_func_array([$this, 'extract_delete'], $args);
- if (!is_null($where)) {
- $options['conds'] = $where;
+ if ($this->chained($args)) {
+ return $this->start('delete', $options);
}
- $options = recursive_overwrite($defaults, $options);
- list($table, $where) = array_values($options);
+ extract($options);
- if ($this->chained(func_get_args())) {
- return $this->start('delete', $options);
+ if (!is_array($table)) {
+ $table = [$table];
}
- $table = $this->backtick($table);
+ $table = $this->table($table);
$query = 'DELETE FROM ' . $table;
if (count($where) > 0) $query .= ' ' . $this->where_array($where);
@@ -437,4 +398,19 @@ protected function escape($value) {
return $value;
}
+
+ protected function table($table) {
+ $parts = [];
+ foreach ($table as $key => $val) {
+ $col = $this->backtick(is_int($key) ? $val : $key);
+
+ if (!is_int($key)) {
+ $col .= ' AS ' . $this->backtick($val);
+ }
+
+ $parts[] = $col;
+ }
+
+ return implode(',', $parts);
+ }
}

0 comments on commit d77d320

Please sign in to comment.