1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder; |
6
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder; |
7
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException as BindException; |
8
|
|
|
|
9
|
|
|
trait CompilesInserts |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Compile an insert statement into AQL. |
13
|
|
|
* |
14
|
|
|
* @param IlluminateQueryBuilder $query |
15
|
|
|
* @param array $values |
16
|
|
|
* |
17
|
|
|
* @throws BindException |
18
|
|
|
* |
19
|
|
|
* @return string |
20
|
|
|
*/ |
21
|
|
|
public function compileInsert(Builder|IlluminateQueryBuilder $query, array $values, string $bindVar = null) |
22
|
|
|
{ |
23
|
|
|
$table = $this->prefixTable($query->from); |
|
|
|
|
24
|
|
|
|
25
|
|
|
if (empty($values)) { |
26
|
|
|
$aql = /** @lang AQL */ 'INSERT {} INTO ' . $table . ' RETURN NEW._key'; |
27
|
|
|
|
28
|
|
|
return $aql; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return /** @lang AQL */ 'LET values = ' . $bindVar |
32
|
|
|
. ' FOR value IN values' |
33
|
|
|
. ' INSERT value INTO ' . $table |
34
|
|
|
. ' RETURN NEW._key'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Compile an insert and get ID statement into SQL. |
39
|
|
|
* |
40
|
|
|
* @param array<mixed> $values |
41
|
|
|
*/ |
42
|
|
|
public function compileInsertGetId(IlluminateQueryBuilder $query, $values, $sequence = '_key', string $bindVar = null) |
43
|
|
|
{ |
44
|
|
|
$table = $this->prefixTable($query->from); |
45
|
|
|
|
46
|
|
|
$sequence = $this->convertIdToKey($sequence); |
|
|
|
|
47
|
|
|
|
48
|
|
|
if (empty($values)) { |
49
|
|
|
$aql = /** @lang AQL */ 'INSERT {} INTO ' . $table . ' RETURN NEW.' . $sequence; |
50
|
|
|
|
51
|
|
|
return $aql; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$aql = /** @lang AQL */ 'LET values = ' . $bindVar |
55
|
|
|
. ' FOR value IN values' |
56
|
|
|
. ' INSERT value INTO ' . $table |
57
|
|
|
. ' RETURN NEW.' . $sequence; |
58
|
|
|
|
59
|
|
|
return $aql; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Compile an insert statement into AQL. |
64
|
|
|
* |
65
|
|
|
* @param IlluminateQueryBuilder $query |
66
|
|
|
* @param array<mixed> $values |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function compileInsertOrIgnore(IlluminateQueryBuilder $query, array $values, string $bindVar = null) |
70
|
|
|
{ |
71
|
|
|
$table = $this->prefixTable($query->from); |
72
|
|
|
|
73
|
|
|
if (empty($values)) { |
74
|
|
|
$aql = /** @lang AQL */ "INSERT {} INTO $table RETURN NEW._key"; |
75
|
|
|
|
76
|
|
|
return $aql; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$aql = /** @lang AQL */ "LET values = $bindVar " |
80
|
|
|
. "FOR value IN values " |
81
|
|
|
. "INSERT value INTO $table " |
82
|
|
|
. "OPTIONS { ignoreErrors: true } " |
83
|
|
|
. "RETURN NEW._key"; |
84
|
|
|
|
85
|
|
|
return $aql; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Compile an insert statement using a subquery into SQL. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Database\Query\Builder $query |
92
|
|
|
* @param array $columns |
93
|
|
|
* @param string $sql |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function compileInsertUsing(IlluminateQueryBuilder $query, array $columns, string $sql) |
97
|
|
|
{ |
98
|
|
|
$table = $this->wrapTable($query->from); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$insertDoc = ''; |
101
|
|
|
if (empty($columns) || $columns === ['*']) { |
102
|
|
|
$insertDoc = 'docDoc'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
if ($insertDoc === '') { |
107
|
|
|
$insertValues = []; |
108
|
|
|
foreach($columns as $column) { |
109
|
|
|
$insertValues[$column] = $this->normalizeColumnReferences($query, $column, 'docs'); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
$insertDoc = $this->generateAqlObject($insertValues); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
// if ($insertDoc !== '') { |
116
|
|
|
// $insertValues; |
117
|
|
|
// } |
118
|
|
|
|
119
|
|
|
$aql = /** @lang AQL */ 'LET docs = ' . $sql |
120
|
|
|
. ' FOR docDoc IN docs' |
121
|
|
|
. ' INSERT ' . $insertDoc . ' INTO ' . $table |
122
|
|
|
. ' RETURN NEW._key'; |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
return $aql; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|