#include "Parser.h"
#include "ParserIdDef.h"
#include "Scanner.h"
#include "SqlObjs.h"
#include <fstream>
#include <iostream>
enum
{
MAIN_SUCCESS = 0,
MAIN_ARG_ERROR,
MAIN_OPEN_FILE_ERROR,
MAIN_NOT_ACCEPTED,
MAIN_NULL_RESULT,
MAIN_CAUGHT
};
void printSqlCommands(const std::vector<std::string> &sqls)
{
int ind = 0;
for (auto &i: sqls)
std::cout <<(++ind) <<": " <<i <<";\n";
}
int parseSQL(const char *filename, std::istream &in)
{
C_SQLParser parser;
C_SQLScanner scanner(screener);
size_t ind{};
parser.eachAccepted([&ind](auto &p){
if (auto sql = dynamic_cast<const I_SqlCmd*>(p.get()))
{
++ind;
std::cout <<"MySQL["<<ind<<"]:\n";
printSqlCommands(sql->to(SST_MYSQL, {}));
std::cout <<"\n"
"SQLite["<<ind<<"]:\n";
printSqlCommands(sql->to(SST_SQLITE, {}));
}
else
std::cout <<"Null SQL["<<ind<<"]\n";
});
if (!ind)
{
std::cout <<"Not accepted on EOF\n";
return MAIN_NOT_ACCEPTED;
}
return MAIN_SUCCESS;
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout <<
"\n"
"parsesql <SqlCmd>\n"
"parsesql @ <FileContainingSqlCmd>\n"
"\n"
"If <SqlCmd> starts with a '@', the rest chars denote a file name from which the input SQL will be read.\n";
return MAIN_ARG_ERROR;
}
try
{
if (argv[1][0] == '@')
{
auto file = argv[1][1]? argv[1]+1: argv[2];
std::ifstream in(file);
if (!in)
{
std::cout <<"Fail to read \"" <<file <<"\"\n";
return MAIN_OPEN_FILE_ERROR;
}
return parseSQL(file, in);
}
else
{
return parseSQL("ARG1", in);
}
}
catch (const std::exception &t)
{
std::cout <<
HRTN(t) <<
": " <<t.what() <<
'\n';
return MAIN_CAUGHT;
}
catch (...)
{
std::cout <<"Unknown exception\n";
return MAIN_CAUGHT;
}
}
void scanFile(std::string_view filename, std::istream &in, I_Scanner< T_Char > &scanner, T_LexID endToken=TID_EOF, T_Encoding encoding=0)