Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .env.example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

return [
'DB_CONNECTION' => 'mysql',
'DB_HOST' => '127.0.0.1',
'DB_USERNAME' => 'root',
'DB_PASSWORD' => '',
'DB_DATABASE' => 'test',
];
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ composer.lock
package-lock.json
vendor/
test/
.env.php

# OS Generated
.DS_Store*
Expand Down
86 changes: 1 addition & 85 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,92 +355,8 @@ public function orWhere($condition, $comparator = null, $value = null): self
return $this;
}

*
* @param string $column The JSON column
* @param string $jsonKey The key within the JSON structure
* @param mixed $value The value to compare against
* @param string $comparator The comparison operator (default '=')
*/
public function whereJson(string $column, string $jsonKey, $value, string $comparator = '='): self
{
// Check if the value is an integer, and cast the JSON value to an integer
$jsonExpression = is_int($value)
# just incase: ? "CAST(JSON_EXTRACT($column, '$.$jsonKey') AS UNSIGNED)";
? "JSON_EXTRACT($column, '$.$jsonKey') + 0"
: "JSON_EXTRACT($column, '$.$jsonKey')";

$this->query = Builder::where($this->query, $jsonExpression, $value, $comparator);
$this->bind(...(Builder::$bindings));

return $this;
}

/**
* Add a JSON where clause with OR comparator to the query
*
* @param string $column The JSON column
* @param string $jsonKey The key within the JSON structure
* @param mixed $value The value to compare against
* @param string $comparator The comparison operator (default '=')
*/
public function orWhereJson(string $column, string $jsonKey, $value, string $comparator = '='): self
{
$jsonExpression = is_int($value)
? "JSON_EXTRACT($column, '$.$jsonKey') + 0"
: "JSON_EXTRACT($column, '$.$jsonKey')";

$this->query = Builder::where($this->query, $jsonExpression, $value, $comparator, 'OR');
$this->bind(...(Builder::$bindings));

return $this;
}

/**
* Add a JSON contains clause to the query
*
* @param string $column The JSON column
* @param mixed $value The value to check for
* @param string|null $jsonKey The key within the JSON structure (optional)
*/
public function whereJsonContains(string $column, $value, ?string $jsonKey = null): self
{
$jsonValue = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$jsonExpression = $jsonKey
? "JSON_CONTAINS($column, ?, '$.$jsonKey')"
: "JSON_CONTAINS($column, ?)";

Builder::$bindings[] = $jsonValue;
$this->query = Builder::where($this->query, $jsonExpression, 1, '=');
$this->bind(...(Builder::$bindings));

return $this;
}

/**
* Add a JSON contains clause with OR comparator to the query
*
* @param string $column The JSON column
* @param mixed $value The value to check for
* @param string|null $jsonKey The key within the JSON structure (optional)
*/
public function orWhereJsonContains(string $column, $value, ?string $jsonKey = null): self
{
$jsonValue = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

$jsonExpression = $jsonKey
? "JSON_CONTAINS($column, ?, '$.$jsonKey')"
: "JSON_CONTAINS($column, ?)";

Builder::$bindings[] = $jsonValue;
$this->query = Builder::where($this->query, $jsonExpression, 1, '=', 'OR');
$this->bind(...(Builder::$bindings));

return $this;
}

/**
* Fetch current query with all related data
* Add a JSON where clause to db query
*
* @param string $column The JSON column
* @param string $jsonKey The key within the JSON structure
Expand Down
17 changes: 12 additions & 5 deletions tests/mysql/connect.test.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php

beforeAll(function () {
$pdo = new \PDO('mysql:host=eu-cdbr-west-03.cleardb.net;dbname=heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
if (file_exists(__DIR__ . '/../../.env.php')) {
$_ENV += require __DIR__ . '/../../.env.php';
}

$_ENV += require __DIR__ . '/../../.env.example.php';

Comment thread
fadrian06 marked this conversation as resolved.
$pdo = new \PDO("mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}", $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$query = '
DROP TABLE IF EXISTS `test`;
Expand All @@ -24,8 +30,9 @@

try {
$db = new \Leaf\Db();
expect($db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17'))
->toBeInstanceOf(\PDO::class);
$pdo = $db->connectSync($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);
expect($pdo)->toBeInstanceOf(\PDO::class);
$db->connection($pdo);
$db->close();
Comment thread
fadrian06 marked this conversation as resolved.

Comment thread
fadrian06 marked this conversation as resolved.
$success = true;
Expand All @@ -38,7 +45,7 @@
it('inserts dummy user into `test` table', function () {
$success = false;
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

try {
$db->insert('test')
Expand Down Expand Up @@ -67,7 +74,7 @@

it('selects dummy user from `test` table', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$user = $db->select('test')
->where('name', 'Name')
Expand Down
32 changes: 28 additions & 4 deletions tests/mysql/leaf-builder.test.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
<?php

beforeAll(function () {
if (file_exists(__DIR__ . '/../../.env.php')) {
$_ENV += require __DIR__ . '/../../.env.php';
}

$_ENV += require __DIR__ . '/../../.env.example.php';

Comment thread
fadrian06 marked this conversation as resolved.
$pdo = new \PDO("mysql:host={$_ENV['DB_HOST']};dbname={$_ENV['DB_DATABASE']}", $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$query = '
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
';

$pdo->exec($query);
$pdo = null;
Comment thread
fadrian06 marked this conversation as resolved.
});

it('orders results in ascending order', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

Comment thread
fadrian06 marked this conversation as resolved.
$users = $db->select('test')->orderBy("created_at", "asc")->all();
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -12,7 +36,7 @@

it('orders results in descending order', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$users = $db->select('test')->orderBy("created_at", "desc")->all();
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -22,7 +46,7 @@

it('orders by dummy name and count', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$data = $db->select('test', 'name, COUNT(*)')->groupBy("created_at")->all();
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -31,7 +55,7 @@

it('orders by dummy name and count with limit and offset', function () {
$db = new \Leaf\Db();
$db->connect('eu-cdbr-west-03.cleardb.net', 'heroku_fb1311a639bb407', 'b9607a8a6d5ebb', 'cc589b17');
$db->connect($_ENV['DB_HOST'], $_ENV['DB_DATABASE'], $_ENV['DB_USERNAME'], $_ENV['DB_PASSWORD']);

$data = $db->select('test', 'name, COUNT(*)')->groupBy("created_at")->limit(1)->offset(1)->all();
Comment thread
fadrian06 marked this conversation as resolved.

Expand Down