changeset 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 8170329c5dfc
children 1673804f49a3
files main/database/DESCRIPTION main/database/NEWS main/database/doc/README-postgresql main/database/src/command.cc main/database/src/command.h main/database/src/pq_exec.cc
diffstat 6 files changed, 55 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/main/database/DESCRIPTION	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/DESCRIPTION	Sat Mar 02 08:38:16 2013 +0000
@@ -1,6 +1,6 @@
 Name: database
-Version: 2.0.1
-Date: 2013-02-27
+Version: 2.1.0
+Date: 2013-03-02
 Author: Olaf Till <i7tiol@t-online.de>
 Maintainer: Olaf Till <i7tiol@t-online.de>
 Title: Database.
--- a/main/database/NEWS	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/NEWS	Sat Mar 02 08:38:16 2013 +0000
@@ -1,3 +1,9 @@
+database 2.1.0:
+---------------
+
+ ** pq_exec_params: An Octave cell-array can be used as data for 'copy
+    from stdin'.
+
 database 2.0.1:
 ---------------
 
--- a/main/database/doc/README-postgresql	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/doc/README-postgresql	Sat Mar 02 08:38:16 2013 +0000
@@ -30,6 +30,8 @@
 
 - arbitrarily deeply nested composite- and array-types,
 
+- caches converter lookups for composite type elements,
+
 - binary data transfer,
 
 - reads postgresql system catalogs to get type information.
@@ -40,7 +42,10 @@
 
 Not all base types implemented yet. All commands should work with
 pq_exec_params, copy from stdin and copy to stdout are implemented
-from/to files at client side. The whole framework including conversion
+from/to files at client side, copy from stdin is also possible from an
+Octave cell-array. There are no plans to implement copy to stdout into
+an Octave cell-array (does not safely fit into the framework, and
+select should be equivalent). The whole framework including conversion
 of composite and array types with the resulting possible recursion,
 enum types, and a number of base types are implemented. Large object
 import, export, and unlink works, also import/export from/to pipes.
--- a/main/database/src/command.cc	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/src/command.cc	Sat Mar 02 08:38:16 2013 +0000
@@ -33,6 +33,10 @@
    return NULL;                                                         \
  }
 
+#define COPY_HEADER_SIZE 19
+
+#define COUT_RESIZE_STEP 1000 // resize result only after this number of rows
+
 command::command (octave_pq_connection &connection, std::string &cmd,
                   Cell &rtypes,std::string &who) :
   valid (1), conn (connection), caller (who), res (NULL), all_fetched (0),
@@ -274,8 +278,7 @@
                                              const Cell &cdata,
                                              const Cell &ctypes,
                                              bool coids,
-                                             bool cin_var,
-                                             bool cout_var)
+                                             bool cin_var)
 {
   octave_value retval;
 
@@ -305,7 +308,7 @@
           retval = tuples_ok_handler ();
           break;
         case PGRES_COPY_OUT:
-          retval = copy_out_handler (outfile, cout_var);
+          retval = copy_out_handler (outfile);
           break;
         case PGRES_COPY_IN:
           retval = copy_in_handler (infile, cdata, ctypes, coids, cin_var);
@@ -460,7 +463,7 @@
     }
 }
 
-octave_value command::copy_out_handler (const std::string &outfile, bool var)
+octave_value command::copy_out_handler (const std::string &outfile)
 {
   octave_value retval;
 
@@ -468,6 +471,15 @@
     {
       // store unchecked output in file
 
+      if (outfile.empty ())
+        {
+          error ("no output file given");
+
+          valid = 0;
+
+          return retval;
+        }
+
       std::ofstream ostr (outfile.c_str (), std::ios_base::out);
       if (ostr.fail ())
         error ("could not open output file %s", outfile.c_str ());
@@ -504,7 +516,7 @@
         }
     }
   else
-    error ("no filename given for copy-out");
+    error ("no output file given");
 
   if (error_state)
     valid = 0;
@@ -627,12 +639,12 @@
         }
       else
         {
-          for (octave_idx_type j = 0; j < c; j++)
+          for (octave_idx_type j = 0; j < nf; j++)
             if (! PQfformat (res, j))
               {
-                error ("copy-in from argument must use binary mode in all columns");
+                error ("copy-in from variable must use binary mode in all columns");
 
-                PQputCopyEnd (cptr, "copy-in from argument must use binary mode in all columns");
+                PQputCopyEnd (cptr, "copy-in from variable must use binary mode in all columns");
 
                 break;
               }
@@ -647,7 +659,6 @@
           return retval;
         }
 
-#define COPY_HEADER_SIZE 19
       char header [COPY_HEADER_SIZE];
       memset (header, 0, COPY_HEADER_SIZE);
       strcpy (header, "PGCOPY\n\377\r\n\0");
