2015-12-23 16:47:04 +01:00
|
|
|
|
2016-01-16 12:36:58 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string>
|
|
|
|
|
2015-12-23 17:00:41 +01:00
|
|
|
// include the sql parser
|
2015-12-23 16:47:04 +01:00
|
|
|
#include "SQLParser.h"
|
|
|
|
|
2016-01-16 12:36:58 +01:00
|
|
|
// contains printing utilities
|
2017-04-07 15:47:51 +02:00
|
|
|
#include "util/sqlhelper.h"
|
2016-01-16 12:36:58 +01:00
|
|
|
|
2015-12-23 16:47:04 +01:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc <= 1) {
|
|
|
|
fprintf(stderr, "Usage: ./example \"SELECT * FROM test;\"\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
std::string query = argv[1];
|
|
|
|
|
2015-12-23 17:00:41 +01:00
|
|
|
// parse a given query
|
2017-04-07 16:26:00 +02:00
|
|
|
hsql::SQLParserResult result;
|
|
|
|
hsql::SQLParser::parseSQLString(query, &result);
|
2015-12-23 16:47:04 +01:00
|
|
|
|
2015-12-23 17:00:41 +01:00
|
|
|
// check whether the parsing was successful
|
2017-04-05 11:56:17 +02:00
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
if (result.isValid()) {
|
2015-12-23 16:47:04 +01:00
|
|
|
printf("Parsed successfully!\n");
|
2017-04-07 16:26:00 +02:00
|
|
|
printf("Number of statements: %lu\n", result.size());
|
2016-01-16 12:36:58 +01:00
|
|
|
|
2017-04-07 16:26:00 +02:00
|
|
|
for (uint i = 0; i < result.size(); ++i) {
|
2017-02-08 13:51:50 +01:00
|
|
|
// Print a statement summary.
|
2017-04-07 16:26:00 +02:00
|
|
|
hsql::printStatementInfo(result.getStatement(i));
|
2016-01-16 12:36:58 +01:00
|
|
|
}
|
2016-02-27 16:40:24 +01:00
|
|
|
return 0;
|
2015-12-23 16:47:04 +01:00
|
|
|
} else {
|
2017-03-06 18:30:35 +01:00
|
|
|
fprintf(stderr, "Given string is not a valid SQL query.\n");
|
|
|
|
fprintf(stderr, "%s (L%d:%d)\n",
|
2017-04-07 16:26:00 +02:00
|
|
|
result.errorMsg(),
|
|
|
|
result.errorLine(),
|
|
|
|
result.errorColumn());
|
2016-02-27 16:40:24 +01:00
|
|
|
return -1;
|
2015-12-23 16:47:04 +01:00
|
|
|
}
|
2017-02-08 13:51:50 +01:00
|
|
|
}
|