Skip to content

Commit 1dab1c0

Browse files
authored
refactored files
1 parent ce454e2 commit 1dab1c0

File tree

4 files changed

+88
-39
lines changed

4 files changed

+88
-39
lines changed

index.php

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,75 @@
44
ini_set('display_startup_errors', '1');
55
error_reporting(E_ERROR | E_PARSE);
66

7-
require __DIR__.'/meme-sql-conf.php';
8-
require __DIR__.'/meme-sql-lib.php';
7+
require __DIR__.'/meme-db.php';
98
require __DIR__.'/meme-parse.php';
9+
require __DIR__.'/meme-sql.php';
1010

1111
?>
1212
<!DOCTYPE HTML>
1313
<html lang="en-US">
1414
<head>
1515
<title>Memelang SQL Demo</title>
16-
<link rel="stylesheet" type="text/css" href="./style.css">
16+
<style type="text/css">
17+
body { line-height:150%; background:rgb(11,11,11); color:rgb(230,230,230); font-family:sans-serif; margin:60px 0 100px 0; }
18+
a { color:rgb(170,170,192); text-decoration:none; }
19+
a:hover { text-decoration:underline; }
20+
main { max-width: 700px; width: 94%; margin:0 auto; }
21+
pre { display: block; padding:20px; border-left:4px solid rgb(85,85,85); background:rgb(43,43,43); white-space:pre-wrap; font-size:1rem; }
22+
23+
code { font-size:1rem; }
24+
25+
var { font-style:normal; }
26+
.meme .v3 { color:rgb(0,170,170); } /* A */
27+
.meme .v4 { color:rgb(213,0,213); } /* :B */
28+
.meme .v5 { color:rgb(213,128,0); font-style:italic; } /* 'R */
29+
.meme .v6 { color:rgb(213,128,0); } /* .R */
30+
.meme .v8 { color:rgb(0,192,0); font-style:italic; } /* =Q */
31+
.meme .v9 { color:rgb(0,192,0); } /* #=Q */
32+
33+
.meme .v11,
34+
.meme .v12,
35+
.meme .v13,
36+
.meme .v14,
37+
.meme .v15,
38+
.meme .v16,
39+
.meme .v17 { color:rgb(0,192,0); } /* <=Q */
40+
41+
.meme .v41 { color:rgb(128,170,128); font-style:italic; vertical-align: baseline; font-size:80%; } /* ORG */
42+
43+
.meme .v33 { color:rgb(213,150,0); } /* .R.R */
44+
.meme .v34 { color:rgb(213,150,0); font-style:italic; } /* .R'R */
45+
.meme .v36,
46+
.meme .v35 { color:rgb(213,170,0); } /* ?R */
47+
48+
49+
.off { color:rgb(128,128,128); font-style:italic; }
50+
51+
textarea { width:100%; font-size:1.1rem; box-sizing:border-box; }
52+
input { width:100%; box-sizing:border-box; font-size: 1.1rem; text-align:center; }
53+
form { display:block; margin-bottom:20px; text-align:center; }
54+
form, pre, table, .mbe { width:100%; margin-bottom:20px; box-sizing: border-box; }
55+
56+
th { text-align:left; background:rgb(43,43,43); padding:8px 12px; }
57+
td { background:rgb(36,36,36); padding:8px 12px; }
58+
td.a {width:30%; max-width:166px; overflow:hidden; }
59+
td.r {width:30%; max-width:166px; }
60+
td.b {width:30%; max-width:166px; overflow:hidden; }
61+
th.q, td.q {width:10%; text-align: right; }
62+
63+
td.code { display:block; white-space:pre-wrap; }
64+
td.spa { margin-bottom:6px; }
65+
66+
67+
table.err th { background:rgb(85,21,21); }
68+
table.err td { background:rgb(43,11,11); }
69+
70+
table.sql th { background:rgb(30,43,30); }
71+
table.sql td { background:rgb(30,36,30); color:rgb(192,230,192); font-style:italic; padding:20px; }
72+
73+
table.meme th { background:rgb(43,43,30); }
74+
table.meme td { background:rgb(36,36,30); padding:20px; }
75+
</style>
1776
</head>
1877
<body>
1978
<main>

