Mercurial > forge
annotate main/database/src/pq_lo.cc @ 12720:52ca082757c2 octave-forge tip
Update copyright notices.
author | i7tiol |
---|---|
date | Sat, 27 Feb 2016 11:21:29 +0000 |
parents | 1af86934c14e |
children |
rev | line source |
---|---|
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
1 /* |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
2 |
12720 | 3 Copyright (C) 2013-2016 Olaf Till <i7tiol@t-online.de> |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
4 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
5 This program is free software; you can redistribute it and/or modify |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
8 (at your option) any later version. |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
9 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
13 GNU General Public License for more details. |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
14 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
16 along with this program; If not, see <http://www.gnu.org/licenses/>. |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
17 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
18 */ |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
19 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
20 #include <octave/oct.h> |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
21 #include <octave/ov-struct.h> |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
22 #include <octave/Cell.h> |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
23 |
11422 | 24 #include <stdio.h> |
25 | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
26 #include "command.h" |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
27 #include "error-helpers.h" |
11553
35e9e4b6ab34
Fix configuration of postgresql include directories.
i7tiol
parents:
11422
diff
changeset
|
28 #include <libpq/libpq-fs.h> |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
29 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
30 // PKG_ADD: autoload ("pq_lo_import", "pq_interface.oct"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
31 // PKG_ADD: autoload ("pq_lo_export", "pq_interface.oct"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
32 // PKG_ADD: autoload ("pq_lo_unlink", "pq_interface.oct"); |
12613 | 33 // PKG_DEL: autoload ("pq_lo_import", "pq_interface.oct", "remove"); |
34 // PKG_DEL: autoload ("pq_lo_export", "pq_interface.oct", "remove"); | |
35 // PKG_DEL: autoload ("pq_lo_unlink", "pq_interface.oct", "remove"); | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
36 |
11422 | 37 #define OCT_PQ_BUFSIZE 1024 |
38 | |
39 // For cleanup handling this is a class. | |
40 class pipe_to_lo | |
41 { | |
42 public: | |
43 | |
12612 | 44 pipe_to_lo (octave_pq_connection_rep &, const char *, bool, std::string &); |
11422 | 45 |
46 ~pipe_to_lo (void); | |
47 | |
48 bool valid (void) { return oid_valid; } | |
49 | |
50 Oid get_oid (void) { return oid; } | |
51 | |
52 std::string &msg; | |
53 | |
54 private: | |
55 | |
12612 | 56 octave_pq_connection_rep &oct_pq_conn; |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
57 |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
58 PGconn *conn; |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
59 |
11422 | 60 Oid oid; |
61 | |
62 FILE *fp; | |
63 | |
64 bool oid_valid; | |
65 | |
66 int lod; | |
67 | |
68 bool commit; | |
69 }; | |
70 | |
12612 | 71 pipe_to_lo::pipe_to_lo (octave_pq_connection_rep &a_oct_pq_conn, |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
72 const char *cmd, bool acommit, std::string &amsg) |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
73 : msg (amsg), oct_pq_conn (a_oct_pq_conn), |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
74 conn (a_oct_pq_conn.octave_pq_get_conn ()), oid (0), fp (NULL), |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
75 oid_valid (false), lod (-1), commit (acommit) |
11422 | 76 { |
77 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
78 oid = lo_creat (conn, INV_WRITE); | |
79 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
80 if (! oid || oid == InvalidOid) | |
81 { | |
82 msg = PQerrorMessage (conn); | |
83 | |
84 oid = 0; | |
85 | |
86 return; | |
87 } | |
88 | |
89 if (! (fp = popen (cmd, "r"))) | |
90 { | |
91 msg = "could not create pipe"; | |
92 | |
93 return; | |
94 } | |
95 | |
96 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
97 lod = lo_open (conn, oid, INV_WRITE); | |
98 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
99 if (lod == -1) | |
100 { | |
101 msg = PQerrorMessage (conn); | |
102 | |
103 return; | |
104 } | |
105 | |
106 char buff [OCT_PQ_BUFSIZE]; | |
107 | |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
108 int nb = 0, pnb = 0; // silence inadequate warnings by initializing |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
109 // them |
11422 | 110 |
111 while (true) | |
112 { | |
113 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
114 nb = fread (buff, 1, OCT_PQ_BUFSIZE, fp); | |
115 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
116 | |
117 if (! nb) break; | |
118 | |
119 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
120 pnb = lo_write (conn, lod, buff, nb); | |
121 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
122 if (pnb != nb) | |
123 { | |
124 msg = PQerrorMessage (conn); | |
125 | |
126 break; | |
127 } | |
128 } | |
129 if (nb) return; | |
130 | |
131 if (pclose (fp) == -1) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
132 c_verror ("error closing pipe"); |
11422 | 133 |
134 fp = NULL; | |
135 | |
136 if (lo_close (conn, lod)) | |
137 msg = PQerrorMessage (conn); | |
138 else | |
139 oid_valid = true; | |
140 | |
141 lod = -1; | |
142 } | |
143 | |
144 pipe_to_lo::~pipe_to_lo (void) | |
145 { | |
146 if (lod != -1) | |
147 { | |
148 if (lo_close (conn, lod)) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
149 c_verror ("%s", PQerrorMessage (conn)); |
11422 | 150 |
151 lod = -1; | |
152 } | |
153 | |
154 if (oid && ! oid_valid) | |
155 { | |
156 if (lo_unlink (conn, oid) == -1) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
157 c_verror ("error unlinking new large object with oid %i", oid); |
11422 | 158 } |
159 else | |
160 oid = 0; | |
161 | |
162 if (fp) | |
163 { | |
164 if (pclose (fp) == -1) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
165 c_verror ("error closing pipe"); |
11422 | 166 |
167 fp = NULL; | |
168 } | |
169 | |
170 if (commit) | |
171 { | |
172 std::string cmd ("commit;"); | |
173 Cell params; | |
174 Cell ptypes (1, 0); | |
175 Cell rtypes; | |
176 std::string caller ("pq_lo_import"); | |
177 command c (oct_pq_conn, cmd, params, ptypes, rtypes, caller); | |
178 | |
179 if (c.good ()) | |
180 c.process_single_result (); | |
181 | |
182 if (! c.good ()) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
183 c_verror ("%s: could not commit", caller.c_str ()); |
11422 | 184 } |
185 } | |
186 | |
187 // For cleanup handling this is a class. | |
188 class lo_to_pipe | |
189 { | |
190 public: | |
191 | |
12612 | 192 lo_to_pipe (octave_pq_connection_rep &, Oid, const char *, bool, std::string &); |
11422 | 193 |
194 ~lo_to_pipe (void); | |
195 | |
196 bool valid (void) { return success; } | |
197 | |
198 std::string &msg; | |
199 | |
200 private: | |
201 | |
12612 | 202 octave_pq_connection_rep &oct_pq_conn; |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
203 |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
204 PGconn *conn; |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
205 |
11422 | 206 Oid oid; |
207 | |
208 FILE *fp; | |
209 | |
210 bool success; | |
211 | |
212 int lod; | |
213 | |
214 bool commit; | |
215 }; | |
216 | |
12612 | 217 lo_to_pipe::lo_to_pipe (octave_pq_connection_rep &a_oct_pq_conn, Oid aoid, |
11422 | 218 const char *cmd, bool acommit, std::string &amsg) : |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
219 msg (amsg), oct_pq_conn (a_oct_pq_conn), |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
220 conn (a_oct_pq_conn.octave_pq_get_conn ()), oid (aoid), fp (NULL), |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
221 success (false), lod (-1), commit (acommit) |
11422 | 222 { |
223 if (! (fp = popen (cmd, "w"))) | |
224 { | |
225 msg = "could not create pipe"; | |
226 | |
227 return; | |
228 } | |
229 | |
230 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
231 lod = lo_open (conn, oid, INV_READ); | |
232 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
233 if (lod == -1) | |
234 { | |
235 msg = PQerrorMessage (conn); | |
236 | |
237 return; | |
238 } | |
239 | |
240 char buff [OCT_PQ_BUFSIZE]; | |
241 | |
11645
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
242 int nb = 0, pnb = 0; // silence inadequate warnings by initializing |
bd5a8f1bdfb8
Clean out harmless warnings and one unnecessarily returned column.
i7tiol
parents:
11553
diff
changeset
|
243 // them |
11422 | 244 |
245 while (true) | |
246 { | |
247 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
248 pnb = lo_read (conn, lod, buff, OCT_PQ_BUFSIZE); | |
249 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
250 | |
251 if (pnb == -1) | |
252 { | |
253 msg = PQerrorMessage (conn); | |
254 | |
255 break; | |
256 } | |
257 | |
258 if (! pnb) break; | |
259 | |
260 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
261 nb = fwrite (buff, 1, pnb, fp); | |
262 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; | |
263 if (nb != pnb) | |
264 { | |
265 msg = "error writing to pipe"; | |
266 | |
267 break; | |
268 } | |
269 } | |
270 if (pnb) return; | |
271 | |
272 if (pclose (fp) == -1) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
273 c_verror ("error closing pipe"); |
11422 | 274 |
275 fp = NULL; | |
276 | |
277 if (lo_close (conn, lod)) | |
278 msg = PQerrorMessage (conn); | |
279 else | |
280 success = true; | |
281 | |
282 lod = -1; | |
283 } | |
284 | |
285 lo_to_pipe::~lo_to_pipe (void) | |
286 { | |
287 if (lod != -1) | |
288 { | |
289 if (lo_close (conn, lod)) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
290 c_verror ("%s", PQerrorMessage (conn)); |
11422 | 291 |
292 lod = -1; | |
293 } | |
294 | |
295 if (fp) | |
296 { | |
297 if (pclose (fp) == -1) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
298 c_verror ("error closing pipe"); |
11422 | 299 |
300 fp = NULL; | |
301 } | |
302 | |
303 if (commit) | |
304 { | |
305 std::string cmd ("commit;"); | |
306 Cell params; | |
307 Cell ptypes (1, 0); | |
308 Cell rtypes; | |
309 std::string caller ("pq_lo_export"); | |
310 command c (oct_pq_conn, cmd, params, ptypes, rtypes, caller); | |
311 | |
312 if (c.good ()) | |
313 c.process_single_result (); | |
314 | |
315 if (! c.good ()) | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
316 c_verror ("%s: could not commit", caller.c_str ()); |
11422 | 317 } |
318 } | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
319 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
320 DEFUN_DLD (pq_lo_import, args, , |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
321 "-*- texinfo -*-\n\ |
11660 | 322 @deftypefn {Loadable Function} {@var{oid} =} pq_lo_import (@var{connection}, @var{path})\n\ |
11422 | 323 Imports the file in @var{path} on the client side as a large object into the database associated with @var{connection} and returns the Oid of the new large object. If @var{path} ends with a @code{|}, it is take as a shell command whose output is piped into a large object.\n\ |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
324 @end deftypefn") |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
325 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
326 std::string fname ("pq_lo_import"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
327 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
328 octave_value retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
329 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
330 if (args.length () != 2 || |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
331 args(0).type_id () != octave_pq_connection::static_type_id ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
332 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
333 print_usage (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
334 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
335 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
336 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
337 |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
338 std::string path; |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
339 CHECK_ERROR (path = args(1).string_value (), retval, |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
340 "%s: second argument can not be converted to a string", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
341 fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
342 |
11422 | 343 bool from_pipe = false; |
344 unsigned int l = path.size (); | |
345 if (l && path[l - 1] == '|') | |
346 { | |
347 unsigned int pos; | |
348 // There seemed to be a bug in my C++ library so that | |
349 // path.find_last_not_of (" \t\n\r\f", l - 1)) | |
350 // returned l - 1 ! This is the workaround. | |
351 path.erase (l - 1, 1); | |
352 if ((pos = path.find_last_not_of (" \t\n\r\f")) | |
353 == std::string::npos) | |
354 { | |
355 error ("%s: no command found to pipe from", fname.c_str ()); | |
356 | |
357 return retval; | |
358 } | |
359 path.erase (pos + 1, std::string::npos); | |
360 | |
361 from_pipe = true; | |
362 } | |
363 | |
12612 | 364 const octave_base_value& rep = (args(0).get_rep ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
365 |
12612 | 366 const octave_pq_connection &oct_pq_conn = |
367 dynamic_cast<const octave_pq_connection&> (rep); | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
368 |
12612 | 369 PGconn *conn = oct_pq_conn.get_rep ()->octave_pq_get_conn (); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
370 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
371 if (! conn) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
372 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
373 error ("%s: connection not open", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
374 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
375 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
376 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
377 bool make_tblock = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
378 switch (PQtransactionStatus (conn)) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
379 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
380 case PQTRANS_IDLE: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
381 make_tblock = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
382 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
383 case PQTRANS_INTRANS: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
384 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
385 case PQTRANS_INERROR: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
386 error ("%s: can't manipulate large objects within a failed transaction block", |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
387 fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
388 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
389 case PQTRANS_UNKNOWN: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
390 error ("%s: connection is bad", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
391 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
392 default: // includes PQTRANS_ACTIVE |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
393 error ("%s: unexpected connection state", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
394 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
395 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
396 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
397 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
398 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
399 std::string cmd ("begin;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
400 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
401 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
402 Cell rtypes; |
12612 | 403 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
404 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
405 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
406 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
407 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
408 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
409 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
410 error ("%s: could not begin transaction", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
411 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
412 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
413 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
414 |
11422 | 415 Oid oid = 0; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
416 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
417 bool import_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
418 std::string msg; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
419 |
11422 | 420 if (from_pipe) |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
421 { |
12612 | 422 pipe_to_lo tp (*(oct_pq_conn.get_rep ()), path.c_str (), make_tblock, msg); |
11422 | 423 |
424 make_tblock = false; // commit handled by destructor of pipe_to_lo | |
425 | |
426 if (tp.valid ()) | |
427 oid = tp.get_oid (); | |
428 else | |
429 import_error = true; | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
430 } |
11422 | 431 else |
432 if (! (oid = lo_import (conn, path.c_str ()))) | |
433 { | |
434 import_error = true; | |
435 msg = PQerrorMessage (conn); | |
436 } | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
437 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
438 // if we started the transaction, commit it even in case of import failure |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
439 bool commit_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
440 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
441 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
442 std::string cmd ("commit;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
443 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
444 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
445 Cell rtypes; |
12612 | 446 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
447 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
448 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
449 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
450 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
451 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
452 commit_error = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
453 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
454 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
455 if (import_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
456 c_verror ("%s: large object import failed: %s", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
457 fname.c_str (), msg.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
458 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
459 if (commit_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
460 c_verror ("%s: could not commit transaction", fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
461 |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
462 if (import_error || commit_error) |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
463 { |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
464 error ("%s failed", fname.c_str ()); |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
465 return retval; |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
466 } |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
467 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
468 retval = octave_value (octave_uint32 (oid)); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
469 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
470 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
471 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
472 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
473 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
474 DEFUN_DLD (pq_lo_export, args, , |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
475 "-*- texinfo -*-\n\ |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
476 @deftypefn {Loadable Function} pq_lo_export (@var{connection}, @var{oid}, @var{path})\n\ |
11422 | 477 Exports the large object of Oid @var{oid} in the database associated with @var{connection} to the file @var{path} on the client side. If @var{path} starts with a @code{|}, it is taken as a shell commant to pipe to.\n\ |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
478 @end deftypefn") |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
479 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
480 std::string fname ("pq_lo_export"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
481 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
482 octave_value retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
483 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
484 if (args.length () != 3 || |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
485 args(0).type_id () != octave_pq_connection::static_type_id ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
486 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
487 print_usage (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
488 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
489 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
490 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
491 |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
492 std::string path; |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
493 CHECK_ERROR (path = args(2).string_value (), retval, |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
494 "%s: third argument can not be converted to a string", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
495 fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
496 |
11422 | 497 bool to_pipe = false; |
498 if (! path.empty () && path[0] == '|') | |
499 { | |
500 unsigned int pos; | |
501 if ((pos = path.find_first_not_of (" \t\n\r\f", 1)) | |
502 == std::string::npos) | |
503 { | |
504 error ("%s: no command found to pipe to", fname.c_str ()); | |
505 | |
506 return retval; | |
507 } | |
508 path.erase (0, pos); | |
509 | |
510 to_pipe = true; | |
511 } | |
512 | |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
513 Oid oid; |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
514 CHECK_ERROR (oid = args(1).uint_value (), retval, |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
515 "%s: second argument can not be converted to an oid", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
516 fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
517 |
12612 | 518 const octave_base_value& rep = (args(0).get_rep ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
519 |
12612 | 520 const octave_pq_connection &oct_pq_conn = |
521 dynamic_cast<const octave_pq_connection&> (rep); | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
522 |
12612 | 523 PGconn *conn = oct_pq_conn.get_rep ()->octave_pq_get_conn (); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
524 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
525 if (! conn) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
526 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
527 error ("%s: connection not open", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
528 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
529 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
530 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
531 bool make_tblock = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
532 switch (PQtransactionStatus (conn)) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
533 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
534 case PQTRANS_IDLE: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
535 make_tblock = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
536 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
537 case PQTRANS_INTRANS: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
538 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
539 case PQTRANS_INERROR: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
540 error ("%s: can't manipulate large objects within a failed transaction block", |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
541 fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
542 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
543 case PQTRANS_UNKNOWN: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
544 error ("%s: connection is bad", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
545 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
546 default: // includes PQTRANS_ACTIVE |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
547 error ("%s: unexpected connection state", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
548 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
549 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
550 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
551 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
552 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
553 std::string cmd ("begin;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
554 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
555 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
556 Cell rtypes; |
12612 | 557 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
558 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
559 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
560 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
561 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
562 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
563 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
564 error ("%s: could not begin transaction", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
565 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
566 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
567 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
568 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
569 bool export_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
570 std::string msg; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
571 |
11422 | 572 if (to_pipe) |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
573 { |
12612 | 574 lo_to_pipe tp (*(oct_pq_conn.get_rep ()), oid, path.c_str (), make_tblock, msg); |
11422 | 575 |
576 make_tblock = false; // commit handled by destructor of lo_to_pipe | |
577 | |
578 if (! tp.valid ()) | |
579 export_error = true; | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
580 } |
11422 | 581 else |
582 if (lo_export (conn, oid, path.c_str ()) == -1) | |
583 { | |
584 export_error = true; | |
585 msg = PQerrorMessage (conn); | |
586 } | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
587 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
588 // if we started the transaction, commit it even in case of export failure |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
589 bool commit_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
590 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
591 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
592 std::string cmd ("commit;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
593 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
594 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
595 Cell rtypes; |
12612 | 596 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
597 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
598 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
599 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
600 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
601 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
602 commit_error = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
603 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
604 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
605 if (export_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
606 c_verror ("%s: large object export failed: %s", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
607 fname.c_str (), msg.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
608 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
609 if (commit_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
610 c_verror ("%s: could not commit transaction", fname.c_str ()); |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
611 |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
612 if (export_error || commit_error) |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
613 error ("%s failed", fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
614 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
615 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
616 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
617 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
618 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
619 DEFUN_DLD (pq_lo_unlink, args, , |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
620 "-*- texinfo -*-\n\ |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
621 @deftypefn {Loadable Function} pq_lo_unlink (@var{connection}, @var{oid})\n\ |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
622 Removes the large object of Oid @var{oid} from the database associated with @var{connection}.\n\ |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
623 @end deftypefn") |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
624 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
625 std::string fname ("pq_lo_unlink"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
626 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
627 octave_value retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
628 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
629 if (args.length () != 2 || |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
630 args(0).type_id () != octave_pq_connection::static_type_id ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
631 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
632 print_usage (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
633 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
634 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
635 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
636 |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
637 Oid oid; |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
638 CHECK_ERROR (oid = args(1).uint_value (), retval, |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
639 "%s: second argument can not be converted to an oid", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
640 fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
641 |
12612 | 642 const octave_base_value& rep = (args(0).get_rep ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
643 |
12612 | 644 const octave_pq_connection &oct_pq_conn = |
645 dynamic_cast<const octave_pq_connection&> (rep); | |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
646 |
12612 | 647 PGconn *conn = oct_pq_conn.get_rep ()->octave_pq_get_conn (); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
648 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
649 if (! conn) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
650 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
651 error ("%s: connection not open", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
652 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
653 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
654 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
655 bool make_tblock = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
656 switch (PQtransactionStatus (conn)) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
657 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
658 case PQTRANS_IDLE: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
659 make_tblock = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
660 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
661 case PQTRANS_INTRANS: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
662 break; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
663 case PQTRANS_INERROR: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
664 error ("%s: can't manipulate large objects within a failed transaction block", |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
665 fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
666 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
667 case PQTRANS_UNKNOWN: |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
668 error ("%s: connection is bad", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
669 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
670 default: // includes PQTRANS_ACTIVE |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
671 error ("%s: unexpected connection state", fname.c_str ()); |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
672 return retval; |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
673 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
674 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
675 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
676 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
677 std::string cmd ("begin;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
678 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
679 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
680 Cell rtypes; |
12612 | 681 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
682 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
683 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
684 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
685 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
686 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
687 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
688 error ("%s: could not begin transaction", fname.c_str ()); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
689 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
690 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
691 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
692 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
693 bool unlink_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
694 std::string msg; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
695 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
696 if (lo_unlink (conn, oid) == -1) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
697 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
698 unlink_error = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
699 msg = PQerrorMessage (conn); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
700 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
701 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
702 // if we started the transaction, commit it even in case of unlink failure |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
703 bool commit_error = false; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
704 if (make_tblock) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
705 { |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
706 std::string cmd ("commit;"); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
707 Cell params; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
708 Cell ptypes (1, 0); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
709 Cell rtypes; |
12612 | 710 command c (*(oct_pq_conn.get_rep ()), cmd, params, ptypes, rtypes, fname); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
711 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
712 if (c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
713 c.process_single_result (); |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
714 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
715 if (! c.good ()) |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
716 commit_error = true; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
717 } |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
718 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
719 if (unlink_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
720 c_verror ("%s: large object unlink failed: %s", |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
721 fname.c_str (), msg.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
722 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
723 if (commit_error) |
12718
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
724 c_verror ("%s: could not commit transaction", fname.c_str ()); |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
725 |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
726 if (unlink_error || commit_error) |
1af86934c14e
Make compatible with Octaves new exception-based error handling.
i7tiol
parents:
12613
diff
changeset
|
727 error ("%s failed", fname.c_str ()); |
11394
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
728 |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
729 return retval; |
9aee227e296c
Populated new database package with initial postgresql interface.
i7tiol
parents:
diff
changeset
|
730 } |