~ubuntu-branches/debian/sid/pgadmin3/sid

« back to all changes in this revision

Viewing changes to xtra/pgscript/bin/pgsMain.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Gerfried Fuchs
  • Date: 2009-07-30 12:27:16 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730122716-fddbh42on721bbs2
Tags: 1.10.0-1
* New upstream release.
* Adjusted watch file to match release candidates.
* Updated to Standards-Version 3.8.2:
  - Moved to Section: database.
  - Add DEB_BUILD_OPTIONS support for parallel building.
  - Move from findstring to filter suggestion for DEB_BUILD_OPTIONS parsing.
* pgagent got split into its own separate source package by upstream.
* Exclude Docs.vcproj from installation.
* Move doc-base.enus from pgadmin3 to pgadmin3-data package, the files are
  in there too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
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
 
7
//
 
8
//////////////////////////////////////////////////////////////////////////
 
9
 
 
10
// Comand-line parser
 
11
#include <wx/cmdline.h>
 
12
#include <wx/msgout.h>
 
13
 
 
14
// Output stream
 
15
#include <wx/wfstream.h>
 
16
#include <wx/txtstrm.h>
 
17
 
 
18
// pgScript
 
19
#include "pgscript/pgsApplication.h"
 
20
 
 
21
// If defined then messages are logged into debug.log
 
22
#undef USER_DEFINED_LOGGER
 
23
 
 
24
int main(int argc, char * argv[])
 
25
{
 
26
        // Initilize application
 
27
        wxInitializer initializer;
 
28
        if (!initializer.IsOk())
 
29
        {
 
30
                wxLogError(wxT("Cannot initialize the application"));
 
31
                return EXIT_FAILURE;
 
32
        }
 
33
        
 
34
        // Logging options
 
35
        sysLogger::logLevel = LOG_ERRORS;
 
36
#if defined(USER_DEFINED_LOGGER)
 
37
        wxLog * logger = new sysLogger();
 
38
        delete wxLog::SetActiveTarget(logger);
 
39
        wxLog::Resume();
 
40
#endif
 
41
        
 
42
        wxMessageOutput::Set(new wxMessageOutputStderr);
 
43
        wxCmdLineParser parser(argc, argv);
 
44
        
 
45
        wxString logo(wxT("                            ___         _      _\n")
 
46
                        wxT("                 _ __  __ _/ __| __ _ _(_)_ __| |_\n")
 
47
                        wxT("                | '_ \\/ _` \\__ \\/ _| '_| | '_ \\  _|\n")
 
48
                        wxT("                | .__/\\__, |___/\\__|_| |_| .__/\\__|\n")
 
49
                        wxT("                |_|   |___/              |_|\n\n"));
 
50
        
 
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");
 
53
        
 
54
        parser.SetLogo(logo);
 
55
        
 
56
        wxString host, db, user, pass, enc, file;
 
57
        long port = pgsApplication::default_port;
 
58
        
 
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);
 
72
        
 
73
        bool success = (parser.Parse(true) == 0);
 
74
        if (success)
 
75
        {
 
76
                // Mandatory parameters
 
77
                parser.Found(wxT("h"), &host);
 
78
                parser.Found(wxT("d"), &db);
 
79
                parser.Found(wxT("U"), &user);
 
80
                
 
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);
 
88
        
 
89
                // Create application
 
90
                pgsApplication app(host, db, user, pass, port);
 
91
                
 
92
                if (app.IsConnectionValid())
 
93
                {
 
94
                        for (size_t i = 0; i < parser.GetParamCount(); i++)
 
95
                        {
 
96
                                file = parser.GetParam(i);
 
97
                        }
 
98
                        
 
99
                        // Output stream
 
100
                        wxFileOutputStream output(2);
 
101
                        wxTextOutputStream out(output, wxEOL_UNIX);
 
102
                        
 
103
                        // Encoding
 
104
                        wxMBConv * conv = 0;
 
105
                        if (enc.IsEmpty())
 
106
                                conv = &wxConvLocal; // pnew wxConvAuto(); in the future
 
107
                        else
 
108
                                conv = pnew wxCSConv(enc);
 
109
                        
 
110
                        // Launch parser
 
111
                        app.ParseFile(file, out, conv);
 
112
                        app.Wait();
 
113
                        
 
114
                        // Delete encoding
 
115
                        if (!enc.IsEmpty())
 
116
                                pdelete(conv);
 
117
                }
 
118
        }
 
119
        
 
120
#if defined(PGSDEBUG)
 
121
        pgsAlloc::instance().dump();
 
122
#endif
 
123
        
 
124
#if defined(USER_DEFINED_LOGGER)
 
125
        delete wxLog::SetActiveTarget(NULL);
 
126
#endif
 
127
        
 
128
        return EXIT_SUCCESS;
 
129
}