Skip to content

Commit d35434f

Browse files
committed
v1.1.8 QueryBuilder uses values by converting them to a parameter.
1 parent 06e5ab7 commit d35434f

34 files changed

Lines changed: 316 additions & 298 deletions

src/Connection/Connection.php

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.1.8
10+
* @version 1.1.9
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -25,6 +25,12 @@ class Connection implements ConnectionInterface
2525

2626
private static ?PDO $global = null;
2727

28+
private array $transaction = [
29+
'enable' => false,
30+
'testMode' => false,
31+
'status' => false,
32+
];
33+
2834
private array $credentials = [
2935
'dsn' => '',
3036
'username' => '',
@@ -93,4 +99,60 @@ public function close(): void
9399
self::$global = null;
94100
}
95101

102+
/**
103+
* @inheritDoc
104+
*/
105+
public function beginTransaction(bool $testMode = false): bool
106+
{
107+
$this->transaction = [
108+
'enable' => true,
109+
'testMode' => $testMode,
110+
'status' => true,
111+
];
112+
return $this->getPDO()->beginTransaction();
113+
}
114+
115+
/**
116+
* @inheritDoc
117+
*/
118+
public function completeTransaction(): bool
119+
{
120+
121+
if($this->transaction['status'] !== FALSE && $this->transaction['testMode'] === FALSE){
122+
$res = $this->getPDO()->commit();
123+
}else{
124+
$res = $this->getPDO()->rollBack();
125+
}
126+
$this->transaction = [
127+
'enable' => false,
128+
'testMode' => false,
129+
'status' => false,
130+
];
131+
return (bool)$res;
132+
}
133+
134+
/**
135+
* @inheritDoc
136+
*/
137+
public function statusTransaction(): bool
138+
{
139+
return $this->transaction['status'];
140+
}
141+
142+
/**
143+
* @inheritDoc
144+
*/
145+
public function isTransaction(): bool
146+
{
147+
return $this->transaction['enable'];
148+
}
149+
150+
/**
151+
* @inheritDoc
152+
*/
153+
public function failedTransaction(): void
154+
{
155+
$this->transaction['status'] = false;
156+
}
157+
96158
}

src/Connection/ConnectionInterface.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.1.8
10+
* @version 1.1.9
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -39,4 +39,30 @@ public function getPDO(): \PDO;
3939
*/
4040
public function close(): void;
4141

42+
/**
43+
* @param bool $testMode
44+
* @return bool
45+
*/
46+
public function beginTransaction(bool $testMode = false): bool;
47+
48+
/**
49+
* @return bool
50+
*/
51+
public function completeTransaction(): bool;
52+
53+
/**
54+
* @return bool
55+
*/
56+
public function statusTransaction(): bool;
57+
58+
/**
59+
* @return bool
60+
*/
61+
public function isTransaction(): bool;
62+
63+
/**
64+
* @return void
65+
*/
66+
public function failedTransaction(): void;
67+
4268
}

src/DB.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
88
* @copyright Copyright © 2022 Muhammet ŞAFAK
99
* @license ./LICENSE MIT
10-
* @version 1.1.8
10+
* @version 1.1.9
1111
* @link https://www.muhammetsafak.com.tr
1212
*/
1313

@@ -88,11 +88,9 @@ public function __construct(array $configurations)
8888
if(isset($this->configurations['fetch'])){
8989
$dataMapperOptions['fetch'] = $this->configurations['fetch'];
9090
}
91-
$this->_dataMapper = new DataMapper($this->_connection, $dataMapperOptions);
92-
$this->_queryBuilder = new QueryBuilder([
93-
'allowedFields' => ($this->configurations['allowedFields'] ?? null),
94-
'schema' => ($this->configurations['tableSchema'] ?? null),
95-
'schemaID' => ($this->configurations['tableSchemaID'] ?? null),
91+
$this->_dataMapper = new DataMapper($this, $dataMapperOptions);
92+
$this->_queryBuilder = new QueryBuilder($this, [
93+
'allowedFields' => ($this->configurations['allowedFields'] ?? null)
9694
]);
9795
$this->_validation = new Validation($this->configurations['validation']['methods'], $this->configurations['validation']['messages'], $this->configurations['validation']['labels'], $this);
9896
}
@@ -140,11 +138,9 @@ public function __call($name, $arguments)
140138
throw new DatabaseException('The "' . $name . '" method does not exist.');
141139
}
142140

