#!/usr/bin/perl
# DBURL - DataBase Uniform Resource Location

require "dburl.pl";

tryit('pgsql://');  # all default
tryit('pgsqls://'); # SSL, other default

tryit('pgsql://joe:foo@example.com:5432/mydb/SELECT * FROM person');
tryit('pgsqls://joe:foo@example.com:5432/mydb/SELECT firstname, gender FROM person');
tryit('pgsql://joe:foo@example.com:5432/mydb/person/firstname,gender');
tryit('pgsql://joe@127.0.0.1:2/mydb/person/firstname,gender');
tryit('pgsql://joe:@127.0.0.1:2/mydb/person/firstname,gender');
tryit('pgsqls://joe@127.0.0.1:2/mydb/person/firstname,gender');
tryit('pgsql://127.0.0.1/');
tryit('pgsql://127.0.0.1');	# actually, this should fail
tryit('pgsql:///');		# socket and default port and default database
tryit('pgsql:///mydb');		# socket and default port and mydb database
tryit('pgsql:///mydb/');	# socket and default port and mydb database
tryit('pgsql://:1234/');	# socket and port 1234
tryit('pgsql://localhost');
tryit('pgsql://localhost/');
tryit('pgsql://localhost:5432/');
tryit('pgsql://localhost:5432');
tryit('pgsql://localhost/mydb');
tryit('pgsql://localhost/mydb/');
tryit('pgsql://localhost/mydb/person');
tryit('pgsql://localhost/mydb/person/');
tryit('pgsql://joe:@');	# OK, password is blank

# ERROR
tryit('pgsql://:localhost');	# port number error
tryit('pgsql://@');	# no user name given

sub tryit() {
	my $dburltext = shift;
	my $d = dburlparse($dburltext);
	print "dburl: $dburltext\n";
	if (!$d->{status}) {
		print "\e[31mError: can not parse URL\e[0m\n";
	} else {
		print "scheme: $d->{scheme}\n";
		print "user: $d->{user}\n";
		print "password: $d->{password}\n";
		print "host: $d->{host}\n";
		print "port: $d->{port}\n";
		print "database: $d->{database}\n";
		print "table: $d->{table}\n";
		print "column: $d->{column}\n";	# comma delimited
		print "sql: $d->{sql}\n";
	}
	print "\n";
}

