1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
8
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException; |
9
|
|
|
|
10
|
|
|
trait BuildsInserts |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Insert a new record into the database. |
14
|
|
|
* |
15
|
|
|
* @param array<mixed> $values |
16
|
|
|
* @throws BindException |
17
|
|
|
*/ |
18
|
80 |
|
public function insert(array $values): bool |
19
|
|
|
{ |
20
|
|
|
assert($this->grammar instanceof Grammar); |
21
|
|
|
|
22
|
80 |
|
if (!array_is_list($values)) { |
23
|
72 |
|
$values = [$values]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Convert id to _key |
27
|
80 |
|
foreach ($values as $key => $value) { |
28
|
80 |
|
$values[$key] = $this->convertIdToKey($value); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
80 |
|
$bindVar = $this->bindValue($values, 'insert'); |
|
|
|
|
32
|
|
|
|
33
|
80 |
|
$aql = $this->grammar->compileInsert($this, $values, $bindVar); |
34
|
80 |
|
return $this->getConnection()->insert($aql, $this->getBindings()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Insert a new record and get the value of the primary key. |
39
|
|
|
* |
40
|
|
|
* @param array<mixed> $values |
41
|
|
|
* @param string|null $sequence |
42
|
|
|
* @return int|string |
43
|
|
|
*/ |
44
|
32 |
|
public function insertGetId(array $values, $sequence = null) |
45
|
|
|
{ |
46
|
|
|
assert($this->grammar instanceof Grammar); |
47
|
|
|
|
48
|
32 |
|
if (!array_is_list($values)) { |
49
|
32 |
|
$values = [$values]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Convert id to _key |
53
|
32 |
|
foreach ($values as $key => $value) { |
54
|
32 |
|
$values[$key] = $this->convertIdToKey($value); |
55
|
|
|
} |
56
|
|
|
|
57
|
32 |
|
$bindVar = $this->bindValue($values, 'insert'); |
58
|
|
|
|
59
|
32 |
|
$aql = $this->grammar->compileInsertGetId($this, $values, $sequence, $bindVar); |
|
|
|
|
60
|
32 |
|
$response = $this->getConnection()->execute($aql, $this->getBindings()); |
61
|
|
|
|
62
|
32 |
|
return (is_array($response)) ? end($response) : $response; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Insert a new record into the database. |
67
|
|
|
* |
68
|
|
|
* @param array<mixed> $values |
69
|
|
|
* |
70
|
|
|
* @throws BindException |
71
|
|
|
*/ |
72
|
|
|
/** @phpstan-ignore-next-line */ |
73
|
64 |
|
public function insertOrIgnore(array $values): bool |
74
|
|
|
{ |
75
|
|
|
assert($this->grammar instanceof Grammar); |
76
|
|
|
|
77
|
64 |
|
if (!array_is_list($values)) { |
78
|
64 |
|
$values = [$values]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Convert id to _key |
82
|
64 |
|
foreach ($values as $key => $value) { |
83
|
64 |
|
$values[$key] = $this->convertIdToKey($value); |
84
|
|
|
} |
85
|
|
|
|
86
|
64 |
|
$bindVar = $this->bindValue($values, 'insert'); |
87
|
|
|
|
88
|
|
|
|
89
|
64 |
|
$aql = $this->grammar->compileInsertOrIgnore($this, $values, $bindVar); |
|
|
|
|
90
|
64 |
|
return $this->getConnection()->insert($aql, $this->getBindings()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|