File: PHPEntityNamespace.cpp

package info (click to toggle)
codelite 10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 71,364 kB
  • sloc: cpp: 415,397; ansic: 18,277; php: 9,547; lex: 4,181; yacc: 2,820; python: 2,294; sh: 383; makefile: 51; xml: 13
file content (129 lines) | stat: -rw-r--r-- 4,885 bytes parent folder | download
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
#include "PHPEntityNamespace.h"
#include <wx/tokenzr.h>

PHPEntityNamespace::PHPEntityNamespace() {}

PHPEntityNamespace::~PHPEntityNamespace() {}

void PHPEntityNamespace::PrintStdout(int indent) const
{
    wxString indentString(' ', indent);
    wxPrintf("%sNamespace name: %s\n", indentString, GetFullName());

    PHPEntityBase::List_t::const_iterator iter = m_children.begin();
    for(; iter != m_children.end(); ++iter) {
        (*iter)->PrintStdout(indent + 4);
    }
}

void PHPEntityNamespace::Store(wxSQLite3Database& db)
{
    try {
        // A namespace, unlike other PHP entities, can be defined in various files
        // and in multiple locations. This means, that by definition, there can be multiple entries
        // for the same namespace, however, since our relations in the database is ID based,
        // we try to locate the namespace in the DB before we attempt to insert it
        {
            wxSQLite3Statement statement =
                db.PrepareStatement("SELECT * FROM SCOPE_TABLE WHERE FULLNAME=:FULLNAME LIMIT 1");
            statement.Bind(statement.GetParamIndex(":FULLNAME"), GetFullName());
            wxSQLite3ResultSet res = statement.ExecuteQuery();
            if(res.NextRow()) {
                // we have a match, update this item database ID to match
                // what we have found in the database
                PHPEntityNamespace ns;
                ns.FromResultSet(res);
                SetDbId(ns.GetDbId());
                return;
            }
        }

        // Get the 'parent' namespace part
        wxString parentPath = GetFullName().BeforeLast('\\');
        DoEnsureNamespacePathExists(db, parentPath);

        {
            wxSQLite3Statement statement = db.PrepareStatement(
                "INSERT INTO SCOPE_TABLE (ID, SCOPE_TYPE, SCOPE_ID, NAME, FULLNAME, LINE_NUMBER, FILE_NAME) "
                "VALUES (NULL, 0, -1, :NAME, :FULLNAME, :LINE_NUMBER, :FILE_NAME)");
            statement.Bind(statement.GetParamIndex(":NAME"), GetShortName());
            statement.Bind(statement.GetParamIndex(":FULLNAME"), GetFullName());
            statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
            statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
            statement.ExecuteUpdate();
            SetDbId(db.GetLastRowId());
        }
    } catch(wxSQLite3Exception& exc) {
        wxUnusedVar(exc);
    }
}

void PHPEntityNamespace::FromResultSet(wxSQLite3ResultSet& res)
{
    SetDbId(res.GetInt64("ID"));
    SetFullName(res.GetString("FULLNAME"));
    SetShortName(res.GetString("NAME"));
    SetFilename(res.GetString("FILE_NAME"));
    SetLine(res.GetInt("LINE_NUMBER"));
    SetFlags(0);
}

wxString PHPEntityNamespace::Type() const { return GetFullName(); }
bool PHPEntityNamespace::Is(eEntityType type) const { return type == kEntityTypeNamespace; }
wxString PHPEntityNamespace::GetDisplayName() const { return GetFullName(); }
wxString PHPEntityNamespace::FormatPhpDoc(const CommentConfigData& data) const
{
    wxString doc;
    doc << data.GetCommentBlockPrefix() << "\n"
        << " * @brief \n";
    doc << " */";
    return doc;
}

void PHPEntityNamespace::DoEnsureNamespacePathExists(wxSQLite3Database& db, const wxString& path)
{
    wxArrayString paths = ::wxStringTokenize(path, "\\", wxTOKEN_STRTOK);
    if(paths.IsEmpty()) return;

    wxString currentPath;
    try {
        for(size_t i = 0; i < paths.GetCount(); ++i) {
            if(!currentPath.EndsWith("\\")) {
                currentPath << "\\";
            }
            currentPath << paths.Item(i);
            wxSQLite3Statement statement = db.PrepareStatement(
                "INSERT OR IGNORE INTO SCOPE_TABLE (ID, SCOPE_TYPE, SCOPE_ID, NAME, FULLNAME, LINE_NUMBER, FILE_NAME) "
                "VALUES (NULL, 0, -1, :NAME, :FULLNAME, :LINE_NUMBER, :FILE_NAME)");
            statement.Bind(statement.GetParamIndex(":NAME"), paths.Item(i));
            statement.Bind(statement.GetParamIndex(":FULLNAME"), currentPath);
            statement.Bind(statement.GetParamIndex(":LINE_NUMBER"), GetLine());
            statement.Bind(statement.GetParamIndex(":FILE_NAME"), GetFilename().GetFullPath());
            statement.ExecuteUpdate();
            // SetDbId(db.GetLastRowId());
        }

    } catch(wxSQLite3Exception& exc) {
        wxUnusedVar(exc);
    }
}

wxString PHPEntityNamespace::GetParentNamespace() const
{
    if(GetFullName() == "\\") {
        // this is the global namespace
        return "\\";
    }
    wxString parentPath = GetFullName().BeforeLast('\\');
    if(parentPath.IsEmpty()) return "\\";
    return parentPath;
}

wxString PHPEntityNamespace::BuildNamespace(const wxString& part1, const wxString& part2)
{
    wxString ns;
    ns << part1 << "\\" << part2;
    while(ns.Replace("\\\\", "\\")) {
    }
    return ns;
}