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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
52ca082757c2 Update copyright notices.
i7tiol
parents: 12718
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
24 #include <stdio.h>
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
9cb56e0cd09b Remove autoloaded functions at package unload.
i7tiol
parents: 12612
diff changeset
33 // PKG_DEL: autoload ("pq_lo_import", "pq_interface.oct", "remove");
9cb56e0cd09b Remove autoloaded functions at package unload.
i7tiol
parents: 12612
diff changeset
34 // PKG_DEL: autoload ("pq_lo_export", "pq_interface.oct", "remove");
9cb56e0cd09b Remove autoloaded functions at package unload.
i7tiol
parents: 12612
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
37 #define OCT_PQ_BUFSIZE 1024
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
38
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
39 // For cleanup handling this is a class.
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
40 class pipe_to_lo
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
41 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
42 public:
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
43
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
44 pipe_to_lo (octave_pq_connection_rep &, const char *, bool, std::string &);
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
45
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
46 ~pipe_to_lo (void);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
47
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
48 bool valid (void) { return oid_valid; }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
49
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
50 Oid get_oid (void) { return oid; }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
51
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
52 std::string &msg;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
53
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
54 private:
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
55
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
60 Oid oid;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
61
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
62 FILE *fp;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
63
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
64 bool oid_valid;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
65
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
66 int lod;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
67
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
68 bool commit;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
69 };
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
70
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
76 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
77 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
78 oid = lo_creat (conn, INV_WRITE);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
79 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
80 if (! oid || oid == InvalidOid)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
81 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
82 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
83
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
84 oid = 0;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
85
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
86 return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
87 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
88
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
89 if (! (fp = popen (cmd, "r")))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
90 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
91 msg = "could not create pipe";
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
92
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
93 return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
94 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
95
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
96 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
97 lod = lo_open (conn, oid, INV_WRITE);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
98 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
99 if (lod == -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
100 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
101 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
102
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
103 return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
104 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
105
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
106 char buff [OCT_PQ_BUFSIZE];
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
110
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
111 while (true)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
112 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
113 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
114 nb = fread (buff, 1, OCT_PQ_BUFSIZE, fp);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
115 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
116
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
117 if (! nb) break;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
118
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
119 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
120 pnb = lo_write (conn, lod, buff, nb);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
121 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
122 if (pnb != nb)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
123 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
124 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
125
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
126 break;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
127 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
128 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
129 if (nb) return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
130
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
133
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
134 fp = NULL;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
135
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
136 if (lo_close (conn, lod))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
137 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
138 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
139 oid_valid = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
140
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
141 lod = -1;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
142 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
143
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
144 pipe_to_lo::~pipe_to_lo (void)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
145 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
146 if (lod != -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
147 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
150
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
151 lod = -1;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
152 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
153
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
154 if (oid && ! oid_valid)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
155 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
158 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
159 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
160 oid = 0;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
161
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
162 if (fp)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
163 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
166
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
167 fp = NULL;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
168 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
169
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
170 if (commit)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
171 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
172 std::string cmd ("commit;");
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
173 Cell params;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
174 Cell ptypes (1, 0);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
175 Cell rtypes;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
176 std::string caller ("pq_lo_import");
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
177 command c (oct_pq_conn, cmd, params, ptypes, rtypes, caller);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
178
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
179 if (c.good ())
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
180 c.process_single_result ();
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
181
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
184 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
185 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
186
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
187 // For cleanup handling this is a class.
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
188 class lo_to_pipe
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
189 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
190 public:
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
191
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
192 lo_to_pipe (octave_pq_connection_rep &, Oid, const char *, bool, std::string &);
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
193
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
194 ~lo_to_pipe (void);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
195
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
196 bool valid (void) { return success; }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
197
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
198 std::string &msg;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
199
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
200 private:
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
201
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
206 Oid oid;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
207
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
208 FILE *fp;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
209
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
210 bool success;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
211
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
212 int lod;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
213
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
214 bool commit;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
215 };
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
216
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
217 lo_to_pipe::lo_to_pipe (octave_pq_connection_rep &a_oct_pq_conn, Oid aoid,
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
222 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
223 if (! (fp = popen (cmd, "w")))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
224 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
225 msg = "could not create pipe";
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
226
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
227 return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
228 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
229
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
230 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
231 lod = lo_open (conn, oid, INV_READ);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
232 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
233 if (lod == -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
234 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
235 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
236
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
237 return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
238 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
239
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
240 char buff [OCT_PQ_BUFSIZE];
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
244
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
245 while (true)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
246 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
247 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
248 pnb = lo_read (conn, lod, buff, OCT_PQ_BUFSIZE);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
249 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
250
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
251 if (pnb == -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
252 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
253 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
254
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
255 break;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
256 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
257
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
258 if (! pnb) break;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
259
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
260 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
261 nb = fwrite (buff, 1, pnb, fp);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
262 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
263 if (nb != pnb)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
264 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
265 msg = "error writing to pipe";
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
266
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
267 break;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
268 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
269 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
270 if (pnb) return;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
271
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
274
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
275 fp = NULL;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
276
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
277 if (lo_close (conn, lod))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
278 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
279 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
280 success = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
281
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
282 lod = -1;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
283 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
284
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
285 lo_to_pipe::~lo_to_pipe (void)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
286 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
287 if (lod != -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
288 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
291
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
292 lod = -1;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
293 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
294
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
295 if (fp)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
296 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
299
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
300 fp = NULL;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
301 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
302
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
303 if (commit)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
304 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
305 std::string cmd ("commit;");
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
306 Cell params;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
307 Cell ptypes (1, 0);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
308 Cell rtypes;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
309 std::string caller ("pq_lo_export");
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
310 command c (oct_pq_conn, cmd, params, ptypes, rtypes, caller);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
311
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
312 if (c.good ())
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
313 c.process_single_result ();
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
314
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
317 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
cbff4e8a8af8 Correct typo in pq_lo_import helptext.
i7tiol
parents: 11645
diff changeset
322 @deftypefn {Loadable Function} {@var{oid} =} pq_lo_import (@var{connection}, @var{path})\n\
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
343 bool from_pipe = false;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
344 unsigned int l = path.size ();
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
345 if (l && path[l - 1] == '|')
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
346 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
347 unsigned int pos;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
348 // There seemed to be a bug in my C++ library so that
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
349 // path.find_last_not_of (" \t\n\r\f", l - 1))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
350 // returned l - 1 ! This is the workaround.
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
351 path.erase (l - 1, 1);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
352 if ((pos = path.find_last_not_of (" \t\n\r\f"))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
353 == std::string::npos)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
354 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
355 error ("%s: no command found to pipe from", fname.c_str ());
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
356
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
357 return retval;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
358 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
359 path.erase (pos + 1, std::string::npos);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
360
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
361 from_pipe = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
362 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
363
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
366 const octave_pq_connection &oct_pq_conn =
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
367 dynamic_cast<const octave_pq_connection&> (rep);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
368
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
420 if (from_pipe)
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
421 {
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
422 pipe_to_lo tp (*(oct_pq_conn.get_rep ()), path.c_str (), make_tblock, msg);
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
423
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
424 make_tblock = false; // commit handled by destructor of pipe_to_lo
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
425
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
426 if (tp.valid ())
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
427 oid = tp.get_oid ();
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
428 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
429 import_error = true;
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
430 }
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
431 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
432 if (! (oid = lo_import (conn, path.c_str ())))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
433 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
434 import_error = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
435 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
497 bool to_pipe = false;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
498 if (! path.empty () && path[0] == '|')
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
499 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
500 unsigned int pos;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
501 if ((pos = path.find_first_not_of (" \t\n\r\f", 1))
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
502 == std::string::npos)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
503 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
504 error ("%s: no command found to pipe to", fname.c_str ());
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
505
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
506 return retval;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
507 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
508 path.erase (0, pos);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
509
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
510 to_pipe = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
511 }
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
520 const octave_pq_connection &oct_pq_conn =
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
521 dynamic_cast<const octave_pq_connection&> (rep);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
522
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
572 if (to_pipe)
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
573 {
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
574 lo_to_pipe tp (*(oct_pq_conn.get_rep ()), oid, path.c_str (), make_tblock, msg);
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
575
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
576 make_tblock = false; // commit handled by destructor of lo_to_pipe
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
577
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
578 if (! tp.valid ())
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
579 export_error = true;
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
580 }
11422
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
581 else
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
582 if (lo_export (conn, oid, path.c_str ()) == -1)
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
583 {
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
584 export_error = true;
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
585 msg = PQerrorMessage (conn);
b1f26b72b61a Large object import/export also from/to pipes.
i7tiol
parents: 11394
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
644 const octave_pq_connection &oct_pq_conn =
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
645 dynamic_cast<const octave_pq_connection&> (rep);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
646
12612
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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
2257648e8ce1 avoid using const_cast, new release 2.3.2
i7tiol
parents: 11660
diff changeset
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 }