All Projects → ScientaNL → Doctrinejsonfunctions

ScientaNL / Doctrinejsonfunctions

Licence: mit
Doctrine DQL functions for SQL JSON data type

Projects that are alternatives of or similar to Doctrinejsonfunctions

Typeorm
ORM for TypeScript and JavaScript (ES7, ES6, ES5). Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
Stars: ✭ 26,559 (+8072%)
Mutual labels:  orm, mysql, postgresql, sqlite, mariadb
Qxorm
QxOrm library - C++ Qt ORM (Object Relational Mapping) and ODM (Object Document Mapper) library - Official repository
Stars: ✭ 176 (-45.85%)
Mutual labels:  orm, mysql, postgresql, sqlite, mariadb
Prisma
Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite & MongoDB (Preview)
Stars: ✭ 18,168 (+5490.15%)
Mutual labels:  orm, mysql, postgresql, mariadb, sqlite
Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+580.31%)
Mutual labels:  orm, mysql, postgresql, sqlite, mariadb
Denodb
MySQL, SQLite, MariaDB, PostgreSQL and MongoDB ORM for Deno
Stars: ✭ 498 (+53.23%)
Mutual labels:  orm, mysql, postgresql, sqlite, mariadb
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+7722.15%)
Mutual labels:  orm, mysql, postgresql, sqlite, mariadb
Elefant
Elefant, the refreshingly simple PHP CMS and web framework.
Stars: ✭ 188 (-42.15%)
Mutual labels:  orm, mysql, postgresql, sqlite
Ebean
Ebean ORM
Stars: ✭ 1,172 (+260.62%)
Mutual labels:  orm, mysql, sqlite, mariadb
Pony
Pony Object Relational Mapper
Stars: ✭ 2,762 (+749.85%)
Mutual labels:  orm, mysql, postgresql, sqlite
Xorm
Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
Stars: ✭ 6,464 (+1888.92%)
Mutual labels:  orm, mysql, postgresql, sqlite
Chloe
A lightweight and high-performance Object/Relational Mapping(ORM) library for .NET --C#
Stars: ✭ 1,248 (+284%)
Mutual labels:  orm, mysql, postgresql, sqlite
Node Orm2
Object Relational Mapping
Stars: ✭ 3,063 (+842.46%)
Mutual labels:  orm, mysql, postgresql, sqlite
Hunt Entity
An object-relational mapping (ORM) framework for D language (Similar to JPA / Doctrine), support PostgreSQL and MySQL.
Stars: ✭ 51 (-84.31%)
Mutual labels:  orm, mysql, postgresql, sqlite
Pop
A Tasty Treat For All Your Database Needs
Stars: ✭ 1,045 (+221.54%)
Mutual labels:  orm, mysql, postgresql, sqlite
Nut
Advanced, Powerful and easy to use ORM for Qt
Stars: ✭ 181 (-44.31%)
Mutual labels:  orm, mysql, postgresql, sqlite
Diesel
A safe, extensible ORM and Query Builder for Rust
Stars: ✭ 7,702 (+2269.85%)
Mutual labels:  orm, mysql, postgresql, sqlite
Xo
Command line tool to generate idiomatic Go code for SQL databases supporting PostgreSQL, MySQL, SQLite, Oracle, and Microsoft SQL Server
Stars: ✭ 2,974 (+815.08%)
Mutual labels:  orm, mysql, postgresql, sqlite
Tortoise Orm
Familiar asyncio ORM for python, built with relations in mind
Stars: ✭ 2,558 (+687.08%)
Mutual labels:  orm, mysql, postgresql, sqlite
Mikro Orm
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, MariaDB, PostgreSQL and SQLite databases.
Stars: ✭ 3,874 (+1092%)
Mutual labels:  orm, mysql, postgresql, sqlite
Bookshelf
A simple Node.js ORM for PostgreSQL, MySQL and SQLite3 built on top of Knex.js
Stars: ✭ 6,252 (+1823.69%)
Mutual labels:  orm, mysql, postgresql, sqlite

