Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mkgor/humble
Lightweight, conceptual, expandable query builder
https://github.com/mkgor/humble
database database-management mysql php7 query-builder
Last synced: 7 days ago
JSON representation
Lightweight, conceptual, expandable query builder
- Host: GitHub
- URL: https://github.com/mkgor/humble
- Owner: mkgor
- License: mit
- Created: 2019-11-19T11:10:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-30T20:34:56.000Z (over 4 years ago)
- Last Synced: 2024-04-17T06:00:54.799Z (7 months ago)
- Topics: database, database-management, mysql, php7, query-builder
- Language: PHP
- Homepage:
- Size: 42 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Humble
Lightweight, expandable and easy-to-use query builder.## Contents
- [Requirments](https://github.com/mkgor/humble#requirments)
- [Installation](https://github.com/mkgor/humble#installation)
- [Quick start](https://github.com/mkgor/humble#quick-start)
- [Query building blocks](https://github.com/mkgor/humble#query-building-blocks)
- [Where, AndWhere, OrWhere](https://github.com/mkgor/humble#where-andwhere-orwhere)
- [Join](https://github.com/mkgor/humble#join)## Requirments
- PHP 7.1+
- PDO## Installation
Installtion via composer:````
composer require mkgor/humble
````## Quick start
```php
[
'db' => 'test',
'host' => '127.0.0.1',
'user' => 'root',
'password' => ''
]
]);/** After initializing, you can get any table from specified db */
/** @var Humble\Database\RepositoryInterface $user */
$user = $db->user;/** @var stdClass $someUser */
$someUser = $user->get(1);/** @var stdClass[] $administrators */
$administrators = $user->getBy([
new \Humble\Query\Where(['admin' => 1])
]);$authorsAndTheirBooks = $user->getBy([
new \Humble\Query\Join('books', ['user.id' => 'books.author_id'], \Humble\Query\Join::HUMBLE_LEFT_JOIN)
]);/** Creating new record */
$user->insert([
'name' => 'John Doe',
'admin' => 1
]);/** Updating record */
$user->update([
new \Humble\Query\Where(['name' => 'John Doe'])
], [
'name' => 'John Doe jr.'
]);/** Deleting record */
$user->delete([
new \Humble\Query\Where(['id' => 2])
]);
```## Query building blocks
You can build complicated queries by using query building blocks. Query building block is a class, which add block of
SQL code to query.Example:
```php
user;$authorsAndTheirBooks = $user->getBy([
new \Humble\Query\Join('books', ['user.id' => 'books.author_id'], \Humble\Query\Join::HUMBLE_LEFT_JOIN),
new \Humble\Query\Where(['books.views','>',1000])
]);
```That code will generate SQL
```sql
SELECT * FROM `user` LEFT JOIN `books` `books_1234` ON user.id = `books_1234`.author_id WHERE `books_1234`.views > 1000
```### Where, AndWhere, OrWhere
Adding WHERE to query
**Usage:**
```php
user;// ... WHERE `admin` = 1
$administrators = $user->getBy([
new \Humble\Query\Where(['admin' => 1])
]);// ... WHERE `admin` = 1 AND `registration_date` > '01-01-2020'
$newAdministrators = $user->getBy([
new \Humble\Query\Where(['admin' => 1],['registration_date', '>' ,'01-01-2020'])
]);// Doing the same, but it is unsafe if you are putting non-escaped values into query
$inlineWhere = $user->getBy([
new \Humble\Query\Where('admin = 1 AND registration_date > "01-01-2020"')
]);// ... WHERE `name` = 'John Doe' OR (`name` = 'Sarah Doe')
$objectInWhere = $user->getBy([
new \Humble\Query\Where(['name' => 'John Doe'], new \Humble\Query\OrWhere(['name' => 'Sarah Doe']))
]);// ... WHERE `name` = 'John Doe' OR (`name` = 'Sarah Doe' AND (`admin` = 1))
$complexWhere = $user->getBy([
new \Humble\Query\Where(['name' => 'John Doe']),
new \Humble\Query\OrWhere(['name' => 'Sarah Doe'], new \Humble\Query\AndWhere(['admin' => 1]))
]);
```### Join
Adding JOIN into your query
```php
user;$authorsAndTheirBooks = $user->getBy([
new \Humble\Query\Join('books', ['user.id' => 'books.author_id'], \Humble\Query\Join::HUMBLE_LEFT_JOIN),
]);$outerJoinFlagDemo = $user->getBy([
new \Humble\Query\Join('books', ['user.id' => 'books.author_id'], \Humble\Query\Join::HUMBLE_LEFT_JOIN, [\Humble\Query\Join::HUMBLE_OUTER_JOIN_FLAG]),
]);
```**List of all constants**
```php
\Humble\Query\Join::HUMBLE_JOIN
```
```php
\Humble\Query\Join::HUMBLE_LEFT_JOIN
```
```php
\Humble\Query\Join::HUMBLE_RIGHT_JOIN
```
```php
\Humble\Query\Join::HUMBLE_CROSS_JOIN
```
```php
\Humble\Query\Join::HUMBLE_INNER_JOIN
```
```php
\Humble\Query\Join::HUMBLE_NATURAL_JOIN
```**List of all flags**
```php
\Humble\Query\Join::HUMBLE_OUTER_JOIN_FLAG
```