Skip to content

Commit 3411ffc

Browse files
committed
更新 READMEcomposer.json
1 parent b004fef commit 3411ffc

File tree

4 files changed

+187
-36
lines changed

4 files changed

+187
-36
lines changed

README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# PD\SQL
22

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.
3+
> PD\SQL is a PDO-based SQL query builder that provides an elegant and secure way to construct and execute database queries in PHP.
44
55
![tag](https://img.shields.io/badge/tag-PHP%20Library-bb4444)
66
![size](https://img.shields.io/github/size/pardnchiu/PHP-SQL/src/SQL.php)<br>
@@ -35,39 +35,54 @@
3535

3636
### Install
3737

38-
```SHELL
38+
```shell
3939
composer require pardnchiu/sql
4040
```
4141

42-
### Use
43-
44-
```PHP
42+
```php
4543
<?php
4644

4745
use PD\SQL;
4846

4947
$result_user_0 = SQL::table('users')
50-
->where('status', 'active')
51-
->where('age', '>', 18)
48+
->where("status", "active")
49+
->where("age", ">", 18)
5250
->get();
5351

54-
$result_order = SQL::table('orders')
55-
->select('orders.*', 'users.name')
56-
->join('users', 'orders.user_id', 'users.id')
57-
->where('orders.status', 'pending')
52+
$result_order = SQL::table("orders")
53+
->select("orders.*", "users.name")
54+
->join("users", "orders.user_id", "users.id")
55+
->where("orders.status", "pending")
5856
->get();
5957

60-
$result_product = SQL::table('products')
58+
$result_product = SQL::table("products")
6159
->total()
6260
->limit(10)
6361
->offset(0)
64-
->order_by('created_at', 'DESC')
62+
->order_by("created_at", "DESC")
6563
->get();
6664

6765
$result_user_1 = SQL::query(
6866
"SELECT * FROM users WHERE status = ? AND role = ?",
69-
['active', 'admin']
67+
["active", "admin"]
7068
);
69+
70+
try {
71+
$result = SQL::table("users")
72+
->where("id", 1)
73+
->update([
74+
"status" => "active",
75+
"updated_at" => "NOW()"
76+
]);
77+
78+
if (!empty($result["info"])) {
79+
echo "Performance: " . $result["info"];
80+
};
81+
} catch (\PDOException $e) {
82+
echo "Database Error: " . $e->getMessage();
83+
} catch (\Exception $e) {
84+
echo "General Error: " . $e->getMessage();
85+
};
7186
```
7287

7388
## License

README.zh.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# PD\SQL
2+
3+
> PD\SQL 是一個基於 PDO 的 SQL 查詢建構器,為 PHP 提供優雅且安全的方式來建立和執行資料庫查詢。
4+
5+
![tag](https://img.shields.io/badge/tag-PHP%20Library-bb4444)
6+
![size](https://img.shields.io/github/size/pardnchiu/PHP-SQL/src/SQL.php)<br>
7+
![version](https://img.shields.io/packagist/v/pardnchiu/sql)
8+
![download](https://img.shields.io/packagist/dm/pardnchiu/sql)
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)

composer.json

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,40 @@
22
"name": "pardnchiu/sql",
33
"description": "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.",
44
"type": "library",
5+
"keywords": [
6+
"php",
7+
"sql",
8+
"mysql",
9+
"database",
10+
"query-builder",
11+
"pdo",
12+
"orm",
13+
"mysql-pdo",
14+
"pardnchiu",
15+
"邱敬幃"
16+
],
17+
"homepage": "https://pardn.io",
518
"license": "MIT",
619
"authors": [
720
{
821
"name": "邱敬幃 Pardn Chiu",
9-
"email": "dev@pardn.io"
22+
"email": "dev@pardn.io",
23+
"homepage": "https://pardn.io",
24+
"role": "Developer"
1025
}
1126
],
27+
"support": {
28+
"email": "dev@pardn.io",
29+
"issues": "https://github.com/pardnchiu/PHP-SQL/issues",
30+
"source": "https://github.com/pardnchiu/PHP-SQL",
31+
"docs": "https://github.com/pardnchiu/PHP-SQL/blob/main/README.md"
32+
},
1233
"minimum-stability": "stable",
34+
"prefer-stable": true,
1335
"require": {
1436
"php": ">=8.0"
1537
},
16-
"require-dev": {
17-
},
38+
"require-dev": {},
1839
"autoload": {
1940
"psr-4": {
2041
"PD\\": "src/"
@@ -26,16 +47,22 @@
2647
]
2748
},
2849
"autoload-dev": {
29-
"psr-4": {
30-
}
31-
},
32-
"scripts": {
50+
"psr-4": {}
3351
},
52+
"scripts": {},
3453
"extra": {
35-
"branch-alias": {
36-
}
54+
"branch-alias": {}
3755
},
3856
"config": {
39-
"sort-packages": true
57+
"sort-packages": true,
58+
"optimize-autoloader": true,
59+
"preferred-install": "dist"
60+
},
61+
"archive": {
62+
"exclude": [
63+
"/.git",
64+
"/*.md",
65+
"/.gitignore"
66+
]
4067
}
4168
}

src/SQL.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function select($fields)
7676
if (is_string($fields)) {
7777
$fields = func_get_args();
7878
};
79-
79+
8080
self::$selects = $fields;
8181

8282
return new static();
@@ -261,7 +261,7 @@ public static function insertGetId(array $data)
261261
public static function query($query, $params = [])
262262
{
263263
if (!isset(self::$client)) {
264-
throw new \Exception("[no_database]");
264+
throw new \Exception("數據庫連接未初始化");
265265
}
266266

267267
try {
@@ -281,32 +281,39 @@ public static function query($query, $params = [])
281281
break;
282282
default:
283283
$stmt->bindValue($index + 1, $val, \PDO::PARAM_STR);
284-
}
285-
}
286-
}
284+
};
285+
};
286+
};
287287

288288
$start = microtime(true) * 1000;
289289
$stmt->execute();
290290
$end = microtime(true) * 1000;
291291
$ms = number_format($end - $start, 2);
292292

293293
if ($ms > 20) {
294-
PrintDebug("[" . $ms . "ms] [" . $query . "]");
295-
}
294+
$info = sprintf("[Info] PD\SQL: [Slow Query: %sms] [%s]", $ms, $query);
295+
error_log($info);
296+
};
296297

297298
if (stripos($query, "UPDATE") === 0 || stripos($query, "INSERT") === 0) {
298299
return [
300+
"info" => $info ?? "",
299301
"insert_id" => self::$client->lastInsertId(),
300302
"affected_rows" => $stmt->rowCount()
301303
];
302-
}
304+
};
303305

304306
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
305307
} catch (\PDOException $e) {
306-
http_response_code(500);
307-
PrintError("[MysqlClient] [" . $e->getMessage() . "]");
308-
return null;
309-
}
308+
$info = $e->errorInfo ?? null;
309+
$code = $info[1] ?? $e->getCode();
310+
$message = $info[2] ?? $e->getMessage();
311+
$result = sprintf("[Error] PD\SQL: [Code: %s] [Message: %s] [%s]", $code, $message);
312+
313+
error_log($result);
314+
315+
throw new \PDOException($result, (int) $code, $e);
316+
};
310317
}
311318

312319
public static function close()

0 commit comments

Comments
 (0)