Пример #1
0
        string GatewayPath()
        {
            var path = Path.Combine(_base_path, _gateway_filename);

            if (!File.Exists(path))
            {
                throw ProgramError.Fatal($"File not found: {path}.");
            }
            return(path);
        }
Пример #2
0
        private void Connect(ConnectionInfo conn)
        {
            _connection = PQconnectdb(conn.PgConnectionString);
            if (PQstatus(_connection) != ConnStatusType.CONNECTION_OK)
            {
                throw ProgramError.Fatal($"Connection failed: {PQerrorMessageW(_connection)}.");
            }

            PQnoticeProcessorCallback cb = (a, m) => WriteNotice(m);

            PQsetNoticeProcessor(_connection, cb, IntPtr.Zero);
        }
Пример #3
0
 public void RunSql(string program, string source)
 {
     Logger.WriteLine(1, $"RunSql {source}");
     using (var cmd = new NpgsqlCommand()) {
         cmd.Connection  = _connection;
         cmd.CommandText = program;
         try {
             _output.WriteLine(cmd.ExecuteScalar() as string);
         } catch (Exception ex) {
             throw ProgramError.Fatal($"Sql failed: {ex.Message}.");
         }
     }
 }
Пример #4
0
 public void Compile(string program, string source)
 {
     Logger.WriteLine(1, $"Compile {program}");
     using (var cmd = new NpgsqlCommand()) {
         cmd.Connection  = _connection;
         cmd.CommandText = "SELECT andl_compile(@program, @source)";
         cmd.Parameters.AddWithValue("@program", program);
         cmd.Parameters.AddWithValue("@source", source);
         try {
             _output.WriteLine(cmd.ExecuteScalar() as string);
         } catch (Exception ex) {
             throw ProgramError.Fatal($"Compile failed: {ex.Message}.");
         }
     }
 }
Пример #5
0
        static internal WrapNpgsql Create(ConnectionInfo conn, TextWriter output)
        {
            Logger.WriteLine(1, $"Connect {conn.AdoConnectionString}");
            var pgw = new WrapNpgsql()
            {
                _output = output
            };

            try {
                pgw.Connect(conn);
            } catch (Exception ex) {
                throw ProgramError.Fatal($"Connection failed: {ex.Message}.");
            }
            return(pgw);
        }
Пример #6
0
        void ExecSql(string sql, string[] args)
        {
            Logger.WriteLine(1, $">>> {sql}");
            var result = PQexecParams(_connection, sql, args.Length, IntPtr.Zero, args, IntPtr.Zero, IntPtr.Zero, 0);
            var status = PQresultStatus(result);

            if (status == ExecStatusType.PGRES_COMMAND_OK)
            {
                Logger.WriteLine(1, "OK");
            }
            else if (status == ExecStatusType.PGRES_TUPLES_OK)
            {
                if (Logger.Level >= 1)
                {
                    ShowSingleTuple(result, _output);
                }
            }
            else
            {
                throw ProgramError.Fatal($"(Exec Sql) status:{status} : {PQresultErrorMessageW(result)}.");
            }
            PQclear(result);
        }
Пример #7
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;
            }
        }