Latest Stable Version Total Downloads License

DoctrineJsonFunctions

A set of extensions to Doctrine 2 that add support for json query functions. +Functions are available for MySQL, MariaDb and PostgreSQL.

DB Functions
MySQL JSON_APPEND, JSON_ARRAY, JSON_ARRAY_APPEND, JSON_ARRAY_INSERT, JSON_CONTAINS, JSON_CONTAINS_PATH, JSON_DEPTH, JSON_EXTRACT, JSON_INSERT, JSON_KEYS, JSON_LENGTH, JSON_MERGE, JSON_MERGE_PATCH, JSON_OBJECT, JSON_PRETTY, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SEARCH, JSON_SET, JSON_TYPE, JSON_UNQUOTE, JSON_VALID
PostgreSQL JSON_EXTRACT_PATH, GT, GT_GT, SHARP_GT, SHARP_GT_GT
MariaDb JSON_VALUE, JSON_EXISTS
SQLite JSON, JSON_ARRAY, JSON_ARRAY_LENGTH, JSON_EXTRACT, JSON_GROUP_ARRAY, JSON_GROUP_OBJECT, JSON_INSERT, JSON_OBJECT, JSON_PATCH, JSON_QUOTE, JSON_REMOVE, JSON_REPLACE, JSON_SET, JSON_TYPE, JSON_VALID

Table of Contents

Changelog

Changes per release are documented with each github release. You can find an overview here: https://github.com/ScientaNL/DoctrineJsonFunctions/releases

Installation

The recommended way to install DoctrineJsonFunctions is through Composer. Add the following dependency to your composer.json

{
	"require": {
		"scienta/doctrine-json-functions": "~4.3"
	}
}

Alternatively, you can download the source code as a file and extract it.

Testing

This repository uses phpunit for testing purposes. If you just want to run the tests you can use the docker composer image to install and run phpunit. There is a docker-compose file with the correct mount but if you want to use just docker you can run this:

php7

docker run -it -v ${PWD}:/app scienta/php-composer:php7 /bin/bash -c "composer install && ./vendor/bin/phpunit"

php8

docker run -it -v ${PWD}:/app scienta/php-composer:php8 /bin/bash -c "composer install && ./vendor/bin/phpunit"

Functions Registration

Doctrine 2 ORM

Doctrine 2 documentation: "DQL User Defined Functions"

<?php

use Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql as DqlFunctions;

$config = new \Doctrine\ORM\Configuration();
$config->addCustomStringFunction(DqlFunctions\JsonExtract::FUNCTION_NAME, DqlFunctions\JsonExtract::class);
$config->addCustomStringFunction(DqlFunctions\JsonSearch::FUNCTION_NAME, DqlFunctions\JsonSearch::class);

$em = EntityManager::create($dbParams, $config);
$queryBuilder = $em->createQueryBuilder();

Symfony 2 & 3 with Doctrine bundle

Symfony documentation: "DoctrineBundle Configuration"

# app/config/config.yml
doctrine:
    orm:
        entity_managers:
            some_em: # usually also "default"
                dql:
                    string_functions:
                        JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                        JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch

Symfony 5 with Doctrine bundle

Symfony documentation: "DoctrineBundle Configuration"

# config/packages/doctrine.yaml
doctrine:
    orm:
        dql:
            string_functions:
                JSON_EXTRACT: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonExtract
                JSON_SEARCH: Scienta\DoctrineJsonFunctions\Query\AST\Functions\Mysql\JsonSearch

Usage

Mind the comparison when creating the expression and escape the parameters to be valid JSON.

Using Mysql 5.7+ JSON operators

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_CONTAINS(c.attributes, :certificates, '$.certificates') = 1");

$result = $q->execute(array(
  'certificates' => '"BIO"',
));

Using PostgreSQL 9.3+ JSON operators

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_GET_TEXT(c.attributes, 'gender') = :gender");

 $result = $q->execute(array(
    'gender' => 'male',
 ));

Using SQLite JSON operators

