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 summary present urls ;
11 : postgresql-result-error-message ( res -- str/f )
15 PQresultErrorMessage [ blank? ] trim
18 : postgres-result-error ( res -- )
19 postgresql-result-error-message [ throw ] when* ;
21 : (postgresql-error-message) ( handle -- str )
23 "\n" split [ [ blank? ] trim ] map "\n" join ;
25 : postgresql-error-message ( -- str )
26 db get handle>> (postgresql-error-message) ;
28 : postgresql-error ( res -- res )
29 dup [ postgresql-error-message throw ] unless ;
31 ERROR: postgresql-result-null ;
33 M: postgresql-result-null summary ( obj -- str )
34 drop "PQexec returned f." ;
36 : postgresql-result-ok? ( res -- ? )
37 [ postgresql-result-null ] unless*
39 PGRES_COMMAND_OK PGRES_TUPLES_OK 2array member? ;
41 : connect-postgres ( host port pgopts pgtty db user pass -- conn )
43 dup PQstatus zero? [ (postgresql-error-message) throw ] unless ;
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
50 : type>oid ( symbol -- n )
51 dup array? [ first ] when
53 { BLOB [ BYTEA-OID ] }
54 { FACTOR-BLOB [ BYTEA-OID ] }
58 : type>param-format ( symbol -- n )
59 dup array? [ first ] when
66 : param-types ( statement -- seq )
67 in-params>> [ type>> type>oid ] map >c-uint-array ;
69 : malloc-byte-array/length ( byte-array -- alien length )
70 [ malloc-byte-array &free ] [ length ] bi ;
72 : default-param-value ( obj -- alien n )
73 number>string* dup [ utf8 malloc-string &free ] when 0 ;
75 : param-values ( statement -- seq seq2 )
76 [ bind-params>> ] [ in-params>> ] bi
78 >r value>> r> type>> {
80 dup [ object>bytes malloc-byte-array/length ] [ 0 ] if
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 { URL [ dup [ present ] when default-param-value ] }
88 [ drop default-param-value ]
93 first2 [ >c-void*-array ] [ >c-uint-array ] bi*
96 : param-formats ( statement -- seq )
97 in-params>> [ type>> type>param-format ] map >c-uint-array ;
99 : do-postgresql-bound-statement ( statement -- res )
101 >r db get handle>> r>
104 [ bind-params>> length ]
109 0 PQexecParams dup postgresql-result-ok? [
110 [ postgresql-result-error-message ] [ PQclear ] bi throw
114 : pq-get-is-null ( handle row column -- ? )
117 : pq-get-string ( handle row column -- obj )
118 3dup PQgetvalue utf8 alien>string
119 dup empty? [ >r pq-get-is-null f r> ? ] [ 3nip ] if ;
121 : pq-get-number ( handle row column -- obj )
122 pq-get-string dup [ string>number ] when ;
124 TUPLE: postgresql-malloc-destructor alien ;
125 C: <postgresql-malloc-destructor> postgresql-malloc-destructor
127 M: postgresql-malloc-destructor dispose ( obj -- )
130 : &postgresql-free ( alien -- alien )
131 dup <postgresql-malloc-destructor> &dispose drop ; inline
133 : pq-get-blob ( handle row column -- obj/f )
134 [ PQgetvalue ] 3keep 3dup PQgetlength
138 memory>byte-array >string
141 PQunescapeBytea dup zero? [
142 postgresql-result-error-message throw
147 *uint memory>byte-array
150 drop pq-get-is-null nip [ f ] [ B{ } clone ] if
153 : postgresql-column-typed ( handle row column type -- obj )
154 dup array? [ first ] when
156 { +db-assigned-id+ [ pq-get-number ] }
157 { +random-id+ [ pq-get-number ] }
158 { INTEGER [ pq-get-number ] }
159 { BIG-INTEGER [ pq-get-number ] }
160 { DOUBLE [ pq-get-number ] }
161 { TEXT [ pq-get-string ] }
162 { VARCHAR [ pq-get-string ] }
163 { DATE [ pq-get-string dup [ ymd>timestamp ] when ] }
164 { TIME [ pq-get-string dup [ hms>timestamp ] when ] }
165 { TIMESTAMP [ pq-get-string dup [ ymdhms>timestamp ] when ] }
166 { DATETIME [ pq-get-string dup [ ymdhms>timestamp ] when ] }
167 { BLOB [ pq-get-blob ] }
168 { URL [ pq-get-string dup [ >url ] when ] }
171 dup [ bytes>object ] when ] }