Skip to content

Commit e5c18f8

Browse files
committed
feat: add core types — StringRef, Dialect, StmtType, NodeType enums
1 parent 5257914 commit e5c18f8

1 file changed

Lines changed: 150 additions & 0 deletions

File tree

include/sql_parser/common.h

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#ifndef SQL_PARSER_COMMON_H
2+
#define SQL_PARSER_COMMON_H
3+
4+
#include <cstdint>
5+
#include <cstring>
6+
#include <type_traits>
7+
8+
namespace sql_parser {
9+
10+
// -- Dialect --
11+
12+
enum class Dialect : uint8_t {
13+
MySQL,
14+
PostgreSQL
15+
};
16+
17+
// -- StringRef: zero-copy view into input buffer --
18+
19+
struct StringRef {
20+
const char* ptr = nullptr;
21+
uint32_t len = 0;
22+
23+
bool empty() const { return len == 0; }
24+
25+
bool equals_ci(const char* s, uint32_t slen) const {
26+
if (len != slen) return false;
27+
for (uint32_t i = 0; i < len; ++i) {
28+
char a = ptr[i];
29+
char b = s[i];
30+
if (a >= 'A' && a <= 'Z') a += 32;
31+
if (b >= 'A' && b <= 'Z') b += 32;
32+
if (a != b) return false;
33+
}
34+
return true;
35+
}
36+
37+
bool operator==(const StringRef& o) const {
38+
return len == o.len && (ptr == o.ptr || std::memcmp(ptr, o.ptr, len) == 0);
39+
}
40+
bool operator!=(const StringRef& o) const { return !(*this == o); }
41+
};
42+
static_assert(std::is_trivially_copyable_v<StringRef>);
43+
44+
// Case-insensitive comparison for keyword lookup (used by keyword tables)
45+
inline int ci_cmp(const char* a, uint32_t alen, const char* b, uint8_t blen) {
46+
uint32_t minlen = alen < blen ? alen : blen;
47+
for (uint32_t i = 0; i < minlen; ++i) {
48+
char ca = a[i];
49+
char cb = b[i];
50+
if (ca >= 'a' && ca <= 'z') ca -= 32;
51+
if (cb >= 'a' && cb <= 'z') cb -= 32;
52+
if (ca != cb) return ca < cb ? -1 : 1;
53+
}
54+
if (alen < blen) return -1;
55+
if (alen > blen) return 1;
56+
return 0;
57+
}
58+
59+
// -- Statement type (always set, even for PARTIAL/ERROR) --
60+
61+
enum class StmtType : uint8_t {
62+
UNKNOWN = 0,
63+
SELECT,
64+
INSERT,
65+
UPDATE,
66+
DELETE_STMT,
67+
REPLACE,
68+
SET,
69+
USE,
70+
SHOW,
71+
BEGIN,
72+
START_TRANSACTION,
73+
COMMIT,
74+
ROLLBACK,
75+
SAVEPOINT,
76+
PREPARE,
77+
EXECUTE,
78+
DEALLOCATE,
79+
CREATE,
80+
ALTER,
81+
DROP,
82+
TRUNCATE,
83+
GRANT,
84+
REVOKE,
85+
LOCK,
86+
UNLOCK,
87+
LOAD_DATA,
88+
RESET,
89+
};
90+
91+
// -- AST node types --
92+
93+
enum class NodeType : uint16_t {
94+
NODE_UNKNOWN = 0,
95+
96+
// Tier 2 lightweight nodes
97+
NODE_STATEMENT,
98+
NODE_TABLE_REF,
99+
NODE_SCHEMA_REF,
100+
NODE_IDENTIFIER,
101+
NODE_QUALIFIED_NAME,
102+
103+
// Tier 1 nodes (SELECT)
104+
NODE_SELECT_STMT,
105+
NODE_SELECT_OPTIONS,
106+
NODE_SELECT_ITEM_LIST,
107+
NODE_SELECT_ITEM,
108+
NODE_FROM_CLAUSE,
109+
NODE_JOIN_CLAUSE,
110+
NODE_WHERE_CLAUSE,
111+
NODE_GROUP_BY_CLAUSE,
112+
NODE_HAVING_CLAUSE,
113+
NODE_ORDER_BY_CLAUSE,
114+
NODE_ORDER_BY_ITEM,
115+
NODE_LIMIT_CLAUSE,
116+
NODE_LOCKING_CLAUSE,
117+
NODE_INTO_CLAUSE,
118+
NODE_ALIAS,
119+
120+
// Tier 1 nodes (SET)
121+
NODE_SET_STMT,
122+
NODE_SET_NAMES,
123+
NODE_SET_CHARSET,
124+
NODE_SET_TRANSACTION,
125+
NODE_VAR_ASSIGNMENT,
126+
NODE_VAR_TARGET,
127+
128+
// Expression nodes
129+
NODE_EXPRESSION,
130+
NODE_BINARY_OP,
131+
NODE_UNARY_OP,
132+
NODE_FUNCTION_CALL,
133+
NODE_LITERAL_INT,
134+
NODE_LITERAL_FLOAT,
135+
NODE_LITERAL_STRING,
136+
NODE_LITERAL_NULL,
137+
NODE_PLACEHOLDER,
138+
NODE_SUBQUERY,
139+
NODE_COLUMN_REF,
140+
NODE_ASTERISK,
141+
NODE_IS_NULL,
142+
NODE_IS_NOT_NULL,
143+
NODE_BETWEEN,
144+
NODE_IN_LIST,
145+
NODE_CASE_WHEN,
146+
};
147+
148+
} // namespace sql_parser
149+
150+
#endif // SQL_PARSER_COMMON_H

0 commit comments

Comments
 (0)