QueryLite is a fast, lightweight, and easy-to-use database query builder that simplifies SQL interactions using native PDO. It provides an intuitive model-based API while ensuring raw SQL performance.
Install the package via Composer:
composer require sukhrobnurali/querylite
Extend QueryLite
to define your own database model.
class UserModel extends QueryLite
{
const TABLE = 'users';
protected function create($name, $email) {
$this->insert(['name' => $name, 'email' => $email]);
}
}
Before using QueryLite, set up a PDO connection:
$connection = new PDO(
"mysql:host=127.0.0.1;dbname=your_database;charset=utf8mb4",
"your_user",
"your_password",
[
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
);
🔹 Select Data
$userModel = new UserModel($connection);
$users = $userModel->select(['id', 'name'])
->where('age', '>', 18)
->orderBy('name ASC')
->limit(100)
->getAllRows();
🔹 Insert Data
$userModel->insert([
'name' => 'John Doe',
'email' => '[email protected]'
]);
🔹 Update Data
$userModel->update(['email' => '[email protected]'])
->whereEqual('id', 1);
🔹 Delete Data
$userModel->delete()
->whereEqual('id', 5);
Method | Description | Example |
---|---|---|
select($columns) |
Select specific columns from a table | $userModel->select(['id', 'name']); |
where($column, $operator, $value) |
Add a WHERE condition | $userModel->where('status', '=', 'active'); |
orderBy($column, $direction) |
Add sorting to queries | $userModel->orderBy('id', 'DESC'); |
limit($count) |
Limit query results | $userModel->limit(50); |
insert($data) |
Insert new records | $userModel->insert(['name' => 'Jane']); |
update($data) |
Update records | $userModel->update(['email' => '[email protected]']); |
delete() |
Delete records | $userModel->delete()->whereEqual('id', 1); |
getAllRows() |
Fetch all results | $userModel->getAllRows(); |
To ensure QueryLite functions correctly, run:
# Run all tests using Composer
composer test
✅ Minimal & Fast – Built on native PDO for raw SQL execution.
✅ Chainable Query Methods – Clean and readable query building.
✅ Supports MySQL & SQLite – Works seamlessly with popular databases.
✅ No Dependencies – Fully self-contained, no external libraries needed.
✅ Customizable & Extendable – Easily define models and extend functionality.
Feel free to contribute or provide feedback to improve QueryLite. If you encounter any issues, submit a bug report or suggest new features.
📩 Contact: Open an issue on GitHub or email [email protected]