1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* ___ _ _ |
5
|
|
|
* | _ \__ _| |_ __ _| |__ __ _ ___ ___ |
6
|
|
|
* | _/ _` | _/ _` | '_ \/ _` (_-</ -_) |
7
|
|
|
* |_| \__,_|\__\__,_|_.__/\__,_/__/\___| |
8
|
|
|
* |
9
|
|
|
* This file is part of Kristuff\Patabase. |
10
|
|
|
* (c) Kristuff <[email protected]> |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
* |
15
|
|
|
* @version 1.0.1 |
16
|
|
|
* @copyright 2017-2022 Christophe Buliard |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Kristuff\Patabase\Query; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Insert |
23
|
|
|
* |
24
|
|
|
* Represents a [INSERT INTO] SQL query |
25
|
|
|
*/ |
26
|
|
|
class Insert extends \Kristuff\Patabase\Query\InsertBase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Build the SQL INSERT query |
30
|
|
|
* |
31
|
|
|
* @access public |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function sql(): string |
35
|
|
|
{ |
36
|
|
|
$columnsNames = array(); |
37
|
|
|
$columnsValues = array(); |
38
|
|
|
|
39
|
|
|
foreach ($this->parameters as $key => $val) { |
40
|
|
|
$arg = $this->getArgName($key); |
41
|
|
|
$columnsNames[] = $this->escape($key); |
42
|
|
|
$columnsValues[] = ':' . $arg; |
43
|
|
|
} |
44
|
|
|
return trim(sprintf( |
45
|
|
|
'INSERT INTO %s (%s) VALUES (%s)', |
46
|
|
|
$this->escape($this->tableName), |
47
|
|
|
implode(', ', $columnsNames), |
48
|
|
|
implode(', ', $columnsValues) |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Bind values parameters |
54
|
|
|
* |
55
|
|
|
* @access public |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
public function bindValues(): void |
59
|
|
|
{ |
60
|
|
|
foreach ($this->parameters as $key => $val) { |
61
|
|
|
$arg = $this->getArgName($key); |
62
|
|
|
$this->pdoParameters[$arg] = $val; |
63
|
|
|
} |
64
|
|
|
parent::bindValues(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns the ID of the last inserted row or sequence value |
69
|
|
|
* This function returns false if there is no executed query. |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @return string|false The last inserted id if found, otherwise false. |
73
|
|
|
*/ |
74
|
|
|
public function lastId() |
75
|
|
|
{ |
76
|
|
|
return (empty(!$this->pdoStatement)) ? $this->driver->lastInsertedId() : false; |
77
|
|
|
} |
78
|
|
|
} |