跳到內容

XOOPS 查詢產生器

XOOPS 查詢產生器提供現代流暢的介面來構建 SQL 查詢。它有助於防止 SQL 注入、改進可讀性,並為多個資料庫系統提供資料庫抽象化。

graph TD
A[QueryBuilder] -->|builds| B[SELECT Queries]
A -->|builds| C[INSERT Queries]
A -->|builds| D[UPDATE Queries]
A -->|builds| E[DELETE Queries]
F[Table] -->|chains| G[select]
F -->|chains| H[where]
F -->|chains| I[orderBy]
F -->|chains| J[limit]
G -->|chains| K[join]
G -->|chains| H
H -->|chains| I
I -->|chains| J
L[Execute Methods] -->|returns| M[Results]
L -->|returns| N[Count]
L -->|returns| O[First/Last]

主要查詢產生器類別,具有流暢介面。

namespace Xoops\Database;
class QueryBuilder
{
protected string $table = '';
protected string $type = 'SELECT';
protected array $selects = [];
protected array $joins = [];
protected array $wheres = [];
protected array $orders = [];
protected int $limit = 0;
protected int $offset = 0;
protected array $bindings = [];
}

為表格建立新查詢產生器。

public static function table(string $table): QueryBuilder

參數:

參數型別描述
$tablestring表格名稱(具有或不具有首碼)

傳回值: QueryBuilder - 查詢產生器實例

範例:

$query = QueryBuilder::table('users');
$query = QueryBuilder::table('xoops_users'); // With prefix

指定要選擇的欄位。

public function select(...$columns): self

參數:

參數型別描述
...$columnsarray欄位名稱或運算式

傳回值: self - 用於方法鏈式

範例:

// Simple select
QueryBuilder::table('users')
->select('id', 'username', 'email')
->get();
// Select with aliases
QueryBuilder::table('users')
->select('id as user_id', 'username as name')
->get();
// Select all columns
QueryBuilder::table('users')
->select('*')
->get();
// Select with expressions
QueryBuilder::table('orders')
->select('id', 'COUNT(*) as total_items')
->groupBy('id')
->get();

添加 WHERE 條件。

public function where(string $column, string $operator = '=', mixed $value = null): self

參數:

參數型別描述
$columnstring欄位名稱
$operatorstring比較運算子
$valuemixed要比較的值

傳回值: self - 用於方法鏈式

運算子:

運算子描述範例
=等於->where('status', '=', 'active')
!= or <>不等於->where('status', '!=', 'deleted')
>大於->where('price', '>', 100)
<小於->where('price', '<', 100)
>=大於或等於->where('age', '>=', 18)
<=小於或等於->where('age', '<=', 65)
LIKE模式匹配->where('name', 'LIKE', '%john%')
IN在清單中->where('status', 'IN', ['active', 'pending'])
NOT IN不在清單中->where('id', 'NOT IN', [1, 2, 3])
BETWEEN範圍->where('age', 'BETWEEN', [18, 65])
IS NULL為空->where('deleted_at', 'IS NULL')
IS NOT NULL不為空->where('deleted_at', 'IS NOT NULL')

範例:

// Single condition
QueryBuilder::table('users')
->select('*')
->where('status', '=', 'active')
->get();
// Multiple conditions (AND)
QueryBuilder::table('users')
->select('*')
->where('status', '=', 'active')
->where('age', '>=', 18)
->get();
// IN operator
QueryBuilder::table('products')
->select('*')
->where('category_id', 'IN', [1, 2, 3])
->get();
// LIKE operator
QueryBuilder::table('users')
->select('*')
->where('email', 'LIKE', '%@example.com')
->get();
// NULL check
QueryBuilder::table('users')
->select('*')
->where('deleted_at', 'IS NULL')
->get();

添加 OR 條件。

public function orWhere(string $column, string $operator = '=', mixed $value = null): self

範例:

QueryBuilder::table('users')
->select('*')
->where('status', '=', 'active')
->orWhere('premium', '=', 1)
->get();
// SELECT * FROM users WHERE status = 'active' OR premium = 1

IN/NOT IN 的便捷方法。

public function whereIn(string $column, array $values): self
public function whereNotIn(string $column, array $values): self

範例:

QueryBuilder::table('posts')
->select('*')
->whereIn('status', ['published', 'scheduled'])
->get();
QueryBuilder::table('comments')
->select('*')
->whereNotIn('spam_score', [8, 9, 10])
->get();

NULL 檢查的便捷方法。

public function whereNull(string $column): self
public function whereNotNull(string $column): self

範例:

QueryBuilder::table('users')
->select('*')
->whereNotNull('verified_at')
->get();

檢查值是否在兩個值之間。

public function whereBetween(string $column, array $values): self

範例:

QueryBuilder::table('products')
->select('*')
->whereBetween('price', [10, 100])
->get();
QueryBuilder::table('orders')
->select('*')
->whereBetween('created_at', ['2024-01-01', '2024-12-31'])
->get();

