Skip to content

Commit

Permalink
Merge pull request #12 from moitran/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
moitran authored Dec 18, 2018
2 parents 8d9c792 + e72dcb3 commit 32d1ab5
Show file tree
Hide file tree
Showing 16 changed files with 786 additions and 430 deletions.
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: php
php:
- '7.1'
- '7.2'
before_script:
- travis_retry composer self-update
- travis_retry composer install --no-interaction --prefer-source --dev

script:
- vendor/bin/phpunit --coverage-clover=coverage.xml
- vendor/bin/phpcs -p --standard=PSR2 src/

after_success:
- travis_retry bash <(curl -s https://codecov.io/bash)
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Presto Query Builder
===================

This package provides a set of classes and methods that is able to programmatically build presto queries.

[![Latest Stable Version](https://poser.pugx.org/moitran/package-presto-query-builder-php/v/stable)](https://packagist.org/packages/moitran/package-presto-query-builder-php)
[![Latest Unstable Version](https://poser.pugx.org/moitran/package-presto-query-builder-php/v/unstable)](https://packagist.org/packages/moitran/package-presto-query-builder-php)
[![Build Status](https://travis-ci.org/moitran/package-presto-query-builder-php.svg?branch=master)](https://travis-ci.org/moitran/package-presto-query-builder-php)
[![codecov](https://codecov.io/gh/moitran/package-presto-query-builder-php/branch/master/graphs/badge.svg)](https://codecov.io/gh/moitran/package-presto-query-builder-php)
[![License](https://poser.pugx.org/moitran/package-presto-query-builder-php/license)](https://packagist.org/packages/moitran/package-presto-query-builder-php)
[![composer.lock](https://poser.pugx.org/moitran/package-presto-query-builder-php/composerlock)](https://packagist.org/packages/moitran/package-presto-query-builder-php)

- [Installation](#installation)
- [Usage](#usage)
- [Testing](#testing)
Expand Down Expand Up @@ -137,4 +146,4 @@ Contributing
License
-------

[MIT](https://github.com/moitran/package-presto-query-builder-php/blob/master/LICENSE)
This package is under [MIT License](https://github.com/moitran/package-presto-query-builder-php/blob/master/LICENSE)
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "moitran/package-presto-query-builder-php",
"description": "Build presto query string",
"type": "package",
"license": "MIT",
"authors": [
{
Expand All @@ -12,7 +13,10 @@
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^7.3"
"phpunit/phpunit": "^7.3",
"symfony/var-dumper": "^4.2",
"phpmd/phpmd": "^2.6",
"squizlabs/php_codesniffer": "^3.3"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
verbose="true">
<testsuites>
<testsuite name="MoiTran-Presto-Query-Builder Test Suite">
<directory>tests</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private function removeSpecialCharsFromStr($str)

$str = str_replace(
['\\', "\0", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'],
['\\\\', '\\0', '\\n', '\\r', "''", '\\"', '\\Z'],
$str
);

Expand Down
80 changes: 78 additions & 2 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Query extends Base
* @var bool
*/
private $isFirstUnionAll = true;
/**
* @var bool
*/
private $isFirstHaving = true;

const SORT_DESC = 'DESC';
const SORT_ASC = 'ASC';
Expand Down Expand Up @@ -195,8 +199,12 @@ public function whereAndGroup(WhereGroup $whereGroup)
{
$whereStr = $whereGroup->getWhereConditions();

if ($whereStr == '') {
return $this;
}

if ($this->isFirstWhere) {
$whereStr = sprintf(' WHERE AND (%s)', $whereStr);
$whereStr = sprintf(' WHERE (%s)', $whereStr);
$this->isFirstWhere = false;
} else {
$whereStr = sprintf(' AND (%s)', $whereStr);
Expand All @@ -216,8 +224,12 @@ public function whereOrGroup(WhereGroup $whereGroup)
{
$whereStr = $whereGroup->getWhereConditions();

if ($whereStr == '') {
return $this;
}

if ($this->isFirstWhere) {
$whereStr = sprintf(' WHERE OR (%s)', $whereStr);
$whereStr = sprintf(' WHERE (%s)', $whereStr);
$this->isFirstWhere = false;
} else {
$whereStr = sprintf(' OR (%s)', $whereStr);
Expand All @@ -228,6 +240,32 @@ public function whereOrGroup(WhereGroup $whereGroup)
return $this;
}

/**
* @param $column
* @param $condition
* @param $value
*
* @return $this
* @throws InvalidArgumentException
*/
public function andHaving($column, $condition, $value)
{
return $this->having($column, $condition, $this->removeSpecialChars($value), 'AND');
}

/**
* @param $column
* @param $condition
* @param $value
*
* @return $this
* @throws InvalidArgumentException
*/
public function orHaving($column, $condition, $value)
{
return $this->having($column, $condition, $this->removeSpecialChars($value), 'OR');
}

/**
* @param Query $query
*
Expand Down Expand Up @@ -349,6 +387,44 @@ private function where($column, $condition, $value, $whereType)
return $this;
}

/**
* @param $column
* @param $condition
* @param $value
* @param $havingType
*
* @return $this
* @throws InvalidArgumentException
*/
private function having($column, $condition, $value, $havingType)
{
if (!is_string($column) || !is_string($condition)) {
throw new InvalidArgumentException('$column and $condition argument must be a string');
}

if (!(is_string($value) || is_array($value) || is_null($value) || is_numeric($value))) {
throw new InvalidArgumentException('$value argument must be a string, a numeric or an array');
}

if (is_null($value)) {
$valueStr = 'NULL';
} elseif (is_numeric($value)) {
$valueStr = $value;
} else {
$valueStr = is_string($value) ? sprintf("'%s'", $value) : sprintf("('%s')", implode("', '", $value));
}

if ($this->isFirstHaving) {
$havingType = 'HAVING';
$this->isFirstHaving = false;
}

$havingStr = sprintf(" %s %s %s %s", $havingType, $column, $condition, $valueStr);
$this->combineQueryStr($havingStr);

return $this;
}

/**
* @param $table
* @param $alias
Expand Down
2 changes: 1 addition & 1 deletion src/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private function where($column, $condition, $value, $whereType)
} elseif (is_numeric($value)) {
$valueStr = $value;
} else {
$valueStr = is_string($value) ? sprintf("'%s'", $value) : sprintf("('%s')", implode("','", $value));
$valueStr = is_string($value) ? sprintf("'%s'", $value) : sprintf("('%s')", implode("', '", $value));
}
$whereStr = sprintf(" %s %s %s %s", $whereType, $column, $condition, $valueStr);

Expand Down
8 changes: 8 additions & 0 deletions src/WhereGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function whereAndGroup(WhereGroup $whereGroup)
{
$whereStr = $whereGroup->getWhereConditions();

if ($whereStr == '') {
return $this;
}

$whereStr = sprintf(' AND (%s)', $whereStr);

if ($this->isFirstCondition) {
Expand All @@ -96,6 +100,10 @@ public function whereOrGroup(WhereGroup $whereGroup)
{
$whereStr = $whereGroup->getWhereConditions();

if ($whereStr == '') {
return $this;
}

$whereStr = sprintf(' OR (%s)', $whereStr);

if ($this->isFirstCondition) {
Expand Down
30 changes: 2 additions & 28 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,15 @@
namespace MoiTran\PrestoQueryBuilder\Tests;

use MoiTran\PrestoQueryBuilder\Base;
use MoiTran\PrestoQueryBuilder\Tests\Provider\BaseTestProvider;

/**
* Class BaseTest
* @package MoiTran\PrestoQueryBuilder\Tests
*/
class BaseTest extends TestCases
{
/**
* @return array
*/
public function providerRemoveSpecialChars()
{
return [
'numeric' => [
'input' => 111,
'expected' => 111,
],
'special-char' => [
'input' => '"test"',
'expected' => '\"test\"',
],
'percent-char' => [
'input' => '%test%',
'expected' => '%test%',
],
'array-value' => [
'input' => [1, '"test"', "\n", "\r", "%test"],
'expected' => ['1', '\"test\"', '\\n', '\\r', '%test'],
],
'not-accept-value-type' => [
'input' => new \stdClass(),
'expected' => new \stdClass(),
],
];
}
use BaseTestProvider;

/**
* @param $input
Expand Down
39 changes: 39 additions & 0 deletions tests/Provider/BaseTestProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace MoiTran\PrestoQueryBuilder\Tests\Provider;

/**
* Trait BaseTestProvider
* @package MoiTran\PrestoQueryBuilder\Tests\Provider
*/
trait BaseTestProvider
{
/**
* @return array
*/
public function providerRemoveSpecialChars()
{
return [
'numeric' => [
'input' => 111,
'expected' => 111,
],
'special-char' => [
'input' => '"test"',
'expected' => '\"test\"',
],
'percent-char' => [
'input' => '%test%',
'expected' => '%test%',
],
'array-value' => [
'input' => [1, '"test"', "\n", "\r", "%test"],
'expected' => ['1', '\"test\"', '\\n', '\\r', '%test'],
],
'not-accept-value-type' => [
'input' => new \stdClass(),
'expected' => new \stdClass(),
],
];
}
}
Loading

0 comments on commit 32d1ab5

Please sign in to comment.