File: mysql_parser_services.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (215 lines) | stat: -rw-r--r-- 7,234 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
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
/*
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "mysql_parser_services.h"

#include "base/string_utilities.h"

#include "mysql-parser.h"
#include "mysql-syntax-check.h"
#include "mysql-scanner.h"

using namespace parser;

//--------------------------------------------------------------------------------------------------

long short_version(const GrtVersionRef &version)
{
  ssize_t short_version;
  if (version.is_valid())
  {
    short_version = version->majorNumber() * 10000;
    if (version->minorNumber() > -1)
      short_version += version->minorNumber() * 100;
    else
      short_version += 500;
    if (version->releaseNumber() > -1)
      short_version += version->releaseNumber();
  }
  else
    short_version = 50501; // Assume some reasonable default (5.5.1).

  return (long)short_version;
}

//------------------ ParserContext -----------------------------------------------------------------

ParserContext::ParserContext(GrtCharacterSetsRef charsets, GrtVersionRef version,
  bool case_sensitive)
{
  _version = version;
  _case_sensitive = case_sensitive;

  for (size_t i = 0; i < charsets->count(); i++)
    _filtered_charsets.insert(base::tolower(*charsets[i]->name()));

  long server_version = short_version(_version);
  update_filtered_charsets(server_version);

  // Both, parser and syntax checker are only a few hundreds of bytes in size (except for any
  // stored token strings or the AST), so we can always simply create both without serious memory
  // concerns (the syntax checker has no AST).
  _recognizer = new MySQLRecognizer(server_version, "", _filtered_charsets);
  _syntax_checker = new MySQLSyntaxChecker(server_version, "", _filtered_charsets);
}

//--------------------------------------------------------------------------------------------------

ParserContext::~ParserContext()
{
  delete _recognizer;
  delete _syntax_checker;
}

//--------------------------------------------------------------------------------------------------

void ParserContext::update_filtered_charsets(long version)
{
  if (version < 50503)
  {
    _filtered_charsets.erase("utf8mb4");
    _filtered_charsets.erase("utf16");
    _filtered_charsets.erase("utf32");
  }
  else
  {
    // Duplicates are automatically ignored.
    _filtered_charsets.insert("utf8mb4");
    _filtered_charsets.insert("utf16");
    _filtered_charsets.insert("utf32");
  }
}

//--------------------------------------------------------------------------------------------------

boost::shared_ptr<MySQLScanner> ParserContext::createScanner(const std::string &text)
{
  long server_version = short_version(_version);
  return boost::shared_ptr<MySQLScanner>(new MySQLScanner(text.c_str(), text.size(), true, server_version,
    _sql_mode, _filtered_charsets));
}

//--------------------------------------------------------------------------------------------------

boost::shared_ptr<MySQLQueryIdentifier> ParserContext::createQueryIdentifier()
{
  long version = short_version(_version);
  return boost::shared_ptr<MySQLQueryIdentifier>(new MySQLQueryIdentifier(version, _sql_mode, _filtered_charsets));
}

//--------------------------------------------------------------------------------------------------

void ParserContext::use_sql_mode(const std::string &mode)
{
  _sql_mode = mode;
  _recognizer->set_sql_mode(mode);
  _syntax_checker->set_sql_mode(mode);
}

//--------------------------------------------------------------------------------------------------

std::string ParserContext::get_sql_mode()
{
  return _sql_mode;
}

//--------------------------------------------------------------------------------------------------

void ParserContext::use_server_version(GrtVersionRef version)
{
  if (_version == version)
    return;

  _version = version;

  long server_version = short_version(_version);
  update_filtered_charsets(server_version);

  _recognizer->set_server_version(server_version);
  _syntax_checker->set_server_version(server_version);
}

//--------------------------------------------------------------------------------------------------

/**
* Returns a collection of errors from the last parser run. The start position is offset by the given
* value (used to adjust error position in a larger context).
*/
std::vector<ParserErrorEntry> ParserContext::get_errors_with_offset(size_t offset, bool for_syntax_check)
{
  std::vector<ParserErrorEntry> errors;

  MySQLRecognitionBase *recognizer = _recognizer;
  if (for_syntax_check)
    recognizer = _syntax_checker;
  if (recognizer->has_errors())
  {
    const std::vector<MySQLParserErrorInfo> error_info = recognizer->error_info();
    for (std::vector<MySQLParserErrorInfo>::const_iterator error_iterator = error_info.begin();
      error_iterator != error_info.end(); ++error_iterator)
    {
      ParserErrorEntry entry = { error_iterator->message, error_iterator->charOffset + offset,
        error_iterator->line, error_iterator->length };
      errors.push_back(entry);
    }
  }

  return errors;
}

//--------------------------------------------------------------------------------------------------

uint32_t ParserContext::get_keyword_token(const std::string &keyword)
{
  return _recognizer->get_keyword_token(keyword);
}

//--------------------------------------------------------------------------------------------------

/**
* Returns the pointer to the internal keyword strings array in the parser, so we can directly
* work with those keywords. The position in the keywords list corresponds to their token value.
* The first user defined token name starts at index 4.
*/
char ** ParserContext::get_token_name_list()
{
  return _recognizer->get_token_list();
}

//------------------ MySQLParserServices -----------------------------------------------------------

ParserContext::Ref MySQLParserServices::createParserContext(GrtCharacterSetsRef charsets,
  GrtVersionRef version, bool case_sensitive)
{
  boost::shared_ptr<ParserContext> result(new ParserContext(charsets, version, case_sensitive));

  return result;
}

//--------------------------------------------------------------------------------------------------

MySQLParserServices::Ref MySQLParserServices::get(grt::GRT *grt)
{
  MySQLParserServices::Ref module = dynamic_cast<MySQLParserServices::Ref>(grt->get_module("MySQLParserServices"));
  if (!module)
    throw std::runtime_error("Can't get MySQLParserServices module.");
  return module;
}

//--------------------------------------------------------------------------------------------------