File: sql_parser.cpp

package info (click to toggle)
poco 1.14.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 56,460 kB
  • sloc: cpp: 340,542; ansic: 245,601; makefile: 1,742; yacc: 1,005; sh: 698; sql: 312; lex: 282; xml: 128; perl: 29; python: 24
file content (44 lines) | stat: -rw-r--r-- 1,279 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
#include "thirdparty/microtest/microtest.h"

#include <iostream>
#include <map>
#include <string>

#include "SQLParser.h"
#include "parser/bison_parser.h"
#include "sql_asserts.h"

using namespace hsql;

void test_tokens(const std::string& query, const std::vector<int16_t>& expected_tokens) {
  std::vector<int16_t> tokens;
  ASSERT(SQLParser::tokenize(query, &tokens));

  ASSERT_EQ(expected_tokens.size(), tokens.size());

  for (unsigned i = 0; i < expected_tokens.size(); ++i) {
    ASSERT_EQ(expected_tokens[i], tokens[i]);
  }
}

TEST(SQLParserTokenizeTest) {
  test_tokens("SELECT * FROM test;", {SQL_SELECT, '*', SQL_FROM, SQL_IDENTIFIER, ';'});
  test_tokens("SELECT a, 'b' FROM test WITH HINT;",
              {SQL_SELECT, SQL_IDENTIFIER, ',', SQL_STRING, SQL_FROM, SQL_IDENTIFIER, SQL_WITH, SQL_HINT, ';'});
}

TEST(SQLParserTokenizeStringifyTest) {
  const std::string query = "SELECT * FROM test;";
  std::vector<int16_t> tokens;
  ASSERT(SQLParser::tokenize(query, &tokens));

  // Make u16string.
  std::u16string token_string(tokens.cbegin(), tokens.cend());

  // Check if u16 string is cacheable.
  std::map<std::u16string, std::string> cache;
  cache[token_string] = query;

  ASSERT(query == cache[token_string]);
  ASSERT(&query != &cache[token_string]);
}