|
| 1 | +# PD\SQL |
| 2 | + |
| 3 | +> PD\SQL 是一個基於 PDO 的 SQL 查詢建構器,為 PHP 提供優雅且安全的方式來建立和執行資料庫查詢。 |
| 4 | +
|
| 5 | + |
| 6 | +<br> |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## 功能特點 |
| 11 | + |
| 12 | +- 流暢的介面用於建立 SQL 查詢 |
| 13 | +- 安全的參數綁定以防止 SQL 注入 |
| 14 | +- 支援複雜的 JOIN 操作(INNER, LEFT, RIGHT) |
| 15 | +- 動態 WHERE 子句建構 |
| 16 | +- 排序和分頁支援 |
| 17 | +- 交易處理 |
| 18 | +- 查詢執行時間監控 |
| 19 | +- 基於環境的配置 |
| 20 | +- 自動連接管理 |
| 21 | + |
| 22 | +## 可用函式 |
| 23 | + |
| 24 | +- 使用 `table()` 進行表格選擇 |
| 25 | +- 使用 `select()` 進行自定義欄位選擇 |
| 26 | +- 使用 `where()` 進行條件過濾 |
| 27 | +- 使用 `join()`、`left_join()`、`right_join()` 進行連接操作 |
| 28 | +- 使用 `order_by()` 進行結果排序 |
| 29 | +- 使用 `limit()` 和 `offset()` 進行分頁 |
| 30 | +- 使用 `insertGetId()` 進行記錄創建 |
| 31 | +- 使用 `update()` 進行記錄更新 |
| 32 | +- 使用 `total()` 獲取總行數 |
| 33 | +- 使用 `query()` 執行複雜的自定義查詢 |
| 34 | + |
| 35 | +## 使用方式 |
| 36 | + |
| 37 | +### 安裝 |
| 38 | + |
| 39 | +```shell |
| 40 | +composer require pardnchiu/sql |
| 41 | +``` |
| 42 | + |
| 43 | +```php |
| 44 | +<?php |
| 45 | + |
| 46 | +use PD\SQL; |
| 47 | + |
| 48 | +$result_user_0 = SQL::table('users') |
| 49 | + ->where("status", "active") |
| 50 | + ->where("age", ">", 18) |
| 51 | + ->get(); |
| 52 | + |
| 53 | +$result_order = SQL::table("orders") |
| 54 | + ->select("orders.*", "users.name") |
| 55 | + ->join("users", "orders.user_id", "users.id") |
| 56 | + ->where("orders.status", "pending") |
| 57 | + ->get(); |
| 58 | + |
| 59 | +$result_product = SQL::table("products") |
| 60 | + ->total() |
| 61 | + ->limit(10) |
| 62 | + ->offset(0) |
| 63 | + ->order_by("created_at", "DESC") |
| 64 | + ->get(); |
| 65 | + |
| 66 | +$result_user_1 = SQL::query( |
| 67 | + "SELECT * FROM users WHERE status = ? AND role = ?", |
| 68 | + ['active', 'admin'] |
| 69 | +); |
| 70 | + |
| 71 | +try { |
| 72 | + $result = SQL::table("users") |
| 73 | + ->where("id", 1) |
| 74 | + ->update([ |
| 75 | + "status" => "active", |
| 76 | + "updated_at" => "NOW()" |
| 77 | + ]); |
| 78 | + |
| 79 | + if (!empty($result["info"])) { |
| 80 | + echo "效能警告: " . $result["info"]; |
| 81 | + }; |
| 82 | +} catch (\PDOException $e) { |
| 83 | + echo "資料庫錯誤: " . $e->getMessage(); |
| 84 | +} catch (\Exception $e) { |
| 85 | + echo "一般錯誤: " . $e->getMessage(); |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +## 授權條款 |
| 90 | +此原始碼專案採用 [MIT](https://github.com/pardnchiu/PHP-SQL/blob/main/LICENSE) 授權。 |
| 91 | + |
| 92 | +## 創作者 |
| 93 | +<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;"> |
| 94 | +<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4> |
| 95 | +<a href="mailto:dev@pardn.io" target="_blank"> |
| 96 | + <img src="https://pardn.io/image/email.svg" width="48" height="48"> |
| 97 | +</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank"> |
| 98 | + <img src="https://pardn.io/image/linkedin.svg" width="48" height="48"> |
| 99 | +</a> |
| 100 | + |
| 101 | +--- |
| 102 | +©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io) |
0 commit comments