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