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 ;
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 [ drop default-param-value ]
89 ] 2map flip dup empty? [
92 first2 [ >c-void*-array ] [ >c-uint-array ] bi*
95 : param-formats ( statement -- seq )
96 in-params>> [ type>> type>param-format ] map >c-uint-array ;
98 : do-postgresql-bound-statement ( statement -- res )
100 >r db get handle>> r>
103 [ bind-params>> length ]
108 0 PQexecParams dup postgresql-result-ok? [
109 [ postgresql-result-error-message ] [ PQclear ] bi throw
113 : pq-get-is-null ( handle row column -- ? )
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 ;
120 : pq-get-number ( handle row column -- obj )
121 pq-get-string dup [ string>number ] when ;
123 TUPLE: postgresql-malloc-destructor alien ;
124 C: <postgresql-malloc-destructor> postgresql-malloc-destructor
126 M: postgresql-malloc-destructor dispose ( obj -- )
129 : &postgresql-free ( alien -- alien )
130 dup <postgresql-malloc-destructor> &dispose drop ; inline
132 : pq-get-blob ( handle row column -- obj/f )
133 [ PQgetvalue ] 3keep 3dup PQgetlength
137 memory>byte-array >string
140 PQunescapeBytea dup zero? [
141 postgresql-result-error-message throw
146 *uint memory>byte-array
149 drop pq-get-is-null nip [ f ] [ B{ } clone ] if
152 : postgresql-column-typed ( handle row column type -- obj )
153 dup array? [ first ] when
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 ] }
169 dup [ bytes>object ] when ] }