143-
public function setSchemaID(?string $schemaID): self
141+
public function with(): self
144142
{
145-
$this->configurations['tableSchemaID'] = $schemaID;
146-
$this->getQueryBuilder()->setSchemaID($schemaID);
147-
return $this;
143+
return clone $this;
148144
}
149145

150146
/**
@@ -155,6 +151,21 @@ public function getSchema()
155151
return $this->configurations['tableSchema'];
156152
}
157153

154+
public function setSchema(string $schema): self
155+
{
156+
if($schema === ''){
157+
$this->configurations['tableSchema'] = null;
158+
}else{
159+
$this->configurations['tableSchema'] = $schema;
160+
}
161+
return $this;
162+
}
163+
164+
public function withSchema(string $schema): self
165+
{
166+
return $this->with()->setSchema($schema);
167+
}
168+
158169
/**
159170
* @return string|null
160171
*/
@@ -163,6 +174,21 @@ public function getSchemaID()
163174
return $this->configurations['tableSchemaID'];
164175
}
165176

177+
public function setSchemaID(string $schemaID): self
178+
{
179+
if($schemaID === ''){
180+
$this->configurations['tableSchemaID'] = null;
181+
}else{
182+
$this->configurations['tableSchemaID'] = $schemaID;
183+
}
184+
return $this;
185+
}
186+
187+
public function withSchemaID(string $schemaID): self
188+
{
189+
return $this->with()->setSchemaID($schemaID);
190+
}
191+
166192
public function isError(): bool
167193
{
168194
$this->get_error_merge();
@@ -211,23 +237,25 @@ public function getValidation(): Validation
211237
*/
212238
public function create(array $fields)
213239
{
214-
$data = []; $parameters = [];
240+
$data = [];
215241
$isCreatedField = !empty($this->configurations['createdField']);
242+
if($isCreatedField){
243+
$createdFieldParameterName = $this->getDataMapper()->addParameter($this->configurations['createdField'], \date($this->configurations['timestampFormat']));
244+
}
216245
if(\count($fields) === \count($fields, \COUNT_RECURSIVE)){
217246
$this->getValidation()->setData($fields);
218247
foreach ($fields as $column => $value) {
219248
if($this->getValidation()->validation($column, null) === FALSE){
220249
$this->errors[] = $this->getValidation()->getError();
221250
return false;
222251
}
223-
$data[$column] = ':'.$column;
224-
$parameters[':' . $column] = $value;
252+
$data[$column] = $value;
225253
}
226254
if(empty($data)){
227255
return false;
228256
}
229257
if($isCreatedField){
230-
$data[$this->configurations['createdField']] = ':' . $this->configurations['createdField'];
258+
$data[$this->configurations['createdField']] = $createdFieldParameterName;
231259
}
232260
}else{
233261
$i = 0;
@@ -239,24 +267,19 @@ public function create(array $fields)
239267
$this->errors[] = $this->getValidation()->getError();
240268
return false;
241269
}
242-
$data[$i][$column] = ':' . $column . '_' . $i;
243-
$parameters[':' . $column . '_' . $i] = $value;
270+
$data[$i][$column] = $value;
244271
}
245272
if(empty($data)){
246273
continue;
247274
}
248275
if($isCreatedField){
249-
$data[$i][$this->configurations['createdField']] = ':' . $this->configurations['createdField'];
276+
$data[$i][$this->configurations['createdField']] = $createdFieldParameterName;
250277
}
251278
++$i;
252279
}
253280
}
254-
if($isCreatedField){
255-
$parameters[':' . $this->configurations['createdField']] = \date($this->configurations['timestampFormat']);
256-
}
257-
$this->getDataMapper()->setParameters($parameters);
258-
unset($parameters);
259281
$query = $this->getQueryBuilder()->insertQuery($data);
282+
$this->getQueryBuilder()->reset();
260283
$this->getDataMapper()->persist($query, []);
261284
return $this->getDataMapper()->numRows() > 0;
262285
}

0 commit comments

Comments
 (0)