Skip to content

Commit

Permalink
feat: SQLite support has been added
Browse files Browse the repository at this point in the history
  • Loading branch information
lapaliv committed Dec 12, 2023
1 parent 13b4308 commit a2fe1ed
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ The documentation for version 1.x you can see [here](https://github.com/lapaliv/
- Database:
- MySQL: __5.7+__
- PostgreSQL __9.6+__
- SQLite __3.32+__
- PHP: __8.0+__
- Laravel: __8.0+__

Expand Down Expand Up @@ -768,8 +769,8 @@ class BulkRow {
* Bulk touching
* Bulk updating without updating timestamps
* Supporting `DB::raw()` as a value
* Supporting `SQLite`
* Support a custom database driver
* Update rows and return the number of updated ones

### Tests

Expand Down
33 changes: 33 additions & 0 deletions src/Drivers/SqLite/SqLiteDriverForceDeleteFeature.php
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 src/Drivers/SqLite/SqLiteDriverInsertWithResultFeature.php
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
);
}
}
33 changes: 33 additions & 0 deletions src/Drivers/SqLite/SqLiteDriverQuietInsertFeature.php
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);
}
}
33 changes: 33 additions & 0 deletions src/Drivers/SqLite/SqLiteDriverUpdateFeature.php
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;
}
}
49 changes: 49 additions & 0 deletions src/Drivers/SqLiteBulkDriver.php
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);
}
}
26 changes: 26 additions & 0 deletions src/Entities/BulkSqLiteInsertResult.php
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;
}
}
Loading

0 comments on commit a2fe1ed

Please sign in to comment.