forked from ruben2020/codequery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlquery.cpp
666 lines (622 loc) · 28.6 KB
/
sqlquery.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// Library to query CodeQuery database files
//
// This library is MIT-licensed, so that it may be used to create plugins
// for editors, IDEs and other software without license restrictions
//
// This license applies only to this file:
//
// Copyright (c) 2011 ruben2020 https://github.com/ruben2020/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
#include <stdio.h>
#include <stdlib.h>
//#include <unistd.h>
#include <algorithm>
#include <sqlite3.h>
#include "small_lib.h"
#include "sqlquery.h"
#define SQL_SYM "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_FUNC_MACRO "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\") AND (symtbl.symType=\"$\" OR symtbl.symType=\"#\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_CLASS_STRUCT "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\") AND (symtbl.symType=\"c\" OR symtbl.symType=\"s\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_CALLINGFUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT callerID FROM calltbl WHERE calledID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_CALLEDFUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT calledID FROM calltbl WHERE callerID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_CALLS_OF_FUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\") AND (symtbl.symType=\"`\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_MEMBERS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT memberID FROM membertbl WHERE groupID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_OWNERCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT groupID FROM membertbl WHERE memberID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_PARENTCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT parentID FROM inherittbl WHERE childID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_CHILDCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT childID FROM inherittbl WHERE parentID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_INCLUDE "SELECT filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName LIKE ? ESCAPE \";\" AND symType=\"~\") INNER JOIN filestbl ON linestbl.fileID=filestbl.fileID;"
#define SQL_FILEPATH "SELECT DISTINCT filePath FROM filestbl WHERE filePath LIKE ? ESCAPE \";\";"
#define SQL_FILESLIST "SELECT DISTINCT filePath FROM filestbl WHERE filePath LIKE ? ESCAPE \";\" ORDER BY filePath ASC;"
#define SQL_AUTOCOMPLETE "SELECT DISTINCT symName FROM symtbl WHERE symName LIKE ? ORDER BY symName LIMIT 20;"
#define SQL_FUNCSINFILE "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE (symtbl.symType=\"$\" OR symtbl.symType=\"#\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_FUNCSINONEFILE "SELECT symtbl.symName,filestbl.filePath,linestbl.linenum FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE (symtbl.symType=\"$\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_FUNCSINONEFILE2 "SELECT symtbl.symName,filestbl.filePath,linestbl.linenum FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE (symtbl.symType=\"$\")) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND linestbl.fileID=?);"
#define SQL_EM_SYM "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=?) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_FUNC_MACRO "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=?) AND (symtbl.symType=\"$\" OR symtbl.symType=\"#\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_CLASS_STRUCT "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=?) AND (symtbl.symType=\"c\" OR symtbl.symType=\"s\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_CALLINGFUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT callerID FROM calltbl WHERE calledID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_CALLEDFUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT calledID FROM calltbl WHERE callerID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_CALLS_OF_FUNC "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=?) AND (symtbl.symType=\"`\") INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_MEMBERS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT memberID FROM membertbl WHERE groupID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_OWNERCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT groupID FROM membertbl WHERE memberID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_PARENTCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT parentID FROM inherittbl WHERE childID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_CHILDCLASS "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT childID FROM inherittbl WHERE parentID IN (SELECT symID FROM symtbl WHERE symName=?)) INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID AND filestbl.filePath LIKE ? ESCAPE \";\");"
#define SQL_EM_INCLUDE "SELECT filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.lineID=linestbl.lineID AND symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=? AND symType=\"~\") INNER JOIN filestbl ON linestbl.fileID=filestbl.fileID;"
#define SQL_EM_FILEPATH "SELECT DISTINCT filePath FROM filestbl WHERE filePath=?;"
#define SQL_EM_FILESLIST "SELECT DISTINCT filePath FROM filestbl WHERE filePath=? ORDER BY filePath ASC;"
#define SQL_DECLARATION "SELECT symtbl.symName,symtbl.symType,filestbl.filePath,linestbl.linenum,linestbl.linetext,linestbl.fileID FROM symtbl INNER JOIN linestbl ON symtbl.symID IN (SELECT symID FROM symtbl WHERE symName=?) AND (symtbl.symType=\"$\" OR symtbl.symType=\"#\" OR symtbl.symType=\"c\" OR symtbl.symType=\"s\") AND symtbl.lineID=linestbl.lineID INNER JOIN filestbl ON (linestbl.fileID=filestbl.fileID) LIMIT 1;"
struct nameasc
{
bool operator()( const sqlqueryresult& lx, const sqlqueryresult& rx ) const {
return lx.symname2.compare(rx.symname2) < 0;
}
};
struct numasc
{
bool operator()( const sqlqueryresult& lx, const sqlqueryresult& rx ) const {
return lx.intLinenum < rx.intLinenum;
}
};
tempstmt::tempstmt()
:m_stmt(NULL)
{
}
tempstmt::~tempstmt()
{
finalize();
}
void tempstmt::finalize(void)
{
sqlite3_finalize(m_stmt);
m_stmt = NULL;
qry.clear();
}
sqlite3_stmt* tempstmt::get(void)
{
return m_stmt;
}
sqlqueryresultlist::sqlqueryresultlist()
:result_type(sqlresultERROR)
,sqlerrmsg("empty")
{
}
sqlqueryresultlist::sqlqueryresultlist(const sqlqueryresultlist& copy)
{
result_type = copy.result_type;
sqlerrmsg = copy.sqlerrmsg;
resultlist = copy.resultlist;
}
sqlqueryresultlist& sqlqueryresultlist::operator= (const sqlqueryresultlist& copy)
{
if (© != this)
{
result_type = copy.result_type;
sqlerrmsg = copy.sqlerrmsg;
resultlist.clear();
resultlist = copy.resultlist;
}
return *this;
}
void sqlqueryresultlist::sort_by_name(void)
{
std::sort(resultlist.begin(), resultlist.end(), nameasc());
}
void sqlqueryresultlist::sort_by_linenum(void)
{
std::sort(resultlist.begin(), resultlist.end(), numasc());
}
sqlquery::sqlquery()
:m_db(NULL)
{
}
sqlquery::~sqlquery()
{
close_dbfile();
}
sqlquery::en_filereadstatus sqlquery::open_dbfile(tStr dbfn)
{
if (dbfn.empty()) return sqlfileOPENERROR;
smartFILE fp;
// Does the file exist?
if (check_fileExists(dbfn.c_str()) == false) {return sqlfileOPENERROR;}
// Try to open the file for reading
fp = fopen(dbfn.c_str(), "r");
if (fp == NULL) {return sqlfileOPENERROR;}
fp.close_file();
int rc = sqlite3_open_v2(dbfn.c_str(),
&m_db, SQLITE_OPEN_READONLY, NULL);
if ((rc != SQLITE_OK)||(m_db == NULL))
{
close_dbfile();
return sqlfileOPENERROR;
}
tempstmt stmt;
sqlite3_exec(m_db, /*"PRAGMA synchronous = OFF;"
"PRAGMA journal_mode = OFF;"
"PRAGMA locking_mode = EXCLUSIVE;"
"PRAGMA automatic_index = FALSE;"*/
"PRAGMA cache_size = 20000;", NULL, 0, NULL);
tStr majorver = read_configtbl("DB_MAJOR_VER", stmt.get());
tStr minorver = read_configtbl("DB_MINOR_VER", stmt.get());
if ((majorver.empty())||(minorver.empty()))
{return sqlfileNOTCORRECTDB;}
if (majorver.compare(tStr("0")) != 0) return sqlfileINCORRECTVER;
if (minorver.compare(tStr("1")) != 0) return sqlfileINCORRECTVER;
m_basepath = read_configtbl("DB_BASE_PATH", stmt.get());
if (m_basepath.empty()) {return sqlfileNOTCORRECTDB;}
rc = sqlite3_prepare_v2(m_db, SQL_AUTOCOMPLETE, strlen(SQL_AUTOCOMPLETE),
&(m_autocompstmt.m_stmt), NULL);
rc = sqlite3_prepare_v2(m_db, SQL_FUNCSINONEFILE, strlen(SQL_FUNCSINONEFILE),
&(m_funclistfilenamestmt.m_stmt), NULL);
rc = sqlite3_prepare_v2(m_db, SQL_FUNCSINONEFILE2, strlen(SQL_FUNCSINONEFILE2),
&(m_funclistfileidstmt.m_stmt), NULL);
rc = sqlite3_prepare_v2(m_db, SQL_DECLARATION, strlen(SQL_DECLARATION),
&(m_declarationstmt.m_stmt), NULL);
if (rc != SQLITE_OK) {return sqlfileNOTCORRECTDB;}
return sqlfileOK;
}
void sqlquery::close_dbfile(void)
{
m_declarationstmt.finalize();
m_autocompstmt.finalize();
m_funclistfilenamestmt.finalize();
m_funclistfileidstmt.finalize();
m_searchstmt.finalize();
sqlite3_close(m_db);
m_db = NULL;
m_basepath.clear();
}
tStr sqlquery::read_configtbl(const char *key, sqlite3_stmt *stmt)
{
tStr result = "";
if ((key == NULL)||(strlen(key) == 0)||(m_db == NULL)) return result;
int rc;
if (stmt == NULL)
{
rc = sqlite3_prepare_v2(m_db, "SELECT configVal FROM configtbl WHERE configKey=?;",
strlen("SELECT configVal FROM configtbl WHERE configKey=?;"),
&stmt, NULL);
if (rc != SQLITE_OK) {return result;}
}
else sqlite3_reset(stmt);
rc = sqlite3_bind_text(stmt, 1, key, strlen(key), SQLITE_STATIC);
if (rc != SQLITE_OK) {return result;}
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW) {return result;}
result = (const char*) sqlite3_column_text(stmt, 0);
return result;
}
sqlqueryresultlist sqlquery::search_funclist_filename(const char* searchstr)
{
//printf("search_funclist_filename %s\n", searchstr);
sqlqueryresultlist result;
if (searchstr == NULL) return result;
result.result_type = sqlqueryresultlist::sqlresultERROR;
tStr srchterm("%");
srchterm.append(searchstr);
if ((searchstr == NULL)||(strlen(searchstr) < 1)||(m_db == NULL)) return result;
sqlite3_reset(m_funclistfilenamestmt.get());
int rc = sqlite3_bind_text(m_funclistfilenamestmt.get(), 1, srchterm.c_str(), srchterm.size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {printf("Err: %s\n", sqlite3_errmsg(m_db)); return result;}
result = search_func_in_one_file(m_funclistfilenamestmt.get());
return result;
}
sqlqueryresultlist sqlquery::search_funclist_fileid(int& fileid)
{
//printf("search_funclist_fileid %d\n", fileid);
sqlqueryresultlist result;
result.result_type = sqlqueryresultlist::sqlresultERROR;
if ((fileid < 0)||(m_db == NULL)) return result;
sqlite3_reset(m_funclistfileidstmt.get());
int rc = sqlite3_bind_int(m_funclistfileidstmt.get(), 1, fileid);
if (rc != SQLITE_OK) {printf("Err: %s\n", sqlite3_errmsg(m_db)); return result;}
result = search_func_in_one_file(m_funclistfileidstmt.get());
return result;
}
tVecStr sqlquery::search_autocomplete(const char* searchstr)
{
tVecStr result;
//int ctr = 0;
if ((searchstr == NULL)||(strlen(searchstr) < 1)||(m_db == NULL)) return result;
tStr srchterm = process_searchterm_autocomplete(searchstr);
sqlite3_reset(m_autocompstmt.get());
int rc = sqlite3_bind_text(m_autocompstmt.get(), 1, srchterm.c_str(), srchterm.size(), SQLITE_STATIC);
if (rc != SQLITE_OK) {printf("Err: %s\n", sqlite3_errmsg(m_db)); return result;}
do
{
rc = sqlite3_step(m_autocompstmt.get());
if (rc == SQLITE_ROW)
{
result.push_back(tStr((const char*) sqlite3_column_text(m_autocompstmt.get(), 0)));
//if (ctr++ > 300) rc = SQLITE_DONE;
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
printf("Err: %s\n", sqlite3_errmsg(m_db));
}
return result;
}
sqlqueryresultlist sqlquery::search(
tStr searchstr,
sqlquery::en_queryType querytype,
bool exactmatch,
tStr filterstr)
{
sqlqueryresultlist result;
int rc;
bool twoTerms = true;
result.result_type = sqlqueryresultlist::sqlresultERROR;
if ((m_db == NULL)||(searchstr.empty())||(m_basepath.empty())) return result;
tStr sqlqry, srchterm, filterterm;
sqlqueryresultlist::en_resultType resultType = sqlqueryresultlist::sqlresultFULL;
if (exactmatch && (querytype == sqlresultFUNCSINFILE)) {searchstr.insert(0, "%");}
srchterm = process_searchterm(searchstr.c_str(), exactmatch);
if (filterstr.empty()) {filterterm = "%";}
else {filterterm = process_searchterm(filterstr.c_str(), false);}
switch (querytype)
{
case sqlquerySYMBOL:
sqlqry = exactmatch ? SQL_EM_SYM : SQL_SYM;
break;
case sqlresultFUNC_MACRO:
sqlqry = exactmatch ? SQL_EM_FUNC_MACRO : SQL_FUNC_MACRO;
break;
case sqlresultCLASS_STRUCT:
sqlqry = exactmatch ? SQL_EM_CLASS_STRUCT : SQL_CLASS_STRUCT;
break;
case sqlresultINCLUDE:
sqlqry = exactmatch ? SQL_EM_INCLUDE : SQL_INCLUDE;
resultType = sqlqueryresultlist::sqlresultFILE_LINE;
twoTerms = false;
break;
case sqlresultFILEPATH:
sqlqry = exactmatch ? SQL_EM_FILEPATH : SQL_FILEPATH;
resultType = sqlqueryresultlist::sqlresultFILE_ONLY;
twoTerms = false;
break;
case sqlresultFILESLIST:
sqlqry = exactmatch ? SQL_EM_FILESLIST : SQL_FILESLIST;
resultType = sqlqueryresultlist::sqlresultFILE_ONLY;
twoTerms = false;
break;
case sqlresultCALLINGFUNC:
sqlqry = exactmatch ? SQL_EM_CALLINGFUNC : SQL_CALLINGFUNC;
break;
case sqlresultCALLEDFUNC:
sqlqry = exactmatch ? SQL_EM_CALLEDFUNC : SQL_CALLEDFUNC;
break;
case sqlresultCALLSOFFUNC:
sqlqry = exactmatch ? SQL_EM_CALLS_OF_FUNC : SQL_CALLS_OF_FUNC;
break;
case sqlresultMEMBERS:
sqlqry = exactmatch ? SQL_EM_MEMBERS : SQL_MEMBERS;
break;
case sqlresultOWNERCLASS:
sqlqry = exactmatch ? SQL_EM_OWNERCLASS : SQL_OWNERCLASS;
break;
case sqlresultPARENTCLASS:
sqlqry = exactmatch ? SQL_EM_PARENTCLASS : SQL_PARENTCLASS;
break;
case sqlresultCHILDCLASS:
sqlqry = exactmatch ? SQL_EM_CHILDCLASS : SQL_CHILDCLASS;
break;
case sqlresultFUNCSINFILE:
sqlqry = SQL_FUNCSINFILE;
twoTerms = false;
break;
case sqlresultAUTOCOMPLETE:
resultType = sqlqueryresultlist::sqlresultSYM_ONLY;
twoTerms = false;
break;
default:
result.sqlerrmsg = "Unknown search type";
return result;
}
if (m_searchstmt.qry.compare(sqlqry) != 0)
{
sqlite3_finalize(m_searchstmt.get());
rc = sqlite3_prepare_v2(m_db, sqlqry.c_str(),
sqlqry.size(),
&(m_searchstmt.m_stmt), NULL);
m_searchstmt.qry = (rc == SQLITE_OK) ? sqlqry : "";
}
else
{
rc = sqlite3_reset(m_searchstmt.get());
}
if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
rc = sqlite3_bind_text(m_searchstmt.get(), 1, srchterm.c_str(), srchterm.size(), SQLITE_TRANSIENT);
if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
if (twoTerms)
{
rc = sqlite3_bind_text(m_searchstmt.get(), 2, filterterm.c_str(), filterterm.size(), SQLITE_TRANSIENT);
if (rc != SQLITE_OK) {result.sqlerrmsg = sqlite3_errmsg(m_db); return result;}
}
if (resultType == sqlqueryresultlist::sqlresultFULL) result = search_full(m_searchstmt.get());
else if (resultType == sqlqueryresultlist::sqlresultFILE_LINE) result = search_file_line(m_searchstmt.get());
else if (resultType == sqlqueryresultlist::sqlresultFILE_ONLY)
{
if (querytype == sqlresultFILESLIST)
result = search_filepath_only(m_searchstmt.get());
else
result = search_file_only(m_searchstmt.get());
}
return result;
}
tStr sqlquery::process_searchterm(const char* searchterm, const bool& exactmatch)
{
tStr srchterm, srchterm2;
if (!exactmatch)
{
srchterm2 = add_escape_char(searchterm, '%', ';').c_str();
srchterm2 = add_escape_char( srchterm2.c_str(), '_', ';').c_str();
srchterm = "%";
srchterm += srchterm2;
srchterm += "%";
replacechar( srchterm.begin(), srchterm.end(), '*', '%');
replacechar( srchterm.begin(), srchterm.end(), '?', '_');
}
else srchterm = searchterm;
return srchterm;
}
tStr sqlquery::process_searchterm_autocomplete(const char* searchterm)
{
tStr srchterm(searchterm);
srchterm += "%";
replacechar( srchterm.begin(), srchterm.end(), '*', '%');
replacechar( srchterm.begin(), srchterm.end(), '?', '_');
return srchterm;
}
sqlqueryresultlist sqlquery::search_declaration(const char* searchstr)
{
int rc;
sqlqueryresultlist result;
tStr fp;
sqlite3_stmt* stmt = m_declarationstmt.get();
if ((searchstr == NULL)||(strlen(searchstr) < 1)||(m_db == NULL)) return result;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
sqlite3_reset(stmt);
rc = sqlite3_bind_text(stmt, 1, searchstr, strlen(searchstr), SQLITE_STATIC);
if (rc != SQLITE_OK) {printf("Err: %s\n", sqlite3_errmsg(m_db)); return result;}
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
item.symname = (const char*) sqlite3_column_text(stmt, 0);
item.symtype = (const char*) sqlite3_column_text(stmt, 1);
fp = (const char*) sqlite3_column_text(stmt, 2);
item.linenum = (const char*) sqlite3_column_text(stmt, 3);
item.linetext = (const char*) sqlite3_column_text(stmt, 4);
item.filename = extract_filename(fp.c_str());
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFULL;
}
return result;
}
sqlqueryresultlist sqlquery::search_full(sqlite3_stmt* stmt)
{
int rc;
sqlqueryresultlist result;
tStr fp;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
item.symname = (const char*) sqlite3_column_text(stmt, 0);
item.symtype = (const char*) sqlite3_column_text(stmt, 1);
fp = (const char*) sqlite3_column_text(stmt, 2);
item.linenum = (const char*) sqlite3_column_text(stmt, 3);
item.linetext = (const char*) sqlite3_column_text(stmt, 4);
item.fileid = sqlite3_column_int (stmt, 5);
item.filename = extract_filename(fp.c_str());
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFULL;
}
return result;
}
sqlqueryresultlist sqlquery::search_func_in_one_file(sqlite3_stmt* stmt)
{
int rc;
sqlqueryresultlist result;
tStr fp;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
item.symname = (const char*) sqlite3_column_text(stmt, 0);
fp = (const char*) sqlite3_column_text(stmt, 1);
item.linenum = (const char*) sqlite3_column_text(stmt, 2);
item.filename = extract_filename(fp.c_str());
item.intLinenum = atoi(item.linenum.c_str());
item.symname2 = item.symname;
std::transform(item.symname2.begin(), item.symname2.end(), item.symname2.begin(), ::tolower);
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFUNC_IN_ONE_FILE;
}
return result;
}
sqlqueryresultlist sqlquery::search_file_line(sqlite3_stmt* stmt)
{
int rc;
sqlqueryresultlist result;
tStr fp;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
fp = (const char*) sqlite3_column_text(stmt, 0);
item.linenum = (const char*) sqlite3_column_text(stmt, 1);
item.linetext = (const char*) sqlite3_column_text(stmt, 2);
item.fileid = sqlite3_column_int (stmt, 3);
item.filename = extract_filename(fp.c_str());
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFILE_LINE;
}
return result;
}
sqlqueryresultlist sqlquery::search_file_only(sqlite3_stmt* stmt)
{
int rc;
sqlqueryresultlist result;
tStr fp;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
fp = (const char*) sqlite3_column_text(stmt, 0);
item.linenum = "1";
item.filename = extract_filename(fp.c_str());
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFILE_ONLY;
}
return result;
}
sqlqueryresultlist sqlquery::search_filepath_only(sqlite3_stmt* stmt)
{
int rc;
sqlqueryresultlist result;
tStr fp;
result.result_type = sqlqueryresultlist::sqlresultERROR;
sqlqueryresult item;
do
{
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
{
fp = (const char*) sqlite3_column_text(stmt, 0);
item.linenum = "1";
item.filename = fp;
if (isAbsolutePath(fp) == false)
{
item.filepath = m_basepath;
item.filepath += DIRSEP;
item.filepath += fp;
}
else item.filepath = fp;
result.resultlist.push_back(item);
}
} while (rc == SQLITE_ROW);
if (rc != SQLITE_DONE)
{
result.result_type = sqlqueryresultlist::sqlresultERROR;
result.sqlerrmsg = sqlite3_errmsg(m_db);
}
else
{
result.result_type = sqlqueryresultlist::sqlresultFILE_ONLY;
}
return result;
}