]> gitweb.factorcode.org Git - factor.git/blob - extra/db/postgresql/lib/lib.factor
Fixing everything for mandatory stack effects
[factor.git] / extra / db / postgresql / lib / lib.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays continuations db io kernel math namespaces
4 quotations sequences db.postgresql.ffi alien alien.c-types
5 db.types tools.walker ascii splitting math.parser combinators
6 libc shuffle calendar.format byte-arrays destructors prettyprint
7 accessors strings serialize io.encodings.binary io.encodings.utf8
8 alien.strings io.streams.byte-array inspector ;
9 IN: db.postgresql.lib
10
11 : postgresql-result-error-message ( res -- str/f )
12     dup zero? [
13         drop f
14     ] [
15         PQresultErrorMessage [ blank? ] trim
16     ] if ;
17
18 : postgres-result-error ( res -- )
19     postgresql-result-error-message [ throw ] when* ;
20
21 : (postgresql-error-message) ( handle -- str )
22     PQerrorMessage
23     "\n" split [ [ blank? ] trim ] map "\n" join ;
24
25 : postgresql-error-message ( -- str )
26     db get handle>> (postgresql-error-message) ;
27
28 : postgresql-error ( res -- res )
29     dup [ postgresql-error-message throw ] unless ;
30
31 ERROR: postgresql-result-null ;
32
33 M: postgresql-result-null summary ( obj -- str )
34     drop "PQexec returned f." ;
35
36 : postgresql-result-ok? ( res -- ? )
37     [ postgresql-result-null ] unless*
38     PQresultStatus
39     PGRES_COMMAND_OK PGRES_TUPLES_OK 2array member? ;
40
41 : connect-postgres ( host port pgopts pgtty db user pass -- conn )
42     PQsetdbLogin
43     dup PQstatus zero? [ (postgresql-error-message) throw ] unless ;
44
45 : do-postgresql-statement ( statement -- res )
46     db get handle>> swap sql>> PQexec dup postgresql-result-ok? [
47         [ postgresql-result-error-message ] [ PQclear ] bi throw
48     ] unless ;
49
50 : type>oid ( symbol -- n )
51     dup array? [ first ] when
52     {
53         { BLOB [ BYTEA-OID ] }
54         { FACTOR-BLOB [ BYTEA-OID ] }
55         [ drop 0 ]
56     } case ;
57
58 : type>param-format ( symbol -- n )
59     dup array? [ first ] when
60     {
61         { BLOB [ 1 ] }
62         { FACTOR-BLOB [ 1 ] }
63         [ drop 0 ]
64     } case ;
65
66 : param-types ( statement -- seq )
67     in-params>> [ type>> type>oid ] map >c-uint-array ;
68
69 : malloc-byte-array/length ( byte-array -- alien length )
70     [ malloc-byte-array &free ] [ length ] bi ;
71
72 : default-param-value ( obj -- alien n )
73     number>string* dup [ utf8 malloc-string &free ] when 0 ;
74
75 : param-values ( statement -- seq seq2 )
76     [ bind-params>> ] [ in-params>> ] bi
77     [
78         >r value>> r> type>> {
79             { FACTOR-BLOB [
80                 dup [ object>bytes malloc-byte-array/length ] [ 0 ] if
81             ] }
82             { BLOB [ dup [ malloc-byte-array/length ] [ 0 ] if ] }
83             { DATE [ dup [ timestamp>ymd ] when default-param-value ] }
84             { TIME [ dup [ timestamp>hms ] when default-param-value ] }
85             { DATETIME [ dup [ timestamp>ymdhms ] when default-param-value ] }
86             { TIMESTAMP [ dup [ timestamp>ymdhms ] when default-param-value ] }
87             [ drop default-param-value ]
88         } case 2array
89     ] 2map flip dup empty? [
90         drop f f
91     ] [
92         first2 [ >c-void*-array ] [ >c-uint-array ] bi*
93     ] if ;
94
95 : param-formats ( statement -- seq )
96     in-params>> [ type>> type>param-format ] map >c-uint-array ;
97
98 : do-postgresql-bound-statement ( statement -- res )
99     [
100         >r db get handle>> r>
101         {
102             [ sql>> ]
103             [ bind-params>> length ]
104             [ param-types ]
105             [ param-values ]
106             [ param-formats ]
107         } cleave
108         0 PQexecParams dup postgresql-result-ok? [
109             [ postgresql-result-error-message ] [ PQclear ] bi throw
110         ] unless
111     ] with-destructors ;
112
113 : pq-get-is-null ( handle row column -- ? )
114     PQgetisnull 1 = ;
115
116 : pq-get-string ( handle row column -- obj )
117     3dup PQgetvalue utf8 alien>string
118     dup empty? [ >r pq-get-is-null f r> ? ] [ 3nip ] if ;
119
120 : pq-get-number ( handle row column -- obj )
121     pq-get-string dup [ string>number ] when ;
122
123 TUPLE: postgresql-malloc-destructor alien ;
124 C: <postgresql-malloc-destructor> postgresql-malloc-destructor
125
126 M: postgresql-malloc-destructor dispose ( obj -- )
127     alien>> PQfreemem ;
128
129 : &postgresql-free ( alien -- alien )
130     dup <postgresql-malloc-destructor> &dispose drop ; inline
131
132 : pq-get-blob ( handle row column -- obj/f )
133     [ PQgetvalue ] 3keep 3dup PQgetlength
134     dup 0 > [
135         3nip
136         [
137             memory>byte-array >string
138             0 <uint>
139             [
140                 PQunescapeBytea dup zero? [
141                     postgresql-result-error-message throw
142                 ] [
143                     &postgresql-free
144                 ] if
145             ] keep
146             *uint memory>byte-array
147         ] with-destructors 
148     ] [
149         drop pq-get-is-null nip [ f ] [ B{ } clone ] if
150     ] if ;
151
152 : postgresql-column-typed ( handle row column type -- obj )
153     dup array? [ first ] when
154     {
155         { +db-assigned-id+ [ pq-get-number ] }
156         { +random-id+ [ pq-get-number ] }
157         { INTEGER [ pq-get-number ] }
158         { BIG-INTEGER [ pq-get-number ] }
159         { DOUBLE [ pq-get-number ] }
160         { TEXT [ pq-get-string ] }
161         { VARCHAR [ pq-get-string ] }
162         { DATE [ pq-get-string dup [ ymd>timestamp ] when ] }
163         { TIME [ pq-get-string dup [ hms>timestamp ] when ] }
164         { TIMESTAMP [ pq-get-string dup [ ymdhms>timestamp ] when ] }
165         { DATETIME [ pq-get-string dup [ ymdhms>timestamp ] when ] }
166         { BLOB [ pq-get-blob ] }
167         { FACTOR-BLOB [
168             pq-get-blob
169             dup [ bytes>object ] when ] }
170         [ no-sql-type ]
171     } case ;