Skip to content

Commit 4c4f15c

Browse files
committed
Changed tests, all Numbers are now enclosed by single quotes, test for "update", changes to the internal structure.
1 parent d86671e commit 4c4f15c

File tree

19 files changed

+293
-64
lines changed

19 files changed

+293
-64
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/.idea
22
/vendor
3-
/composer.lock
3+
/composer.lock
4+
/atlassian-ide-plugin.xml

src/Kir/MySQL/Builder/Delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function from($name) {
3232
*/
3333
public function where($expr) {
3434
$arguments = array_slice(func_get_args(), 1);
35-
$expr = $this->mysql()->quoteExpression($expr, $arguments);
35+
$expr = $this->db()->quoteExpression($expr, $arguments);
3636
$this->where[] = "({$expr})";
3737
return $this;
3838
}

src/Kir/MySQL/Builder/Insert.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function add($field, $value) {
7474
throw new UnexpectedValueException('Field name is invalid');
7575
}
7676
$sqlField = $field;
77-
$sqlValue = $this->mysql()->quote($value);
77+
$sqlValue = $this->db()->quote($value);
7878
$this->fields[$sqlField] = $sqlValue;
7979
return $this;
8080
}
@@ -90,7 +90,7 @@ public function update($field, $value) {
9090
throw new UnexpectedValueException('Field name is invalid');
9191
}
9292
$sqlField = $field;
93-
$sqlValue = $this->mysql()->quote($value);
93+
$sqlValue = $this->db()->quote($value);
9494
$this->update[$sqlField] = $sqlValue;
9595
return $this;
9696
}
@@ -184,7 +184,7 @@ public function __toString() {
184184
}
185185

186186
$fields = $this->fields;
187-
$tableFields = $this->getTableFields($this->table);
187+
$tableFields = $this->db()->getTableFields($this->table);
188188

189189
$insertData = $this->buildFieldList($fields, $tableFields);
190190
$updateData = $this->buildUpdate();
@@ -212,7 +212,7 @@ public function __toString() {
212212
*/
213213
private function buildUpdate() {
214214
$queryArr = array();
215-
$tableFields = $this->getTableFields($this->table);
215+
$tableFields = $this->db()->getTableFields($this->table);
216216
if(!empty($this->update)) {
217217
$queryArr[] = "ON DUPLICATE KEY UPDATE\n";
218218
$updateArr = array();

src/Kir/MySQL/Builder/InsertUpdateStatement.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php
22
namespace Kir\MySQL\Builder;
33

4-
use PDO;
5-
64
abstract class InsertUpdateStatement extends Statement {
7-
/**
8-
* @var array
9-
*/
10-
private static $tableFields = array();
11-
125
/**
136
* @var array
147
*/
@@ -23,22 +16,6 @@ public function setMask(array $mask) {
2316
return $this;
2417
}
2518

26-
/**
27-
* @param string $table
28-
* @return array
29-
*/
30-
protected function getTableFields($table) {
31-
if(array_key_exists($table, self::$tableFields)) {
32-
return self::$tableFields[$table];
33-
}
34-
$stmt = $this->mysql()->query("DESCRIBE {$table}");
35-
$stmt->execute();
36-
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
37-
self::$tableFields[$table] = array_map(function ($row) { return $row['Field']; }, $rows);
38-
$stmt->closeCursor();
39-
return self::$tableFields[$table];
40-
}
41-
4219
/**
4320
* @param array $fields
4421
* @param array $tableFields
@@ -50,7 +27,7 @@ protected function buildFieldList(array $fields, array $tableFields, array $resu
5027
if (is_int($fieldName)) {
5128
$result[] = $fieldValue;
5229
} elseif ($this->isFieldAccessible($fieldName, $tableFields)) {
53-
$fieldName = $this->mysql()->quoteField($fieldName);
30+
$fieldName = $this->db()->quoteField($fieldName);
5431
$result[] = "\t{$fieldName} = {$fieldValue}";
5532
}
5633
}

src/Kir/MySQL/Builder/RunnableDelete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class RunnableDelete extends Delete {
77
*/
88
public function run() {
99
$query = (string)$this;
10-
return $this->mysql()->exec($query);
10+
return $this->db()->exec($query);
1111
}
1212
}

src/Kir/MySQL/Builder/RunnableInsert.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class RunnableInsert extends Insert {
66
*/
77
public function run() {
88
$query = $this->__toString();
9-
$this->mysql()->exec($query);
10-
return (int) $this->mysql()->getLastInsertId();
9+
$this->db()->exec($query);
10+
return (int) $this->db()->getLastInsertId();
1111
}
1212
}

src/Kir/MySQL/Builder/RunnableSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ public function fetchValue($default = null) {
6262
* @return PDOStatement
6363
*/
6464
private function createStatement() {
65-
return $this->mysql()->query($this->__toString());
65+
return $this->db()->query($this->__toString());
6666
}
6767
}

src/Kir/MySQL/Builder/RunnableUpdate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ class RunnableUpdate extends Update {
77
*/
88
public function run() {
99
$query = $this->__toString();
10-
return $this->mysql()->exec($query);
10+
return $this->db()->exec($query);
1111
}
1212
}

src/Kir/MySQL/Builder/Select.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function orderBy($expression, $direction = 'asc') {
147147
$expression[0],
148148
array_slice($expression, 1)
149149
);
150-
$expression = call_user_func_array(array($this->mysql(), 'quoteExpression'), $arguments);
150+
$expression = call_user_func_array(array($this->db(), 'quoteExpression'), $arguments);
151151
}
152152
$this->orderBy[] = array($expression, $direction);
153153
return $this;
@@ -167,7 +167,7 @@ public function groupBy($expression) {
167167
$expression[0],
168168
array_slice($expression, 1)
169169
);
170-
$expression = call_user_func_array(array($this->mysql(), 'quoteExpression'), $arguments);
170+
$expression = call_user_func_array(array($this->db(), 'quoteExpression'), $arguments);
171171
}
172172
$this->groupBy[] = $expression;
173173
}
@@ -298,7 +298,7 @@ private function buildConditions($type, array $conditions, $query) {
298298
$arr = array();
299299
foreach($conditions as $condition) {
300300
list($expression, $arguments) = $condition;
301-
$expr = $this->mysql()->quoteExpression($expression, $arguments);
301+
$expr = $this->db()->quoteExpression($expression, $arguments);
302302
$arr[] = "\t{$expr}";
303303
}
304304
$query .= join("\n\tAND\n", $arr);
@@ -368,6 +368,6 @@ private function buildTableName($alias, $name) {
368368
* @return string
369369
*/
370370
private function buildExpression($expression, array $arguments) {
371-
return $this->mysql()->quoteExpression($expression, $arguments);
371+
return $this->db()->quoteExpression($expression, $arguments);
372372
}
373373
}

src/Kir/MySQL/Builder/Statement.php

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

4-
use Kir\MySQL\MySQL;
4+
use Kir\MySQL\Database;
55

66
abstract class Statement {
77
/**
88
* @var
99
*/
10-
private $mysql;
10+
private $db;
1111

1212
/**
13-
* @param MySQL $mysql
13+
* @param Database $db
1414
*/
15-
public function __construct(MySQL $mysql) {
16-
$this->mysql = $mysql;
15+
public function __construct(Database $db) {
16+
$this->db = $db;
1717
}
1818

1919
/**
20-
* @return MySQL
20+
* @return Database
2121
*/
22-
protected function mysql() {
23-
return $this->mysql;
22+
protected function db() {
23+
return $this->db;
2424
}
2525

2626
/**

0 commit comments

Comments
 (0)