Skip to content

Commit

Permalink
Merge pull request #3 from martin-georgiev/develop
Browse files Browse the repository at this point in the history
Release version 0.9
  • Loading branch information
martin-georgiev authored Mar 2, 2017
2 parents 9c7679a + eb89bc1 commit f0b0ef5
Show file tree
Hide file tree
Showing 44 changed files with 244 additions and 61 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: php
php:
- '5.5'
- '5.6'
- '7.0'
- '7.1'
- hhvm
- nightly
install: composer install
script:
- vendor/bin/phpunit
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/b/martin_georgiev/postgresql-for-doctrine/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/b/martin_georgiev/postgresql-for-doctrine/?branch=master)
[![Build Status](https://api.travis-ci.org/martin-georgiev/postgresql-for-doctrine.svg?branch=master)](https://api.travis-ci.org/martin-georgiev/postgresql-for-doctrine.svg?branch=master)
----
## What's this?
This package provides Doctrine2 support for some specific PostgreSql 9.4+ features:
This package provides Doctrine2 support for some specific PostgreSQL 9.4+ features:

* Support of JSONB and array datatypes for integers, TEXT and JSONB
* Implementation of most used and useful functions related to the array and JSON datatypes
* Support of JSONB and array data types for integers, TEXT and JSONB
* Implementation of the most commonly used functions when working with array and JSON data types
* Functions for basic support of text search

It can be integrated in a simple manner with Symfony, Laravel or any other framework that benefits from Doctrine2 usage.

You can easily extend package's behavior with your own array-like datatypes or other desired functions. Just follow the few steps in section **Extend It!** below.
You can easily extend package's behaviour with your own array-like data types or other desired functions. Just follow the few steps in section **Extend It!** below.

----
## How to Install?
Easiest possible way is through [Composer](https://getcomposer.org/download/)

composer require "martin-georgiev/postgresql-for-doctrine=~0.8"
composer require "martin-georgiev/postgresql-for-doctrine=~0.9"

----
## Integration with Symfony2
Expand All @@ -31,7 +33,7 @@ Easiest possible way is through [Composer](https://getcomposer.org/download/)
bigint[]: MartinGeorgiev\Doctrine\DBAL\Types\BigIntArray
text[]: MartinGeorgiev\Doctrine\DBAL\Types\TextArray

*Add mapping between DBAL and PostgreSql datatypes*
*Add mapping between DBAL and PostgreSQL data types*

# Usually part of config.yml
doctrine:
Expand All @@ -53,8 +55,8 @@ Easiest possible way is through [Composer](https://getcomposer.org/download/)


## Integration with Laravel 5
Unfortunetly Laravel still doesn't have native integration with Doctrine.
The steps below are based on [FoxxMD's fork](https://github.com/FoxxMD/laravel-doctrine) of [mitchellvanw/laravel-doctrine](https://github.com/mitchellvanw/laravel-doctrine) integration.
Unfortunately, Laravel still doesn't come with native Doctrine2 integration.
The steps below are based on [FoxxMD's fork](https://github.com/FoxxMD/laravel-doctrine) of [mitchellvanw/laravel-doctrine](https://github.com/mitchellvanw/laravel-doctrine) integration. The package also works smoothly with [Laravel Doctrine](http://www.laraveldoctrine.org/).

1) Register the functions and datatype mappings:

Expand All @@ -67,13 +69,14 @@ The steps below are based on [FoxxMD's fork](https://github.com/FoxxMD/laravel-d
'dql' => [
'string_functions' => [
// Array data types related functions
'ALL' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\All',
'ANY' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Any',
'ALL_OF' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\All', // Avoid conflict with Doctrine's ALL implementation
'ANY_OF' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Any', // Avoid conflict with Doctrine's ANY implementation
'ARRAY' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Arr',
'ARRAY_APPEND' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayAppend',
'ARE_OVERLAPING_EACH_OTHER' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayAreOverlapingEachOther',
'ARRAY_CARDINALITY' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayCardinality',
'ARRAY_CAT' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayCat',
'ARRAY_LENGTH' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayLength',
'ARRAY_PREPEND' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayPrepend',
'ARRAY_REMOVE' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayRemove',
'ARRAY_REPLACE' => 'MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\ArrayReplace',
Expand Down Expand Up @@ -246,17 +249,17 @@ The steps below are based on [FoxxMD's fork](https://github.com/FoxxMD/laravel-d
----
## Extend It!
**How to add more array-like datatypes?**
**How to add more array-like data types?**
1) Extend *MartinGeorgiev\Doctrine\DBAL\Types\AbstractTypeArray*
2) You must give the new datatype a unique within your application name. For this purpose you can use the *TYPE_NAME* constant.
2) You must give the new datatype a unique within your application name. For this purpose, you can use the *TYPE_NAME* constant.
3) Depending on your new datatype's nature you may also need to overwrite some of the following methods:
transformPostgresArrayToPHPArray() # E.g. this will be valid for postgresql's JSON datatype
transformArrayItemForPHP() # In almost every case you will need to adjust the returned method to your specifc needs
isValidArrayItemForDatabase() # It is encoraged to check that every element part of your php array is actually compatibale with your database datatype
transformPostgresArrayToPHPArray() # E.g. this will be valid for PostgreSQL's JSON datatype
transformArrayItemForPHP() # In almost every case you will need to adjust the returned method to your specific needs
isValidArrayItemForDatabase() # It is encouraged to check that every element part of your PHP array is actually compatible with your database datatype
**How to add more functions?**
Expand All @@ -279,7 +282,7 @@ The steps below are based on [FoxxMD's fork](https://github.com/FoxxMD/laravel-d
}
}

*Have in mind that you cannot use **?** as part of any function protoype with Doctrine as this will result in faulty query parsing.*
*Beware that you cannot use **?** as part of any function prototype in Doctrine as this will result in faulty query parsing.*

----
## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Abstract handling of PostgreSql array data types
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/MartinGeorgiev/Doctrine/DBAL/Types/BigIntArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql BIGINT[] data type
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/MartinGeorgiev/Doctrine/DBAL/Types/IntegerArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql INTEGER[] data type
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/MartinGeorgiev/Doctrine/DBAL/Types/Jsonb.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

/**
* Implementation of PostgreSql JSONB data type
* @see http://www.postgresql.org/docs/9.4/static/datatype-json.html
* @see https://www.postgresql.org/docs/9.4/static/datatype-json.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/MartinGeorgiev/Doctrine/DBAL/Types/JsonbArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql JSONB[] data type
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion src/MartinGeorgiev/Doctrine/DBAL/Types/SmallIntArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql SMALLINT[] data type
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
15 changes: 8 additions & 7 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/TextArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
namespace MartinGeorgiev\Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use MartinGeorgiev\Utils\DataStructure;

/**
* Implementation of PostgreSql TEXT[] data type
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.6
* @author Martin Georgiev <[email protected]>
Expand All @@ -24,7 +25,7 @@ class TextArray extends AbstractType
* @param mixed $value The value to convert.
* @param AbstractPlatform $platform The currently used database platform.
*
* @return string The database representation of the value.
* @return null|string The database representation of the value.
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
Expand All @@ -38,7 +39,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
/**
* @param array $phpTextArray
*
* @return string
* @return bool|string
*/
protected function transformToPostgresTextArray($phpTextArray)
{
Expand All @@ -48,7 +49,7 @@ protected function transformToPostgresTextArray($phpTextArray)
if (empty($phpTextArray)) {
return '{}';
}
return '{"' . join('","', $phpTextArray) . '"}';
return DataStructure::transformPHPArrayToPostgresTextArray($phpTextArray);
}

/**
Expand All @@ -57,7 +58,7 @@ protected function transformToPostgresTextArray($phpTextArray)
* @param string $value The value to convert.
* @param AbstractPlatform $platform The currently used database platform.
*
* @return array The PHP representation of the value.
* @return null|array The PHP representation of the value.
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
Expand All @@ -78,7 +79,7 @@ protected function transformFromPostgresTextArray($postgresValue)
if ($postgresValue === '{}') {
return [];
}
$trimmedPostgresValue = mb_substr($postgresValue, 2, -2);
return explode('","', $trimmedPostgresValue);

return DataStructure::transformPostgresTextArrayToPHPArray($postgresValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ALL()
* @see http://www.postgresql.org/docs/9.4/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ALL
* @see https://www.postgresql.org/docs/9.4/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ALL
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ANY()
* @see http://www.postgresql.org/docs/9.4/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ANY-SOME
* @see https://www.postgresql.org/docs/9.4/static/functions-subquery.html#FUNCTIONS-SUBQUERY-ANY-SOME
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY[]
* @see http://www.postgresql.org/docs/9.4/static/arrays.html
* @see https://www.postgresql.org/docs/9.4/static/arrays.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_APPEND()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql check if two arrays have elements in common (using &&)
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql CARDINALITY()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_CAT()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions;

/**
* Implementation of PostgreSql ARRAY_LENGTH()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.9
* @author Martin Georgiev <[email protected]>
*/
class ArrayLength extends AbstractFunction
{
protected function customiseFunction()
{
$this->setFunctionPrototype('array_length(%s, %s)');
$this->addLiteralMapping('StringPrimary');
$this->addLiteralMapping('InputParameter');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_PREPEND()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_REMOVE()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_REPLACE()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql ARRAY_TO_STRING()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql check if left side contains right side (using @>)
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

/**
* Implementation of PostgreSql GREATEST()
* @see http://www.postgresql.org/docs/9.4/static/functions-conditional.html#FUNCTIONS-GREATEST-LEAST
* @see https://www.postgresql.org/docs/9.4/static/functions-conditional.html#FUNCTIONS-GREATEST-LEAST
*
* @since 0.7
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql STRING_TO_ARRAY()
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.4
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql check if left side is contained by right side (using <@)
* @see http://www.postgresql.org/docs/9.4/static/functions-array.html
* @see https://www.postgresql.org/docs/9.4/static/functions-array.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql json field retrival, filtered by key (using ->)
* @see http://www.postgresql.org/docs/9.4/static/functions-json.html
* @see https://www.postgresql.org/docs/9.4/static/functions-json.html
*
* @since 0.1
* @author Martin Georgiev <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Implementation of PostgreSql json field retrival as integer, filtered by key (using ->> and type casting to BIGINT)
* @see http://www.postgresql.org/docs/9.4/static/functions-json.html
* @see https://www.postgresql.org/docs/9.4/static/functions-json.html
*
* @since 0.3
* @author Martin Georgiev <[email protected]>
Expand Down
Loading

0 comments on commit f0b0ef5

Please sign in to comment.