-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lapaliv
committed
Dec 12, 2023
1 parent
13b4308
commit a2fe1ed
Showing
10 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Lapaliv\BulkUpsert\Drivers\SqLite; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Lapaliv\BulkUpsert\Builders\DeleteBulkBuilder; | ||
use Lapaliv\BulkUpsert\Converters\MixedValueToSqlConverter; | ||
use Lapaliv\BulkUpsert\Grammars\SqLiteGrammar; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class SqLiteDriverForceDeleteFeature | ||
{ | ||
public function __construct( | ||
private MixedValueToSqlConverter $mixedValueToSqlConverter, | ||
) { | ||
// | ||
} | ||
|
||
public function handle( | ||
ConnectionInterface $connection, | ||
DeleteBulkBuilder $builder, | ||
): int { | ||
$grammar = new SqLiteGrammar($this->mixedValueToSqlConverter); | ||
|
||
$result = $connection->delete($grammar->delete($builder), $grammar->getBindings()); | ||
|
||
unset($grammar); | ||
|
||
return $result; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Drivers/SqLite/SqLiteDriverInsertWithResultFeature.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Lapaliv\BulkUpsert\Drivers\SqLite; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Lapaliv\BulkUpsert\Builders\InsertBuilder; | ||
use Lapaliv\BulkUpsert\Converters\MixedValueToSqlConverter; | ||
use Lapaliv\BulkUpsert\Entities\BulkSqLiteInsertResult; | ||
use Lapaliv\BulkUpsert\Grammars\SqLiteGrammar; | ||
|
||
class SqLiteDriverInsertWithResultFeature | ||
{ | ||
public function __construct( | ||
private MixedValueToSqlConverter $mixedValueToSqlConverter, | ||
) { | ||
// | ||
} | ||
|
||
public function handle( | ||
ConnectionInterface $connection, | ||
InsertBuilder $builder, | ||
?string $primaryKeyName, | ||
): BulkSqLiteInsertResult { | ||
$lastPrimaryBeforeInserting = null; | ||
|
||
if ($primaryKeyName !== null) { | ||
$lastRow = $connection->selectOne( | ||
sprintf( | ||
'select max(%s) as id from %s', | ||
$primaryKeyName, | ||
$builder->getInto() | ||
) | ||
); | ||
|
||
$lastPrimaryBeforeInserting = $lastRow->id ?? 0; | ||
} | ||
|
||
$grammar = new SqLiteGrammar($this->mixedValueToSqlConverter); | ||
$sql = $grammar->insert($builder); | ||
$bindings = $grammar->getBindings(); | ||
$connection->insert($sql, $bindings); | ||
|
||
unset($grammar); | ||
|
||
return new BulkSqLiteInsertResult( | ||
is_numeric($lastPrimaryBeforeInserting) | ||
? (int) $lastPrimaryBeforeInserting | ||
: null | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** @noinspection PhpPluralMixedCanBeReplacedWithArrayInspection */ | ||
|
||
/** @noinspection PhpArrayShapeAttributeCanBeAddedInspection */ | ||
|
||
namespace Lapaliv\BulkUpsert\Drivers\SqLite; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Lapaliv\BulkUpsert\Builders\InsertBuilder; | ||
use Lapaliv\BulkUpsert\Converters\MixedValueToSqlConverter; | ||
use Lapaliv\BulkUpsert\Grammars\SqLiteGrammar; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class SqLiteDriverQuietInsertFeature | ||
{ | ||
public function __construct( | ||
private MixedValueToSqlConverter $mixedValueToSqlConverter, | ||
) { | ||
// | ||
} | ||
|
||
public function handle(ConnectionInterface $connection, InsertBuilder $builder): void | ||
{ | ||
$grammar = new SqLiteGrammar($this->mixedValueToSqlConverter); | ||
|
||
$connection->insert($grammar->insert($builder), $grammar->getBindings()); | ||
|
||
unset($grammar); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Lapaliv\BulkUpsert\Drivers\SqLite; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Lapaliv\BulkUpsert\Builders\UpdateBulkBuilder; | ||
use Lapaliv\BulkUpsert\Converters\MixedValueToSqlConverter; | ||
use Lapaliv\BulkUpsert\Grammars\SqLiteGrammar; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class SqLiteDriverUpdateFeature | ||
{ | ||
public function __construct( | ||
private MixedValueToSqlConverter $mixedValueToSqlConverter | ||
) { | ||
// | ||
} | ||
|
||
public function handle( | ||
ConnectionInterface $connection, | ||
UpdateBulkBuilder $builder, | ||
): int { | ||
$grammar = new SqLiteGrammar($this->mixedValueToSqlConverter); | ||
|
||
$result = $connection->update($grammar->update($builder), $grammar->getBindings()); | ||
|
||
unset($grammar); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Lapaliv\BulkUpsert\Drivers; | ||
|
||
use Illuminate\Database\ConnectionInterface; | ||
use Lapaliv\BulkUpsert\Builders\DeleteBulkBuilder; | ||
use Lapaliv\BulkUpsert\Builders\InsertBuilder; | ||
use Lapaliv\BulkUpsert\Builders\UpdateBulkBuilder; | ||
use Lapaliv\BulkUpsert\Contracts\BulkDriver; | ||
use Lapaliv\BulkUpsert\Contracts\BulkInsertResult; | ||
use Lapaliv\BulkUpsert\Drivers\SqLite\SqLiteDriverForceDeleteFeature; | ||
use Lapaliv\BulkUpsert\Drivers\SqLite\SqLiteDriverInsertWithResultFeature; | ||
use Lapaliv\BulkUpsert\Drivers\SqLite\SqLiteDriverQuietInsertFeature; | ||
use Lapaliv\BulkUpsert\Drivers\SqLite\SqLiteDriverUpdateFeature; | ||
|
||
class SqLiteBulkDriver implements BulkDriver | ||
{ | ||
public function __construct( | ||
private SqLiteDriverInsertWithResultFeature $insertWithResultFeature, | ||
private SqLiteDriverQuietInsertFeature $quietInsertFeature, | ||
private SqLiteDriverUpdateFeature $updateFeature, | ||
private SqLiteDriverForceDeleteFeature $forceDeleteFeature, | ||
) { | ||
// | ||
} | ||
|
||
public function insertWithResult( | ||
ConnectionInterface $connection, | ||
InsertBuilder $builder, | ||
?string $primaryKeyName, | ||
): BulkInsertResult { | ||
return $this->insertWithResultFeature->handle($connection, $builder, $primaryKeyName); | ||
} | ||
|
||
public function quietInsert(ConnectionInterface $connection, InsertBuilder $builder): void | ||
{ | ||
$this->quietInsertFeature->handle($connection, $builder); | ||
} | ||
|
||
public function update(ConnectionInterface $connection, UpdateBulkBuilder $builder): int | ||
{ | ||
return $this->updateFeature->handle($connection, $builder); | ||
} | ||
|
||
public function forceDelete(ConnectionInterface $connection, DeleteBulkBuilder $builder): int | ||
{ | ||
return $this->forceDeleteFeature->handle($connection, $builder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Lapaliv\BulkUpsert\Entities; | ||
|
||
use Lapaliv\BulkUpsert\Contracts\BulkInsertResult; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class BulkSqLiteInsertResult implements BulkInsertResult | ||
{ | ||
public function __construct(private ?int $maxPrimaryBeforeInserting) | ||
{ | ||
// | ||
} | ||
|
||
public function getRows(): ?array | ||
{ | ||
return null; | ||
} | ||
|
||
public function getMaxPrimaryBeforeInserting(): null|int|string | ||
{ | ||
return $this->maxPrimaryBeforeInserting; | ||
} | ||
} |
Oops, something went wrong.