Skip to content

Commit a0f0877

Browse files
Merge pull request #183 from LaravelFreelancerNL/120-support-insertorignoreusing
120 support insertorignoreusing
2 parents e392f13 + 53b9875 commit a0f0877

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

docs/compatibility-list.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Union orders / Union aggregates / Union groupBy
6565
Expression / raw
6666

6767
### Joins
68-
crossJoin / join / joinSub? / leftJoin / leftJoinSub?
68+
crossJoin / join / joinSub / lateralJoin / leftJoin / leftJoinSub
6969

7070
#### Unsupported join clauses
7171
rightJoin / rightJoinSub / joinWhere?
@@ -103,7 +103,7 @@ limit / offset / take / skip
103103
when
104104

105105
### Insert statements
106-
insert / insertOrIgnore / insertUsing / insertGetId
106+
insert / insertOrIgnore / insertUsing / insertOrIgnoreUsing / insertGetId
107107

108108
### Update statements
109109
update / updateOrInsert / upsert /
@@ -183,14 +183,14 @@ updateExistingPivot /
183183

184184
## <a name="artisan"></a> Artisan commands
185185
The following database-related artisan commands are supported:
186-
make:model / db / db:wipe /
187-
make:migration / migrate:install / migrate /
186+
db / db:monitor / db:show / db:table / db:wipe /
187+
make:migration / make:model / migrate:install / migrate /
188188
migrate:fresh / migrate:refresh / migrate:reset / migrate:rollback /
189189
migrate:status / convert:migrations
190190

191191
The following database-related artisan commands are NOT support at this time:
192192

193-
db:monitor / db:show / db:table / schema:dump
193+
schema:dump
194194

195195
## <a name="testing"></a> Testing
196196

@@ -207,16 +207,6 @@ castAsJson (dummy method)
207207
## <a name="database-connection"></a> Database connection
208208
escape
209209

210-
## Console commands
211-
The following database related console commands are compatible with vanilla Laravel:
212-
213-
db:monitor / db:seed / db:wipe
214-
make:migrate / migrate /
215-
migrate:fresh / migrate:install / migrate:refresh / migrate:reset / migrate:rollback / migrate:status
216-
217-
### Incompatible console commands
218-
db:show / db:table
219-
220210
## <a name="known-incompatibilities"></a> Known incompatibilities
221211
Not all features can be made compatible. Known issues are listed below:
222212

@@ -241,8 +231,9 @@ These methods don't work as ArangoDB requires you to declare the locking mechani
241231
### Raw SQL
242232
Any raw SQL needs to be replaced by raw AQL.
243233

244-
### Separate read and write connections
245-
Aranguent currently doesn't support the combination of a separate read and write connection
234+
### Database replication: separate read and write connections
235+
ArangoDB offers a cluster setup for high availability instead of replication and as such
236+
doesn't support the combination of a separate read and write connection.
246237

247238
### Transactions
248239
[At the beginning of a transaction you must declare collections that are used in (write) statements.](transactions.md)

src/Query/Concerns/CompilesDataManipulations.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,41 @@ public function compileInsertUsing(IlluminateQueryBuilder $query, array $columns
122122
return $aql;
123123
}
124124

125+
/**
126+
* Compile an insert statement using a subquery into SQL.
127+
*
128+
* @param IlluminateQueryBuilder $query
129+
* @param array<mixed> $columns
130+
* @param string $sql
131+
* @return string
132+
*/
133+
public function compileInsertOrIgnoreUsing(IlluminateQueryBuilder $query, array $columns, string $sql)
134+
{
135+
$table = $this->wrapTable($query->from);
136+
137+
$insertDoc = '';
138+
if (empty($columns) || $columns === ['*']) {
139+
$insertDoc = 'docDoc';
140+
}
141+
142+
143+
if ($insertDoc === '') {
144+
$insertValues = [];
145+
foreach ($columns as $column) {
146+
$insertValues[$column] = $this->normalizeColumnReferences($query, $column, 'docs');
147+
}
148+
$insertDoc = $this->generateAqlObject($insertValues);
149+
}
150+
151+
$aql = /** @lang AQL */ 'LET docs = ' . $sql
152+
. ' FOR docDoc IN docs'
153+
. ' INSERT ' . $insertDoc . ' INTO ' . $table
154+
. ' OPTIONS { ignoreErrors: true }'
155+
. ' RETURN NEW._key';
156+
157+
return $aql;
158+
}
159+
125160
/**
126161
* @param array<mixed> $values
127162
* @return string

tests/Query/InsertTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,30 @@
136136

137137
expect($user->surname)->toBe('Baelish');
138138
});
139+
140+
test('insertOrIgnoreUsing', function () {
141+
// Let's give Baelish a user, what could possibly go wrong? Everyone trusts him...
142+
$baelishes = DB::table('characters')
143+
->where('surname', 'Baelish');
144+
145+
DB::table('users')->insertOrIgnoreUsing(['name', 'surname'], $baelishes);
146+
147+
$user = DB::table('users')->where("surname", "=", "Baelish")->first();
148+
149+
expect($user->surname)->toBe('Baelish');
150+
});
151+
152+
test("insertOrIgnoreUsing doesn't error on duplicates", function () {
153+
// Let's give Baelish a user, what could possibly go wrong? Everyone trusts him...
154+
$baelish = DB::table('characters')
155+
->where('surname', 'Baelish');
156+
157+
DB::table('users')->insertUsing(['name', 'surname'], $baelish);
158+
159+
// Let's do it again.
160+
DB::table('users')->insertOrIgnoreUsing(['name', 'surname'], $baelish);
161+
162+
$user = DB::table('users')->where("surname", "=", "Baelish")->first();
163+
164+
expect($user->surname)->toBe('Baelish');
165+
});

0 commit comments

Comments
 (0)