better parser for broken quoted encoding (starting dot)
[chiroptera.git] / sqbase_experiment / zsq_query_fts5.d
blob4402e0ab27861d390f70b23f81478a2db779f895
1 /* E-Mail Client
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // this rebuilds FTS5 tables in SQLite supplementary mail database.
18 module zsq_rebuild_support is aliced;
20 import iv.encoding;
21 import iv.sq3;
22 import iv.strex;
23 import iv.timer;
24 import iv.vfs;
25 import iv.vfs.io;
27 import chibackend;
30 // ////////////////////////////////////////////////////////////////////////// //
31 void printReply (SQ3Text infield, SQ3Text reply) {
32 if (reply.length == 0 || reply.indexOf('\x02') < 0) return;
33 write("-- IN ", infield, " --: ");
34 reply = reply.recodeToKOI8;
35 char[] res;
36 scope(exit) delete res;
37 res.reserve(reply.length+64);
38 foreach (char ch; reply) {
39 if (ch >= 32) { res ~= ch; continue; }
40 if (ch == 1) { res ~= "\x1b[1m...\x1b[0m"; continue; }
41 if (ch == 2) { res ~= "\x1b[4m"; continue; }
42 if (ch == 3) { res ~= "\x1b[0m"; continue; }
43 if (ch == 26) { res ~= "|"; continue; }
44 res ~= ' ';
46 writeln(res);
50 // ////////////////////////////////////////////////////////////////////////// //
51 void main (string[] args) {
52 ChiroTimerEnabled = true;
53 chiroParseCommonCLIArgs(args);
55 // here, i don't have any threads at all
56 if (sqlite3_config(SQLITE_CONFIG_SINGLETHREAD) != SQLITE_OK) throw new Exception("cannot switch SQLite to multi-threaded mode");
58 writeln("opening message support db...");
59 chiroOpenViewDB();
61 /* sender, subj, text, html */
62 //dbView.execute(`INSERT INTO fts5_messages(fts5_messages, rank) VALUES('rank', 'bm25(1.0, 3.0, 10.0, 6.0)');`);
64 //writeln("selecting max recorded message...");
65 //foreach (auto row; dbView.statement(`SELECT MAX(uid) AS cnt FROM fts5_messages;`).range) writeln(row.cnt!uint);
67 auto stSelFTS5 = dbView.persistentStatement(`
68 SELECT
69 snippet(fts5_messages, 0, CHAR(2), CHAR(3), CHAR(1), 6) AS sender
70 , snippet(fts5_messages, 1, CHAR(2), CHAR(3), CHAR(1), 6) AS subj
71 , snippet(fts5_messages, 2, CHAR(2), CHAR(3), CHAR(1), 16) AS text
72 , snippet(fts5_messages, 3, CHAR(2), CHAR(3), CHAR(1), 16) AS html
73 , rowid as uid
74 FROM fts5_messages
75 WHERE fts5_messages MATCH :query
76 ORDER BY rank;
77 ;`);
79 foreach (string q; args[1..$]) {
80 q = q.xstrip;
81 if (q.length == 0) continue;
82 writeln("================================");
83 writeln(q);
84 writeln("--------------------------------");
85 q = q.recode("utf-8", "koi8-u");
86 Timer ctm = Timer(true);
87 foreach (auto frow; stSelFTS5.bindConstText(":query", q).range) {
88 auto sender = frow.sender!SQ3Text;
89 auto subj = frow.subj!SQ3Text;
90 auto text = frow.text!SQ3Text;
91 auto html = frow.html!SQ3Text;
92 writeln(" * * * * * * * * * * * * * * * * * * * UID:", frow.uid!uint);
93 printReply("SENDER", sender);
94 printReply("SUBJ", subj);
95 printReply("TEXT", text);
96 printReply("HTML", html);
98 ctm.stop;
99 writeln("--------------------------------");
100 writeln("time: ", ctm.toString, "\x1b[K");
103 dbView.close();