Coverage Report

Created: 2024-11-19 06:35

/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.85k
bool SQLParser::parse(const std::string& sql, SQLParserResult* result) {
14
5.85k
  yyscan_t scanner;
15
5.85k
  YY_BUFFER_STATE state;
16
17
5.85k
  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.85k
  const char* text = sql.c_str();
23
5.85k
  state = hsql__scan_string(text, scanner);
24
25
  // Parse the tokens.
26
  // If parsing fails, the result will contain an error object.
27
5.85k
  int ret = hsql_parse(result, scanner);
28
5.85k
  bool success = (ret == 0);
29
5.85k
  result->setIsValid(success);
30
31
5.85k
  hsql__delete_buffer(state, scanner);
32
5.85k
  hsql_lex_destroy(scanner);
33
34
5.85k
  return true;
35
5.85k
}
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