Debian Codegraph @

CodeLite/PHPLookupTable.cpp

raw
1 #include "PHPLookupTable.h"
2
3 #include "PHPEntityClass.h"
4 #include "PHPEntityFunction.h"
5 #include "PHPEntityFunctionAlias.h"
6 #include "PHPEntityNamespace.h"
7 #include "PHPEntityVariable.h"
8 #include "clFilesCollector.h"
9 #include "event_notifier.h"
10 #include "file_logger.h"
11 #include "fileextmanager.h"
12 #include "fileutils.h"
13
14 #include <algorithm>
15 #include <wx/filename.h>
16 #include <wx/log.h>
17 #include <wx/stopwatch.h>
18 #include <wx/tokenzr.h>
19
20 wxDEFINE_EVENT(wxPHP_PARSE_STARTED, clParseEvent);
21 wxDEFINE_EVENT(wxPHP_PARSE_ENDED, clParseEvent);
22 wxDEFINE_EVENT(wxPHP_PARSE_PROGRESS, clParseEvent);
23
24 static wxString PHP_SCHEMA_VERSION = "9.3.0.1";
25
26 //------------------------------------------------
27 // Metadata table
28 //------------------------------------------------
29 const static wxString CREATE_METADATA_TABLE_SQL =
30 "CREATE TABLE IF NOT EXISTS METADATA_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
31 "SCHEMA_NAME TEXT, "
32 "SCHEMA_VERSION TEXT)";
33 const static wxString CREATE_METADATA_TABLE_SQL_IDX1 =
34 "CREATE UNIQUE INDEX IF NOT EXISTS METADATA_TABLE_IDX_1 ON METADATA_TABLE(SCHEMA_NAME)";
35 //------------------------------------------------
36 // Scope table
37 //------------------------------------------------
38 const static wxString CREATE_SCOPE_TABLE_SQL =
39 "CREATE TABLE IF NOT EXISTS SCOPE_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
40 "SCOPE_TYPE INTEGER, " // The scope type: 0 for namespace, 1 for class
41 "SCOPE_ID INTEGER NOT NULL DEFAULT -1, "
42 "NAME TEXT, " // no scope, just the class name
43 "FULLNAME TEXT, " // full path
44 "EXTENDS TEXT DEFAULT '', "
45 "IMPLEMENTS TEXT DEFAULT '', "
46 "USING_TRAITS TEXT DEFAULT '', "
47 "FLAGS INTEGER DEFAULT 0, "
48 "DOC_COMMENT TEXT DEFAULT '', "
49 "LINE_NUMBER INTEGER NOT NULL DEFAULT 0, "
50 "FILE_NAME TEXT DEFAULT '')";
51
52 const static wxString CREATE_SCOPE_TABLE_SQL_IDX1 =
53 "CREATE INDEX IF NOT EXISTS SCOPE_TABLE_IDX_1 ON SCOPE_TABLE(SCOPE_ID)";
54 const static wxString CREATE_SCOPE_TABLE_SQL_IDX2 =
55 "CREATE INDEX IF NOT EXISTS SCOPE_TABLE_IDX_2 ON SCOPE_TABLE(FILE_NAME)";
56 const static wxString CREATE_SCOPE_TABLE_SQL_IDX3 = "CREATE INDEX IF NOT EXISTS SCOPE_TABLE_IDX_3 ON SCOPE_TABLE(NAME)";
57 const static wxString CREATE_SCOPE_TABLE_SQL_IDX4 =
58 "CREATE INDEX IF NOT EXISTS SCOPE_TABLE_IDX_4 ON SCOPE_TABLE(SCOPE_TYPE)";
59 const static wxString CREATE_SCOPE_TABLE_SQL_IDX5 =
60 "CREATE UNIQUE INDEX IF NOT EXISTS SCOPE_TABLE_IDX_5 ON SCOPE_TABLE(FULLNAME)";
61
62 //------------------------------------------------
63 // Function table
64 //------------------------------------------------
65 const static wxString CREATE_FUNCTION_TABLE_SQL =
66 "CREATE TABLE IF NOT EXISTS FUNCTION_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
67 "SCOPE_ID INTEGER NOT NULL DEFAULT -1, "
68 "NAME TEXT, " // no scope, just the function name
69 "FULLNAME TEXT, " // Fullname with scope
70 "SCOPE TEXT, " // Usually, this means the namespace\class
71 "SIGNATURE TEXT, " // Formatted signature
72 "RETURN_VALUE TEXT, " // Fullname (including namespace)
73 "FLAGS INTEGER DEFAULT 0, "
74 "DOC_COMMENT TEXT, "
75 "LINE_NUMBER INTEGER NOT NULL DEFAULT 0, "
76 "FILE_NAME TEXT )";
77
78 const static wxString CREATE_FUNCTION_TABLE_SQL_IDX1 =
79 "CREATE INDEX IF NOT EXISTS FUNCTION_TABLE_IDX_1 ON FUNCTION_TABLE(SCOPE_ID)";
80 const static wxString CREATE_FUNCTION_TABLE_SQL_IDX2 =
81 "CREATE INDEX IF NOT EXISTS FUNCTION_TABLE_IDX_2 ON FUNCTION_TABLE(FILE_NAME)";
82 const static wxString CREATE_FUNCTION_TABLE_SQL_IDX3 =
83 "CREATE UNIQUE INDEX IF NOT EXISTS FUNCTION_TABLE_IDX_3 ON FUNCTION_TABLE(FULLNAME)";
84 const static wxString CREATE_FUNCTION_TABLE_SQL_IDX4 =
85 "CREATE INDEX IF NOT EXISTS FUNCTION_TABLE_IDX_4 ON FUNCTION_TABLE(NAME)";
86 const static wxString CREATE_FUNCTION_TABLE_SQL_IDX5 =
87 "CREATE INDEX IF NOT EXISTS FUNCTION_TABLE_IDX_5 ON FUNCTION_TABLE(LINE_NUMBER)";
88
89 //------------------------------------------------
90 // Function Alias table
91 //------------------------------------------------
92 const static wxString CREATE_FUNCTION_ALIAS_TABLE_SQL =
93 "CREATE TABLE IF NOT EXISTS FUNCTION_ALIAS_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
94 "SCOPE_ID INTEGER NOT NULL DEFAULT -1, "
95 "NAME TEXT, " // no scope, just the function name
96 "REALNAME TEXT, " // The fullname of the actual function we are referencing
97 "FULLNAME TEXT, " // Fullname with scope (of the alias name)
98 "SCOPE TEXT, " // Usually, this means the namespace\class
99 "LINE_NUMBER INTEGER NOT NULL DEFAULT 0, "
100 "FILE_NAME TEXT )";
101
102 const static wxString CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX1 =
103 "CREATE INDEX IF NOT EXISTS FUNCTION_ALIAS_TABLE_IDX_1 ON FUNCTION_ALIAS_TABLE(SCOPE_ID)";
104 const static wxString CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX2 =
105 "CREATE INDEX IF NOT EXISTS FUNCTION_ALIAS_TABLE_IDX_2 ON FUNCTION_ALIAS_TABLE(NAME)";
106 const static wxString CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX3 =
107 "CREATE INDEX IF NOT EXISTS FUNCTION_ALIAS_TABLE_IDX_3 ON FUNCTION_ALIAS_TABLE(REALNAME)";
108 const static wxString CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX4 =
109 "CREATE UNIQUE INDEX IF NOT EXISTS FUNCTION_ALIAS_TABLE_IDX_4 ON FUNCTION_ALIAS_TABLE(NAME,REALNAME,SCOPE_ID)";
110
111 //------------------------------------------------
112 // Variables table
113 //------------------------------------------------
114 const static wxString CREATE_VARIABLES_TABLE_SQL =
115 "CREATE TABLE IF NOT EXISTS VARIABLES_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
116 "SCOPE_ID INTEGER NOT NULL DEFAULT -1, " // for global variable or class member this will be the scope_id parent id
117 "FUNCTION_ID INTEGER NOT NULL DEFAULT -1, " // for function argument
118 "NAME TEXT, " // no scope, just the function name
119 "FULLNAME TEXT, " // Fullname with scope
120 "SCOPE TEXT, " // Usually, this means the namespace\class
121 "TYPEHINT TEXT, " // the Variable type hint
122 "DEFAULT_VALUE TEXT, " // Default value
123 "FLAGS INTEGER DEFAULT 0, "
124 "DOC_COMMENT TEXT, "
125 "LINE_NUMBER INTEGER NOT NULL DEFAULT 0, "
126 "FILE_NAME TEXT )";
127
128 const static wxString CREATE_VARIABLES_TABLE_SQL_IDX1 =
129 "CREATE INDEX IF NOT EXISTS VARIABLES_TABLE_IDX_1 ON VARIABLES_TABLE(SCOPE_ID)";
130 const static wxString CREATE_VARIABLES_TABLE_SQL_IDX2 =
131 "CREATE UNIQUE INDEX IF NOT EXISTS VARIABLES_TABLE_IDX_2 ON VARIABLES_TABLE(SCOPE, NAME, FUNCTION_ID, SCOPE_ID)";
132 const static wxString CREATE_VARIABLES_TABLE_SQL_IDX3 =
133 "CREATE INDEX IF NOT EXISTS VARIABLES_TABLE_IDX_3 ON VARIABLES_TABLE(FILE_NAME)";
134 const static wxString CREATE_VARIABLES_TABLE_SQL_IDX4 =
135 "CREATE INDEX IF NOT EXISTS VARIABLES_TABLE_IDX_4 ON VARIABLES_TABLE(FUNCTION_ID)";
136
137 //------------------------------------------------
138 // Variables table
139 //------------------------------------------------
140 const static wxString CREATE_PHPDOC_VAR_TABLE_SQL =
141 "CREATE TABLE IF NOT EXISTS PHPDOC_VAR_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
142 "SCOPE_ID INTEGER NOT NULL DEFAULT -1, " // for global variable or class member this will be the scope_id parent id
143 "NAME TEXT, " // the name
144 "TYPE TEXT, " // the type, full path
145 "LINE_NUMBER INTEGER NOT NULL DEFAULT 0, "
146 "FILE_NAME TEXT )";
147
148 const static wxString CREATE_PHPDOC_VAR_TABLE_SQL_IDX1 =
149 "CREATE INDEX IF NOT EXISTS PHPDOC_VAR_TABLE_SQL_IDX1 ON PHPDOC_VAR_TABLE(SCOPE_ID)";
150 const static wxString CREATE_PHPDOC_VAR_TABLE_SQL_IDX2 =
151 "CREATE INDEX IF NOT EXISTS PHPDOC_VAR_TABLE_SQL_IDX2 ON PHPDOC_VAR_TABLE(FILE_NAME)";
152
153 //------------------------------------------------
154 // Files table
155 //------------------------------------------------
156 const static wxString CREATE_FILES_TABLE_SQL =
157 "CREATE TABLE IF NOT EXISTS FILES_TABLE(ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "
158 "FILE_NAME TEXT, " // for global variable or class member this will be the scope_id parent id
159 "LAST_UPDATED INTEGER NOT NULL DEFAULT 0" // for function argument
160 ")";
161 const static wxString CREATE_FILES_TABLE_SQL_IDX1 =
162 "CREATE UNIQUE INDEX IF NOT EXISTS FILES_TABLE_IDX_1 ON FILES_TABLE(FILE_NAME)";
163
164 PHPLookupTable::PHPLookupTable()
165 : m_sizeLimit(50)
166 {
167 }
168
169 PHPLookupTable::~PHPLookupTable() { Close(); }
170
171 PHPEntityBase::Ptr_t PHPLookupTable::FindMemberOf(wxLongLong parentDbId, const wxString& exactName, size_t flags)
172 {
173 // find the entity
174 PHPEntityBase::Ptr_t scope = DoFindScope(parentDbId);
175 if(scope && scope->Cast<PHPEntityClass>()) {
176 std::vector<wxLongLong> parents;
177 std::set<wxLongLong> parentsVisited;
178
179 DoGetInheritanceParentIDs(scope, parents, parentsVisited, flags & kLookupFlags_Parent);
180 // std::reverse(parents.begin(), parents.end());
181
182 // Parents should now contain an ordered list of all the inheritance
183 for(size_t i = 0; i < parents.size(); ++i) {
184 PHPEntityBase::Ptr_t match = DoFindMemberOf(parents.at(i), exactName);
185 if(match) {
186 PHPEntityBase::List_t matches;
187 matches.push_back(match);
188 DoFixVarsDocComment(matches, parentDbId);
189 return match;
190 }
191 }
192 } else {
193 // namespace
194 return DoFindMemberOf(parentDbId, exactName, true);
195 }
196 return PHPEntityBase::Ptr_t(NULL);
197 }
198
199 PHPEntityBase::Ptr_t PHPLookupTable::FindScope(const wxString& fullname)
200 {
201 wxString scopeName = fullname;
202 scopeName.Trim().Trim(false);
203 if(scopeName.EndsWith("\\") && scopeName.length() > 1) {
204 scopeName.RemoveLast();
205 }
206 return DoFindScope(scopeName);
207 }
208
209 void PHPLookupTable::Open(const wxFileName& dbfile)
210 {
211 try {
212
213 if(dbfile.Exists()) {
214 // Check for its integrity. If the database is corrupted,
215 // it will be deleted
216 EnsureIntegrity(dbfile);
217 }
218
219 wxFileName::Mkdir(dbfile.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
220 m_db.Open(dbfile.GetFullPath());
221 m_db.SetBusyTimeout(10); // Don't lock when we cant access to the database
222 m_filename = dbfile;
223 CreateSchema();
224
225 } catch(wxSQLite3Exception& e) {
226 clWARNING() << "PHPLookupTable::Open" << e.GetMessage() << clEndl;
227 }
228 }
229
230 void PHPLookupTable::Open(const wxString& workspacePath)
231 {
232 wxFileName fnDBFile(workspacePath, "phpsymbols.db");
233
234 // ensure that the database directory exists
235 fnDBFile.AppendDir(".codelite");
236 fnDBFile.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
237 Open(fnDBFile);
238 }
239
240 void PHPLookupTable::CreateSchema()
241 {
242 wxString schemaVersion;
243 try {
244 wxString sql;
245 sql = wxT("PRAGMA journal_mode = ON;");
246 m_db.ExecuteUpdate(sql);
247
248 sql = wxT("PRAGMA synchronous = OFF;");
249 m_db.ExecuteUpdate(sql);
250
251 sql = wxT("PRAGMA temp_store = MEMORY;");
252 m_db.ExecuteUpdate(sql);
253
254 wxSQLite3Statement st =
255 m_db.PrepareStatement("select SCHEMA_VERSION from METADATA_TABLE where SCHEMA_NAME=:SCHEMA_NAME");
256 st.Bind(st.GetParamIndex(":SCHEMA_NAME"), "CODELITEPHP");
257 wxSQLite3ResultSet res = st.ExecuteQuery();
258 if(res.NextRow()) {
259 schemaVersion = res.GetString("SCHEMA_VERSION");
260 }
261 } catch(wxSQLite3Exception& e) {
262 wxUnusedVar(e);
263 }
264
265 if(schemaVersion != PHP_SCHEMA_VERSION) {
266 // Drop the tables and recreate the schema from scratch
267 m_db.ExecuteUpdate("drop table if exists SCHEMA_VERSION");
268 m_db.ExecuteUpdate("drop table if exists SCOPE_TABLE");
269 m_db.ExecuteUpdate("drop table if exists FUNCTION_TABLE");
270 m_db.ExecuteUpdate("drop table if exists FUNCTION_ALIAS_TABLE");
271 m_db.ExecuteUpdate("drop table if exists VARIABLES_TABLE");
272 m_db.ExecuteUpdate("drop table if exists FILES_TABLE");
273 m_db.ExecuteUpdate("drop table if exists PHPDOC_VAR_TABLE");
274 }
275
276 try {
277
278 // Metadata
279 m_db.ExecuteUpdate(CREATE_METADATA_TABLE_SQL);
280 m_db.ExecuteUpdate(CREATE_METADATA_TABLE_SQL_IDX1);
281
282 // class / namespace table "scope"
283 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL);
284 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL_IDX1);
285 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL_IDX2);
286 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL_IDX3);
287 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL_IDX4);
288 m_db.ExecuteUpdate(CREATE_SCOPE_TABLE_SQL_IDX5);
289
290 // function table
291 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL);
292 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL_IDX1);
293 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL_IDX2);
294 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL_IDX3);
295 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL_IDX4);
296 m_db.ExecuteUpdate(CREATE_FUNCTION_TABLE_SQL_IDX5);
297
298 // function alias table
299 m_db.ExecuteUpdate(CREATE_FUNCTION_ALIAS_TABLE_SQL);
300 m_db.ExecuteUpdate(CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX1);
301 m_db.ExecuteUpdate(CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX2);
302 m_db.ExecuteUpdate(CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX3);
303 m_db.ExecuteUpdate(CREATE_FUNCTION_ALIAS_TABLE_SQL_IDX4);
304
305 // variables (function args, globals class members and consts)
306 m_db.ExecuteUpdate(CREATE_VARIABLES_TABLE_SQL);
307 m_db.ExecuteUpdate(CREATE_VARIABLES_TABLE_SQL_IDX1);
308 m_db.ExecuteUpdate(CREATE_VARIABLES_TABLE_SQL_IDX2);
309 m_db.ExecuteUpdate(CREATE_VARIABLES_TABLE_SQL_IDX3);
310 m_db.ExecuteUpdate(CREATE_VARIABLES_TABLE_SQL_IDX4);
311
312 // phpdoc var table
313 m_db.ExecuteUpdate(CREATE_PHPDOC_VAR_TABLE_SQL);
314 m_db.ExecuteUpdate(CREATE_PHPDOC_VAR_TABLE_SQL_IDX1);
315 m_db.ExecuteUpdate(CREATE_PHPDOC_VAR_TABLE_SQL_IDX2);
316
317 // Files
318 m_db.ExecuteUpdate(CREATE_FILES_TABLE_SQL);
319 m_db.ExecuteUpdate(CREATE_FILES_TABLE_SQL_IDX1);
320
321 // Update the schema version
322 wxSQLite3Statement st =
323 m_db.PrepareStatement("replace into METADATA_TABLE (ID, SCHEMA_NAME, SCHEMA_VERSION) VALUES (NULL, "
324 ":SCHEMA_NAME, :SCHEMA_VERSION)");
325 st.Bind(st.GetParamIndex(":SCHEMA_NAME"), "CODELITEPHP");
326 st.Bind(st.GetParamIndex(":SCHEMA_VERSION"), PHP_SCHEMA_VERSION);
327 st.ExecuteUpdate();
328
329 } catch(wxSQLite3Exception& e) {
330 clWARNING() << "PHPLookupTable::CreateSchema" << e.GetMessage() << endl;
331 }
332 }
333
334 void PHPLookupTable::UpdateSourceFile(PHPSourceFile& source, bool autoCommit)
335 {
336 try {
337 if(autoCommit)
338 m_db.Begin();
339
340 // Delete all entries for this file
341 DeleteFileEntries(source.GetFilename(), false);
342
343 // Store new entries
344 PHPEntityBase::Ptr_t topNamespace = source.Namespace();
345 if(topNamespace) {
346 topNamespace->StoreRecursive(this);
347 UpdateFileLastParsedTimestamp(source.GetFilename());
348 }
349
350 // Store defines
351 // --------------
352 // 'defines' are handled separately as they dont really comply to the standard PHP rules
353 // define() will define constants exactly as specified.
354 // The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").
355 // <?php
356 // namespace test;
357 // define('MESSAGE', 'Hello world!');
358 // ?>
359 // In the above code, the constant 'MESSAGE' is defined in the GLOBAL namespace even though the
360 // define was called inside namespace 'test'
361 // For this reason, we need get the list of defined parsed in the source file and associate them
362 // with their namespace (we either load the namespace from the database or create one)
363
364 if(!source.GetDefines().empty()) {
365 const PHPEntityBase::List_t& defines = source.GetDefines();
366 PHPEntityBase::List_t::const_iterator iter = defines.begin();
367 PHPEntityBase::Map_t nsMap;
368 for(; iter != defines.end(); ++iter) {
369 PHPEntityBase::Ptr_t pDefine = *iter;
370 PHPEntityBase::Ptr_t pNamespace(NULL);
371
372 wxString nameSpaceName, shortName;
373 DoSplitFullname(pDefine->GetFullName(), nameSpaceName, shortName);
374
375 PHPEntityBase::Map_t::iterator nsIter = nsMap.find(nameSpaceName);
376 if(nsIter == nsMap.end()) {
377 // we did not load this namespace yet => load and cache it
378 pNamespace = CreateNamespaceForDefine(pDefine);
379 nsMap.insert(std::make_pair(pNamespace->GetFullName(), pNamespace));
380
381 } else {
382 // We already loaded this namespace
383 pNamespace = nsIter->second;
384 }
385 pNamespace->AddChild(pDefine);
386 }
387
388 // Now, loop over the namespace map and store all entries
389 PHPEntityBase::Map_t::iterator nsIter = nsMap.begin();
390 for(; nsIter != nsMap.end(); ++nsIter) {
391 nsIter->second->StoreRecursive(this);
392 }
393 }
394
395 if(autoCommit)
396 m_db.Commit();
397
398 } catch(wxSQLite3Exception& e) {
399 if(autoCommit)
400 m_db.Rollback();
401 clWARNING() << "PHPLookupTable::SaveSourceFile" << e.GetMessage() << endl;
402 }
403 }
404
405 PHPEntityBase::Ptr_t PHPLookupTable::DoFindMemberOf(wxLongLong parentDbId, const wxString& exactName,
406 bool parentIsNamespace)
407 {
408 // Find members of of parentDbID
409 try {
410 PHPEntityBase::List_t matches;
411 {
412 wxString sql;
413 sql << "SELECT * from FUNCTION_TABLE WHERE SCOPE_ID=" << parentDbId << " AND NAME='" << exactName << "'";
414 wxSQLite3Statement st = m_db.PrepareStatement(sql);
415 wxSQLite3ResultSet res = st.ExecuteQuery();
416
417 while(res.NextRow()) {
418 PHPEntityBase::Ptr_t match(new PHPEntityFunction());
419 match->FromResultSet(res);
420 matches.push_back(match);
421 }
422 }
423
424 if(matches.empty()) {
425 // Search functions alias table
426 wxString sql;
427 sql << "SELECT * from FUNCTION_ALIAS_TABLE WHERE SCOPE_ID=" << parentDbId << " AND NAME='" << exactName
428 << "'";
429 wxSQLite3Statement st = m_db.PrepareStatement(sql);
430 wxSQLite3ResultSet res = st.ExecuteQuery();
431
432 while(res.NextRow()) {
433 PHPEntityBase::Ptr_t match(new PHPEntityFunctionAlias());
434 match->FromResultSet(res);
435 PHPEntityBase::Ptr_t pFunc = FindFunction(match->Cast<PHPEntityFunctionAlias>()->GetRealname());
436 if(pFunc) {
437 match->Cast<PHPEntityFunctionAlias>()->SetFunc(pFunc);
438 matches.push_back(match);
439 }
440 }
441 }
442
443 if(matches.empty() && parentIsNamespace) {
444 // search the scope table as well
445 wxString sql;
446 sql << "SELECT * from SCOPE_TABLE WHERE SCOPE_ID=" << parentDbId << " AND NAME='" << exactName << "'";
447 wxSQLite3Statement st = m_db.PrepareStatement(sql);
448 wxSQLite3ResultSet res = st.ExecuteQuery();
449
450 while(res.NextRow()) {
451 ePhpScopeType st = kPhpScopeTypeAny;
452 st =
453 res.GetInt("SCOPE_TYPE", 1) == kPhpScopeTypeNamespace ? kPhpScopeTypeNamespace : kPhpScopeTypeClass;
454
455 PHPEntityBase::Ptr_t match = NewEntity("SCOPE_TABLE", st);
456 if(match) {
457 match->FromResultSet(res);
458 matches.push_back(match);
459 }
460 }
461 }
462
463 if(matches.empty()) {
464 // Could not find a match in the function table, check the variable table
465 wxString sql;
466 wxString nameWDollar, namwWODollar;
467 nameWDollar = exactName;
468 if(exactName.StartsWith("$")) {
469 namwWODollar = exactName.Mid(1);
470 } else {
471 namwWODollar = exactName;
472 nameWDollar.Prepend("$");
473 }
474
475 sql << "SELECT * from VARIABLES_TABLE WHERE SCOPE_ID=" << parentDbId << " AND NAME IN ('" << nameWDollar
476 << "', '" << namwWODollar << "')";
477 wxSQLite3Statement st = m_db.PrepareStatement(sql);
478 wxSQLite3ResultSet res = st.ExecuteQuery();
479
480 while(res.NextRow()) {
481 PHPEntityBase::Ptr_t match(new PHPEntityVariable());
482 match->FromResultSet(res);
483 matches.push_back(match);
484 }
485
486 // Fix variables type using the PHPDOC_VAR_TABLE content for this class
487 DoFixVarsDocComment(matches, parentDbId);
488
489 if(matches.empty() || matches.size() > 1) {
490 return PHPEntityBase::Ptr_t(NULL);
491 } else {
492 return (*matches.begin());
493 }
494 } else if(matches.size() > 1) {
495 // we found more than 1 match in the function table
496 // return NULL
497 return PHPEntityBase::Ptr_t(NULL);
498 } else {
499 // exactly one match was found in the function table
500 // return it
501 return (*matches.begin());
502 }
503
504 } catch(wxSQLite3Exception& e) {
505 clWARNING() << "PHPLookupTable::DoFindMemberOf" << e.GetMessage() << endl;
506 }
507 return PHPEntityBase::Ptr_t(NULL);
508 }
509
510 void PHPLookupTable::DoGetInheritanceParentIDs(PHPEntityBase::Ptr_t cls, std::vector<wxLongLong>& parents,
511 std::set<wxLongLong>& parentsVisited, bool excludeSelf)
512 {
513 if(!excludeSelf) {
514 parents.push_back(cls->GetDbId());
515 }
516
517 parentsVisited.insert(cls->GetDbId());
518 wxArrayString parentsArr = cls->Cast<PHPEntityClass>()->GetInheritanceArray();
519 for(size_t i = 0; i < parentsArr.GetCount(); ++i) {
520 PHPEntityBase::Ptr_t parent = FindClass(parentsArr.Item(i));
521 if(parent && !parentsVisited.count(parent->GetDbId())) {
522 DoGetInheritanceParentIDs(parent, parents, parentsVisited, false);
523 }
524 parent.Reset(nullptr);
525 }
526 }
527
528 PHPEntityBase::Ptr_t PHPLookupTable::DoFindScope(const wxString& fullname, ePhpScopeType scopeType)
529 {
530 // locate the scope
531 try {
532 wxString sql;
533
534 // limit by 2 for performance reason
535 // we will return NULL incase the number of matches is greater than 1...
536 sql << "SELECT * from SCOPE_TABLE WHERE FULLNAME='" << fullname << "'";
537 if(scopeType != kPhpScopeTypeAny) {
538 sql << " AND SCOPE_TYPE = " << static_cast<int>(scopeType);
539 }
540 sql << " LIMIT 2 ";
541
542 wxSQLite3Statement st = m_db.PrepareStatement(sql);
543 wxSQLite3ResultSet res = st.ExecuteQuery();
544 PHPEntityBase::Ptr_t match(NULL);
545
546 while(res.NextRow()) {
547 if(match) {
548 // only one match
549 return PHPEntityBase::Ptr_t(NULL);
550 }
551
552 int scopeType = res.GetInt("SCOPE_TYPE", 1);
553 if(scopeType == 0) {
554 // namespace
555 match.Reset(new PHPEntityNamespace());
556 } else {
557 // class
558 match.Reset(new PHPEntityClass());
559 }
560 match->FromResultSet(res);
561 }
562 return match;
563
564 } catch(wxSQLite3Exception& e) {
565 clWARNING() << "PHPLookupTable::FindScope" << e.GetMessage() << endl;
566 }
567 return PHPEntityBase::Ptr_t(NULL);
568 }
569
570 PHPEntityBase::Ptr_t PHPLookupTable::FindClass(const wxString& fullname)
571 {
572 return DoFindScope(fullname, kPhpScopeTypeClass);
573 }
574
575 PHPEntityBase::Ptr_t PHPLookupTable::DoFindScope(wxLongLong id, ePhpScopeType scopeType)
576 {
577 // locate the scope
578 try {
579 wxString sql;
580
581 // limit by 2 for performance reason
582 // we will return NULL incase the number of matches is greater than 1...
583 sql << "SELECT * from SCOPE_TABLE WHERE ID=" << id;
584 if(scopeType != kPhpScopeTypeAny) {
585 sql << " AND SCOPE_TYPE = " << static_cast<int>(scopeType);
586 }
587 sql << " LIMIT 1";
588
589 wxSQLite3Statement st = m_db.PrepareStatement(sql);
590 wxSQLite3ResultSet res = st.ExecuteQuery();
591
592 if(res.NextRow()) {
593 PHPEntityBase::Ptr_t match(NULL);
594 int scopeType = res.GetInt("SCOPE_TYPE", 1);
595 if(scopeType == kPhpScopeTypeNamespace) {
596 // namespace
597 match.Reset(new PHPEntityNamespace());
598 } else {
599 // class
600 match.Reset(new PHPEntityClass());
601 }
602 match->FromResultSet(res);
603 return match;
604 }
605 } catch(wxSQLite3Exception& e) {
606 clWARNING() << "PHPLookupTable::DoFindScope" << e.GetMessage() << endl;
607 }
608 return PHPEntityBase::Ptr_t(NULL);
609 }
610
611 PHPEntityBase::Ptr_t PHPLookupTable::FindClass(wxLongLong id) { return DoFindScope(id, kPhpScopeTypeClass); }
612
613 PHPEntityBase::List_t PHPLookupTable::FindChildren(wxLongLong parentId, size_t flags, const wxString& nameHint)
614 {
615 PHPEntityBase::List_t matches, matchesNoAbstracts;
616 PHPEntityBase::Ptr_t scope = DoFindScope(parentId);
617 if(scope && scope->Is(kEntityTypeClass)) {
618 std::vector<wxLongLong> parents;
619 std::set<wxLongLong> parentsVisited;
620
621 DoGetInheritanceParentIDs(scope, parents, parentsVisited, flags & kLookupFlags_Parent);
622 // Reverse the order of the parents
623 std::reverse(parents.begin(), parents.end());
624
625 for(size_t i = 0; i < parents.size(); ++i) {
626 DoFindChildren(matches, parents.at(i), flags, nameHint);
627 }
628
629 // Filter out abstract functions
630 if(!(flags & kLookupFlags_IncludeAbstractMethods)) {
631 PHPEntityBase::List_t::iterator iter = matches.begin();
632 for(; iter != matches.end(); ++iter) {
633 PHPEntityBase::Ptr_t child = *iter;
634 if(child->Is(kEntityTypeFunction) && child->HasFlag(kFunc_Abstract))
635 continue;
636 matchesNoAbstracts.push_back(child);
637 }
638 matches.swap(matchesNoAbstracts);
639 }
640 } else if(scope && scope->Is(kEntityTypeNamespace)) {
641 DoFindChildren(matches, parentId, flags | kLookupFlags_NameHintIsScope, nameHint);
642 }
643 return matches;
644 }
645
646 PHPEntityBase::List_t PHPLookupTable::LoadFunctionArguments(wxLongLong parentId)
647 {
648 PHPEntityBase::List_t matches;
649
650 try {
651 {
652 // load functions
653 wxString sql;
654 sql << "SELECT * from VARIABLES_TABLE WHERE FUNCTION_ID=" << parentId << " ORDER BY ID ASC";
655
656 wxSQLite3Statement st = m_db.PrepareStatement(sql);
657 wxSQLite3ResultSet res = st.ExecuteQuery();
658
659 while(res.NextRow()) {
660 PHPEntityBase::Ptr_t match(new PHPEntityVariable());
661 match->FromResultSet(res);
662 matches.push_back(match);
663 }
664 }
665 } catch(wxSQLite3Exception& e) {
666 clWARNING() << "PHPLookupTable::LoadFunctionArguments" << e.GetMessage() << endl;
667 }
668 return matches;
669 }
670
671 wxString PHPLookupTable::EscapeWildCards(const wxString& str)
672 {
673 wxString s(str);
674 s.Replace(wxT("_"), wxT("^_"));
675 return s;
676 }
677
678 void PHPLookupTable::DoAddLimit(wxString& sql) { sql << " LIMIT " << m_sizeLimit; }
679
680 void PHPLookupTable::DoAddNameFilter(wxString& sql, const wxString& nameHint, size_t flags)
681 {
682 wxString name = nameHint;
683 name.Trim().Trim(false);
684
685 if(name.IsEmpty()) {
686 sql.Trim();
687 if(sql.EndsWith("AND") || sql.EndsWith("and")) {
688 sql.RemoveLast(3);
689 }
690 sql << " ";
691 return;
692 }
693
694 if(flags & kLookupFlags_ExactMatch && !name.IsEmpty()) {
695 sql << " NAME = '" << name << "'";
696
697 } else if(flags & kLookupFlags_Contains && !name.IsEmpty()) {
698 sql << " NAME LIKE '%%" << EscapeWildCards(name) << "%%' ESCAPE '^'";
699
700 } else if(flags & kLookupFlags_StartsWith && !name.IsEmpty()) {
701 sql << " NAME LIKE '" << EscapeWildCards(name) << "%%' ESCAPE '^'";
702 }
703 }
704
705 void PHPLookupTable::LoadAllByFilter(PHPEntityBase::List_t& matches, const wxString& nameHint, eLookupFlags flags)
706 {
707 try {
708 // Split the nameHint to spaces/tabs
709 LoadFromTableByNameHint(matches, "SCOPE_TABLE", nameHint, flags);
710 LoadFromTableByNameHint(matches, "FUNCTION_TABLE", nameHint, flags);
711
712 } catch(wxSQLite3Exception& e) {
713 clWARNING() << "PHPLookupTable::LoadAllByFilter:" << e.GetMessage() << clEndl;
714 }
715 }
716
717 PHPEntityBase::Ptr_t PHPLookupTable::NewEntity(const wxString& tableName, ePhpScopeType scopeType)
718 {
719 if(tableName == "FUNCTION_TABLE") {
720 return PHPEntityBase::Ptr_t(new PHPEntityFunction());
721 } else if(tableName == "VARIABLES_TABLE") {
722 return PHPEntityBase::Ptr_t(new PHPEntityVariable());
723 } else if(tableName == "SCOPE_TABLE" && scopeType == kPhpScopeTypeNamespace) {
724 return PHPEntityBase::Ptr_t(new PHPEntityNamespace());
725 } else if(tableName == "SCOPE_TABLE" && scopeType == kPhpScopeTypeClass) {
726 return PHPEntityBase::Ptr_t(new PHPEntityClass());
727 } else {
728 return PHPEntityBase::Ptr_t(NULL);
729 }
730 }
731
732 void PHPLookupTable::LoadFromTableByNameHint(PHPEntityBase::List_t& matches, const wxString& tableName,
733 const wxString& nameHint, eLookupFlags flags)
734 {
735 wxArrayString parts = ::wxStringTokenize(nameHint, " \t", wxTOKEN_STRTOK);
736 if(parts.IsEmpty()) {
737 return;
738 }
739
740 // Build the filter query
741 wxString filterQuery = "where ";
742 wxString sql;
743 for(size_t i = 0; i < parts.size(); ++i) {
744 wxString tmpName = parts.Item(i);
745 tmpName.Replace(wxT("_"), wxT("^_"));
746 filterQuery << "fullname like '%%" << tmpName << "%%' " << ((i == (parts.size() - 1)) ? "" : "AND ");
747 }
748 sql << "select * from " << tableName << " " << filterQuery << " ESCAPE '^' ";
749 DoAddLimit(sql);
750
751 try {
752 wxSQLite3Statement st = m_db.PrepareStatement(sql);
753 wxSQLite3ResultSet res = st.ExecuteQuery();
754
755 while(res.NextRow()) {
756 ePhpScopeType st = kPhpScopeTypeAny;
757 if(tableName == "SCOPE_TABLE") {
758 st =
759 res.GetInt("SCOPE_TYPE", 1) == kPhpScopeTypeNamespace ? kPhpScopeTypeNamespace : kPhpScopeTypeClass;
760 }
761
762 PHPEntityBase::Ptr_t match = NewEntity(tableName, st);
763 if(match) {
764 match->FromResultSet(res);
765 matches.push_back(match);
766 }
767 }
768 } catch(wxSQLite3Exception& e) {
769 clWARNING() << "PHPLookupTable::LoadFromTableByNameHint:" << sql << ":" << e.GetMessage() << clEndl;
770 }
771 }
772
773 void PHPLookupTable::DeleteFileEntries(const wxFileName& filename, bool autoCommit)
774 {
775 try {
776 if(autoCommit)
777 m_db.Begin();
778 {
779 // When deleting from the 'SCOPE_TABLE' don't remove namespaces
780 // since they can be still be pointed by other entries in the database
781 wxString sql;
782 sql << "delete from SCOPE_TABLE where FILE_NAME=:FILE_NAME AND SCOPE_TYPE != "
783 << (int)kPhpScopeTypeNamespace;
784 wxSQLite3Statement st = m_db.PrepareStatement(sql);
785 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
786 st.ExecuteUpdate();
787 }
788
789 {
790 wxString sql;
791 sql << "delete from FUNCTION_TABLE where FILE_NAME=:FILE_NAME";
792 wxSQLite3Statement st = m_db.PrepareStatement(sql);
793 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
794 st.ExecuteUpdate();
795 }
796
797 {
798 wxString sql;
799 sql << "delete from FUNCTION_ALIAS_TABLE where FILE_NAME=:FILE_NAME";
800 wxSQLite3Statement st = m_db.PrepareStatement(sql);
801 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
802 st.ExecuteUpdate();
803 }
804
805 {
806 wxString sql;
807 sql << "delete from VARIABLES_TABLE where FILE_NAME=:FILE_NAME";
808 wxSQLite3Statement st = m_db.PrepareStatement(sql);
809 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
810 st.ExecuteUpdate();
811 }
812
813 {
814 wxString sql;
815 sql << "delete from FILES_TABLE where FILE_NAME=:FILE_NAME";
816 wxSQLite3Statement st = m_db.PrepareStatement(sql);
817 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
818 st.ExecuteUpdate();
819 }
820
821 {
822 wxString sql;
823 sql << "delete from PHPDOC_VAR_TABLE where FILE_NAME=:FILE_NAME";
824 wxSQLite3Statement st = m_db.PrepareStatement(sql);
825 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
826 st.ExecuteUpdate();
827 }
828
829 if(autoCommit)
830 m_db.Commit();
831 } catch(wxSQLite3Exception& e) {
832 if(autoCommit)
833 m_db.Rollback();
834 clWARNING() << "PHPLookupTable::DeleteFileEntries" << e.GetMessage() << endl;
835 }
836 }
837
838 void PHPLookupTable::Close()
839 {
840 try {
841 if(m_db.IsOpen()) {
842 m_db.Close();
843 }
844 m_filename.Clear();
845 m_allClasses.clear();
846
847 } catch(wxSQLite3Exception& e) {
848 clWARNING() << "PHPLookupTable::Close" << e.GetMessage() << endl;
849 }
850 }
851
852 bool PHPLookupTable::IsOpened() const { return m_db.IsOpen(); }
853
854 void PHPLookupTable::DoFindChildren(PHPEntityBase::List_t& matches, wxLongLong parentId, size_t flags,
855 const wxString& nameHint)
856 {
857 // Find members of of parentDbID
858 try {
859 // Load classes
860 if(!(flags & kLookupFlags_FunctionsAndConstsOnly)) {
861 wxString sql;
862 sql << "SELECT * from SCOPE_TABLE WHERE SCOPE_ID=" << parentId << " AND SCOPE_TYPE = 1 AND ";
863 DoAddNameFilter(sql, nameHint, flags);
864 DoAddLimit(sql);
865
866 wxSQLite3Statement st = m_db.PrepareStatement(sql);
867 wxSQLite3ResultSet res = st.ExecuteQuery();
868
869 while(res.NextRow()) {
870 PHPEntityBase::Ptr_t match(new PHPEntityClass());
871 match->FromResultSet(res);
872 matches.push_back(match);
873 }
874 }
875
876 {
877 // load functions
878 wxString sql;
879 sql << "SELECT * from FUNCTION_TABLE WHERE SCOPE_ID=" << parentId << " AND ";
880 DoAddNameFilter(sql, nameHint, flags);
881 DoAddLimit(sql);
882
883 wxSQLite3Statement st = m_db.PrepareStatement(sql);
884 wxSQLite3ResultSet res = st.ExecuteQuery();
885
886 while(res.NextRow()) {
887 PHPEntityBase::Ptr_t match(new PHPEntityFunction());
888 match->FromResultSet(res);
889 bool isStaticFunction = match->HasFlag(kFunc_Static);
890 if(isStaticFunction) {
891 // always return static functions
892 matches.push_back(match);
893
894 } else {
895 // Non static function.
896 if(!(flags & kLookupFlags_Static)) {
897 matches.push_back(match);
898 }
899 }
900 }
901 }
902
903 {
904 // load function aliases
905 wxString sql;
906 sql << "SELECT * from FUNCTION_ALIAS_TABLE WHERE SCOPE_ID=" << parentId << " AND ";
907 DoAddNameFilter(sql, nameHint, flags);
908 DoAddLimit(sql);
909
910 wxSQLite3Statement st = m_db.PrepareStatement(sql);
911 wxSQLite3ResultSet res = st.ExecuteQuery();
912 while(res.NextRow()) {
913 PHPEntityBase::Ptr_t match(new PHPEntityFunctionAlias());
914 match->FromResultSet(res);
915 const wxString& realFuncName = match->Cast<PHPEntityFunctionAlias>()->GetRealname();
916 // Load the function pointed by this reference
917 PHPEntityBase::Ptr_t pFunc = FindFunction(realFuncName);
918 if(pFunc) {
919 // Keep the reference to the real function
920 match->Cast<PHPEntityFunctionAlias>()->SetFunc(pFunc);
921 matches.push_back(match);
922 }
923 }
924 }
925
926 {
927 // Add members from the variables table
928 wxString sql;
929 sql << "SELECT * from VARIABLES_TABLE WHERE SCOPE_ID=" << parentId << " AND ";
930 DoAddNameFilter(sql, nameHint, flags);
931 DoAddLimit(sql);
932
933 wxSQLite3Statement st = m_db.PrepareStatement(sql);
934 wxSQLite3ResultSet res = st.ExecuteQuery();
935
936 while(res.NextRow()) {
937 PHPEntityBase::Ptr_t match(new PHPEntityVariable());
938 match->FromResultSet(res);
939
940 if(flags & kLookupFlags_FunctionsAndConstsOnly) {
941 // Filter non consts from the list
942 if(!match->Cast<PHPEntityVariable>()->IsConst() && !match->Cast<PHPEntityVariable>()->IsDefine()) {
943 continue;
944 }
945 }
946
947 bool isConst = match->Cast<PHPEntityVariable>()->IsConst();
948 bool isStatic = match->Cast<PHPEntityVariable>()->IsStatic();
949 bool bAddIt = ((isStatic || isConst) && CollectingStatics(flags)) ||
950 (!isStatic && !isConst && !CollectingStatics(flags));
951 if(bAddIt) {
952 matches.push_back(match);
953 }
954 }
955 DoFixVarsDocComment(matches, parentId);
956 }
957
958 } catch(wxSQLite3Exception& e) {
959 clWARNING() << "PHPLookupTable::FindChildren" << e.GetMessage() << endl;
960 }
961 }
962
963 wxLongLong PHPLookupTable::GetFileLastParsedTimestamp(const wxFileName& filename)
964 {
965 try {
966 wxSQLite3Statement st =
967 m_db.PrepareStatement("SELECT LAST_UPDATED FROM FILES_TABLE WHERE FILE_NAME=:FILE_NAME");
968 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
969 wxSQLite3ResultSet res = st.ExecuteQuery();
970 if(res.NextRow()) {
971 return res.GetInt64("LAST_UPDATED");
972 }
973 } catch(wxSQLite3Exception& e) {
974 clWARNING() << "PHPLookupTable::FindChildren" << e.GetMessage() << endl;
975 }
976 return 0;
977 }
978
979 void PHPLookupTable::UpdateFileLastParsedTimestamp(const wxFileName& filename)
980 {
981 try {
982 wxSQLite3Statement st = m_db.PrepareStatement(
983 "REPLACE INTO FILES_TABLE (ID, FILE_NAME, LAST_UPDATED) VALUES (NULL, :FILE_NAME, :LAST_UPDATED)");
984 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
985 st.Bind(st.GetParamIndex(":LAST_UPDATED"), (wxLongLong)time(NULL));
986 st.ExecuteUpdate();
987
988 } catch(wxSQLite3Exception& e) {
989 clWARNING() << "PHPLookupTable::UpdateFileLastParsedTimestamp" << e.GetMessage() << endl;
990 }
991 }
992
993 void PHPLookupTable::ClearAll(bool autoCommit)
994 {
995 try {
996 if(autoCommit)
997 m_db.Begin();
998 {
999 wxString sql;
1000 sql << "delete from SCOPE_TABLE";
1001 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1002 st.ExecuteUpdate();
1003 }
1004
1005 {
1006 wxString sql;
1007 sql << "delete from FUNCTION_TABLE";
1008 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1009 st.ExecuteUpdate();
1010 }
1011
1012 {
1013 wxString sql;
1014 sql << "delete from VARIABLES_TABLE";
1015 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1016 st.ExecuteUpdate();
1017 }
1018
1019 {
1020 wxString sql;
1021 sql << "delete from FILES_TABLE";
1022 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1023 st.ExecuteUpdate();
1024 }
1025
1026 {
1027 wxString sql;
1028 sql << "delete from FUNCTION_ALIAS_TABLE";
1029 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1030 st.ExecuteUpdate();
1031 }
1032
1033 if(autoCommit)
1034 m_db.Commit();
1035 } catch(wxSQLite3Exception& e) {
1036 if(autoCommit)
1037 m_db.Rollback();
1038 clWARNING() << "PHPLookupTable::ClearAll" << e.GetMessage() << endl;
1039 }
1040 }
1041
1042 PHPEntityBase::Ptr_t PHPLookupTable::FindFunction(const wxString& fullname)
1043 {
1044 // locate the scope
1045 try {
1046 wxString sql;
1047
1048 // limit by 2 for performance reason
1049 // we will return NULL incase the number of matches is greater than 1...
1050 sql << "SELECT * from FUNCTION_TABLE WHERE FULLNAME='" << fullname << "'";
1051 sql << " LIMIT 2";
1052
1053 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1054 wxSQLite3ResultSet res = st.ExecuteQuery();
1055 PHPEntityBase::Ptr_t match(NULL);
1056
1057 while(res.NextRow()) {
1058 if(match) {
1059 // only one match
1060 return PHPEntityBase::Ptr_t(NULL);
1061 }
1062
1063 match.Reset(new PHPEntityFunction());
1064 match->FromResultSet(res);
1065 }
1066 return match;
1067
1068 } catch(wxSQLite3Exception& e) {
1069 clWARNING() << "PHPLookupTable::FindFunction" << e.GetMessage() << endl;
1070 }
1071 return PHPEntityBase::Ptr_t(NULL);
1072 }
1073
1074 PHPEntityBase::List_t PHPLookupTable::FindGlobalFunctionAndConsts(size_t flags, const wxString& nameHint)
1075 {
1076 PHPEntityBase::List_t matches;
1077 // Sanity
1078 if(nameHint.IsEmpty())
1079 return matches;
1080 // First, locate the global namespace in the database
1081 PHPEntityBase::Ptr_t globalNs = FindScope("\\");
1082 if(!globalNs)
1083 return matches;
1084 DoFindChildren(matches, globalNs->GetDbId(), kLookupFlags_FunctionsAndConstsOnly | flags, nameHint);
1085 return matches;
1086 }
1087
1088 PHPEntityBase::Ptr_t PHPLookupTable::CreateNamespaceForDefine(PHPEntityBase::Ptr_t define)
1089 {
1090 wxString nameSpaceName, shortName;
1091 DoSplitFullname(define->GetFullName(), nameSpaceName, shortName);
1092
1093 PHPEntityBase::Ptr_t pNamespace = DoFindScope(nameSpaceName, kPhpScopeTypeNamespace);
1094 if(!pNamespace) {
1095 // Create it
1096 pNamespace.Reset(new PHPEntityNamespace());
1097 pNamespace->SetFullName(nameSpaceName);
1098 pNamespace->SetShortName(nameSpaceName.AfterLast('\\'));
1099 pNamespace->SetFilename(define->GetFilename());
1100 pNamespace->SetLine(define->GetLine());
1101 pNamespace->Store(this);
1102 }
1103 return pNamespace;
1104 }
1105
1106 void PHPLookupTable::DoSplitFullname(const wxString& fullname, wxString& ns, wxString& shortName)
1107 {
1108 // get the namespace part
1109 ns = fullname.BeforeLast('\\');
1110 if(!ns.StartsWith("\\")) {
1111 // This means that the fullname contained a single '\'
1112 // and we removed it
1113 ns.Prepend("\\");
1114 }
1115 // Now the short name
1116 shortName = fullname.AfterLast('\\');
1117 }
1118
1119 PHPEntityBase::List_t PHPLookupTable::FindNamespaces(const wxString& fullnameStartsWith,
1120 const wxString& shortNameContains)
1121 {
1122 PHPEntityBase::List_t matches;
1123 try {
1124 wxString sql;
1125 wxString prefix = fullnameStartsWith;
1126 sql << "SELECT * from SCOPE_TABLE WHERE SCOPE_TYPE = 0 ";
1127 DoAddLimit(sql);
1128
1129 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1130 wxSQLite3ResultSet res = st.ExecuteQuery();
1131
1132 wxString fullpath = fullnameStartsWith;
1133 if(!shortNameContains.IsEmpty()) {
1134 if(!fullpath.EndsWith("\\")) {
1135 fullpath << "\\";
1136 }
1137 fullpath << shortNameContains;
1138 }
1139
1140 while(res.NextRow()) {
1141 PHPEntityBase::Ptr_t match(new PHPEntityNamespace());
1142 match->FromResultSet(res);
1143 if(match->Cast<PHPEntityNamespace>()->GetParentNamespace() == fullnameStartsWith &&
1144 match->GetShortName().StartsWith(shortNameContains)) {
1145 matches.push_back(match);
1146 }
1147 }
1148 } catch(wxSQLite3Exception& e) {
1149 clWARNING() << "PHPLookupTable::FindNamespaces" << e.GetMessage() << endl;
1150 }
1151 return matches;
1152 }
1153
1154 PHPEntityBase::Ptr_t PHPLookupTable::FindFunctionByLineAndFile(const wxFileName& filename, int line)
1155 {
1156 try {
1157 wxString sql;
1158 // Try to locate function first
1159 sql << "SELECT * from FUNCTION_TABLE WHERE FILE_NAME=:FILE_NAME AND LINE_NUMBER=:LINE_NUMBER LIMIT 1";
1160 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1161 st.Bind(st.GetParamIndex(":FILE_NAME"), filename.GetFullPath());
1162 st.Bind(st.GetParamIndex(":LINE_NUMBER"), line);
1163 wxSQLite3ResultSet res = st.ExecuteQuery();
1164 if(res.NextRow()) {
1165 PHPEntityBase::Ptr_t match(new PHPEntityFunction());
1166 match->FromResultSet(res);
1167 return match;
1168 }
1169 } catch(wxSQLite3Exception& e) {
1170 clWARNING() << "PHPLookupTable::FindFunctionByLineAndFile" << e.GetMessage() << endl;
1171 }
1172 return NULL;
1173 }
1174
1175 void PHPLookupTable::ResetDatabase()
1176 {
1177 wxFileName curfile = m_filename;
1178 Close(); // Close the databse releasing any file capture we have
1179 // Delete the file
1180 if(curfile.IsOk() && curfile.Exists()) {
1181 // Delete it from the file system
1182 wxLogNull noLog;
1183 if(!clRemoveFile(curfile.GetFullPath())) {
1184 // ??
1185 }
1186 }
1187 Open(curfile);
1188 }
1189
1190 bool PHPLookupTable::CheckDiskImage(wxSQLite3Database& db, const wxFileName& filename)
1191 {
1192 try {
1193 wxSQLite3ResultSet res = db.ExecuteQuery("PRAGMA integrity_check");
1194 if(res.NextRow()) {
1195 wxString value = res.GetString(0);
1196 clDEBUG() << "PHP: 'PRAGMA integrity_check' returned:" << value << clEndl;
1197 return (value.Lower() == "ok");
1198 } else {
1199 return false;
1200 }
1201 } catch(wxSQLite3Exception& exec) {
1202 // this can only happen if we have a corrupt disk image
1203 clWARNING() << "PHP: exception caught:" << exec.GetMessage() << clEndl;
1204 clWARNING() << "PHP: database image is corrupted:" << filename.GetFullPath() << clEndl;
1205 return false;
1206 }
1207 return true;
1208 }
1209
1210 void PHPLookupTable::EnsureIntegrity(const wxFileName& filename)
1211 {
1212 wxSQLite3Database db;
1213 db.Open(filename.GetFullPath());
1214 if(db.IsOpen()) {
1215 if(!CheckDiskImage(db, filename)) {
1216 // disk image is malformed
1217 db.Close();
1218 wxLogNull noLog;
1219 clRemoveFile(filename.GetFullPath());
1220 }
1221 }
1222 }
1223
1224 PHPEntityBase::List_t PHPLookupTable::FindSymbol(const wxString& name)
1225 {
1226 // locate the scope
1227 PHPEntityBase::List_t matches;
1228 try {
1229 {
1230 //---------------------------------------------------------------------
1231 // Load scopes (classes / namespaces)
1232 //---------------------------------------------------------------------
1233 wxString sql;
1234 sql << "SELECT * from SCOPE_TABLE WHERE NAME='" << name << "'";
1235
1236 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1237 wxSQLite3ResultSet res = st.ExecuteQuery();
1238
1239 while(res.NextRow()) {
1240 ePhpScopeType st = kPhpScopeTypeAny;
1241 st =
1242 res.GetInt("SCOPE_TYPE", 1) == kPhpScopeTypeNamespace ? kPhpScopeTypeNamespace : kPhpScopeTypeClass;
1243
1244 PHPEntityBase::Ptr_t match = NewEntity("SCOPE_TABLE", st);
1245 if(match) {
1246 match->FromResultSet(res);
1247 matches.push_back(match);
1248 }
1249 }
1250 }
1251
1252 {
1253 //---------------------------------------------------------------------
1254 // Load functions
1255 //---------------------------------------------------------------------
1256 wxString sql;
1257 sql << "SELECT * from FUNCTION_TABLE WHERE NAME='" << name << "'";
1258
1259 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1260 wxSQLite3ResultSet res = st.ExecuteQuery();
1261
1262 while(res.NextRow()) {
1263 PHPEntityBase::Ptr_t match(new PHPEntityFunction());
1264 match->FromResultSet(res);
1265 matches.push_back(match);
1266 }
1267 }
1268
1269 {
1270 //---------------------------------------------------------------------
1271 // Load function aliases
1272 //---------------------------------------------------------------------
1273 wxString sql;
1274 sql << "SELECT * from FUNCTION_ALIAS_TABLE WHERE NAME='" << name << "'";
1275
1276 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1277 wxSQLite3ResultSet res = st.ExecuteQuery();
1278
1279 while(res.NextRow()) {
1280 PHPEntityBase::Ptr_t match(new PHPEntityFunction());
1281 match->FromResultSet(res);
1282 matches.push_back(match);
1283 }
1284 }
1285
1286 {
1287 //---------------------------------------------------------------------
1288 // Load variables
1289 //---------------------------------------------------------------------
1290 wxString sql;
1291 sql << "SELECT * from VARIABLES_TABLE WHERE NAME='" << name << "'";
1292
1293 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1294 wxSQLite3ResultSet res = st.ExecuteQuery();
1295
1296 while(res.NextRow()) {
1297 PHPEntityBase::Ptr_t match = NewEntity("VARIABLES_TABLE", kPhpScopeTypeAny);
1298 match->FromResultSet(res);
1299 matches.push_back(match);
1300 }
1301 }
1302
1303 } catch(wxSQLite3Exception& e) {
1304 clWARNING() << "PHPLookupTable::FindSymbol" << e.GetMessage() << endl;
1305 }
1306 return matches;
1307 }
1308
1309 void PHPLookupTable::DoFixVarsDocComment(PHPEntityBase::List_t& matches, wxLongLong parentId)
1310 {
1311 // Load all PHPDocVar belonged to this class
1312 PHPDocVar::Map_t docs;
1313 wxString sql;
1314 sql << "SELECT * from PHPDOC_VAR_TABLE WHERE SCOPE_ID=" << parentId;
1315 DoAddLimit(sql);
1316 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1317 wxSQLite3ResultSet res = st.ExecuteQuery();
1318
1319 while(res.NextRow()) {
1320 PHPDocVar::Ptr_t var(new PHPDocVar());
1321 var->FromResultSet(res);
1322 docs.insert(std::make_pair(var->GetName(), var));
1323 }
1324
1325 // Let the PHPDOC table content override the matches' type
1326 std::for_each(matches.begin(), matches.end(), [&](PHPEntityBase::Ptr_t match) {
1327 if(match->Is(kEntityTypeVariable)) {
1328 if(docs.count(match->GetShortName())) {
1329 PHPDocVar::Ptr_t docvar = docs.find(match->GetShortName())->second;
1330 if(!docvar->GetType().IsEmpty()) {
1331 match->Cast<PHPEntityVariable>()->SetTypeHint(docvar->GetType());
1332 }
1333 }
1334 }
1335 });
1336 }
1337
1338 void PHPLookupTable::UpdateClassCache(const wxString& classname)
1339 {
1340 if(m_allClasses.count(classname) == 0) {
1341 m_allClasses.insert(classname);
1342 }
1343 }
1344
1345 bool PHPLookupTable::ClassExists(const wxString& classname) const { return m_allClasses.count(classname) != 0; }
1346
1347 void PHPLookupTable::RebuildClassCache()
1348 {
1349 // locate the scope
1350 clDEBUG() << "Rebuilding PHP class cache..." << clEndl;
1351 m_allClasses.clear();
1352 size_t count = 0;
1353 try {
1354 wxString sql;
1355 sql << "SELECT FULLNAME from SCOPE_TABLE WHERE SCOPE_TYPE=1";
1356
1357 wxSQLite3ResultSet res = m_db.ExecuteQuery(sql);
1358 while(res.NextRow()) {
1359 UpdateClassCache(res.GetString("FULLNAME"));
1360 ++count;
1361 }
1362
1363 } catch(wxSQLite3Exception& e) {
1364 clWARNING() << "PHPLookupTable::RebuildClassCache:" << e.GetMessage() << clEndl;
1365 return;
1366 }
1367 clDEBUG() << "Loading" << count << "class names into the cache" << clEndl;
1368 clDEBUG() << "Rebuilding PHP class cache...done" << clEndl;
1369 }
1370
1371 PHPEntityBase::Ptr_t PHPLookupTable::FindFunctionNearLine(const wxFileName& filename, int lineNumber)
1372 {
1373 try {
1374 wxString sql;
1375
1376 // limit by 2 for performance reason
1377 // we will return NULL incase the number of matches is greater than 1...
1378 // SELECT * from FUNCTION_TABLE WHERE
1379 sql << "SELECT * from FUNCTION_TABLE WHERE FILE_NAME='" << filename.GetFullPath()
1380 << "' AND LINE_NUMBER <=" << lineNumber << " order by LINE_NUMBER DESC LIMIT 1";
1381
1382 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1383 wxSQLite3ResultSet res = st.ExecuteQuery();
1384 PHPEntityBase::Ptr_t match(NULL);
1385
1386 if(res.NextRow()) {
1387 match.Reset(new PHPEntityFunction());
1388 match->FromResultSet(res);
1389 }
1390 return match;
1391
1392 } catch(wxSQLite3Exception& e) {
1393 clWARNING() << "PHPLookupTable::FindFunctionNearLine:" << e.GetMessage() << clEndl;
1394 }
1395 return PHPEntityBase::Ptr_t(NULL);
1396 }
1397
1398 size_t PHPLookupTable::FindFunctionsByFile(const wxFileName& filename, PHPEntityBase::List_t& functions)
1399 {
1400 wxString sql;
1401 try {
1402
1403 // limit by 2 for performance reason
1404 // we will return NULL incase the number of matches is greater than 1...
1405 // SELECT * from FUNCTION_TABLE WHERE
1406 sql << "SELECT * from FUNCTION_TABLE WHERE FILE_NAME='" << filename.GetFullPath()
1407 << "' order by LINE_NUMBER ASC";
1408 wxSQLite3Statement st = m_db.PrepareStatement(sql);
1409 wxSQLite3ResultSet res = st.ExecuteQuery();
1410 while(res.NextRow()) {
1411 PHPEntityBase::Ptr_t func(new PHPEntityFunction());
1412 func->FromResultSet(res);
1413 functions.push_back(func);
1414 }
1415 } catch(wxSQLite3Exception& e) {
1416 clWARNING() << "SQLite 3 error:" << e.GetMessage() << clEndl;
1417 }
1418 return functions.size();
1419 }
1420
1421 void PHPLookupTable::ParseFolder(const wxString& folder, const wxString& filemask, eUpdateMode updateMode)
1422 {
1423 clFilesScanner scanner;
1424 std::vector<wxString> files;
1425 if(scanner.Scan(folder, files, filemask) == 0) {
1426 return;
1427 }
1428 std::for_each(files.begin(), files.end(), [&](const wxString& file) {
1429 try {
1430 wxFileName fnFile(file);
1431 bool reParseNeeded(true);
1432 if(updateMode == kUpdateMode_Fast) {
1433 // Check to see if we need to re-parse this file
1434 // and store it to the database
1435
1436 if(!fnFile.Exists()) {
1437 reParseNeeded = false;
1438 } else {
1439 time_t lastModifiedOnDisk = fnFile.GetModificationTime().GetTicks();
1440 wxLongLong lastModifiedInDB = GetFileLastParsedTimestamp(fnFile);
1441 if(lastModifiedOnDisk <= lastModifiedInDB.ToLong()) {
1442 reParseNeeded = false;
1443 }
1444 }
1445 }
1446
1447 // Ensure that the file exists
1448 if(!fnFile.Exists()) {
1449 reParseNeeded = false;
1450 }
1451 if(!reParseNeeded)
1452 return;
1453
1454 wxString content;
1455 if(!FileUtils::ReadFileContent(fnFile, content, wxConvISO8859_1)) {
1456 clWARNING() << "PHP: Failed to read file:" << fnFile << "for parsing";
1457 return;
1458 }
1459 LOG_IF_TRACE { clDEBUG1() << "Parsing PHP file:" << fnFile; }
1460 PHPSourceFile sourceFile(content, this);
1461 sourceFile.SetFilename(fnFile);
1462 sourceFile.SetParseFunctionBody(true);
1463 sourceFile.Parse();
1464 UpdateSourceFile(sourceFile, false);
1465 } catch(wxSQLite3Exception& e) {
1466 try {
1467 m_db.Rollback();
1468 } catch(...) {
1469 }
1470 clWARNING() << "PHPLookupTable::ParseFolder:" << e.GetMessage();
1471 }
1472 });
1473 }