XOOPS क्वेरी बिल्डर
XOOPS क्वेरी बिल्डर SQL क्वेरीज़ के निर्माण के लिए एक आधुनिक, धाराप्रवाह इंटरफ़ेस प्रदान करता है। यह SQL इंजेक्शन को रोकने में मदद करता है, पठनीयता में सुधार करता है, और कई डेटाबेस सिस्टम के लिए डेटाबेस एब्स्ट्रैक्शन प्रदान करता है।
क्वेरी बिल्डर आर्किटेक्चर
Section titled “क्वेरी बिल्डर आर्किटेक्चर”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]QueryBuilder कक्षा
Section titled “QueryBuilder कक्षा”धाराप्रवाह इंटरफ़ेस के साथ मुख्य क्वेरी बिल्डर वर्ग।
कक्षा अवलोकन
Section titled “कक्षा अवलोकन”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 = [];}स्थैतिक विधियाँ
Section titled “स्थैतिक विधियाँ”किसी तालिका के लिए एक नया क्वेरी बिल्डर बनाता है।
public static function table(string $table): QueryBuilderपैरामीटर:
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
$table | स्ट्रिंग | तालिका का नाम (उपसर्ग के साथ या बिना) |
रिटर्न: QueryBuilder - क्वेरी बिल्डर उदाहरण
उदाहरण:
$query = QueryBuilder::table('users');$query = QueryBuilder::table('xoops_users'); // With prefixSELECT प्रश्न
Section titled “SELECT प्रश्न”चयन करने के लिए कॉलम निर्दिष्ट करता है।
public function select(...$columns): selfपैरामीटर:
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
...$columns | सारणी | स्तम्भ नाम या भाव |
रिटर्न: self - मेथड चेनिंग के लिए
उदाहरण:
// Simple selectQueryBuilder::table('users') ->select('id', 'username', 'email') ->get();
// Select with aliasesQueryBuilder::table('users') ->select('id as user_id', 'username as name') ->get();
// Select all columnsQueryBuilder::table('users') ->select('*') ->get();
// Select with expressionsQueryBuilder::table('orders') ->select('id', 'COUNT(*) as total_items') ->groupBy('id') ->get();WHERE शर्त जोड़ता है।
public function where(string $column, string $operator = '=', mixed $value = null): selfपैरामीटर:
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
$column | स्ट्रिंग | कॉलम का नाम |
$operator | स्ट्रिंग | तुलना ऑपरेटर |
$value | मिश्रित | तुलना करने के लिए मूल्य |
रिटर्न: self - मेथड चेनिंग के लिए
संचालक:
| ऑपरेटर | विवरण | उदाहरण |
|---|---|---|
= | बराबर | ->where('status', '=', 'active') |
!= या <> | बराबर नहीं | ->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 conditionQueryBuilder::table('users') ->select('*') ->where('status', '=', 'active') ->get();
// Multiple conditions (AND)QueryBuilder::table('users') ->select('*') ->where('status', '=', 'active') ->where('age', '>=', 18) ->get();
// IN operatorQueryBuilder::table('products') ->select('*') ->where('category_id', 'IN', [1, 2, 3]) ->get();
// LIKE operatorQueryBuilder::table('users') ->select('*') ->where('email', 'LIKE', '%@example.com') ->get();
// NULL checkQueryBuilder::table('users') ->select('*') ->where('deleted_at', 'IS NULL') ->get();याकहाँ
Section titled “याकहाँ”एक 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व्हेयरइन / व्हेयरनॉटइन
Section titled “व्हेयरइन / व्हेयरनॉटइन”IN/NOT IN के लिए सुविधाजनक तरीके।
public function whereIn(string $column, array $values): selfpublic 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();###whereNull /whereNotNull
NULL चेक के लिए सुविधाजनक तरीके।
public function whereNull(string $column): selfpublic function whereNotNull(string $column): selfउदाहरण:
QueryBuilder::table('users') ->select('*') ->whereNotNull('verified_at') ->get();कहाँ बीच में
Section titled “कहाँ बीच में”जाँचता है कि मान दो मानों के बीच है या नहीं।
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();शामिल हों
Section titled “शामिल हों”एक 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();बाएँ जुड़ें / दाएँ जुड़ें
Section titled “बाएँ जुड़ें / दाएँ जुड़ें”वैकल्पिक जुड़ाव प्रकार.
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();समूह द्वारा
Section titled “समूह द्वारा”परिणाम column(s) द्वारा समूहित करें।
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();orderBy
Section titled “orderBy”आदेश परिणाम.
public function orderBy(string $column, string $direction = 'ASC'): selfपैरामीटर:
| पैरामीटर | प्रकार | विवरण |
|---|---|---|
$column | स्ट्रिंग | द्वारा ऑर्डर करने के लिए कॉलम |
$direction | स्ट्रिंग | ASC या DESC |
उदाहरण:
// Single orderQueryBuilder::table('users') ->select('*') ->orderBy('created_at', 'DESC') ->get();
// Multiple ordersQueryBuilder::table('posts') ->select('*') ->orderBy('category_id', 'ASC') ->orderBy('created_at', 'DESC') ->get();
// Random orderQueryBuilder::table('quotes') ->select('*') ->orderBy('RAND()') ->get();सीमा / ऑफसेट
Section titled “सीमा / ऑफसेट”सीमाएँ और ऑफसेट परिणाम।
public function limit(int $limit): selfpublic function offset(int $offset): selfउदाहरण:
// Simple limitQueryBuilder::table('posts') ->select('*') ->limit(10) ->get();
// Pagination$page = 2;$perPage = 20;$offset = ($page - 1) * $perPage;
QueryBuilder::table('posts') ->select('*') ->limit($perPage) ->offset($offset) ->get();निष्पादन के तरीके
Section titled “निष्पादन के तरीके”प्राप्त करें
Section titled “प्राप्त करें”क्वेरी निष्पादित करता है और सभी परिणाम लौटाता है।
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 - पहली पंक्ति या शून्य
उदाहरण:
$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";मौजूद है
Section titled “मौजूद है”जाँचता है कि क्या क्वेरी कोई परिणाम देती है।
public function exists(): bool```**रिटर्न:** `bool` - यदि परिणाम मौजूद हैं तो सही है
**उदाहरण:**```phpif (QueryBuilder::table('users')->where('email', '=', 'test@example.com')->exists()) { echo 'User already exists';}समुच्चय
Section titled “समुच्चय”समग्र मान प्राप्त करता है.
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');INSERT प्रश्न
Section titled “INSERT प्रश्न”एक पंक्ति सम्मिलित करता है.
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')]);इन्सर्टमेनी
Section titled “इन्सर्टमेनी”अनेक पंक्तियाँ सम्मिलित करता है.
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()]]);UPDATE प्रश्न
Section titled “UPDATE प्रश्न”अद्यतन
Section titled “अद्यतन”पंक्तियों को अद्यतन करता है.
public function update(array $values): intरिटर्न: int - प्रभावित पंक्तियों की संख्या
उदाहरण:
// Update single userQueryBuilder::table('users') ->where('id', '=', 123) ->update([ 'email' => 'newemail@example.com', 'updated_at' => date('Y-m-d H:i:s') ]);
// Update multiple rowsQueryBuilder::table('posts') ->where('status', '=', 'draft') ->where('created_at', '<', date('Y-m-d', strtotime('-30 days'))) ->update([ 'status' => 'archived' ]);वृद्धि/कमी
Section titled “वृद्धि/कमी”किसी कॉलम को बढ़ाना या घटाना।
public function increment(string $column, int $amount = 1): intpublic function decrement(string $column, int $amount = 1): intउदाहरण:
// Increment view countQueryBuilder::table('posts') ->where('id', '=', 123) ->increment('views');
// Decrement stockQueryBuilder::table('products') ->where('id', '=', 456) ->decrement('stock', 5);DELETE प्रश्न
Section titled “DELETE प्रश्न”पंक्तियाँ हटाता है.
public function delete(): intरिटर्न: int - हटाई गई पंक्तियों की संख्या
उदाहरण:
// Delete single recordQueryBuilder::table('comments') ->where('id', '=', 789) ->delete();
// Delete multiple recordsQueryBuilder::table('log_entries') ->where('created_at', '<', date('Y-m-d', strtotime('-30 days'))) ->delete();काट-छाँट करना
Section titled “काट-छाँट करना”तालिका से सभी पंक्तियाँ हटा देता है.
public function truncate(): boolउदाहरण:
// Clear all sessionsQueryBuilder::table('sessions')->truncate();उन्नत सुविधाएँ
Section titled “उन्नत सुविधाएँ”कच्ची अभिव्यक्तियाँ
Section titled “कच्ची अभिव्यक्तियाँ”QueryBuilder::table('products') ->select('id', 'name', QueryBuilder::raw('price * quantity as total')) ->get();उपश्रेणियाँ
Section titled “उपश्रेणियाँ”$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();SQL प्राप्त करना
Section titled “SQL प्राप्त करना”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 = ?संपूर्ण उदाहरण
Section titled “संपूर्ण उदाहरण”जॉइन के साथ कॉम्प्लेक्स सेलेक्ट
Section titled “जॉइन के साथ कॉम्प्लेक्स सेलेक्ट”<?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>';}QueryBuilder के साथ पृष्ठांकन
Section titled “QueryBuilder के साथ पृष्ठांकन”<?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 resultsforeach ($articles as $article) { echo '<div class="article">' . htmlspecialchars($article['title']) . '</div>';}
// Display pagination linksif ($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>';}समुच्चय के साथ डेटा विश्लेषण
Section titled “समुच्चय के साथ डेटा विश्लेषण”<?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);सर्वोत्तम प्रथाएँ
Section titled “सर्वोत्तम प्रथाएँ”- पैरामीटरयुक्त क्वेरीज़ का उपयोग करें - QueryBuilder स्वचालित रूप से पैरामीटर बाइंडिंग को संभालता है
- श्रृंखला विधियां - पठनीय कोड के लिए धाराप्रवाह इंटरफ़ेस का लाभ उठाएं
- SQL आउटपुट का परीक्षण करें - उत्पन्न प्रश्नों को सत्यापित करने के लिए
toSql()का उपयोग करें - अनुक्रमणिका का उपयोग करें - सुनिश्चित करें कि बार-बार पूछे जाने वाले कॉलम अनुक्रमित हैं
- परिणाम सीमित करें - बड़े डेटासेट के लिए हमेशा
limit()का उपयोग करें - एग्रीगेट्स का उपयोग करें - डेटाबेस को PHP के बजाय गिनती/योग करने दें
- एस्केप आउटपुट - हमेशा
htmlspecialchars()के साथ प्रदर्शित डेटा से बचें - सूचकांक प्रदर्शन - धीमी क्वेरी की निगरानी करें और तदनुसार अनुकूलन करें
संबंधित दस्तावेज़ीकरण
Section titled “संबंधित दस्तावेज़ीकरण”- XoopsDatabase - डेटाबेस परत और कनेक्शन
- Criteria - विरासत Criteria-आधारित क्वेरी प्रणाली
- ../Core/XoopsObject - डेटा ऑब्जेक्ट दृढ़ता
- ../मॉड्यूल/मॉड्यूल-सिस्टम - मॉड्यूल डेटाबेस संचालन
यह भी देखें: XOOPS डेटाबेस API