Debian Codegraph @

CodeLite/PHPEntityNamespace.cpp

raw
1 #include "PHPEntityNamespace.h"
2 #include <wx/tokenzr.h>
3 #include "PHPLookupTable.h"
4
5 PHPEntityNamespace::PHPEntityNamespace() {}
6
7 PHPEntityNamespace::~PHPEntityNamespace() {}
8
9 void PHPEntityNamespace::PrintStdout(int indent) const
10 {
11 wxString indentString(' ', indent);
12 wxPrintf("%sNamespace name: %s\n", indentString, GetFullName());
13
14 PHPEntityBase::List_t::const_iterator iter = m_children.begin();
15 for(; iter != m_children.end(); ++iter) {
16 (*iter)->PrintStdout(indent + 4);
17 }
18 }
19
20 void PHPEntityNamespace::Store(PHPLookupTable* lookup)
21 {
22 try {
23 // A namespace, unlike other PHP entities, can be defined in various files
24 // and in multiple locations. This means, that by definition, there can be multiple entries
25 // for the same namespace, however, since our relations in the database is ID based,
26 // we try to locate the namespace in the DB before we attempt to insert it
27 wxSQLite3Database& db = lookup->Database();
28 {
29 wxSQLite3Statement statement =
30 db.PrepareStatement("SELECT * FROM SCOPE_TABLE WHERE FULLNAME=:FULLNAME LIMIT 1");
31 statement.Bind(statement.GetParamIndex(":FULLNAME"), GetFullName());
32 wxSQLite3ResultSet res = statement.ExecuteQuery();
33 if(res.NextRow()) {
34 // we have a match, update this item database ID to match
35 // what we have found in the database
36 PHPEntityNamespace ns;
37 ns.FromResultSet(res);
38 SetDbId(ns.GetDbId());
39 return;
40 }
41 }
42
43 // Get the 'parent' namespace part
44 wxString parentPath = GetFullName().BeforeLast('\\');
45 DoEnsureNamespacePathExists(db, parentPath);
46
47 {
48 wxSQLite3Statement statement = db.PrepareStatement(
49 "INSERT INTO SCOPE_TABLE (ID, SCOPE_TYPE, SCOPE_ID, NAME, FULLNAME, LINE_NUMBER, FILE_NAME) "
50 "VALUES (NULL, 0, -1, :NAME, :FULLNAME, :LINE_NUMBER, :FILE_NAME)");
51 statement.Bind(statement.GetParamIndex(":NAME"), GetShortName());
52 statement.Bind(statement.GetParamIndex(":FULLNAME"), GetFullName());
53 statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
54 statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
55 statement.ExecuteUpdate();
56 SetDbId(db.GetLastRowId());
57 }
58 } catch(wxSQLite3Exception& exc) {
59 wxUnusedVar(exc);
60 }
61 }
62
63 void PHPEntityNamespace::FromResultSet(wxSQLite3ResultSet& res)
64 {
65 SetDbId(res.GetInt64("ID"));
66 SetFullName(res.GetString("FULLNAME"));
67 SetShortName(res.GetString("NAME"));
68 SetFilename(res.GetString("FILE_NAME"));
69 SetLine(res.GetInt("LINE_NUMBER"));
70 SetFlags(0);
71 }
72
73 wxString PHPEntityNamespace::Type() const { return GetFullName(); }
74 bool PHPEntityNamespace::Is(eEntityType type) const { return type == kEntityTypeNamespace; }
75 wxString PHPEntityNamespace::GetDisplayName() const { return GetFullName(); }
76 wxString PHPEntityNamespace::FormatPhpDoc(const CommentConfigData& data) const
77 {
78 wxString doc;
79 doc << data.GetCommentBlockPrefix() << "\n"
80 << " * @brief \n";
81 doc << " */";
82 return doc;
83 }
84
85 void PHPEntityNamespace::DoEnsureNamespacePathExists(wxSQLite3Database& db, const wxString& path)
86 {
87 wxArrayString paths = ::wxStringTokenize(path, "\\", wxTOKEN_STRTOK);
88 if(paths.IsEmpty()) return;
89
90 wxString currentPath;
91 try {
92 for(size_t i = 0; i < paths.GetCount(); ++i) {
93 if(!currentPath.EndsWith("\\")) {
94 currentPath << "\\";
95 }
96 currentPath << paths.Item(i);
97 wxSQLite3Statement statement = db.PrepareStatement(
98 "INSERT OR IGNORE INTO SCOPE_TABLE (ID, SCOPE_TYPE, SCOPE_ID, NAME, FULLNAME, LINE_NUMBER, FILE_NAME) "
99 "VALUES (NULL, 0, -1, :NAME, :FULLNAME, :LINE_NUMBER, :FILE_NAME)");
100 statement.Bind(statement.GetParamIndex(":NAME"), paths.Item(i));
101 statement.Bind(statement.GetParamIndex(":FULLNAME"), currentPath);
102 statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
103 statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
104 statement.ExecuteUpdate();
105 // SetDbId(db.GetLastRowId());
106 }
107
108 } catch(wxSQLite3Exception& exc) {
109 wxUnusedVar(exc);
110 }
111 }
112
113 wxString PHPEntityNamespace::GetParentNamespace() const
114 {
115 if(GetFullName() == "\\") {
116 // this is the global namespace
117 return "\\";
118 }
119 wxString parentPath = GetFullName().BeforeLast('\\');
120 if(parentPath.IsEmpty()) return "\\";
121 return parentPath;
122 }
123
124 wxString PHPEntityNamespace::BuildNamespace(const wxString& part1, const wxString& part2)
125 {
126 wxString ns;
127 ns << part1 << "\\" << part2;
128 while(ns.Replace("\\\\", "\\")) {
129 }
130 return ns;
131 }
132
133 void PHPEntityNamespace::FromJSON(const JSONItem& json)
134 {
135 BaseFromJSON(json);
136 }
137
138 JSONItem PHPEntityNamespace::ToJSON() const
139 {
140 JSONItem json = BaseToJSON("n"); // n stands for namesapce
141 return json;
142 }