diff --git a/.env.example.php b/.env.example.php new file mode 100644 index 0000000..7e198d0 --- /dev/null +++ b/.env.example.php @@ -0,0 +1,11 @@ + 'mysql', + 'DB_HOST' => '127.0.0.1', + 'DB_USERNAME' => 'root', + 'DB_PASSWORD' => '', + 'DB_DATABASE' => 'test', +]; diff --git a/.gitignore b/.gitignore index f55ac7b..6de8016 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ composer.lock package-lock.json vendor/ test/ +.env.php # OS Generated .DS_Store* diff --git a/src/Db.php b/src/Db.php index 7c1bc01..40fb133 100644 --- a/src/Db.php +++ b/src/Db.php @@ -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 diff --git a/tests/mysql/connect.test.php b/tests/mysql/connect.test.php index 4131811..b4b290a 100644 --- a/tests/mysql/connect.test.php +++ b/tests/mysql/connect.test.php @@ -1,7 +1,13 @@ 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(); $success = true; @@ -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') @@ -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') diff --git a/tests/mysql/leaf-builder.test.php b/tests/mysql/leaf-builder.test.php index 853ef99..a066995 100644 --- a/tests/mysql/leaf-builder.test.php +++ b/tests/mysql/leaf-builder.test.php @@ -1,8 +1,32 @@ exec($query); + $pdo = null; +}); + 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']); $users = $db->select('test')->orderBy("created_at", "asc")->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(); @@ -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(); @@ -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();