Coverage Report

Created: 2024-05-20 06:33

/src/sql-parser/src/SQLParser.cpp
Line
Count
Source (jump to first uncovered line)
1
2
#include "SQLParser.h"
3
#include <stdio.h>
4
#include <string>
5
#include "parser/bison_parser.h"
6
#include "parser/flex_lexer.h"
7
8
namespace hsql {
9
10
0
SQLParser::SQLParser() { fprintf(stderr, "SQLParser only has static methods atm! Do not initialize!\n"); }
11
12
// static
13
5.60k
bool SQLParser::parse(const std::string& sql, SQLParserResult* result) {
14
5.60k
  yyscan_t scanner;
15
5.60k
  YY_BUFFER_STATE state;
16
17
5.60k
  if (hsql_lex_init(&scanner)) {
18
    // Couldn't initialize the lexer.
19
0
    fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
20
0
    return false;
21
0
  }
22
5.60k
  const char* text = sql.c_str();
23
5.60k
  state = hsql__scan_string(text, scanner);
24
25
  // Parse the tokens.
26
  // If parsing fails, the result will contain an error object.
27
5.60k
  int ret = hsql_parse(result, scanner);
28
5.60k
  bool success = (ret == 0);
29
5.60k
  result->setIsValid(success);
30
31
5.60k
  hsql__delete_buffer(state, scanner);
32
5.60k
  hsql_lex_destroy(scanner);
33
34
5.60k
  return true;
35
5.60k
}
36
37
// static
38
0
bool SQLParser::parseSQLString(const char* sql, SQLParserResult* result) { return parse(sql, result); }
39
40
0
bool SQLParser::parseSQLString(const std::string& sql, SQLParserResult* result) { return parse(sql, result); }
41
42
// static
43
0
bool SQLParser::tokenize(const std::string& sql, std::vector<int16_t>* tokens) {
44
  // Initialize the scanner.
45
0
  yyscan_t scanner;
46
0
  if (hsql_lex_init(&scanner)) {
47
0
    fprintf(stderr, "SQLParser: Error when initializing lexer!\n");
48
0
    return false;
49
0
  }
50
51
0
  YY_BUFFER_STATE state;
52
0
  state = hsql__scan_string(sql.c_str(), scanner);
53
54
0
  YYSTYPE yylval;
55
0
  YYLTYPE yylloc;
56
57
  // Step through the string until EOF is read.
58
  // Note: hsql_lex returns int, but we know that its range is within 16 bit.
59
0
  int16_t token = hsql_lex(&yylval, &yylloc, scanner);
60
0
  while (token != 0) {
61
0
    tokens->push_back(token);
62
0
    token = hsql_lex(&yylval, &yylloc, scanner);
63
64
0
    if (token == SQL_IDENTIFIER || token == SQL_STRING) {
65
0
      free(yylval.sval);
66
0
    }
67
0
  }
68
69
0
  hsql__delete_buffer(state, scanner);
70
0
  hsql_lex_destroy(scanner);
71
0
  return true;
72
0
}
73
74
}  // namespace hsql