|
| 1 | +# PD\SQL |
| 2 | + |
| 3 | +> A fluent SQL query builder for PHP that provides an elegant and safe way to build and execute database queries. Built on top of PDO, this library offers a chainable API that makes database interactions more intuitive and maintainable. |
| 4 | +
|
| 5 | + |
| 6 | +<br> |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Features |
| 11 | +- Fluent interface for building SQL queries |
| 12 | +- Safe parameter binding to prevent SQL injection |
| 13 | +- Support for complex JOIN operations (INNER, LEFT, RIGHT) |
| 14 | +- Dynamic WHERE clause construction |
| 15 | +- Ordering and pagination support |
| 16 | +- Transaction handling |
| 17 | +- Query execution time monitoring |
| 18 | +- Environment-based configuration |
| 19 | +- Automatic connection management |
| 20 | + |
| 21 | +## functions |
| 22 | + |
| 23 | +- Table selection with `table()` |
| 24 | +- Custom field selection with `select()` |
| 25 | +- Conditional filtering with `where()` |
| 26 | +- Join operations with `join()`, `left_join()`, `right_join()` |
| 27 | +- Result ordering with `order_by()` |
| 28 | +- Pagination with `limit()` and `offset()` |
| 29 | +- Record creation with `insertGetId()` |
| 30 | +- Record updates with `update()` |
| 31 | +- Total row count with `total()` |
| 32 | +- Raw query execution with `query()` for complex custom queries |
| 33 | + |
| 34 | +## How to Use |
| 35 | + |
| 36 | +### Install |
| 37 | + |
| 38 | +```SHELL |
| 39 | +composer require pardnchiu/sql |
| 40 | +``` |
| 41 | + |
| 42 | +### Use |
| 43 | + |
| 44 | +```PHP |
| 45 | +<?php |
| 46 | + |
| 47 | +use PD\SQL; |
| 48 | + |
| 49 | +$result_user_0 = SQL::table('users') |
| 50 | + ->where('status', 'active') |
| 51 | + ->where('age', '>', 18) |
| 52 | + ->get(); |
| 53 | + |
| 54 | +$result_order = SQL::table('orders') |
| 55 | + ->select('orders.*', 'users.name') |
| 56 | + ->join('users', 'orders.user_id', 'users.id') |
| 57 | + ->where('orders.status', 'pending') |
| 58 | + ->get(); |
| 59 | + |
| 60 | +$result_product = SQL::table('products') |
| 61 | + ->total() |
| 62 | + ->limit(10) |
| 63 | + ->offset(0) |
| 64 | + ->order_by('created_at', 'DESC') |
| 65 | + ->get(); |
| 66 | + |
| 67 | +$result_user_1 = SQL::query( |
| 68 | + "SELECT * FROM users WHERE status = ? AND role = ?", |
| 69 | + ['active', 'admin'] |
| 70 | +); |
| 71 | +``` |
| 72 | + |
| 73 | +## License |
| 74 | + |
| 75 | +This source code project is licensed under the [MIT](https://github.com/pardnchiu/PHPSQL/blob/main/LICENSE) license. |
| 76 | + |
| 77 | +## Creator |
| 78 | + |
| 79 | +<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;"> |
| 80 | + |
| 81 | +<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4> |
| 82 | + |
| 83 | +<a href="mailto:dev@pardn.io" target="_blank"> |
| 84 | + <img src="https://pardn.io/image/email.svg" width="48" height="48"> |
| 85 | +</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank"> |
| 86 | + <img src="https://pardn.io/image/linkedin.svg" width="48" height="48"> |
| 87 | +</a> |
| 88 | + |
| 89 | +*** |
| 90 | + |
| 91 | +©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io) |
0 commit comments