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