annotate main/database/src/command.h @ 11499:1176424db5df octave-forge

Finished copy in from Octave variable. Removed preparations for copy out to variable. New release.
author i7tiol
date Sat, 02 Mar 2013 08:38:16 +0000
parents 154bc776dfa8
children 465192f682f0
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
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
3 Copyright (C) 2012, 2013 Olaf Till <i7tiol@t-online.de>
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 #ifndef __OCT_PQ_COMMAND__
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
21
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
22 #define __OCT_PQ_COMMAND__
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
23
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
24 #include <octave/oct.h>
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
25 #include <octave/ov-struct.h>
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
26 #include <octave/Cell.h>
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
27
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
28 #include "pq_connection.h"
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
29 #include "converters.h"
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
30
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
31 class
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
32 command
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
33 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
34 public:
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
35
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
36 command (octave_pq_connection &connection, std::string &cmd, Cell &rtypes,
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
37 std::string &who);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
38
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
39 command (octave_pq_connection &connection, std::string &cmd, Cell &params,
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
40 Cell &ptypes, Cell &rtypes, std::string &who);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
41
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
42 ~command (void)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
43 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
44 if (res) PQclear (res);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
45
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
46 if (! all_fetched)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
47 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
48 while ((res = PQgetResult (cptr)))
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
49 PQclear (res);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
50 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
51 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
52
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
53 typedef struct
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
54 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
55 dim_vector pd, cur;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
56 octave_idx_type nd;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
57 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
58 count_state;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
59
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
60 typedef int (command::*to_octave_array_fp_t)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
61 (char *, octave_value &, int, oct_pq_conv_t *);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
62
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
63 typedef int (command::*to_octave_composite_fp_t)
11480
d14a23884d9c Cache map lookups of converters for composite type elements.
i7tiol
parents: 11408
diff changeset
64 (char *, octave_value &, int, oct_pq_conv_t *);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
65
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
66 int all_results_fetched (void)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
67 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
68 return all_fetched;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
69 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
70
11398
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
71 octave_value process_single_result (void)
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
72 {
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
73 Cell c;
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
74 // inlining should prevent the additional copy
11499
1176424db5df Finished copy in from Octave variable. Removed preparations for copy out to variable. New release.
i7tiol
parents: 11484
diff changeset
75 return process_single_result ("", "", c, c, false, false);
11398
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
76 }
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
77
913ac5491db1 Copy in/out from/to local file hacked in. Doc change waits till copy in/out from/to variable.
i7tiol
parents: 11394
diff changeset
78 octave_value process_single_result (const std::string &infile,
11484
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
79 const std::string &outfile,
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
80 const Cell &cin_data,
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
81 const Cell &cin_types,
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
82 bool cin_with_oids,
11499
1176424db5df Finished copy in from Octave variable. Removed preparations for copy out to variable. New release.
i7tiol
parents: 11484
diff changeset
83 bool cin_from_variable);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
84
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
85 int good (void) {return valid;}
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
86
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
87 private:
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
88
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
89 typedef enum {simple, array, composite} oct_type_t;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
90
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
91 void check_first_result (void)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
92 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
93 if (! res)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
94 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
95 valid = 0;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
96 error ("%s: could not execute command: %s", caller.c_str (),
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
97 PQerrorMessage (cptr));
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
98
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
99 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
100 else if ((state = PQresultStatus (res)) == PGRES_EMPTY_QUERY)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
101 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
102 valid = 0;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
103 error ("%s: empty command", caller.c_str ());
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
104 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
105 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
106
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
107 octave_value command_ok_handler (void)
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
108 {
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
109 char *c = PQcmdTuples (res);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
110
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
111 return octave_value (atoi (c));
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
112 }
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
113
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
114 octave_value tuples_ok_handler (void);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
115
11499
1176424db5df Finished copy in from Octave variable. Removed preparations for copy out to variable. New release.
i7tiol
parents: 11484
diff changeset
116 octave_value copy_out_handler (const std::string &);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
117
11484
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
118 octave_value copy_in_handler (const std::string &, const Cell &, const Cell &,
154bc776dfa8 Implemented copy in from Octave variable.
i7tiol
parents: 11480
diff changeset
119 bool, bool);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
120
11407
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
121 oct_pq_conv_t *pgtype_from_octtype (const octave_value &);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
122
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
123 oct_pq_conv_t *pgtype_from_spec (std::string &, oct_type_t &);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
124
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
125 oct_pq_conv_t *pgtype_from_spec (Oid, oct_type_t &);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
126
11480
d14a23884d9c Cache map lookups of converters for composite type elements.
i7tiol
parents: 11408
diff changeset
127 oct_pq_conv_t *pgtype_from_spec (Oid, oct_pq_conv_t *&, oct_type_t &);
d14a23884d9c Cache map lookups of converters for composite type elements.
i7tiol
parents: 11408
diff changeset
128
11407
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
129 int from_octave_bin_array (const octave_value &oct_arr, oct_pq_dynvec_t &val,
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
130 oct_pq_conv_t *);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
131
11407
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
132 int from_octave_bin_composite (const octave_value &oct_comp,
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
133 oct_pq_dynvec_t &val, oct_pq_conv_t *);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
134
11407
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
135 int from_octave_str_array (const octave_value &oct_arr, oct_pq_dynvec_t &val,
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
136 octave_value &type);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
137
11407
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
138 int from_octave_str_composite (const octave_value &oct_comp,
f4098c7d5b35 Wrong implementation of copy-in from argument.
i7tiol
parents: 11398
diff changeset
139 oct_pq_dynvec_t &val, octave_value &type);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
140
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
141 int to_octave_bin_array (char *, octave_value &, int, oct_pq_conv_t *);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
142
11480
d14a23884d9c Cache map lookups of converters for composite type elements.
i7tiol
parents: 11408
diff changeset
143 int to_octave_bin_composite (char *, octave_value &, int, oct_pq_conv_t *);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
144
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
145 int to_octave_str_array (char *, octave_value &, int, oct_pq_conv_t *);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
146
11480
d14a23884d9c Cache map lookups of converters for composite type elements.
i7tiol
parents: 11408
diff changeset
147 int to_octave_str_composite (char *, octave_value &, int, oct_pq_conv_t *);
11394
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
148
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
149 octave_idx_type count_row_major_order (dim_vector &, count_state &, bool);
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
150
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
151 PGresult *res;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
152 int all_fetched;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
153 int valid;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
154 ExecStatusType state;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
155 octave_pq_connection &conn;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
156 PGconn *cptr;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
157 Cell &rettypes;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
158 std::string &caller;
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
159
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
160 };
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
161
9aee227e296c Populated new database package with initial postgresql interface.
i7tiol
parents:
diff changeset
162 #endif // __OCT_PQ_COMMAND__