Exemplo n.º 1
0
        static internal WrapLibpq Create(ConnectionInfo conn, TextWriter output)
        {
            Logger.WriteLine(1, $"Connect {conn.PgConnectionString}");
            var pgw = new WrapLibpq()
            {
                _output = output
            };

            pgw.Connect(conn);
            return(pgw);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Logger.Open(1);
            _output = Console.Out;
            _output.WriteLine(AndlVersion);
            var options = OptionParser.Create(_options, Help);

            if (!options.Parse(args))
            {
                return;
            }
            var path     = options.GetPath(0) ?? "test.andl";
            var database = options.GetPath(1) ?? "db";

            try {
                if (!File.Exists(path))
                {
                    throw ProgramError.Fatal($"file does not exist: {path}");
                }
                var input = new StreamReader(path).ReadToEnd();

                var conn = ConnectionInfo.Create("localhost", "postgres", database);
                var pgw  = WrapLibpq.Create(conn, _output);
                // use npgsql instead
                //var conn = ConnectionInfo.Create("localhost", "admin", "zzxx", "Try1");
                //var pgw = WrapNpgsql.Create(conn);

                var bp = new Boilerplate();
                if (_usepreamble)
                {
                    pgw.RunSql(bp.Preamble(), "preamble");
                }

                switch (Path.GetExtension(path))
                {
                case ".andl":
                    pgw.Compile(input, path);
                    break;

                case ".sql":
                case ".pgs":
                case ".pgsql":
                    pgw.RunSql(input, path);
                    break;

                default:
                    throw ProgramError.Fatal($"no action defined for {path}");
                }

                if (_usepostamble)
                {
                    pgw.RunSql(bp.Postamble(), "postamble");
                }

                pgw.Close();
            } catch (ProgramException ex) {
                _output.WriteLine(ex.Message);
                return;
            } catch (Exception ex) {
                _output.WriteLine($"Unexpected exception: {ex.ToString()}");
                return;
            }
        }