1
//////////////////////////////////////////////////////////////////////////
3
// pgScript - PostgreSQL Tools
4
// RCS-ID: $Id: pgsMain.cpp 7758 2009-03-26 20:49:59Z dpage $
5
// Copyright (C) 2002 - 2009, The pgAdmin Development Team
6
// This software is released under the BSD Licence
8
//////////////////////////////////////////////////////////////////////////
11
#include <wx/cmdline.h>
12
#include <wx/msgout.h>
15
#include <wx/wfstream.h>
16
#include <wx/txtstrm.h>
19
#include "pgscript/pgsApplication.h"
21
// If defined then messages are logged into debug.log
22
#undef USER_DEFINED_LOGGER
24
int main(int argc, char * argv[])
26
// Initilize application
27
wxInitializer initializer;
28
if (!initializer.IsOk())
30
wxLogError(wxT("Cannot initialize the application"));
35
sysLogger::logLevel = LOG_ERRORS;
36
#if defined(USER_DEFINED_LOGGER)
37
wxLog * logger = new sysLogger();
38
delete wxLog::SetActiveTarget(logger);
42
wxMessageOutput::Set(new wxMessageOutputStderr);
43
wxCmdLineParser parser(argc, argv);
45
wxString logo(wxT(" ___ _ _\n")
46
wxT(" _ __ __ _/ __| __ _ _(_)_ __| |_\n")
47
wxT(" | '_ \\/ _` \\__ \\/ _| '_| | '_ \\ _|\n")
48
wxT(" | .__/\\__, |___/\\__|_| |_| .__/\\__|\n")
49
wxT(" |_| |___/ |_|\n\n"));
51
logo << wxT("Runs enhanced SQL queries on a PostgreSQL database.") << wxT("\n");
52
logo << wxT("See manual for the input file syntax.") << wxT("\n");
56
wxString host, db, user, pass, enc, file;
57
long port = pgsApplication::default_port;
59
parser.AddOption(wxT('h'), wxT("host"), wxT("database server host"),
60
wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY);
61
parser.AddOption(wxT('p'), wxT("port"), wxT("database server port"),
62
wxCMD_LINE_VAL_NUMBER, wxCMD_LINE_PARAM_OPTIONAL);
63
parser.AddOption(wxT('d'), wxT("db"), wxT("database name"),
64
wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY);
65
parser.AddOption(wxT('U'), wxT("user"), wxT("database user name"),
66
wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY);
67
parser.AddOption(wxT('W'), wxT("pass"), wxT("database user password"),
68
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
69
parser.AddOption(wxT('e'), wxT("enc"), wxT("input file encoding"),
70
wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
71
parser.AddParam(wxT("input file"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY);
73
bool success = (parser.Parse(true) == 0);
76
// Mandatory parameters
77
parser.Found(wxT("h"), &host);
78
parser.Found(wxT("d"), &db);
79
parser.Found(wxT("U"), &user);
81
// Optional parameters
82
if (parser.Found(wxT("p")))
83
parser.Found(wxT("p"), &port);
84
if (parser.Found(wxT("W")))
85
parser.Found(wxT("W"), &pass);
86
if (parser.Found(wxT("e")))
87
parser.Found(wxT("e"), &enc);
90
pgsApplication app(host, db, user, pass, port);
92
if (app.IsConnectionValid())
94
for (size_t i = 0; i < parser.GetParamCount(); i++)
96
file = parser.GetParam(i);
100
wxFileOutputStream output(2);
101
wxTextOutputStream out(output, wxEOL_UNIX);
106
conv = &wxConvLocal; // pnew wxConvAuto(); in the future
108
conv = pnew wxCSConv(enc);
111
app.ParseFile(file, out, conv);
120
#if defined(PGSDEBUG)
121
pgsAlloc::instance().dump();
124
#if defined(USER_DEFINED_LOGGER)
125
delete wxLog::SetActiveTarget(NULL);