Skip to content

moitran/package-presto-query-builder-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Presto Query Builder

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

Latest Stable Version Latest Unstable Version Build Status codecov License composer.lock

Installation

$ composer require moitran/package-presto-query-builder

Usage

Some examples:

  1.     $query = new MoiTran\PrestoQueryBuilder\Query();
        $queryStr = $query->select('*')
           ->from('table1')
           ->whereAnd('col1', 'IS', NULL)
           ->whereAnd('col2', '=', 'value2')
           ->whereOr('col3', 'LIKE', '%test%')
           ->orderBy('col1', 'DESC')
           ->orderBy('col2', 'ASC')
           ->limit(10)
           ->getQueryStr();

    Output:

       SELECT * FROM (table1) WHERE col1 IS NULL AND col2 = 'value2' OR col3 LIKE '%test%' ORDER BY col1 DESC, col2 ASC LIMIT 10
    
  2.     $whereAndGroup = new MoiTran\PrestoQueryBuilder\WhereGroup();
        $whereAndGroup->whereAnd('col4', '!=', 'value4');
        $whereAndGroup->whereOr('col5', 'NOT LIKE', 'value5%');
    
        $whereOrGroup = new MoiTran\PrestoQueryBuilder\WhereGroup();
        $whereOrGroup->whereAnd('col6', '=', 'value6');
        $whereOrGroup->whereOr('col7', 'IN', ['value7', 'value8']);
    
        $query = new MoiTran\PrestoQueryBuilder\Query();
        $queryStr = $query->select([
            'col1',
            'col2' => 'colalias2',
            'col3' => 'colalias3'
        ])
            ->from('table1')
            ->whereAnd('col1', 'IS', NULL)
            ->whereAnd('col2', '=', 'value2')
            ->whereOr('col3', 'LIKE', '%test%')
            ->whereAndGroup($whereAndGroup)
            ->whereOrGroup($whereOrGroup)
            ->groupBy(['col1', 'col2', 'col3'])
            ->orderBy('col1', 'DESC')
            ->orderBy('col2', 'ASC')
            ->limit(10)
            ->getQueryStr();

    Output:

       SELECT col1,
              col2 AS colalias2,
              col3 AS colalias3
       FROM (table1)
       WHERE col1 IS NULL
         AND col2 = 'value2'
         OR col3 LIKE '%test%'
         AND (col4 != 'value4'
              OR col5 NOT LIKE 'value5%')
         OR (col6 = 'value6'
             OR col7 IN ('value7',
                         'value8'))
       GROUP BY col1,
                col2,
                col3
       ORDER BY col1 DESC,
                col2 ASC
       LIMIT 10
  3.     $query = new MoiTran\PrestoQueryBuilder\Query();
        $queryStr = $query->select([
            'a.col1' => 'acol1',
            'a.col2' => 'acol2',
            'b.col1' => 'bcol1',
            'b.col2' => 'bcol2',
        ])
            ->from('table1', 'a')
            ->leftJoin('table2', 'b')
            ->on('a.id', '=', 'b.a_id')
            ->whereAnd('a.col1', '=', 'value1')
            ->limit(10)
            ->getQueryStr();

    Output:

       SELECT a.col1 AS acol1,
              a.col2 AS acol2,
              b.col1 AS bcol1,
              b.col2 AS bcol2
       FROM (table1) AS a
       LEFT JOIN (table2) AS b ON a.id = b.a_id
       WHERE a.col1 = 'value1'
       LIMIT 10

Testing

$ vendor/bin/phpunit --coverage-html=cov/ tests/

Contributing

License

This package is under MIT License