-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathpg.raku
executable file
·62 lines (48 loc) · 1.48 KB
/
pg.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env perl6
use v6;
use lib 'lib';
use DBIish;
use NativeCall;
# Windows support
if $*DISTRO.is-win {
# libpq.dll on windows depends on libeay32.dll which in this path
my constant PG-HOME = 'C:\Program Files\PostgreSQL\9.3';
my $path = sprintf( 'Path=%s;%s\bin', %*ENV<Path>, PG-HOME );
%*ENV<DBIISH_PG_LIB> = (PG-HOME).fmt( '%s\lib\libpq.dll' );
# Since %*ENV<Path> = ... does not actually own process environment
# Weird magic but needed atm :)
sub _putenv(Str) is native('msvcrt') { ... }
_putenv( $path);
}
my $dbh = DBIish.connect("Pg", :database<postgres>, :user<postgres>, :password<sa>, :RaiseError);
my $sth = $dbh.execute(q:to/STATEMENT/);
DROP TABLE IF EXISTS nom
STATEMENT
$sth = $dbh.execute(q:to/STATEMENT/);
CREATE TABLE nom (
name varchar(4),
description varchar(30),
quantity int,
price numeric(5,2)
)
STATEMENT
$sth = $dbh.execute(q:to/STATEMENT/);
INSERT INTO nom (name, description, quantity, price)
VALUES ( 'BUBH', 'Hot beef burrito', 1, 4.95 )
STATEMENT
$sth = $dbh.prepare(q:to/STATEMENT/);
INSERT INTO nom (name, description, quantity, price)
VALUES ( ?, ?, ?, ? )
STATEMENT
$sth.execute('TAFM', 'Mild fish taco', 1, 4.85);
$sth.execute('BEOM', 'Medium size orange juice', 2, 1.20);
$sth = $dbh.prepare(q:to/STATEMENT/);
SELECT name, description, quantity, price, quantity*price AS amount
FROM nom
STATEMENT
$sth.execute;
my @array = $sth.allrows();
say @array.elems; # 3
# Cleanup
$sth.dispose;
$dbh.dispose;