@@ -16,10 +16,11 @@ class Program {
16
16
const string Help = "AndlPg <script.ext> [<database name>] [/options]\n "
17
17
+ "\t \t Script extension must be andl, sql, pgsql or pgs.\n "
18
18
+ "\t \t Default script is test.andl, database is 'db'.\n "
19
- + "\t /d\t Add #source directive for current directory"
20
- + "\t /p[o]\t Add Postgres preamble and postamble (o for preamble only)"
21
- + "\t /s\t Sql (ignored)"
22
- + "\t /n\t n=1 to 4, set tracing level" ;
19
+ + "\t \t Hardwired for user postgres, password from pgpass.\n "
20
+ + "\t /d\t Add #source directive for current directory\n "
21
+ + "\t /p[o]\t Add Postgres preamble and postamble (o for preamble only)\n "
22
+ + "\t /s\t Sql (ignored)\n "
23
+ + "\t /n\t n=1 to 4, set tracing level (and add #noisy directive)" ;
23
24
static readonly Dictionary < string , Action < string > > _options = new Dictionary < string , Action < string > > {
24
25
{ "p" , ( a ) => { _usepreamble = true ; _usepostamble = ( a != "o" ) ; } } ,
25
26
{ "d" , ( a ) => { AddSource = true ; } } ,
@@ -47,13 +48,14 @@ static void Main(string[] args) {
47
48
if ( ! File . Exists ( path ) ) throw ProgramError . Fatal ( $ "file does not exist: { path } ") ;
48
49
var input = new StreamReader ( path ) . ReadToEnd ( ) ;
49
50
50
- var conn = ConnectionInfo . Create ( "localhost" , "postgres" , "zzxx" , database ) ;
51
+ var conn = ConnectionInfo . Create ( "localhost" , "postgres" , database ) ;
51
52
var pgw = WrapLibpq . Create ( conn , _output ) ;
52
53
// use npgsql instead
53
54
//var conn = ConnectionInfo.Create("localhost", "admin", "zzxx", "Try1");
54
55
//var pgw = WrapNpgsql.Create(conn);
55
56
56
- if ( _usepreamble ) pgw . RunSql ( Boilerplate . Preamble , "preamble" ) ;
57
+ var bp = new Boilerplate ( ) ;
58
+ if ( _usepreamble ) pgw . RunSql ( bp . Preamble ( ) , "preamble" ) ;
57
59
58
60
switch ( Path . GetExtension ( path ) ) {
59
61
case ".andl" :
@@ -68,7 +70,7 @@ static void Main(string[] args) {
68
70
throw ProgramError . Fatal ( $ "no action defined for { path } ") ;
69
71
}
70
72
71
- if ( _usepostamble ) pgw . RunSql ( Boilerplate . Postamble , "postamble" ) ;
73
+ if ( _usepostamble ) pgw . RunSql ( bp . Postamble ( ) , "postamble" ) ;
72
74
73
75
pgw . Close ( ) ;
74
76
} catch ( ProgramException ex ) {
@@ -91,17 +93,16 @@ public class ConnectionInfo {
91
93
public string Password { get ; set ; }
92
94
public string Database { get ; set ; }
93
95
public string AdoConnectionString {
94
- get { return $ "Host={ Host } ;Username={ Username } ;password= { Password } ; Database={ Database } "; }
96
+ get { return $ "Host={ Host } ;Username={ Username } ;Database={ Database } "; }
95
97
}
96
98
public string PgConnectionString {
97
- get { return $ "host={ Host } user={ Username } password= { Password } dbname={ Database } "; }
99
+ get { return $ "host={ Host } user={ Username } dbname={ Database } "; }
98
100
}
99
101
100
- public static ConnectionInfo Create ( string host , string username , string password , string database ) {
102
+ public static ConnectionInfo Create ( string host , string username , string database ) {
101
103
return new ConnectionInfo {
102
104
Host = host ,
103
105
Username = username ,
104
- Password = password ,
105
106
Database = database ,
106
107
} ;
107
108
}
@@ -151,21 +152,45 @@ public static ScriptLines Create(TextReader reader) {
151
152
/// </summary>
152
153
public class Boilerplate {
153
154
// preamble for loading plandl. note lines end in ';'
154
- static string plandl_path = @"D:/MyDocs/dev/vs14/Andl/x64/Debug/plandl" ;
155
- static string gateway_path = @"D:\MyDocs\dev\vs14\Andl\Debug\Andl.Gateway.dll" ;
156
- static int _noisy { get { return Logger . Level ; } }
157
-
158
- public static string Preamble {
159
- get { return $@ "
155
+ const string _plandl_filename = @"x64\plandl.dll" ;
156
+ static string _gateway_filename = @"Andl.Gateway.dll" ;
157
+ string _base_path ;
158
+ //static string plandl_path = @"D:/MyDocs/dev/vs14/Andl/x64/Debug/plandl";
159
+ //static string gateway_path = @"D:\MyDocs\dev\vs14\Andl\Debug\Andl.Gateway.dll";
160
+ int _noisy { get { return Logger . Level ; } }
161
+
162
+ public string Preamble ( ) {
163
+ return $@ "
160
164
DROP FUNCTION IF EXISTS plandl_call_handler() CASCADE;
161
- CREATE OR REPLACE FUNCTION plandl_call_handler() RETURNS language_handler AS '{ plandl_path } ' LANGUAGE C;
162
- CREATE OR REPLACE LANGUAGE plandl HANDLER plandl_call_handler;
165
+ CREATE OR REPLACE FUNCTION plandl_call_handler() RETURNS language_handler
166
+ AS '{ PlandlPath ( ) } ' LANGUAGE C;
167
+ CREATE OR REPLACE LANGUAGE plandl HANDLER plandl_call_handler;
163
168
CREATE OR REPLACE FUNCTION plandl_compile(program text, source text) returns text
164
- AS '{ gateway_path } |Noisy={ _noisy } ' LANGUAGE plandl;" ; } // '|Debug,Noisy=2'
169
+ AS '{ GatewayPath ( ) } |Noisy={ _noisy } ' LANGUAGE plandl;" ; // '|Debug,Noisy=2'
170
+ }
171
+
172
+ public string Postamble ( ) {
173
+ return @"DROP FUNCTION plandl_call_handler() CASCADE;" ;
174
+ }
175
+
176
+ string PlandlPath ( ) {
177
+ var path = Path . Combine ( _base_path , _plandl_filename ) ;
178
+ if ( ! File . Exists ( path ) )
179
+ throw ProgramError . Fatal ( $ "File not found: { path } .") ;
180
+ return path ;
181
+ }
182
+
183
+ string GatewayPath ( ) {
184
+ var path = Path . Combine ( _base_path , _gateway_filename ) ;
185
+ if ( ! File . Exists ( path ) )
186
+ throw ProgramError . Fatal ( $ "File not found: { path } .") ;
187
+ return path ;
165
188
}
166
189
167
- public static string Postamble {
168
- get { return @"DROP FUNCTION plandl_call_handler() CASCADE;" ; }
190
+ public Boilerplate ( ) {
191
+ var fn = AppDomain . CurrentDomain . BaseDirectory ;
192
+ _base_path = Path . GetDirectoryName ( fn ) ;
193
+ //_base_path = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
169
194
}
170
195
}
171
196
0 commit comments