CREATE dispatches to TABLE or VIEW.
| 1140 | |
| 1141 | // CREATE dispatches to TABLE or VIEW. |
| 1142 | ParsedStmtPtr Parser::ParseCreateStatement() { |
| 1143 | Expect(TokenType::KW_CREATE, ""); |
| 1144 | |
| 1145 | // CREATE OR REPLACE VIEW |
| 1146 | bool or_replace = false; |
| 1147 | if (CheckKeyword(TokenType::KW_OR)) { |
| 1148 | Advance(); |
| 1149 | if (Check(TokenType::IDENTIFIER) && StringUtil::Upper(Current().value) == "REPLACE") { |
| 1150 | Advance(); |
| 1151 | or_replace = true; |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | // CREATE [OR REPLACE] [LIVE] VIEW |
| 1156 | bool is_live = false; |
| 1157 | if (CheckKeyword(TokenType::KW_LIVE)) { |
| 1158 | Advance(); |
| 1159 | is_live = true; |
| 1160 | } |
| 1161 | |
| 1162 | if (CheckKeyword(TokenType::KW_VIEW)) { |
| 1163 | Advance(); // consume VIEW |
| 1164 | auto stmt = std::make_unique<CreateViewStatement>(); |
| 1165 | stmt->or_replace = or_replace; |
| 1166 | stmt->is_live = is_live; |
| 1167 | stmt->view_name = ExpectIdentifier("for view name").value; |
| 1168 | Expect(TokenType::KW_AS, "after view name"); |
| 1169 | auto inner = ParseSelectStatement(); |
| 1170 | stmt->query = std::unique_ptr<SelectStatement>( |
| 1171 | static_cast<SelectStatement *>(inner.release())); |
| 1172 | return stmt; |
| 1173 | } |
| 1174 | |
| 1175 | // Otherwise expect TABLE - put CREATE back (already consumed). |
| 1176 | // We need to parse the rest of CREATE TABLE without the CREATE keyword. |
| 1177 | Expect(TokenType::KW_TABLE, "after CREATE"); |
| 1178 | |
| 1179 | auto stmt = std::make_unique<CreateTableStatement>(); |
| 1180 | stmt->or_replace = or_replace; |
| 1181 | if (MatchKeyword(TokenType::KW_IF)) { |
| 1182 | Expect(TokenType::KW_NOT, "after IF"); |
| 1183 | Expect(TokenType::KW_EXISTS, "after NOT"); |
| 1184 | stmt->if_not_exists = true; |
| 1185 | } |
| 1186 | |
| 1187 | stmt->table_name = ExpectIdentifier("for table name").value; |
| 1188 | |
| 1189 | // CREATE [OR REPLACE] TABLE <name> AS SELECT ... - CTAS. |
| 1190 | // Schema is inferred at execution from the SELECT's result types, so |
| 1191 | // we just stash the SelectStatement; Connection materializes into a |
| 1192 | // real table. Mirrors the CREATE VIEW branch above. |
| 1193 | if (MatchKeyword(TokenType::KW_AS)) { |
| 1194 | auto inner = ParseSelectStatement(); |
| 1195 | stmt->query = std::unique_ptr<SelectStatement>( |
| 1196 | static_cast<SelectStatement *>(inner.release())); |
| 1197 | return stmt; |
| 1198 | } |
| 1199 |
nothing calls this directly
no outgoing calls
no test coverage detected