$q = $queryBuilder
  ->select('c')
  ->from('Customer', 'c')
  ->where("JSON_EXTRACT(c.attributes, '$.gender') = :gender");

 $result = $q->execute();

DQL Functions

The library provides this set of DQL functions.

Mysql 5.7+ JSON operators

MariaDb 10.2.3 JSON operators

  • JSON_VALUE(json_doc, path)
    • Returns the scalar specified by the path. Returns NULL if there is no match.
  • JSON_EXISTS(json_doc, path)
    • Determines whether a specified JSON value exists in the given data. Returns 1 if found, 0 if not, or NULL if any of the inputs were NULL.

PostgreSQL 9.3+ JSON operators

Basic support for JSON operators is implemented. This works even with Doctrine\DBAL v2.5. Official documentation of JSON operators.

  • JSON_GET(jsondoc, path)

    • expands to jsondoc->path in case of numeric path (use with JSON arrays)
    • expands to jsondoc->'path' in case of non-numeric path (use with JSON objects)
  • JSON_GET_TEXT(jsondoc, path)

    • expands to jsondoc->>path in case of numeric path (use with JSON arrays)
    • expands to jsondoc->>'path' in case of non-numeric path (use with JSON objects)
  • JSON_GET_PATH(jsondoc, path)

    • expands to jsondoc#>'path'
  • JSON_GET_PATH_TEXT(jsondoc, path)

    • expands to jsondoc#>>'path'

Please note that chaining of JSON operators is not supported.

SQLite JSON1 Extension operators

Support for all the scalar and aggregare functions as seen in the JSON1 Extension documentation.

Scalar functions

  • JSON(json)
    • Verifies that its argument is a valid JSON string and returns a minified version of that JSON string.
  • JSON_ARRAY([val[, val] ...])
    • Accepts zero or more arguments and returns a well-formed JSON array that is composed from those arguments.
  • JSON_ARRAY_LENGTH(json[, path])
    • Returns the number of elements in the JSON array json, or 0 if json is some kind of JSON value other than an array.
  • JSON_EXTRACT(json, path[, path ], ...)
    • Extracts and returns one or more values from the well-formed JSON.
  • JSON_INSERT(json[, path, value],...)
    • Given zero or more sets of paths and values, it inserts (without overwriting) each value at its corresponding path of the json.
  • JSON_OBJECT(label, value[, label, value], ...)
    • Accepts zero or more pairs of arguments and returns a well-formed JSON object that is composed from those arguments.
  • JSON_PATCH(target, patch)
    • Applies a patch to target.
  • JSON_QUOTE(value)
    • Converts the SQL value (a number or a string) into its corresponding JSON representation.
  • JSON_REMOVE(json[, path], ...)
    • Removes the values at each given path.
  • JSON_REPLACE(json[, path, value],...)
    • Given zero or more sets of paths and values, it overwrites each value at its corresponding path of the json.
  • JSON_SET(json[, path, value],...)
    • Given zero or more sets of paths and values, it inserts or overwrites each value at its corresponding path of the json.
  • JSON_TYPE(json[, path])
    • Returns the type of the outermost element of json or of the value at path.
  • JSON_VALID(json)
    • Returns 1 if the argument json is well-formed JSON or 0 otherwise.

Aggregate functions

  • JSON_GROUP_ARRAY(value)
    • Returns a JSON array comprised of all value in the aggregation
  • JSON_GROUP_OBJECT(name, value)
    • Returns a JSON object comprised of all name/value pairs in the aggregation.

Extendability and Database Support

Architecture

Platform function classes naming rule is:

Scienta\DoctrineJsonFunctions\Query\AST\Functions\$platformName\$functionName

Adding a new platform

To add support of new platform you just need to create new folder Scienta\DoctrineJsonFunctions\Query\AST\Functions\$platformName and implement required function there according to naming rules

Adding a new function

If you want to add new function to this library feel free to fork it and create pull request with your implementation. Please, remember to update documentation with your new functions.

See also

dunglas/doctrine-json-odm: Serialize / deserialize plain old PHP objects into JSON columns.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].