Skip to content

Commit dba17eb

Browse files
committed
Merge remote-tracking branch 'origin/master'
Conflicts: src/Builder/Insert.php
2 parents 4225052 + 1508e3f commit dba17eb

File tree

21 files changed

+184
-220
lines changed

21 files changed

+184
-220
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Autodetect text files
2+
* text=auto
3+
4+
# Force the following filetypes to have unix eols, so Windows does not break them
5+
*.* text eol=lf

.gitignore

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
/.idea
2-
/vendor
3-
/composer.lock
4-
/atlassian-ide-plugin.xml
5-
/test.php
1+
/*
2+
!/src
3+
!/tests
4+
!/.gitattributes
5+
!/.gitignore
6+
!/.travis.yml
7+
!/composer.json
8+
!/README.md
9+
!/tests.xml

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ before_script:
1010
- composer update
1111

1212
script:
13-
- phpunit -c tests.xml
13+
- phpunit -c tests.xml

src/Builder/Delete.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __toString() {
4646
throw new Exception('Specify a table-name');
4747
}
4848

49-
$sqlTable = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
49+
$sqlTable = $this->aliasReplacer()->replace($this->table);
5050
$queryArr = array();
5151
$queryArr[] = "DELETE "."FROM\n\t{$sqlTable}\n";
5252

src/Builder/Insert.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ class Insert extends InsertUpdateStatement {
99
* @var array
1010
*/
1111
private $fields = array();
12+
1213
/**
1314
* @var array
1415
*/
1516
private $update = array();
17+
1618
/**
1719
* @var string
1820
*/
1921
private $table = null;
22+
2023
/**
2124
* @var string
2225
*/
2326
private $keyField = null;
27+
2428
/**
2529
* @var bool
2630
*/
2731
private $ignore = false;
28-
/**
29-
* @var Select
30-
*/
31-
private $from = null;
3232

3333
/**
3434
* @param string $table
@@ -172,15 +172,6 @@ public function addOrUpdateAll(array $data) {
172172
return $this;
173173
}
174174

175-
/**
176-
* @param Select $select
177-
* @return $this
178-
*/
179-
public function from(Select $select) {
180-
$this->from = $select;
181-
return $this;
182-
}
183-
184175
/**
185176
* @throws Exception
186177
* @return string
@@ -190,6 +181,16 @@ public function __toString() {
190181
throw new Exception('Specify a table-name');
191182
}
192183

184+
$fields = $this->fields;
185+
$tableFields = $this->db()->getTableFields($this->table);
186+
187+
$insertData = $this->buildFieldList($fields, $tableFields);
188+
$updateData = $this->buildUpdate();
189+
190+
if (empty($insertData)) {
191+
throw new Exception('No field-data found');
192+
}
193+
193194
$tableName = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
194195

195196
$queryArr = array();
@@ -231,7 +232,8 @@ public function __toString() {
231232
*/
232233
private function buildUpdate() {
233234
$queryArr = array();
234-
$tableFields = $this->db()->getTableFields($this->table);
235+
$tableName = $this->aliasReplacer()->replace($this->table);
236+
$tableFields = $this->db()->getTableFields($tableName);
235237
if(!empty($this->update)) {
236238
$queryArr[] = "ON DUPLICATE KEY UPDATE\n";
237239
$updateArr = array();

src/Builder/RunnableSelect.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ public function getFoundRows() {
165165
*/
166166
private function createStatement() {
167167
$db = $this->db();
168-
$statement = $db->prepare($this->__toString());
168+
$query = $this->__toString();
169+
$statement = $db->query($query);
169170
$statement->execute($this->values);
170171
if($this->getCalcFoundRows()) {
171172
$this->foundRows = $db->query('SELECT FOUND_ROWS()')->fetchColumn();

src/Builder/Select.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ private function buildTableName($alias, $name) {
436436
$name = join("\n", $lines);
437437
$name = '(' . trim(rtrim(trim($name), ';')) . ')';
438438
}
439-
$name = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($name);
439+
$name = $this->aliasReplacer()->replace($name);
440440
return sprintf("%s %s", $name, $alias);
441441
}
442442

src/Builder/Statement.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22
namespace Kir\MySQL\Builder;
33

44
use Kir\MySQL\Database;
5+
use Kir\MySQL\Tools\AliasReplacer;
56

67
abstract class Statement {
78
/**
8-
* @var
9+
* @var Database
910
*/
1011
private $db;
1112

13+
/**
14+
* @var AliasReplacer
15+
*/
16+
private $aliasReplacer;
17+
1218
/**
1319
* @param Database $db
1420
*/
1521
public function __construct(Database $db) {
1622
$this->db = $db;
23+
$this->aliasReplacer = new AliasReplacer($db->getAliasRegistry());
1724
}
1825

1926
/**
@@ -39,6 +46,13 @@ public function cloneStatement() {
3946
return clone $this;
4047
}
4148

49+
/**
50+
* @return AliasReplacer
51+
*/
52+
public function aliasReplacer() {
53+
return $this->aliasReplacer;
54+
}
55+
4256
/**
4357
* @return Database
4458
*/

src/Builder/Update.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ public function __toString() {
9494
throw new Exception('Specify a table-name');
9595
}
9696

97-
$tableFields = $this->db()->getTableFields($this->table);
97+
$tableName = $this->aliasReplacer()->replace($this->table);
98+
99+
$tableFields = $this->db()->getTableFields($tableName);
98100
$sqlFields = $this->buildFieldList($this->fields, $tableFields);
99101

100102
if (empty($sqlFields)) {
101103
throw new Exception('No field-data found');
102104
}
103105

104-
$tableName = (new AliasReplacer($this->db()->getAliasRegistry()))->replace($this->table);
105-
106106
$queryArr = array();
107107
$queryArr[] = "UPDATE\n\t{$tableName}\nSET\n{$sqlFields}\n";
108108

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function transactionRollback();
101101
/**
102102
* @param callable $callback
103103
* @throws \Exception
104-
* @return $this
104+
* @return mixed
105105
*/
106106
public function transaction($callback);
107107
}

0 commit comments

Comments
 (0)