Skip to content

SQL Query Collection & Examples A comprehensive collection of SQL queries, examples, and best practices for database management. Perfect for developers, data analysts, and SQL learners.

License

Notifications You must be signed in to change notification settings

vigihdev/sql-query

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ VigihDev SQL Query Builder

PHP Version License Tests Coverage

A powerful, fluent SQL Query Builder for PHP with intuitive syntax and comprehensive condition handling.

✨ Features

  • πŸ”₯ Fluent Interface - Chain methods for readable query building
  • 🎯 Type Safety - Full PHP 8+ type declarations
  • 🧩 Flexible Conditions - Support for complex WHERE clauses
  • πŸ”§ Factory Pattern - Clean condition object creation
  • πŸ“ Well Tested - 41 test cases with 66 assertions
  • ⚑ Performance - Optimized for speed and memory usage

πŸš€ Quick Start

Installation

composer require vigihdev/sql-query

Basic Usage

use SqlQuery\Query;

$query = new Query();

// Simple SELECT query
$sql = $query
    ->from('users')
    ->where(['active', '=', 1])
    ->where(['age', '>', 18])
    ->orderBy(['name' => 'ASC'])
    ->limit(10)
    ->toSql();

echo $sql;
// Output: SELECT * FROM users WHERE active = ? AND age > ? ORDER BY name ASC LIMIT 10

πŸ“– Documentation

Basic Queries

// SELECT with specific columns
$query->select('id', 'name', 'email')
      ->from('users');

// DISTINCT queries
$query->distinct()
      ->from('categories');

// LIMIT and OFFSET
$query->from('posts')
      ->limit(20)
      ->offset(40);

WHERE Conditions

// Simple conditions
$query->where(['name', '=', 'John']);
$query->where(['age', '>', 25]);

// Multiple conditions (AND)
$query->where(['status', '=', 'active'])
      ->where(['verified', '=', true]);

// OR conditions
$query->where(['role', '=', 'admin'])
      ->orWhere(['role', '=', 'moderator']);

// IN conditions
$query->where(['status', 'IN', ['active', 'pending']]);

// LIKE conditions
$query->where(['name', 'LIKE', '%john%']);

// NULL conditions
$query->where(['deleted_at', 'IS NULL']);

JOINs

// INNER JOIN
$query->from('users u')
      ->innerJoin('posts p', 'u.id', '=', 'p.user_id');

// LEFT JOIN
$query->from('users u')
      ->leftJoin('profiles pr', 'u.id', '=', 'pr.user_id');

// Multiple JOINs
$query->from('users u')
      ->innerJoin('posts p', 'u.id', '=', 'p.user_id')
      ->leftJoin('categories c', 'p.category_id', '=', 'c.id');

Sorting and Grouping

// ORDER BY
$query->orderBy(['created_at' => 'DESC', 'name' => 'ASC']);

// GROUP BY
$query->from('orders')
      ->groupBy('customer_id');

Complex Conditions

// Hash conditions (key-value pairs)
$query->where([
    'status' => 'active',
    'age' => 25,
    'city' => 'Jakarta'
]);

// Composite conditions with AND/OR
$query->where(['AND', 
    ['name', '=', 'John'],
    ['age', '>', 18]
]);

πŸ—οΈ Architecture

Core Components

  • Query - Main query builder class
  • Conditions - Various condition types (Simple, Composite, In, Like, etc.)
  • Factory - Creates condition objects based on operators
  • Parser - Parses different condition formats
  • Utils - Helper utilities for type handling

Condition Types

Class Purpose Example
SimpleCondition Basic comparisons name = 'John'
CompositeCondition AND/OR logic (age > 18 AND status = 'active')
InCondition IN operations id IN (1,2,3)
LikeCondition Pattern matching name LIKE '%john%'
IsCondition NULL checks deleted_at IS NULL

πŸ§ͺ Testing

Run the comprehensive test suite:

# Run all tests
composer test

# Run with coverage
./vendor/bin/phpunit --coverage-html coverage/

Test Statistics:

  • βœ… 41 Test Cases
  • βœ… 66 Assertions
  • βœ… 100% Pass Rate
  • 🎯 Full Coverage of Core Features

πŸ“ Project Structure

src/
β”œβ”€β”€ Query.php              # Main query builder
β”œβ”€β”€ AbstractQuery.php      # Base query class
β”œβ”€β”€ Conditions/           # Condition classes
β”‚   β”œβ”€β”€ SimpleCondition.php
β”‚   β”œβ”€β”€ CompositeCondition.php
β”‚   └── ...
β”œβ”€β”€ Factories/           # Factory classes
β”‚   └── ConditionFactory.php
β”œβ”€β”€ Parsers/            # Parser classes
β”‚   └── ConditionParser.php
└── Utils/              # Utility classes
    └── ParseValueType.php

tests/
β”œβ”€β”€ QueryTest.php
β”œβ”€β”€ Conditions/         # Condition tests
β”œβ”€β”€ Factories/         # Factory tests
β”œβ”€β”€ Parsers/          # Parser tests
β”œβ”€β”€ Utils/            # Utility tests
└── Integration/      # Integration tests

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Write tests for your changes
  4. Ensure all tests pass (composer test)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Vigih Dev

πŸ™ Acknowledgments

  • Built with ❀️ for the PHP community
  • Inspired by modern query builders
  • Thanks to all contributors and testers

⭐ Star this repo if you find it helpful!

Report Bug β€’ Request Feature β€’ Documentation

About

SQL Query Collection & Examples A comprehensive collection of SQL queries, examples, and best practices for database management. Perfect for developers, data analysts, and SQL learners.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages