elephpant/light-query-builder

Light Query Builder's a simple query builder made for simple applications, but it's powerful

1.1.15 2023-09-04 14:29 UTC

This package is auto-updated.

Last update: 2024-10-04 16:35:55 UTC


README

Maintainer Source Code PHP from Packagist Latest Version Software License Build Quality Score Total Downloads

Firstly Light Query Builder's Awesome! And with it you can construct whatever you want in SQL.

Primeiramente Light Query Builder é Maravilhoso. E com ele você pode construir qualquer querye SQL que quiser.

NOTE: However Light Query Builderjust work currently with MySQL Driver, because a BETA Version. Soon it'll work with others drivers.

Highlights

  • Extremaly Easy
  • reading, writing, updating and removing data from the Database
  • Construct all your queries with this components
  • Improve the funcionalities of this component extendin it
  • It work with enviroment variables to set all settings of the database
  • Composer ready and PSR-2 compliant (Pronto para o Composer e compatível com PSR-2)
BEFORE INSTALL!

For you work with this component, is important work with a component like vlucas/dotenv for you set your enviroment variables;

DB_DRIVER="mysql"
DB_PORT="3306"
DB_HOST="your_database_host"
DB_USER="root"
DB_PASSWORD="passworddb"
DB_NAME="elephpant"

Installation

Ligh Query Builder is available via Composer:

"elephpant/light-query-builder": "*"

or run

composer require elephpant/light-query-builder

Documentation

Quick Start

<?php

require __DIR__ . "/vendor/autoload.php";

use ElePHPant\LightQueryBuilder;

$lightQB = (new LightQueryBuilder())::setTable("users")->setFetchClass(stdClass::class);

Select

$select = $lightQB->select();
//Returns 'SELECT * FROM users'

$selectWithColumns = $lightQB->select("fullname, email");
//Returns 'SELECT fullname, email FROM users';

Where

$where = $select->where("gender = :g", "g=male");
//Returns 'SELECT * FROM users WHERE gender = :g' -> working with bind param in PDO

Operators AND OR BETWEEN

$where->and("id >=2")->or("id <= 10");
//Returns 'SELECT * FROM users WHERE gender = :g AND id >= 2 OR id <= 10'

$between = $select->where("DATE(birth)")->between("'2020-03-17'", "'2020-04-01'");
//Returns 'SELECT * FROM users WHERE DATE(birth) BETWEEN '2020-03-17' AND '2020-04-01''

JOINs

$lightQB->join("fullname", "clients", "client.user=users.id", LightQueryBuilder::INNER_JOIN);
//Returns 'SELECT fullname FROM users INNER JOIN clients ON client.user=users.id'

$lightQB->join("fullname", "clients", "client.user=users.id", LightQueryBuilder::RIGHT_JOIN);
//Returns 'SELECT fullname FROM users RIGHT JOIN clients ON client.user=users.id'

//[...]

Limit and Offset

$select->limit(3)->offset(2);
//Returns 'SELECT * FROM users LIMIT 3 OFFSET 2'

Count */

$select->count();
//Returns all RowCounts of the consult

Match Against

$lightQB->match("fullname, email", "Pedro", true);
//Returns the result of alll users that match with the fullname or email with 'Pedro'.

Write your own Query

$lightQB->toQuery("
    SELECT * FROM my_table 
    WHERE id = 2
")->limit(2)->offset(1);

CRUD

Create

$create = $lightQB->create(array(...));

Featching Data (Read)

$select->get(); //Like that it'll bring only one result (first) [object]
$select->get(true); //Like that it'll bring all results [array]

Update

$update = $lightQB->update(array(...), "WHERE id = :id", "id=2");

Delete

$lightQB->delete("WHERE id = :id", "id=2");

Debugging

var_dump($lightQB->getFail(), $lightQB->getQuery());

LightQueryBuilder's Extensible

use ElePHPant\LightQueryBuilder;

/**
 * Class MyQueryBuilder
 */
class MyQueryBuilder extends LightQueryBuilder
{
    /**
     * @param string $column
     * @param string|null $condition
     * @return MyQueryBuilder
     */
    public function avg(string $column, ?string $condition): self
    {
        $select = $this->select("AVG({$column})");

        if ($condition) {
            return $select->where($condition);
        }

        return $select;
    }

    /**
     * @param string $columns
     * @param string $condition
     * @return MyQueryBuilder
     */
    public function sum(string $columns, string $condition): self
    {
        $select = $this->select("SUM({$columns})");

        if ($condition) {
            return $select->where($condition);
        }

        return $select;
    }

}

Contributing

Please see CONTRIBUTING for details.

Support

Security: If you discover any security related issues, please email sergiodanilojr@hotmail.com instead of using the issue tracker.

Se você descobrir algum problema relacionado à segurança, envie um e-mail para sergiodanilojr@hotmail.com em vez de usar o rastreador de problemas.

Thank you

Credits

License

The MIT License (MIT). Please see License File for more inflight-query-builderation.