Skip to content

Add --syntax option to postgres. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/backend/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "utils/memutils.h"
#include "utils/pg_locale.h"
#include "utils/ps_status.h"
#include "parser/parser.h"


const char *progname;
Expand All @@ -50,6 +51,7 @@ static void startup_hacks(const char *progname);
static void init_locale(const char *categoryname, int category, const char *locale);
static void help(const char *progname);
static void check_root(const char *progname);
static void check_syntax(void);


/*
Expand Down Expand Up @@ -153,6 +155,10 @@ main(int argc, char *argv[])
fputs(PG_BACKEND_VERSIONSTR, stdout);
exit(0);
}
if (strcmp(argv[1], "--syntax") == 0)
{
check_syntax();
}

/*
* In addition to the above, we allow "--describe-config" and "-C var"
Expand Down Expand Up @@ -347,6 +353,7 @@ help(const char *progname)
printf(_(" -s show statistics after each query\n"));
printf(_(" -S WORK-MEM set amount of memory for sorts (in kB)\n"));
printf(_(" -V, --version output version information, then exit\n"));
printf(_(" --syntax checks SQL on STDIN is valid, then exit\n"));
printf(_(" --NAME=VALUE set run-time parameter\n"));
printf(_(" --describe-config describe configuration parameters, then exit\n"));
printf(_(" -?, --help show this help, then exit\n"));
Expand Down Expand Up @@ -422,6 +429,44 @@ check_root(const char *progname)
#endif /* WIN32 */
}

static void
check_syntax() {
List *parsetree_list;
bool valid = false;
char buffer[1024 * 1024];
char ch;
int i = 0;

// TODO: check for buffer overflow
while ((ch = getchar()) != EOF)
{
buffer[i] = ch;
i++;
}

PG_TRY();
{
parsetree_list = raw_parser(buffer, RAW_PARSE_DEFAULT);
valid = true;
}
PG_CATCH();
{
EmitErrorReport();
FlushErrorState();
valid = false;
}
PG_END_TRY();

// TODO: translate output
if (valid && parsetree_list) {
printf("Valid SQL\n");
exit(0);
} else {
printf("Invalid SQL\n");
exit(1);
}
}

/*
* At least on linux, set_ps_display() breaks /proc/$pid/environ. The
* sanitizer library uses /proc/$pid/environ to implement getenv() as it wants
Expand Down