Skip to content

Commit

Permalink
chore: add duster
Browse files Browse the repository at this point in the history
  • Loading branch information
patoui committed Nov 29, 2024
1 parent a213827 commit a2c4970
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 275 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'

- name: "Duster Lint"
uses: tighten/duster-action@v3
with:
args: lint

- name: Install ClickHouse
run: |
sudo apt-get update
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ vendor
coverage
clickhouse
.phpunit.result.cache
.php_cs.cache
.DS_Store
.idea
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"require-dev": {
"orchestra/testbench": "^9.0",
"phpunit/phpunit": "^11.0"
"phpunit/phpunit": "^11.0",
"tightenco/duster": "^3.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,6 +38,8 @@
}
},
"scripts": {
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
"up": "docker compose up -d",
"down": "docker compose down",
"in": "docker exec -it lc_php /bin/bash -c \"composer install\"",
Expand Down
5 changes: 5 additions & 0 deletions duster.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exclude": [
"tests/TestCase.php"
]
}
22 changes: 6 additions & 16 deletions src/ClickhouseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ class ClickhouseBuilder extends Builder
/**
* Retrieve the "count" result of the query.
*
* @param string $columns
* @param string $columns
* @return int
*/
public function count($columns = null)
{
return parent::count($columns ?: []);
}

