-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeme-db.php
127 lines (110 loc) · 3.87 KB
/
meme-db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// Database configuration constants
define('DB_TYPE', 'sqlite3'); // Options: 'sqlite3', 'mysql', 'postgres'
define('DB_PATH', 'data.sqlite'); // Default path for SQLite3
define('DB_HOST', 'localhost'); // Host for MySQL/Postgres
define('DB_USER', 'username'); // Username for MySQL/Postgres
define('DB_PASSWORD', 'password'); // Password for MySQL/Postgres
define('DB_NAME', 'database_name'); // Database name for MySQL/Postgres
define('DB_TABLE_MEME', 'meme'); // Default table name for meme data
define('COL_AID', 0);
define('COL_RID', 1);
define('COL_BID', 2);
define('COL_QNT', 3);
// Main function to process memelang query and return results
function memeQuery($memeString) {
try {
$sqlQuery = memeSQL($memeString);
switch (DB_TYPE) {
case 'sqlite3': return memeSQLite3($sqlQuery);
case 'mysql': return memeMySQL($sqlQuery);
case 'postgres': return memePostgres($sqlQuery);
default: throw new Exception("Unsupported database type: " . DB_TYPE);
}
} catch (Exception $e) {
return "Error: " . $e->getMessage();
}
}
// SQLite3 database query function
function memeSQLite3($sqlQuery) {
$db = new SQLite3(DB_PATH);
$results = [];
$queryResult = $db->query($sqlQuery);
while ($row = $queryResult->fetchArray(SQLITE3_NUM)) {
if (strpos($row[COL_RID], "\t")===false) $results[] = serialize($row);
else {
$aid=$row[COL_AID];
$rids=explode("\t", $row[COL_RID]);
$bids=explode("\t", $row[COL_BID]);
$qnts=explode("\t", $row[COL_QNT]);
foreach ($rids as $j=>$rid) {
if ($aid==='UNK') break;
if (strpos($rid,'?')===0) continue;
$results[]=serialize([COL_AID=>$aid,COL_RID=>$rid,COL_BID=>$bids[$j],COL_QNT=>$qnts[$j]]);
$aid=$bids[$j];
}
}
}
$db->close();
return array_map('unserialize', array_unique($results));
}
// MySQL database query function
function memeMySQL($sqlQuery) {
$connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($connection->connect_error) {
throw new Exception("Connection failed: " . $connection->connect_error);
}
$results = [];
$queryResult = $connection->query($sqlQuery);
if ($queryResult) {
while ($row = $queryResult->fetch_array()) {
if (strpos($row[COL_RID], "\t")===false) $results[] = serialize($row);
else {
$aid=$row[COL_AID];
$rids=explode("\t", $row[COL_RID]);
$bids=explode("\t", $row[COL_BID]);
$qnts=explode("\t", $row[COL_QNT]);
foreach ($rids as $j=>$rid) {
if ($aid==='UNK') continue;
$results[]=serialize([COL_AID=>$aid,COL_RID=>$rid,COL_BID=>$bids[$j],COL_QNT=>$qnts[$j]]);
$aid=$bids[$j];
}
}
}
} else {
throw new Exception("Query failed: " . $connection->error);
}
$connection->close();
return array_map('unserialize', array_unique($results));
}
// PostgreSQL database query function
function memePostgres($sqlQuery) {
$connectionString = "host=" . DB_HOST . " dbname=" . DB_NAME . " user=" . DB_USER . " password=" . DB_PASSWORD;
$connection = pg_connect($connectionString);
if (!$connection) {
throw new Exception("Connection failed: " . pg_last_error());
}
$results = [];
$queryResult = pg_query($connection, $sqlQuery);
if ($queryResult) {
while ($row = pg_fetch_array($queryResult)) {
if (strpos($row[COL_RID], "\t")===false) $results[] = serialize($row);
else {
$aid=$row[COL_AID];
$rids=explode("\t", $row[COL_RID]);
$bids=explode("\t", $row[COL_BID]);
$qnts=explode("\t", $row[COL_QNT]);
foreach ($rids as $j=>$rid) {
if ($aid==='UNK') continue;
$results[]=serialize([COL_AID=>$aid,COL_RID=>$rid,COL_BID=>$bids[$j],COL_QNT=>$qnts[$j]]);
$aid=$bids[$j];
}
}
}
} else {
throw new Exception("Query failed: " . pg_last_error($connection));
}
pg_close($connection);
return array_map('unserialize', array_unique($results));
}
?>