Skip to content

Commit 6c78282

Browse files
committed
- Added $options as the second parameter to MySQL::__construct to specify global behavior
- Added the ability to set user defined factories for the SIUD-Query-Builders - Added `$options`-Parameter `'select-options' > 'preserve-types-default'` - Added `$options`-Parameter `'select-options' > 'fetch-object-class-default'` for `fetchObject*` - Look at the `README.md`/initialization for more information
1 parent 353d0ea commit 6c78282

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed

doc/initialization.md

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,33 @@ $mysql = new MySQL($pdo);
99
$mysql->getAliasRegistry()->add('t', 'testdb.test__');
1010
```
1111

12-
[Back](../README.md)
12+
You can also add options to specify global behavior
13+
14+
```PHP
15+
// All keys are optional
16+
$options = [
17+
'select-factory' => function ($db) {
18+
return new MyRunnableSelect($db, ...);
19+
},
20+
'select-options' => [
21+
'preserve-types-default' => true, // Always preserve types if not defined otherwise per query
22+
'fetch-object-class-default' => 'MyClass', // Standard is stdClass
23+
],
24+
'insert-factory' => function ($db) {
25+
return new MyRunnableInsert($db, ...);
26+
},
27+
'insert-options' => [], // Reserved for future usage
28+
'update-factory' => function ($db) {
29+
return new MyRunnableUpdate($db, ...);
30+
},
31+
'update-options' => [], // Reserved for future usage
32+
'delete-factory' => function ($db) {
33+
return new MyRunnableDelete($db, ...);
34+
},
35+
'delete-options' => [], // Reserved for future usage
36+
];
37+
$mysql = new MySQL($pdo, $options);
38+
$mysql->getAliasRegistry()->add('t', 'testdb.test__');
39+
```
40+
41+
[Back](../README.md)

src/Builder/RunnableDelete.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
use Kir\MySQL\Builder\Internal\DDLPreparable;
55
use Kir\MySQL\Builder\Internal\DDLRunnable;
66
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
7+
use Kir\MySQL\Databases\MySQL;
78

89
class RunnableDelete extends Delete implements DDLPreparable {
910
use CreateDDLRunnable;
11+
12+
/**
13+
* @param MySQL $db
14+
* @param array $options
15+
*/
16+
public function __construct(MySQL $db, array $options = []) {
17+
parent::__construct($db);
18+
}
1019

1120
/**
1221
* @param array $params

src/Builder/RunnableInsert.php

+9
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,19 @@
55
use Kir\MySQL\Builder\Internal\DDLPreparable;
66
use Kir\MySQL\Builder\Internal\DDLRunnable;
77
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
8+
use Kir\MySQL\Databases\MySQL;
89
use Traversable;
910

1011
class RunnableInsert extends Insert implements DDLPreparable {
1112
use CreateDDLRunnable;
13+
14+
/**
15+
* @param MySQL $db
16+
* @param array $options
17+
*/
18+
public function __construct(MySQL $db, array $options = []) {
19+
parent::__construct($db);
20+
}
1221

1322
/**
1423
* @param array|Traversable $rows

src/Builder/RunnableSelect.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Kir\MySQL\Builder\Helpers\FieldValueConverter;
1010
use Kir\MySQL\Builder\Helpers\LazyRowGenerator;
1111
use Kir\MySQL\Builder\Helpers\YieldPolyfillIterator;
12+
use Kir\MySQL\Databases\MySQL;
1213
use PDO;
1314
use Traversable;
1415

@@ -19,9 +20,21 @@ class RunnableSelect extends Select implements IteratorAggregate {
1920
private $values = array();
2021
/** @var bool */
2122
private $preserveTypes = false;
23+
/** @var string */
24+
private $defaultClassName = false;
2225
/** @var int */
2326
private $foundRows = 0;
24-
27+
28+
/**
29+
* @param MySQL $db
30+
* @param array $options
31+
*/
32+
public function __construct(MySQL $db, array $options = []) {
33+
parent::__construct($db);
34+
$this->preserveTypes = array_key_exists('preserve-types-default', $options) ? $options['preserve-types-default'] : false;
35+
$this->defaultClassName = array_key_exists('fetch-object-class-default', $options) ? $options['fetch-object-class-default'] : 'stdClass';
36+
}
37+
2538
/**
2639
* @param array $values
2740
* @return $this
@@ -91,17 +104,17 @@ public function fetchRow(Closure $callback = null) {
91104
* @return object[]
92105
* @throws \Exception
93106
*/
94-
public function fetchObjects($className = 'stdClass', Closure $callback = null) {
95-
return $this->fetchAll($callback, PDO::FETCH_CLASS, $className);
107+
public function fetchObjects($className = null, Closure $callback = null) {
108+
return $this->fetchAll($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName);
96109
}
97110

98111
/**
99112
* @param string $className
100113
* @param Closure $callback
101114
* @return object[]|Generator
102115
*/
103-
public function fetchObjectsLazy($className = 'stdClass', Closure $callback = null) {
104-
return $this->fetchLazy($callback, PDO::FETCH_CLASS, $className);
116+
public function fetchObjectsLazy($className = null, Closure $callback = null) {
117+
return $this->fetchLazy($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName);
105118
}
106119

107120
/**
@@ -110,8 +123,8 @@ public function fetchObjectsLazy($className = 'stdClass', Closure $callback = nu
110123
* @return object[]
111124
* @throws \Exception
112125
*/
113-
public function fetchObject($className = 'stdClass', Closure $callback = null) {
114-
return $this->fetch($callback, PDO::FETCH_CLASS, $className, function ($row) {
126+
public function fetchObject($className = null, Closure $callback = null) {
127+
return $this->fetch($callback, PDO::FETCH_CLASS, $className ?: $this->defaultClassName, function ($row) {
115128
return ['valid' => is_object($row), 'default' => null];
116129
});
117130
}

src/Builder/RunnableUpdate.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33

44
use Kir\MySQL\Builder\Internal\DDLPreparable;
55
use Kir\MySQL\Builder\Traits\CreateDDLRunnable;
6+
use Kir\MySQL\Databases\MySQL;
67

78
class RunnableUpdate extends Update implements DDLPreparable {
89
use CreateDDLRunnable;
10+
11+
/**
12+
* @param MySQL $db
13+
* @param array $options
14+
*/
15+
public function __construct(MySQL $db, array $options = []) {
16+
parent::__construct($db);
17+
}
918

1019
/**
1120
* @param array $params

src/Databases/MySQL.php

+24-6
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,28 @@ class MySQL implements Database {
2929
private $queryLoggers = 0;
3030
/** @var MySQLExceptionInterpreter */
3131
private $exceptionInterpreter = 0;
32-
32+
/** @var array */
33+
private $options;
34+
3335
/**
3436
* @param PDO $pdo
37+
* @param array $options
3538
*/
36-
public function __construct(PDO $pdo) {
39+
public function __construct(PDO $pdo, array $options = []) {
3740
if($pdo->getAttribute(PDO::ATTR_ERRMODE) === PDO::ERRMODE_SILENT) {
3841
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
3942
}
4043
$this->pdo = $pdo;
4144
$this->aliasRegistry = new AliasRegistry();
4245
$this->queryLoggers = new QueryLoggers();
4346
$this->exceptionInterpreter = new MySQLExceptionInterpreter();
47+
$defaultOptions = [
48+
'select-options' => [],
49+
'insert-options' => [],
50+
'update-options' => [],
51+
'delete-options' => [],
52+
];
53+
$this->options = array_merge($defaultOptions, $options);
4454
}
4555

4656
/**
@@ -182,7 +192,9 @@ public function quoteField($field) {
182192
* @return Builder\RunnableSelect
183193
*/
184194
public function select(array $fields = null) {
185-
$select = new Builder\RunnableSelect($this);
195+
$select = array_key_exists('select-factory', $this->options)
196+
? call_user_func($this->options['select-factory'], $this, $this->options['select-options'])
197+
: new Builder\RunnableSelect($this, $this->options['select-options']);
186198
if($fields !== null) {
187199
$select->fields($fields);
188200
}
@@ -194,7 +206,9 @@ public function select(array $fields = null) {
194206
* @return Builder\RunnableInsert
195207
*/
196208
public function insert(array $fields = null) {
197-
$insert = new Builder\RunnableInsert($this);
209+
$insert = array_key_exists('insert-factory', $this->options)
210+
? call_user_func($this->options['insert-factory'], $this, $this->options['insert-options'])
211+
: new Builder\RunnableInsert($this, $this->options['insert-options']);
198212
if($fields !== null) {
199213
$insert->addAll($fields);
200214
}
@@ -206,7 +220,9 @@ public function insert(array $fields = null) {
206220
* @return Builder\RunnableUpdate
207221
*/
208222
public function update(array $fields = null) {
209-
$update = new Builder\RunnableUpdate($this);
223+
$update = array_key_exists('update-factory', $this->options)
224+
? call_user_func($this->options['update-factory'], $this, $this->options['update-options'])
225+
: new Builder\RunnableUpdate($this, $this->options['update-options']);
210226
if($fields !== null) {
211227
$update->setAll($fields);
212228
}
@@ -217,7 +233,9 @@ public function update(array $fields = null) {
217233
* @return Builder\RunnableDelete
218234
*/
219235
public function delete() {
220-
return new Builder\RunnableDelete($this);
236+
return array_key_exists('delete-factory', $this->options)
237+
? call_user_func($this->options['delete-factory'], $this, $this->options['delete-options'])
238+
: new Builder\RunnableDelete($this, $this->options['delete-options']);
221239
}
222240

223241
/**

0 commit comments

Comments
 (0)