Skip to content

Commit b88dba1

Browse files
committed
- Add ::prepare to RunnableDelete/RunnableInsert/RunnableUpdate
1 parent 17170b4 commit b88dba1

File tree

7 files changed

+101
-9
lines changed

7 files changed

+101
-9
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Internal;
3+
4+
interface DDLPreparable {
5+
/**
6+
*/
7+
public function prepare();
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Internal;
3+
4+
use Kir\MySQL\Database\DatabaseStatement;
5+
6+
class DDLRunnable {
7+
/** @var DatabaseStatement */
8+
private $query;
9+
/** @var callable */
10+
private $callbackFn;
11+
12+
/**
13+
* @param DatabaseStatement $query
14+
* @param callable $callbackFn
15+
*/
16+
public function __construct(DatabaseStatement $query, callable $callbackFn = null) {
17+
$this->query = $query;
18+
$this->callbackFn = $callbackFn;
19+
}
20+
21+
/**
22+
* @param array $params
23+
* @return mixed
24+
*/
25+
public function run(array $params = array()) {
26+
$response = $this->query->execute($params);
27+
if($this->callbackFn !== null) {
28+
$response = call_user_func($this->callbackFn, $response);
29+
}
30+
return $response;
31+
}
32+
}

src/Builder/RunnableDelete.php

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

4-
class RunnableDelete extends Delete {
4+
use Kir\MySQL\Builder\Internal\DDLPreparable;
5+
use Kir\MySQL\Builder\Internal\DDLRunnable;
6+
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
7+
8+
class RunnableDelete extends Delete implements DDLPreparable {
9+
use CreateDDLRunnable;
10+
511
/**
612
* @param array $params
713
* @return int
814
*/
915
public function run(array $params = array()) {
10-
$query = (string)$this;
11-
return $this->db()->exec($query, $params);
16+
return $this->prepare()->run($params);
17+
}
18+
19+
/**
20+
* @return DDLRunnable
21+
*/
22+
public function prepare() {
23+
return $this->createPreparable($this->db()->prepare($this));
1224
}
1325
}

src/Builder/RunnableInsert.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22
namespace Kir\MySQL\Builder;
33

44
use BadMethodCallException;
5+
use Kir\MySQL\Builder\Internal\DDLPreparable;
6+
use Kir\MySQL\Builder\Internal\DDLRunnable;
7+
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
58
use Traversable;
69

7-
class RunnableInsert extends Insert {
10+
class RunnableInsert extends Insert implements DDLPreparable {
11+
use CreateDDLRunnable;
12+
813
/**
914
* @param array|Traversable $rows
1015
* @return int[] Insert IDs
@@ -24,14 +29,21 @@ public function insertRows($rows) {
2429
return $result;
2530
}
2631

32+
/**
33+
* @return DDLRunnable
34+
*/
35+
public function prepare() {
36+
return $this->createPreparable($this->db()->prepare($this), function () {
37+
return (int) $this->db()->getLastInsertId();
38+
});
39+
}
40+
2741
/**
2842
* @param array $params
2943
* @return int
3044
* @throws Exception
3145
*/
3246
public function run(array $params = array()) {
33-
$query = $this->__toString();
34-
$this->db()->exec($query, $params);
35-
return (int) $this->db()->getLastInsertId();
47+
return $this->prepare()->run($params);
3648
}
3749
}

src/Builder/RunnableUpdate.php

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

4-
class RunnableUpdate extends Update {
4+
use Kir\MySQL\Builder\Internal\DDLPreparable;
5+
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
6+
7+
class RunnableUpdate extends Update implements DDLPreparable {
8+
use CreateDDLRunnable;
9+
510
/**
611
* @param array $params
712
* @return int
@@ -10,4 +15,10 @@ public function run(array $params = array()) {
1015
$query = $this->__toString();
1116
return $this->db()->exec($query, $params);
1217
}
18+
19+
/**
20+
*/
21+
public function prepare() {
22+
return $this->createPreparable($this->db()->prepare($this));
23+
}
1324
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Kir\MySQL\Builder\Traits;
3+
4+
use Kir\MySQL\Builder\Internal\DDLRunnable;
5+
use Kir\MySQL\Database\DatabaseStatement;
6+
7+
trait CreateDDLRunnable {
8+
/**
9+
* @param DatabaseStatement $query
10+
* @param callback $callbackFn
11+
* @return DDLRunnable
12+
*/
13+
public function createPreparable(DatabaseStatement $query, callable $callbackFn = null) {
14+
$runnable = new DDLRunnable($query, $callbackFn);
15+
return $runnable;
16+
}
17+
}

src/Databases/MySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function query($query) {
7373
* @return QueryStatement
7474
*/
7575
public function prepare($query) {
76-
return $this->buildQueryStatement($query, function ($query) {
76+
return $this->buildQueryStatement((string) $query, function ($query) {
7777
$stmt = $this->pdo->prepare($query);
7878
return $stmt;
7979
});

0 commit comments

Comments
 (0)