添加 INNER JOIN。

public function join(
string $table,
string $first,
string $operator = '=',
string $second = null
): self

範例:

QueryBuilder::table('posts')
->select('posts.*', 'users.username', 'categories.name')
->join('users', 'posts.user_id', '=', 'users.id')
->join('categories', 'posts.category_id', '=', 'categories.id')
->where('posts.published', '=', 1)
->get();

替代 join 型別。

public function leftJoin(
string $table,
string $first,
string $operator = '=',
string $second = null
): self
public function rightJoin(
string $table,
string $first,
string $operator = '=',
string $second = null
): self

範例:

QueryBuilder::table('users')
->select('users.*', 'COUNT(posts.id) as post_count')
->leftJoin('posts', 'users.id', '=', 'posts.user_id')
->groupBy('users.id')
->get();

按欄位分組結果。

public function groupBy(...$columns): self

範例:

QueryBuilder::table('orders')
->select('user_id', 'COUNT(*) as order_count', 'SUM(total) as total_spent')
->groupBy('user_id')
->get();
QueryBuilder::table('sales')
->select('department', 'region', 'SUM(amount) as total')
->groupBy('department', 'region')
->get();

添加 HAVING 條件。

public function having(string $column, string $operator = '=', mixed $value = null): self

範例:

QueryBuilder::table('orders')
->select('user_id', 'COUNT(*) as order_count')
->groupBy('user_id')
->having('order_count', '>', 5)
->get();

排序結果。

public function orderBy(string $column, string $direction = 'ASC'): self

參數:

參數型別描述
$columnstring排序的欄位
$directionstringASCDESC

範例:

// Single order
QueryBuilder::table('users')
->select('*')
->orderBy('created_at', 'DESC')
->get();
// Multiple orders
QueryBuilder::table('posts')
->select('*')
->orderBy('category_id', 'ASC')
->orderBy('created_at', 'DESC')
->get();
// Random order
QueryBuilder::table('quotes')
->select('*')
->orderBy('RAND()')
->get();

限制和偏移結果。

public function limit(int $limit): self
public function offset(int $offset): self

範例:

// Simple limit
QueryBuilder::table('posts')
->select('*')
->limit(10)
->get();
// Pagination
$page = 2;
$perPage = 20;
$offset = ($page - 1) * $perPage;
QueryBuilder::table('posts')
->select('*')
->limit($perPage)
->offset($offset)
->get();

執行查詢並傳回所有結果。

public function get(): array

傳回值: array - 結果列的陣列

範例:

$users = QueryBuilder::table('users')
->select('id', 'username', 'email')
->where('status', '=', 'active')
->orderBy('username')
->get();
foreach ($users as $user) {
echo $user['username'] . ' (' . $user['email'] . ')' . "\n";
}

取得第一個結果。

public function first(): ?array

傳回值: ?array - 第一列或 null

範例:

$user = QueryBuilder::table('users')
->select('*')
->where('id', '=', 123)
->first();
if ($user) {
echo 'Found: ' . $user['username'];
}

取得最後一個結果。

public function last(): ?array

範例:

$latestPost = QueryBuilder::table('posts')
->select('*')
->orderBy('created_at', 'DESC')
->last();

取得結果的計數。

public function count(): int

傳回值: int - 列數

範例:

$activeUsers = QueryBuilder::table('users')
->where('status', '=', 'active')
->count();
echo "Active users: $activeUsers";

檢查查詢是否傳回任何結果。

public function exists(): bool

傳回值: bool - 如果存在結果則為 true

範例:

if (QueryBuilder::table('users')->where('email', '=', 'test@example.com')->exists()) {
echo 'User already exists';
}

取得聚合值。

public function aggregate(string $function, string $column): mixed

範例:

$maxPrice = QueryBuilder::table('products')
->aggregate('MAX', 'price');
$avgAge = QueryBuilder::table('users')
->aggregate('AVG', 'age');
$totalSales = QueryBuilder::table('orders')
->aggregate('SUM', 'total');

插入列。

public function insert(array $values): bool

範例:

QueryBuilder::table('users')->insert([
'username' => 'john',
'email' => 'john@example.com',
'password' => password_hash('secret', PASSWORD_BCRYPT),
'created_at' => date('Y-m-d H:i:s')
]);

插入多列。

public function insertMany(array $rows): bool

範例:

QueryBuilder::table('log_entries')->insertMany([
['action' => 'login', 'user_id' => 1, 'timestamp' => time()],
['action' => 'logout', 'user_id' => 2, 'timestamp' => time()],
['action' => 'update', 'user_id' => 3, 'timestamp' => time()]
]);

更新列。

public function update(array $values): int

傳回值: int - 受影響的列數

範例:

// Update single user
QueryBuilder::table('users')
->where('id', '=', 123)
->update([
'email' => 'newemail@example.com',
'updated_at' => date('Y-m-d H:i:s')
]);
// Update multiple rows
QueryBuilder::table('posts')
->where('status', '=', 'draft')
->where('created_at', '<', date('Y-m-d', strtotime('-30 days')))
->update([
'status' => 'archived'
]);