meme-db.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
define('DB_PASSWORD', 'password'); // Password for MySQL/Postgres
99
define('DB_NAME', 'database_name'); // Database name for MySQL/Postgres
1010
define('DB_TABLE_MEME', 'meme'); // Default table name for meme data
11-
define('DB_TABLE_TERM', 'term'); // Default table name for term data
1211

1312
define('COL_AID', 0);
1413
define('COL_RID', 1);
1514
define('COL_BID', 2);
1615
define('COL_QNT', 3);
1716

1817

19-
function memeSQLDB ($sqlQuery) {
20-
switch (DB_TYPE) {
21-
case 'sqlite3': return memeSQLite3($sqlQuery);
22-
case 'mysql': return memeMySQL($sqlQuery);
23-
case 'postgres': return memePostgres($sqlQuery);
24-
default: throw new Exception("Unsupported database type: " . DB_TYPE);
18+
// Main function to process memelang query and return results
19+
function memeQuery($memeString) {
20+
try {
21+
$sqlQuery = memeSQL($memeString);
22+
23+
switch (DB_TYPE) {
24+
case 'sqlite3': return memeSQLite3($sqlQuery);
25+
case 'mysql': return memeMySQL($sqlQuery);
26+
case 'postgres': return memePostgres($sqlQuery);
27+
default: throw new Exception("Unsupported database type: " . DB_TYPE);
28+
}
29+
} catch (Exception $e) {
30+
return "Error: " . $e->getMessage();
2531
}
2632
}
2733

34+
2835
// SQLite3 database query function
2936
function memeSQLite3($sqlQuery) {
3037
$db = new SQLite3(DB_PATH);

meme-sql.php

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,16 @@
22

33
define('MEME_BID_AS_AID', 'm0.bid AS aid');
44

5-
// Main function to process memelang query and return results
6-
function memeQuery($memeString) {
7-
try {
8-
$sqlQuery = memeSQL($memeString);
9-
10-
switch (DB_TYPE) {
11-
case 'sqlite3': return memeSQLite3($sqlQuery);
12-
case 'mysql': return memeMySQL($sqlQuery);
13-
case 'postgres': return memePostgres($sqlQuery);
14-
default: throw new Exception("Unsupported database type: " . DB_TYPE);
15-
}
16-
} catch (Exception $e) {
17-
return "Error: " . $e->getMessage();
18-
}
19-
}
20-
215
// Translate a Memelang query (which may contain multiple commands) into SQL
22-
function memeSQL($memeString) {
6+
function memeSQL($memeString, $table=DB_TABLE_MEME) {
237
$queries=[];
248
$memeCommands=memeDecode($memeString);
25-
foreach ($memeCommands as $memeCommand) $queries[]=memeCmdSQL($memeCommand);
9+
foreach ($memeCommands as $memeCommand) $queries[]=memeCmdSQL($memeCommand, $table);
2610
return implode(' UNION ALL ', $queries);
2711
}
2812

2913
// Translate one Memelang command into SQL
30-
function memeCmdSQL($memeCommand) {
31-
$table=DB_TABLE_MEME;
14+
function memeCmdSQL($memeCommand, $table=DB_TABLE_MEME) {
3215
$querySettings = ['all' => false];
3316
$trueGroup = [];
3417
$falseGroup = [];
@@ -100,7 +83,7 @@ function memeCmdSQL($memeCommand) {
10083
$cteCount++;
10184

10285
foreach ($bidGroup as $memeStatement) {
103-
list($select, $where)=memeSelectWhere($memeStatement);
86+
list($select, $where)=memeSelectWhere($memeStatement, $table);
10487
if (empty($wheres)) $wheres[]=$where;
10588
else $wheres[]=substr($where, strpos($where, 'qnt')-4, 99);
10689
}
@@ -119,7 +102,7 @@ function memeCmdSQL($memeCommand) {
119102
$cteCount++;
120103
$orSQL = [];
121104
foreach ($orGroup as $memeStatement) {
122-
$orSQL[]=implode(' WHERE ', memeSelectWhere($memeStatement))
105+
$orSQL[]=implode(' WHERE ', memeSelectWhere($memeStatement, $table))
123106
.($cteCount>0 ? ' AND m0.aid IN (SELECT aid FROM z'.($cteCount-1).')' : '');
124107
}
125108
$cteSQL[$cteCount] = "z$cteCount AS (".implode(' UNION ALL ', $orSQL).')';
@@ -131,7 +114,7 @@ function memeCmdSQL($memeCommand) {
131114
if ($trueCount<1) throw new Exception('A query with a false statements must contain at least one non-OR true statement.');
132115

133116
$falseSQL=[];
134-
foreach ($falseGroup as $memeStatement) $falseSQL[]="aid NOT IN (". implode(' WHERE ', memeSelectWhere($memeStatement, true)).')';
117+
foreach ($falseGroup as $memeStatement) $falseSQL[]="aid NOT IN (". implode(' WHERE ', memeSelectWhere($memeStatement, $table, true)).')';
135118

136119
$fsql="SELECT aid FROM z$cteCount WHERE ".implode(' AND ', $falseSQL);
137120
$cteSQL[++$cteCount] = "z$cteCount AS ($fsql)";
@@ -152,7 +135,7 @@ function memeCmdSQL($memeCommand) {
152135
// otherwise select the matching and the GET fields
153136
else {
154137
foreach ($getGroup as $memeStatement)
155-
$selectSQL[]=implode(' WHERE ', memeSelectWhere($memeStatement))." AND m0.aid IN (SELECT aid FROM z{$cteCount})";
138+
$selectSQL[]=implode(' WHERE ', memeSelectWhere($memeStatement, $table))." AND m0.aid IN (SELECT aid FROM z{$cteCount})";
156139
}
157140

158141
foreach ($cteOut as $cteNum)
@@ -163,7 +146,7 @@ function memeCmdSQL($memeCommand) {
163146

164147

165148
// Translate an $memeStatement array of ARBQ into an SQL WHERE clause
166-
function memeSelectWhere($memeStatement, $aidOnly=false) {
149+
function memeSelectWhere($memeStatement, $table=DB_TABLE_MEME, $aidOnly=false) {
167150
global $OPRSTR;
168151

169152
$wheres = [];
@@ -272,7 +255,7 @@ function memeSelectWhere($memeStatement, $aidOnly=false) {
272255
}
273256

274257
return [
275-
'SELECT '.implode(', ', $selects).' FROM '.DB_TABLE_MEME.' m0 '.implode(' ', $joins),
258+
'SELECT '.implode(', ', $selects)." FROM $table m0 ".implode(' ', $joins),
276259
implode(' AND ', $wheres)
277260
];
278261
}
@@ -311,4 +294,4 @@ function memeDBOut ($memeTriples, $set=[]) {
311294
print memeEncode(memeDbDecode($memeTriples), $set);
312295
}
313296

314-
?>
297+
?>

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ Try the demo at http://demo.memelang.net/
88
* *data.sql* sample ARBQ data in SQL format
99
* *data.sqlite* sample ARBQ data in an SQLite binary file
1010
* *index.php* HTML interface to make queries
11+
* *meme-db.php* configuration file to establish database connection
1112
* *meme-parse.php* parses Memelang commands into an array
12-
* *meme-sql-conf.php* configuration file to establish database connection
13-
* *meme-sql-lib.php* library to convert Memelang to SQL and execute on database
13+
* *meme-sql.php* library to convert Memelang to SQL and execute on database

0 commit comments

Comments
 (0)