effectra/sql-query

The Effectra SQL Query package.

v2.0.12 2023-11-11 20:53 UTC

This package is auto-updated.

Last update: 2024-04-11 21:40:16 UTC


README

Effectra/SqlQuery is a powerful database manipulation package for PHP.

Installation

You can install this package via Composer. Run the following command:

composer require effectra/sql-query

Usage

Query Example

use Effectra\SqlQuery\Query;


// Select statement
$query = Query::select('users')->columns(['id','email','password'])->where(['id' => 9841]);

// Print the SQL query
echo $query;

//output
//  SELECT id, email, password FROM users WHERE id = 9841 

Table Example

use Effectra\SqlQuery\Table;
use Effectra\SqlQuery\Charset;
use Effectra\SqlQuery\Driver;
use Effectra\SqlQuery\Engine;

Query::driver(Driver::MySQL);

Query::createTable('users', function (Table $table) {
    $table->autoIncrement();
    $table->username()->unique();
    $table->email()->unique();
    $table->timestamps();
})->engine(Engine::MYSQL_InnoDB)->charset(Charset::MySQL_utf8mb4);

output:

CREATE TABLE users ( 
    id BIGINT NOT NULL AUTO_INCREMENT , 
    username VARCHAR (50) NOT NULL UNIQUE CHECK (username REGEXP '^[a-zA-Z0-9_]+$'), 
    email VARCHAR (255) NOT NULL UNIQUE CHECK (email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'), 
    created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP , 
    updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP 
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4_0900_ai_ci

License

This package is open-source and available under the MIT License.