Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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' => 'eu-cdbr-west-03.cleardb.net',
'DB_USERNAME' => 'b9607a8a6d5ebb',
'DB_PASSWORD' => 'cc589b17',
'DB_DATABASE' => 'heroku_fb1311a639bb407',
];
Comment thread
fadrian06 marked this conversation as resolved.
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
84 changes: 0 additions & 84 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,90 +355,6 @@ 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
*
Expand Down
14 changes: 10 additions & 4 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.

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,7 +30,7 @@

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

Comment thread
fadrian06 marked this conversation as resolved.
Expand All @@ -38,7 +44,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 +73,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
8 changes: 4 additions & 4 deletions tests/mysql/leaf-builder.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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.
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -12,7 +12,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.
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -22,7 +22,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.
Comment thread
fadrian06 marked this conversation as resolved.

Expand All @@ -31,7 +31,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.
Comment thread
fadrian06 marked this conversation as resolved.

Expand Down