遞增或遞減欄位。

public function increment(string $column, int $amount = 1): int
public function decrement(string $column, int $amount = 1): int

範例:

// Increment view count
QueryBuilder::table('posts')
->where('id', '=', 123)
->increment('views');
// Decrement stock
QueryBuilder::table('products')
->where('id', '=', 456)
->decrement('stock', 5);

刪除列。

public function delete(): int

傳回值: int - 刪除的列數

範例:

// Delete single record
QueryBuilder::table('comments')
->where('id', '=', 789)
->delete();
// Delete multiple records
QueryBuilder::table('log_entries')
->where('created_at', '<', date('Y-m-d', strtotime('-30 days')))
->delete();

從表格中刪除所有列。

public function truncate(): bool

範例:

// Clear all sessions
QueryBuilder::table('sessions')->truncate();
QueryBuilder::table('products')
->select('id', 'name', QueryBuilder::raw('price * quantity as total'))
->get();
$recentPostIds = QueryBuilder::table('posts')
->select('id')
->where('created_at', '>', date('Y-m-d', strtotime('-7 days')))
->toSql();
$comments = QueryBuilder::table('comments')
->select('*')
->whereIn('post_id', $recentPostIds)
->get();
public function toSql(): string

範例:

$sql = QueryBuilder::table('users')
->select('id', 'username')
->where('status', '=', 'active')
->toSql();
echo $sql;
// SELECT id, username FROM xoops_users WHERE status = ?
<?php
/**
* Get posts with author and category info
*/
$posts = QueryBuilder::table('posts')
->select(
'posts.id',
'posts.title',
'posts.content',
'posts.created_at',
'users.username as author',
'categories.name as category'
)
->join('users', 'posts.user_id', '=', 'users.id')
->join('categories', 'posts.category_id', '=', 'categories.id')
->where('posts.published', '=', 1)
->orderBy('posts.created_at', 'DESC')
->limit(10)
->get();
foreach ($posts as $post) {
echo '<article>';
echo '<h2>' . htmlspecialchars($post['title']) . '</h2>';
echo '<p class="meta">By ' . htmlspecialchars($post['author']) . ' in ' . htmlspecialchars($post['category']) . '</p>';
echo '<p>' . htmlspecialchars($post['content']) . '</p>';
echo '</article>';
}
<?php
/**
* Paginated results
*/
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 20;
$offset = ($page - 1) * $perPage;
// Get total count
$total = QueryBuilder::table('articles')
->where('status', '=', 'published')
->count();
// Get page results
$articles = QueryBuilder::table('articles')
->select('*')
->where('status', '=', 'published')
->orderBy('created_at', 'DESC')
->limit($perPage)
->offset($offset)
->get();
// Calculate pagination
$pages = ceil($total / $perPage);
// Display results
foreach ($articles as $article) {
echo '<div class="article">' . htmlspecialchars($article['title']) . '</div>';
}
// Display pagination links
if ($pages > 1) {
echo '<nav class="pagination">';
for ($i = 1; $i <= $pages; $i++) {
if ($i == $page) {
echo '<span class="current">' . $i . '</span>';
} else {
echo '<a href="?page=' . $i . '">' . $i . '</a>';
}
}
echo '</nav>';
}
<?php
/**
* Sales analysis
*/
// Total sales by region
$regionSales = QueryBuilder::table('orders')
->select('region', QueryBuilder::raw('SUM(total) as total_sales'), QueryBuilder::raw('COUNT(*) as order_count'))
->groupBy('region')
->orderBy('total_sales', 'DESC')
->get();
foreach ($regionSales as $region) {
echo $region['region'] . ': $' . number_format($region['total_sales'], 2) . ' (' . $region['order_count'] . ' orders)' . "\n";
}
// Average order value
$avgOrderValue = QueryBuilder::table('orders')
->aggregate('AVG', 'total');
echo 'Average order value: $' . number_format($avgOrderValue, 2);
  1. 使用參數化查詢 - QueryBuilder 自動處理參數繫結
  2. 鏈式方法 - 利用流暢介面實現可讀的程式碼
  3. 測試 SQL 輸出 - 使用 toSql() 驗證產生的查詢
  4. 使用索引 - 確保經常查詢的欄位已編制索引
  5. 限制結果 - 始終對大型資料集使用 limit()
  6. 使用聚合 - 讓資料庫執行計數/求和而不是 PHP
  7. 逃脫輸出 - 始終使用 htmlspecialchars() 逃脫顯示的資料
  8. 索引效能 - 監視緩慢查詢並相應地進行最佳化
  • XoopsDatabase - 資料庫層和連線
  • Criteria - 舊版 Criteria 型查詢系統
  • ../Core/XoopsObject - 資料物件持久化
  • ../Module/Module-System - 模組資料庫操作

另請參閱:XOOPS 資料庫 API