#! raku

use v6.d;

use Protocol::Postgres;

unit sub MAIN(Str :$user = ~$*USER, Str :$database = 'test', Str :$password, :$host = 'localhost', :$port = 5432);

my $socket = await IO::Socket::Async.connect($host, $port);
my $client = Protocol::Postgres::Client.new;
$socket.Supply(:bin).act({ $client.incoming-data($^data) });
$client.outbound-data.act({ await $socket.write($^data) });

await $client.startup($user, $database, $password);

for lines() {
	when / ^ '\\'? q[uit]? / {
		$client.terminate;
		last;
	}
	when / ^ p['arameter ']? $<parameter>=[\w+] / {
		say $client.get-parameter(~$<parameter>);
	}
	default {
		CATCH { default { say .message } }
		given await $client.query(~$_) {
			when Protocol::Postgres::ResultSet {
				react {
					whenever .hash-rows -> $row {
						dd $row;
					}
				}
			}
			when Bool {
				say "Succes.";
			}
		}
	}
}