/**
* @return array
*/
public function getBindings(): array
{
$bindings = [];
$keys = [];
$keys = [];

foreach ($this->wheres as &$where) {
if (!empty($where['value']) && !$where['value'] instanceof Expression) {
if (! empty($where['value']) && ! $where['value'] instanceof Expression) {
$col = $where['column'];
if (!isset($keys[$col])) {
if (! isset($keys[$col])) {
$keys[$col] = 0;
}

Expand All @@ -48,9 +45,6 @@ public function getBindings(): array

/**
* Insert a new record into the database.
*
* @param array $values
* @return bool
*/
public function insert(array $values): bool
{
Expand All @@ -61,7 +55,7 @@ public function insert(array $values): bool
return true;
}

if (!is_array(reset($values))) {
if (! is_array(reset($values))) {
$values = [$values];
}

Expand All @@ -88,7 +82,6 @@ public function insert(array $values): bool
/**
* Update a record in the database.
*
* @param array $values
* @return int
*/
public function update(array $values)
Expand All @@ -109,14 +102,11 @@ public function update(array $values)

/**
* Remove all of the expressions from a list of bindings.
*
* @param array $bindings
* @return array
*/
public function cleanBindings(array $bindings): array
{
return array_filter($bindings, static function ($binding) {
return !$binding instanceof Expression;
return ! $binding instanceof Expression;
});
}
}
109 changes: 43 additions & 66 deletions src/ClickhouseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ClickhouseConnection extends BaseConnection

/**
* Connection constructor.
* @param array $config
*/
public function __construct(array $config)
{
Expand All @@ -34,7 +33,6 @@ public function __construct(array $config)

/**
* Get SeasClick client
* @return SeasClick
*/
public function getClient(): SeasClick
{
Expand All @@ -44,19 +42,17 @@ public function getClient(): SeasClick
/**
* Begin a fluent query against a database table.
*
* @param Closure|Builder|string $table
* @param string|null $as
* @param Closure|Builder|string $table
* @param string|null $as
* @return Builder
*/
// public function table($table, $as = null): Builder
// {
// $this->notImplementedException();
// }
// public function table($table, $as = null): Builder
// {
// $this->notImplementedException();
// }

/**
* Get a new query builder instance.
*
* @return ClickhouseBuilder
*/
public function query(): ClickhouseBuilder
{
Expand All @@ -67,45 +63,33 @@ public function query(): ClickhouseBuilder

/**
* Get the query post processor used by the connection.
*
* @return Processor
*/
public function getDefaultPostProcessor(): Processor
{
return new ClickhouseProcessor();
}

/**
* Get the default query grammar instance.
*
* @return ClickhouseGrammar
*/
protected function getDefaultQueryGrammar(): ClickhouseGrammar
{
return new ClickhouseGrammar();
return new ClickhouseProcessor;
}

/**
* Run a select statement and return a single result.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return mixed
*/
public function selectOne($query, $bindings = [], $useReadPdo = true)
{
$records = $this->db->select($query, $bindings);

return array_shift($records);
}

/**
* Run a select statement against the database.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return array
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
*/
public function select($query, $bindings = [], $useReadPdo = true): array
{
Expand All @@ -115,10 +99,9 @@ public function select($query, $bindings = [], $useReadPdo = true): array
/**
* Run a select statement against the database and returns a generator.
*
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
* @return Generator
* @param string $query
* @param array $bindings
* @param bool $useReadPdo
*/
public function cursor($query, $bindings = [], $useReadPdo = true): Generator
{
Expand All @@ -128,22 +111,21 @@ public function cursor($query, $bindings = [], $useReadPdo = true): Generator
/**
* Run an insert statement against the database.
*
* @param string $query Query is table name
* @param array $bindings
* @return bool
* @param string $query Query is table name
* @param array $bindings
*/
public function insert($query, $bindings = []): bool
{
[$keys, $values] = $this->parseBindings($bindings);

return $this->db->insert($query, $keys, $values);
}

/**
* Run an update statement against the database.
*
* @param string $query
* @param array $bindings
* @return int
* @param string $query
* @param array $bindings
*/
public function update($query, $bindings = []): int
{
Expand All @@ -154,9 +136,8 @@ public function update($query, $bindings = []): int
/**
* Run a delete statement against the database.
*
* @param string $query
* @param array $bindings
* @return int
* @param string $query
* @param array $bindings
*/
public function delete($query, $bindings = []): int
{
Expand All @@ -167,9 +148,8 @@ public function delete($query, $bindings = []): int
/**
* Execute an SQL statement and return the boolean result.
*
* @param string $query
* @param array $bindings
* @return bool
* @param string $query
* @param array $bindings
*/
public function statement($query, $bindings = []): bool
{
Expand All @@ -179,9 +159,8 @@ public function statement($query, $bindings = []): bool
/**
* Run an SQL statement and get the number of rows affected.
*
* @param string $query
* @param array $bindings
* @return int
* @param string $query
* @param array $bindings
*/
public function affectingStatement($query, $bindings = []): int
{
Expand All @@ -191,8 +170,7 @@ public function affectingStatement($query, $bindings = []): int
/**
* Run a raw, unprepared query against the PDO connection.
*
* @param string $query
* @return bool
* @param string $query
*/
public function unprepared($query): bool
{
Expand All @@ -202,7 +180,6 @@ public function unprepared($query): bool
/**
* Prepare the query bindings for execution.
*
* @param array $bindings
* @return array
*/
public function prepareBindings(array $bindings)
Expand All @@ -213,8 +190,8 @@ public function prepareBindings(array $bindings)
/**
* Execute a Closure within a transaction.
*
* @param callback|Closure $callback
* @param int $attempts
* @param callable|Closure $callback
* @param int $attempts
* @return mixed
*
* @throws Throwable
Expand All @@ -226,8 +203,6 @@ public function transaction($callback, $attempts = 1)

/**
* Start a new database transaction.
*
* @return void
*/
public function beginTransaction(): void
{
Expand All @@ -236,8 +211,6 @@ public function beginTransaction(): void

/**
* Commit the active database transaction.
*
* @return void
*/
public function commit(): void
{
Expand All @@ -248,7 +221,6 @@ public function commit(): void
* Rollback the active database transaction.
*
* @param int|null $toLevel
* @return void
*/
public function rollBack($toLevel = null): void
{
Expand All @@ -267,26 +239,31 @@ public function transactionLevel()

/**
* Execute the given callback in "dry run" mode.
*
* @param Closure $callback
* @return array
*/
public function pretend(Closure $callback): array
{
$this->notImplementedException();
}

/**
* @param array $bindings i.e. [['name' => 'John', 'user_id' => 321]]
* @return array<array> [['name', 'user_id'], [['John', 321]]]
* Get the default query grammar instance.
*/
protected function getDefaultQueryGrammar(): ClickhouseGrammar
{
return new ClickhouseGrammar;
}

/**
* @param array $bindings i.e. [['name' => 'John', 'user_id' => 321]]
* @return array<array> [['name', 'user_id'], [['John', 321]]]
*/
private function parseBindings(array $bindings): array
{
if (!$bindings) {
if (! $bindings) {
return [[], []];
}

if (!is_string(current(array_flip($bindings)))) {
if (! is_string(current(array_flip($bindings)))) {
throw new InvalidArgumentException(
"Keys must be strings, i.e. ['name' => 'John', 'user_id' => 321]"
);
Expand Down
Loading

0 comments on commit a2c4970

Please sign in to comment.