--- a/main/database/src/command.h	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/src/command.h	Sat Mar 02 08:38:16 2013 +0000
@@ -72,7 +72,7 @@
   {
     Cell c;
     // inlining should prevent the additional copy
-    return process_single_result ("", "", c, c, false, false, false);
+    return process_single_result ("", "", c, c, false, false);
   }
 
   octave_value process_single_result (const std::string &infile,
@@ -80,8 +80,7 @@
                                       const Cell &cin_data,
                                       const Cell &cin_types,
                                       bool cin_with_oids,
-                                      bool cin_from_variable,
-                                      bool cout_to_variable);
+                                      bool cin_from_variable);
 
   int good (void) {return valid;}
 
@@ -114,7 +113,7 @@
 
   octave_value tuples_ok_handler (void);
 
-  octave_value copy_out_handler (const std::string &, bool);
+  octave_value copy_out_handler (const std::string &);
 
   octave_value copy_in_handler (const std::string &, const Cell &, const Cell &,
                                 bool, bool);
--- a/main/database/src/pq_exec.cc	Fri Mar 01 21:52:08 2013 +0000
+++ b/main/database/src/pq_exec.cc	Sat Mar 02 08:38:16 2013 +0000
@@ -112,8 +112,22 @@
 \n\
 Settings currently understood by @code{pq_exec_params}:\n\
 \n\
-@code{param_types}: One-dimensional cell-array with type specifications for parameters in @var{params}. If present, must have the same length as @var{params}. Entries may be empty if no specification is necessary (see below). Type specifications are strings corresponding to the entries returned by @code{SELECT typname FROM pg_type WHERE typarray != 0 OR typtype = 'c';}, optionally having @code{[]} appended (without space) to indicate an array. Type specifications can be schema-qualified, otherwise they refer to the visible type with that name.\n\
-@code{copy_in_path}, @code{copy_out_path}: Path to files at the client side for @code{copy from stdin} and @code{copy to stdout}, respectively.\n\
+@table @code\n\
+@item param_types\n\
+One-dimensional cell-array with type specifications for parameters in @var{params}. If present, must have the same length as @var{params}. Entries may be empty if no specification is necessary (see below). Type specifications are strings corresponding to the entries returned by @code{SELECT typname FROM pg_type WHERE typarray != 0 OR typtype = 'c';}, optionally having @code{[]} appended (without space) to indicate an array. Type specifications can be schema-qualified, otherwise they refer to the visible type with that name.\n\
+@item copy_in_path, copy_out_path\n\
+Path to files at the client side for @code{copy from stdin} and @code{copy to stdout}, respectively.\n\
+@item copy_in_from_variable\n\
+Logical scalar, default @code{false}. If @code{true}, @code{copy from stdin} uses data from an Octave variable instead of from a file.\n\
+@item copy_in_data\n\
+2-dimensional cell-array with columns of suitable type (see below) -- will be used instead of a file as data for @code{copy from stdin} if @code{copy_in_from_variable} is @code{true}.\n\
+@item copy_in_types\n\
+If some columns in @code{copy_in_data} need a type specification (see below), @code{copy_in_types} has to be set to a cell-array with type specifications, with an entry (possibly empty) for each column.\n\
+@item copy_in_with_oids\n\
+If you want to copy in with oids when using data from an Octave variable, the first column of the data must contain the OIDs and @code{copy_in_with_oids} has to be set to @code{true} (default @code{false}); @code{with oids} should be specified together with @code{copy from stdin} in the command, otherwise Postgresql will ignore the copied oids.\n\
+@end table\n\
+\n\
+There is no way to @code{copy to stdout} into an Octave variable, but a @code{select} command can be used for this purpose.\n\
 \n\
 For queries (commands potentially returning data), the output will be a structure with fields @code{data} (containing a cell array with the data, columns correspond to returned database columns, rows correspond to returned tuples) and @code{columns} (containing the column headers). For copy commands nothing is returned. For other commands, the output will be the number of affected rows in the database.\n\
 \n\
@@ -185,7 +199,7 @@
   if (nargs == 1 && args(0).is_string () &&
       args(0).string_value () == "defaults")
     {
-      octave_value_list f_args (16);
+      octave_value_list f_args (14);
       Matrix a;
 
       f_args(0) = octave_value ("param_types");
@@ -202,8 +216,6 @@
       f_args(11) = octave_value (a);
       f_args(12) = octave_value ("copy_in_from_variable");
       f_args(13) = octave_value (false);
-      f_args(14) = octave_value ("copy_out_to_variable");
-      f_args(15) = octave_value (false);
 
       return feval ("setdbopts", f_args, 1);
     }
@@ -399,19 +411,6 @@
       return retval;
     }
 
-  f_args(1) = octave_value ("copy_out_to_variable");
-  f_args(2) = octave_value (false);
-
-  f_ret = feval ("getdbopts", f_args, 1);
-  bool cout_to_variable = f_ret(0).bool_value ();
-  if (error_state)
-    {
-      error ("%s: could not convert copy_out_to_variable to bool",
-             fname.c_str ());
-
-      return retval;
-    }
-
   // check option settings
 
   if (ptypes.length () != nparams)
@@ -449,7 +448,7 @@
   if (c.good ())
     retval = c.process_single_result
       (cin_path, cout_path, cin_data, cin_types, cin_with_oids,
-       cin_from_variable, cout_to_variable);
+       cin_from_variable);
 
   return retval;
 }