Skip to content

Commit b2f653c

Browse files
committed
Added massive functionality
1 parent 3fdcfef commit b2f653c

29 files changed

+846
-512
lines changed

src/Builder/Delete.php

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4-
use Kir\MySQL\Tools\AliasReplacer;
4+
use Kir\MySQL\Builder\Traits\JoinBuilder;
5+
use Kir\MySQL\Builder\Traits\LimitBuilder;
6+
use Kir\MySQL\Builder\Traits\OffsetBuilder;
7+
use Kir\MySQL\Builder\Traits\OrderByBuilder;
8+
use Kir\MySQL\Builder\Traits\TableBuilder;
9+
use Kir\MySQL\Builder\Traits\TableNameBuilder;
10+
use Kir\MySQL\Builder\Traits\WhereBuilder;
511

612
class Delete extends Statement {
7-
/**
8-
* @var string
9-
*/
10-
private $table = null;
13+
use TableNameBuilder;
14+
use TableBuilder;
15+
use JoinBuilder;
16+
use WhereBuilder;
17+
use OrderByBuilder;
18+
use LimitBuilder;
19+
use OffsetBuilder;
1120

1221
/**
13-
* @var array
22+
* @var string[]
1423
*/
15-
private $where = array();
24+
private $aliases = array();
1625

1726
/**
1827
* Name der Tabelle
1928
*
20-
* @param string $name
29+
* @param string $alias
30+
* @param string $table
2131
* @return $this
22-
2332
*/
24-
public function from($name) {
25-
$this->table = $name;
26-
return $this;
27-
}
28-
29-
/**
30-
* @param string $expr
31-
* @return $this
32-
*/
33-
public function where($expr) {
34-
$arguments = array_slice(func_get_args(), 1);
35-
$expr = $this->db()->quoteExpression($expr, $arguments);
36-
$this->where[] = "({$expr})";
33+
public function from($alias, $table = null) {
34+
if($table === null) {
35+
list($alias, $table) = [$table, $alias];
36+
} else {
37+
$this->aliases[] = $alias;
38+
}
39+
$this->addTable($alias, $table);
3740
return $this;
3841
}
3942

@@ -42,20 +45,16 @@ public function where($expr) {
4245
* @throws Exception
4346
*/
4447
public function __toString() {
45-
if ($this->table === null) {
46-
throw new Exception('Specify a table-name');
47-
}
48-
49-
$sqlTable = $this->aliasReplacer()->replace($this->table);
50-
$queryArr = array();
51-
$queryArr[] = "DELETE "."FROM\n\t{$sqlTable}\n";
52-
53-
if (!empty($this->where)) {
54-
$sqlWhere = join("\n\tAND\n\t", $this->where);
55-
$queryArr[] = "WHERE\n\t{$sqlWhere}\n";
56-
}
57-
$queryArr[] = ";";
58-
59-
return join('', $queryArr);
48+
$query = "DELETE ";
49+
$query .= join(', ', $this->aliases);
50+
$query = trim($query) . " FROM\n";
51+
$query = $this->buildTables($query);
52+
$query = $this->buildJoins($query);
53+
$query = $this->buildWhereConditions($query);
54+
$query = $this->buildOrder($query);
55+
$query = $this->buildLimit($query);
56+
$query = $this->buildOffset($query);
57+
$query .= ";\n";
58+
return $query;
6059
}
6160
}

src/Builder/Insert.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,23 @@ class Insert extends InsertUpdateStatement {
2929
* @var Select
3030
*/
3131
private $from = null;
32+
/**
33+
* @var callable
34+
*/
35+
private $tableFields = null;
3236

3337
/**
3438
* @param string $table
3539
* @return $this
36-
3740
*/
3841
public function into($table) {
3942
$this->table = $table;
43+
$this->tableFields = function () {
44+
static $cache = null;
45+
if($cache === null) {
46+
$cache = $this->db()->getTableFields($this->table);
47+
}
48+
};
4049
return $this;
4150
}
4251

@@ -141,6 +150,7 @@ public function addOrUpdateExpr($str) {
141150
* @throws UnexpectedValueException
142151
*/
143152
public function addAll(array $data) {
153+
$data = $this->clearValues($data);
144154
foreach ($data as $field => $value) {
145155
$this->add($field, $value);
146156
}
@@ -153,6 +163,7 @@ public function addAll(array $data) {
153163
* @return $this
154164
*/
155165
public function updateAll(array $data) {
166+
$data = $this->clearValues($data);
156167
foreach ($data as $field => $value) {
157168
if ($field != $this->keyField) {
158169
$this->update($field, $value);
@@ -202,15 +213,11 @@ public function __toString() {
202213
$queryArr[] = trim(trim($this->from), ';');
203214
} else {
204215
$fields = $this->fields;
205-
$tableFields = $this->db()->getTableFields($tableName);
206-
207-
$insertData = $this->buildFieldList($fields, $tableFields);
208-
209-
if (empty($insertData)) {
216+
$insertData = $this->buildFieldList($fields);
217+
if (!count($insertData)) {
210218
throw new Exception('No field-data found');
211219
}
212-
213-
$queryArr[] = "SET\n{$insertData}\n";
220+
$queryArr[] = sprintf("SET\n%s\n", join(",\n", $insertData));
214221
}
215222

216223
$updateData = $this->buildUpdate();
@@ -230,17 +237,15 @@ public function __toString() {
230237
*/
231238
private function buildUpdate() {
232239
$queryArr = array();
233-
$tableName = $this->aliasReplacer()->replace($this->table);
234-
$tableFields = $this->db()->getTableFields($tableName);
235240
if(!empty($this->update)) {
236241
$queryArr[] = "ON DUPLICATE KEY UPDATE\n";
237242
$updateArr = array();
238-
239243
if($this->keyField !== null) {
240-
$updateArr[] = "`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})";
244+
$updateArr[] = "\t`{$this->keyField}` = LAST_INSERT_ID({$this->keyField})";
241245
}
246+
$updateArr = $this->buildFieldList($this->update, $updateArr);
242247

243-
$queryArr[] = $this->buildFieldList($this->update, $tableFields, $updateArr);
248+
$queryArr[] = join(",\n", $updateArr);
244249
}
245250
return join('', $queryArr);
246251
}
@@ -252,4 +257,25 @@ private function buildUpdate() {
252257
private function isFieldNameValid($fieldName) {
253258
return is_numeric($fieldName) || !is_scalar($fieldName);
254259
}
260+
261+
/**
262+
* @param array $values
263+
* @return array
264+
* @throws Exception
265+
*/
266+
private function clearValues(array $values) {
267+
if(!count($values)) {
268+
return [];
269+
}
270+
$fields = $this->db()->getTableFields($this->table);
271+
$result = array();
272+
273+
foreach ($values as $fieldName => $fieldValue) {
274+
if(in_array($fieldName, $fields)) {
275+
$result[$fieldName] = $fieldValue;
276+
}
277+
}
278+
279+
return $result;
280+
}
255281
}

src/Builder/InsertUpdateStatement.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4+
use Kir\MySQL\Builder\Internal\DefaultValue;
5+
46
abstract class InsertUpdateStatement extends Statement {
57
/**
68
* @var array
@@ -25,23 +27,22 @@ public function setMask(array $mask) {
2527

2628
/**
2729
* @param array $fields
28-
* @param array $tableFields
29-
* @param array $result
30+
* @param array $query
3031
* @return string
3132
*/
32-
protected function buildFieldList(array $fields, array $tableFields, array $result = array()) {
33+
protected function buildFieldList(array $fields, array $query = array()) {
3334
foreach ($fields as $fieldName => $fieldValue) {
34-
if(is_array($this->mask) && !in_array($fieldName, $this->mask)) {
35-
continue;
35+
if($fieldValue instanceof DefaultValue) {
36+
$fieldValue = 'DEFAULT';
3637
}
3738
if (is_int($fieldName)) {
38-
$result[] = $fieldValue;
39-
} elseif ($this->isFieldAccessible($fieldName, $tableFields)) {
39+
$query[] = "\t{$fieldValue}";
40+
} else {
4041
$fieldName = $this->db()->quoteField($fieldName);
41-
$result[] = "\t{$fieldName} = {$fieldValue}";
42+
$query[] = "\t{$fieldName}={$fieldValue}";
4243
}
4344
}
44-
return join(",\n", $result);
45+
return $query;
4546
}
4647

4748
/**

src/Builder/Internal/DefaultValue.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Internal;
3+
4+
class DefaultValue {
5+
}

0 commit comments

Comments
 (0)