隨著 Web 邁向資料導向與語義化,傳統關聯式資料庫的 SQL CRUD 操作逐漸轉向基於 RDF 與 SPARQL 的語義 Web 架構。本文詳細對比 SQL 與 SPARQL 的 CRUD 操作差異,並示範如何使用 PHP 實作語義 Web 資料操作。
| 縮寫 | 全稱 | 中文說明 |
|---|---|---|
| RDF | Resource Description Framework | 資源描述框架 |
| SPARQL | Simple Protocol and RDF Query Language | 簡單協議與 RDF 查詢語言(讀作 "sparkle") |
| CRUD | Create/Read/Update/Delete | 建立/讀取/更新/刪除 |
| URI | Uniform Resource Identifier | 統一資源標識符 |
SELECT realname, dob, location
FROM UserTable
WHERE realname = "John Smith";
PREFIX foaf: <https://xmlns.com/foaf/0.1/>
PREFIX rdfs: <https://www.w3.org/2000/01/rdf-schema#>
SELECT ?realname ?dob ?location
FROM <https://example.org/user-data>
WHERE {
?person a foaf:Person ;
foaf:name ?realname ;
foaf:birthday ?dob ;
foaf:based_near ?location .
FILTER (?realname = "John Smith")
}
? 前綴表示查詢變數(如 ?realname)FROM 後接 URI 而非資料表名稱WHERE 區塊描述 RDF 圖形模式,而非關聯式條件INSERT INTO UserTable (realname, dob, location)
VALUES ("John Smith", "1985-01-01", "Bristol, UK");
PREFIX foaf: <https://xmlns.com/foaf/0.1/>
PREFIX xsd: <https://www.w3.org/2001/XMLSchema#>
INSERT DATA {
GRAPH <https://example.org/user-data> {
<https://example.org/user/123> a foaf:Person ;
foaf:name "John Smith" ;
foaf:birthday "1985-01-01"^^xsd:date ;
foaf:based_near "Bristol, UK" .
}
}
DELETE FROM UserTable WHERE realname = "John Smith";
PREFIX foaf: <https://xmlns.com/foaf/0.1/>
DELETE {
?person foaf:name "John Smith" .
}
WHERE {
?person a foaf:Person ;
foaf:name "John Smith" .
}
<?php
// Model: SPARQL 端點封裝
class SparqlEndpoint {
private $endpoint_url;
private $update_url;
public function __construct($query_endpoint, $update_endpoint) {
$this->endpoint_url = $query_endpoint;
$this->update_url = $update_endpoint;
}
public function query($sparql) {
// 使用 cURL 執行 SELECT/ASK/CONSTRUCT 查詢
$ch = curl_init($this->endpoint_url . '?query=' . urlencode($sparql));
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Accept: application/sparql-results+json']
]);
return json_decode(curl_exec($ch), true);
}
public function update($sparql) {
// 使用 POST 執行 INSERT/DELETE/LOAD 更新
$ch = curl_init($this->update_url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'update=' . urlencode($sparql),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded']
]);
return curl_exec($ch);
}
}
// Controller: 業務邏輯
class UserController {
private $sparql;
public function __construct(SparqlEndpoint $sparql) {
$this->sparql = $sparql;
}
public function getUser($name) {
$sparql = $this->buildSelectQuery($name);
return $this->sparql->query($sparql);
}
public function createUser($data) {
$sparql = $this->buildInsertQuery($data);
return $this->sparql->update($sparql);
}
private function buildSelectQuery($name) {
return '
PREFIX foaf: <https://xmlns.com/foaf/0.1/>
SELECT ?person ?name ?dob ?location
WHERE {
?person a foaf:Person ;
foaf:name ?name ;
foaf:birthday ?dob ;
foaf:based_near ?location .
FILTER (?name = "' . $name . '")
}';
}
}
// View: 輸出格式化(略)
語義 Web 不只是技術棧的更換,更是資料建模思維的轉變:從「表格思維」轉向「圖形思維」。