diff main/database/src/command.h @ 11394:9aee227e296c octave-forge

Populated new database package with initial postgresql interface.
author i7tiol
date Wed, 16 Jan 2013 06:17:06 +0000
parents
children 913ac5491db1
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main/database/src/command.h	Wed Jan 16 06:17:06 2013 +0000
@@ -0,0 +1,147 @@
+/*
+
+Copyright (C) 2012, 2013 Olaf Till <i7tiol@t-online.de>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef __OCT_PQ_COMMAND__
+
+#define __OCT_PQ_COMMAND__
+
+#include <octave/oct.h>
+#include <octave/ov-struct.h>
+#include <octave/Cell.h>
+
+#include "pq_connection.h"
+#include "converters.h"
+
+class
+command
+{
+public:
+
+  command (octave_pq_connection &connection, std::string &cmd, Cell &rtypes,
+           std::string &who);
+
+  command (octave_pq_connection &connection, std::string &cmd, Cell &params,
+           Cell &ptypes, Cell &rtypes, std::string &who);
+
+  ~command (void)
+  {
+    if (res) PQclear (res);
+
+    if (! all_fetched)
+      {
+        while ((res = PQgetResult (cptr)))
+          PQclear (res);
+      }
+  }
+
+  typedef struct
+  {
+    dim_vector pd, cur;
+    octave_idx_type nd;
+  }
+  count_state;
+
+  typedef int (command::*to_octave_array_fp_t)
+    (char *, octave_value &, int, oct_pq_conv_t *);
+
+  typedef int (command::*to_octave_composite_fp_t)
+    (char *, octave_value &, int);
+
+  int all_results_fetched (void)
+  {
+    return all_fetched;
+  }
+
+  octave_value process_single_result (void);
+
+  int good (void) {return valid;}
+
+private:
+
+  typedef enum {simple, array, composite} oct_type_t;
+
+  void check_first_result (void)
+    {
+      if (! res)
+        {
+          valid = 0;
+          error ("%s: could not execute command: %s", caller.c_str (),
+                 PQerrorMessage (cptr));
+
+        }
+      else if ((state = PQresultStatus (res)) == PGRES_EMPTY_QUERY)
+        {
+          valid = 0;
+          error ("%s: empty command", caller.c_str ());
+        }
+    }
+
+  octave_value command_ok_handler (void)
+  {
+    char *c = PQcmdTuples (res);
+
+    return octave_value (atoi (c));
+  }
+
+  octave_value tuples_ok_handler (void);
+
+  octave_value copy_out_handler (void);
+
+  octave_value copy_in_handler (void);
+
+  oct_pq_conv_t *pgtype_from_octtype (octave_value &);
+
+  oct_pq_conv_t *pgtype_from_spec (std::string &, oct_type_t &);
+
+  oct_pq_conv_t *pgtype_from_spec (Oid, oct_type_t &);
+
+  int from_octave_bin_array (octave_value &oct_arr, oct_pq_dynvec_t &val,
+                             oct_pq_conv_t *);
+
+  int from_octave_bin_composite (octave_value &oct_comp, oct_pq_dynvec_t &val,
+                                 oct_pq_conv_t *);
+
+  int from_octave_str_array (octave_value &oct_arr, oct_pq_dynvec_t &val,
+                             octave_value &type);
+
+  int from_octave_str_composite (octave_value &oct_comp, oct_pq_dynvec_t &val,
+                                 octave_value &type);
+
+  int to_octave_bin_array (char *, octave_value &, int, oct_pq_conv_t *);
+
+  int to_octave_bin_composite (char *, octave_value &, int);
+
+  int to_octave_str_array (char *, octave_value &, int, oct_pq_conv_t *);
+
+  int to_octave_str_composite (char *, octave_value &, int);
+
+  octave_idx_type count_row_major_order (dim_vector &, count_state &, bool);
+
+  PGresult *res;
+  int all_fetched;
+  int valid;
+  ExecStatusType state;
+  octave_pq_connection &conn;
+  PGconn *cptr;
+  Cell &rettypes;
+  std::string &caller;
+
+};
+
+#endif // __OCT_PQ_COMMAND__