extended example code

This commit is contained in:
Pedro Flemming 2016-01-16 12:36:58 +01:00
parent b2bfe2bb1c
commit f98843a316
3 changed files with 40 additions and 12 deletions

View File

@ -1,7 +1,13 @@
#include <stdlib.h>
#include <string>
// include the sql parser // include the sql parser
#include "SQLParser.h" #include "SQLParser.h"
// contains printing utilities
#include "sqlhelper.h"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc <= 1) { if (argc <= 1) {
fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n"); fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n");
@ -10,13 +16,17 @@ int main(int argc, char *argv[]) {
std::string query = argv[1]; std::string query = argv[1];
// parse a given query // parse a given query
hsql::SQLStatementList* stmt_list = hsql::SQLParser::parseSQLString(query); hsql::SQLStatementList* result = hsql::SQLParser::parseSQLString(query);
// check whether the parsing was successful // check whether the parsing was successful
if (stmt_list->isValid) { if (result->isValid) {
printf("Parsed successfully!\n"); printf("Parsed successfully!\n");
printf("Number of statements: %lu\n", stmt_list->numStatements()); printf("Number of statements: %lu\n", result->numStatements());
// process the statements...
for (hsql::SQLStatement* stmt : result->statements) {
// process the statements...
hsql::printStatementInfo(stmt);
}
} else { } else {
printf("Invalid SQL!\n"); printf("Invalid SQL!\n");
} }

View File

@ -7,13 +7,13 @@ namespace hsql {
void printOperatorExpression(Expr* expr, uint num_indent); void printOperatorExpression(Expr* expr, uint num_indent);
const char* indent(uint num_indent) { return std::string(num_indent, '\t').c_str(); } std::string indent(uint num_indent) { return std::string(num_indent, '\t'); }
void inprint(int64_t val, uint num_indent) { printf("%s%ld \n", indent(num_indent), val); } void inprint(int64_t val, uint num_indent) { printf("%s%lld \n", indent(num_indent).c_str(), val); }
void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent), val); } void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent).c_str(), val); }
void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent), val); } void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent).c_str(), val); }
void inprint(const char* val, const char* val2, uint num_indent) { printf("%s%s->%s\n", indent(num_indent), val, val2); } void inprint(const char* val, const char* val2, uint num_indent) { printf("%s%s->%s\n", indent(num_indent).c_str(), val, val2); }
void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent), val); } void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent).c_str(), val); }
void inprintU(uint64_t val, uint num_indent) { printf("%s%lu\n", indent(num_indent), val); } void inprintU(uint64_t val, uint num_indent) { printf("%s%llu\n", indent(num_indent).c_str(), val); }
void printTableRefInfo(TableRef* table, uint num_indent) { void printTableRefInfo(TableRef* table, uint num_indent) {
switch (table->type) { switch (table->type) {
@ -139,8 +139,25 @@ void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) {
printSelectStatementInfo(stmt->select, num_indent+1); printSelectStatementInfo(stmt->select, num_indent+1);
break; break;
} }
} }
void printStatementInfo(SQLStatement* stmt) {
switch (stmt->type()) {
case kStmtSelect:
printSelectStatementInfo((SelectStatement*) stmt, 0);
break;
case kStmtInsert:
printInsertStatementInfo((InsertStatement*) stmt, 0);
break;
case kStmtCreate:
printCreateStatementInfo((CreateStatement*) stmt, 0);
break;
case kStmtImport:
printImportStatementInfo((ImportStatement*) stmt, 0);
break;
default:
break;
}
}
} // namespace hsql } // namespace hsql

View File

@ -7,6 +7,7 @@
namespace hsql { namespace hsql {
void printStatementInfo(SQLStatement* stmt);
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent); void printSelectStatementInfo(SelectStatement* stmt, uint num_indent);
void printImportStatementInfo(ImportStatement* stmt, uint num_indent); void printImportStatementInfo(ImportStatement* stmt, uint num_indent);
void printInsertStatementInfo(InsertStatement* stmt, uint num_indent); void printInsertStatementInfo(InsertStatement* stmt, uint num_indent);