forked from dstevenson1/purescript-postgresql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rows.pl
121 lines (102 loc) · 3.28 KB
/
Rows.pl
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use strict;
use warnings;
sub vars {
my @as;
for (my $a = 'a', my $i = 0; $i < $_; ++$a, ++$i) {
push @as, $a;
}
@as
}
if (@ARGV != 1) {
die 'Usage: perl Rows.perl src/Database/PostgreSQL/Row.purs';
}
open my $out, '>', $ARGV[0]
or die $!;
print $out <<'EOF';
module Database.PostgreSQL.Row where
import Data.Array as Array
import Data.Either (Either(..))
import Data.Foreign (Foreign)
import Database.PostgreSQL.Value (class FromSQLValue, class ToSQLValue, fromSQLValue, toSQLValue)
import Prelude
-- | Convert things to SQL rows.
class ToSQLRow a where
toSQLRow :: a -> Array Foreign
-- | Convert things from SQL rows.
class FromSQLRow a where
fromSQLRow :: Array Foreign -> Either String a
EOF
for (0 .. 19) {
print $out "\n";
print $out "-- | A row with $_ field" . ($_ == 1 ? '' : 's') . ".\n";
print $out "data Row$_";
print $out map { " $_" } vars($_);
print $out " = Row$_";
print $out map { " $_" } vars($_);
print $out "\n\n";
if ($_ == 0) {
print $out "derive instance eqRow$_ :: Eq Row$_";
} else {
print $out "derive instance eqRow$_ :: (";
print $out join(', ', map { "Eq $_" } vars($_));
print $out ") => Eq (Row$_" . join('', map { " $_" } vars($_)) . ")";
}
print $out "\n\n";
if ($_ == 0) {
print $out "derive instance ordRow$_ :: Ord Row$_";
} else {
print $out "derive instance ordRow$_ :: (";
print $out join(', ', map { "Ord $_" } vars($_));
print $out ") => Ord (Row$_" . join('', map { " $_" } vars($_)) . ")";
}
print $out "\n\n";
if ($_ == 0) {
print $out "instance showRow$_ :: Show Row$_";
} else {
print $out "instance showRow$_ :: (";
print $out join(', ', map { "Show $_" } vars($_));
print $out ") => Show (Row$_" . join('', map { " $_" } vars($_)) . ')';
}
print $out " where\n";
if ($_ == 0) {
print $out " show Row$_ =\n";
print $out " \"Row$_\"";
} else {
print $out " show (Row$_" . join('', map { " $_" } vars($_)) . ") = \n";
print $out " \"(Row$_ \" <> ";
print $out join(' <> " " <> ', map { "show $_" } vars($_));
print $out " <> \")\"";
}
print $out "\n\n";
if ($_ == 0) {
print $out "instance fromSQLRowRow$_ :: FromSQLRow Row$_";
} else {
print $out "instance fromSQLRowRow$_ :: (";
print $out join(', ', map { "FromSQLValue $_" } vars($_));
print $out ") => FromSQLRow (Row$_" . join('', map { " $_" } vars($_)) . ')';
}
print $out " where\n";
print $out ' fromSQLRow [' . join(', ', vars($_)) . "] =\n";
print $out " pure Row$_\n";
for (vars($_)) {
print $out " <*> fromSQLValue $_\n";
}
print $out ' fromSQLRow xs = Left $ "Row has " <> show n <> " fields,';
print $out " expecting $_.\"\n";
print $out ' where n = Array.length xs';
print $out "\n\n";
if ($_ == 0) {
print $out "instance toSQLRowRow$_ :: ToSQLRow Row$_";
} else {
print $out "instance toSQLRowRow$_ :: (";
print $out join(', ', map { "ToSQLValue $_" } vars($_));
print $out ") => ToSQLRow (Row$_" . join('', map { " $_" } vars($_)) . ')';
}
print $out " where\n";
if ($_ == 0) {
print $out " toSQLRow Row$_ = []";
} else {
print $out " toSQLRow (Row$_" . join('', map { " $_" } vars($_)) . ") = \n";
print $out ' [' . join(', ', map { "toSQLValue $_" } vars($_)) . ']';
}
}