changeset 11668:abd563353849 octave-forge

Added remaining date/time converters (time, timetz, date, interval). Fixed date/time converter documentation.
author i7tiol
date Sun, 28 Apr 2013 15:16:11 +0000
parents 2c21253d3341
children 8db5e2631b23
files main/database/NEWS main/database/doc/README-postgresql main/database/doc/dev-postgresql/conversions.tex main/database/doc/dev-postgresql/timeformats.txt main/database/inst/pq_exec_params.m main/database/src/converters.cc main/database/src/converters.h
diffstat 7 files changed, 293 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/main/database/NEWS	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/NEWS	Sun Apr 28 15:16:11 2013 +0000
@@ -1,4 +1,5 @@
- ** New converter for type timestamp.
+ ** New converters for date/time types (timestamp, timestamptz, time,
+    timetz, date, interval).
 
  ** New function pq_conninfo.
 
--- a/main/database/doc/README-postgresql	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/doc/README-postgresql	Sun Apr 28 15:16:11 2013 +0000
@@ -17,7 +17,9 @@
 
 pq_lo_unlink: delete large object,
 
-pq_close: disconnect from database.
+pq_close: disconnect from database,
+
+pq_conninfo: get information on connection and server.
 
 
 Features:
@@ -34,7 +36,9 @@
 
 - binary data transfer,
 
-- reads postgresql system catalogs to get type information.
+- reads postgresql system catalogs to get type information,
+
+- types can be scheme-qualified.
 
 
 State:
--- a/main/database/doc/dev-postgresql/conversions.tex	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/doc/dev-postgresql/conversions.tex	Sun Apr 28 15:16:11 2013 +0000
@@ -117,44 +117,44 @@
 timestamp &
 1114 &
 1115 &
-string &
+int64 or double &
 yes &
 see file timeformats.txt \\
 
 timestamptz &
 1184 &
 1185 &
-string &
+int64 or double &
 yes &
-(put off implementation) \\
+see file timeformats.txt \\
 
 time &
 1083 &
 1183 &
-string &
+int64 or double &
 yes &
-(put off implementation) \\
+see file timeformats.txt \\
 
 timetz &
 1266 &
 1270 &
-string &
+cell: int64 or double, int32 &
 yes &
-(put off implementation) \\
+see file timeformats.txt \\
 
 date &
 1082 &
 1182 &
-string &
+int32 &
 yes &
-(put off implementation) \\
+see file timeformats.txt \\
 
 interval &
 1186 &
 1187 &
 string &
-yes &
-(put off implementation) \\
+cell: int64 or double, int32, int32 &
+see file timeformats.txt \\
 
 bool &
 16 &
--- a/main/database/doc/dev-postgresql/timeformats.txt	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/doc/dev-postgresql/timeformats.txt	Sun Apr 28 15:16:11 2013 +0000
@@ -23,7 +23,7 @@
 TimeADT (8 byte, float or int) (microseconds (int) or seconds (float) ?)
 
 
-timetz (don't implement?):
+timetz:
 
 TimeADT (8 byte, float or int) (microseconds (int) or seconds (float) ?)
 
@@ -32,5 +32,4 @@
 
 date:
 
-int32
-
+int32 (difference to 2000-01-01)
--- a/main/database/inst/pq_exec_params.m	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/inst/pq_exec_params.m	Sun Apr 28 15:16:11 2013 +0000
@@ -127,8 +127,25 @@
 ## digits
 ## @tab yes
 ## @item timestamp
-## @tab 8-byte-time-value (see below), positive or negative time
-## interval from 2000-01-01 00:00.
+## @tab 8-byte-time-value (see below), positive or negative difference
+## to 2000-01-01 00:00
+## @tab yes
+## @item timestamptz
+## @tab as timestamp
+## @tab yes
+## @item time
+## @tab 8-byte-time-value (see below)
+## @tab yes
+## @item timetz
+## @tab 2-element cell array with 8-byte-time-value (see below, time of
+## day) and int32 scalar (time zone in seconds, negative east of UTC)
+## @tab yes
+## @item date
+## @tab int32 scalar, positive or negative difference to 2000-01-01
+## @tab yes
+## @item interval
+## @tab 3-element cell array with 8-byte-time-value (see below), int32
+## (days), and int32 (months)
 ## @tab yes
 ## @item any array
 ## @tab Structure with fields @code{data} (holding a cell-array with
--- a/main/database/src/converters.cc	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/src/converters.cc	Sun Apr 28 15:16:11 2013 +0000
@@ -20,6 +20,7 @@
 #include <octave/oct.h>
 #include <octave/ov-float.h>
 #include <octave/ov-uint8.h>
+#include <octave/Cell.h>
 
 #include <libpq-fe.h>
 
@@ -806,6 +807,252 @@
 
 /* end type timestamptz */
 
+/* type interval */
+
+int to_octave_str_interval (const octave_pq_connection &conn,
+                            const char *c, octave_value &ov, int nb)
+{
+  return 1;
+}
+
+int to_octave_bin_interval (const octave_pq_connection &conn,
+                            const char *c, octave_value &ov, int nb)
+{
+  Cell tp (dim_vector (3, 1));
+
+  tp(0) = time_8byte_to_octave (c, conn.get_integer_datetimes ());
+
+  c += 8;
+
+  tp(1) = octave_value (octave_int32 (int32_t (be32toh (*((int32_t *) c)))));
+
+  c += 4;
+
+  tp(2) = octave_value (octave_int32 (int32_t (be32toh (*((int32_t *) c)))));
+
+  ov = octave_value (tp);
+
+  return 0;
+}
+
+int from_octave_str_interval (const octave_pq_connection &conn,
+                              const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  return 1;
+}
+
+int from_octave_bin_interval (const octave_pq_connection &conn,
+                              const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  Cell iv = ov.cell_value ();
+  if (error_state || iv.numel () != 3)
+    {
+      error ("interval: can not convert octave_value to cell with 3 elements");
+      return 1;
+    }
+
+  if (time_8byte_from_octave (iv(0), val, conn.get_integer_datetimes ()))
+    return 1;
+
+  for (int id = 1; id < 3; id++)
+    {
+      int32_t i4 = iv(id).int_value ();
+
+      if (error_state)
+        {
+          error ("interval: can not convert octave_value to int4 value");
+          return 1;
+        }
+
+      OCT_PQ_PUT(val, int32_t, htobe32 (i4))
+    }
+
+  return 0;
+}
+
+oct_pq_conv_t conv_interval = {0,
+                               0,
+                               oct_pq_el_oids_t (),
+                               oct_pq_conv_cache_t (),
+                               false,
+                               false,
+                               false,
+                               "interval",
+                               &to_octave_str_interval,
+                               &to_octave_bin_interval,
+                               &from_octave_str_interval,
+                               &from_octave_bin_interval};
+
+/* end type interval */
+
+/* type time */
+
+int to_octave_str_time (const octave_pq_connection &conn,
+                        const char *c, octave_value &ov, int nb)
+{
+  return 1;
+}
+
+int to_octave_bin_time (const octave_pq_connection &conn,
+                        const char *c, octave_value &ov, int nb)
+{
+  ov = time_8byte_to_octave (c, conn.get_integer_datetimes ());
+
+  return 0;
+}
+
+int from_octave_str_time (const octave_pq_connection &conn,
+                          const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  return 1;
+}
+
+int from_octave_bin_time (const octave_pq_connection &conn,
+                          const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  return (time_8byte_from_octave (ov, val, conn.get_integer_datetimes ()));
+}
+
+oct_pq_conv_t conv_time = {0,
+                           0,
+                           oct_pq_el_oids_t (),
+                           oct_pq_conv_cache_t (),
+                           false,
+                           false,
+                           false,
+                           "time",
+                           &to_octave_str_time,
+                           &to_octave_bin_time,
+                           &from_octave_str_time,
+                           &from_octave_bin_time};
+
+/* end type time */
+
+/* type timetz */
+
+int to_octave_str_timetz (const octave_pq_connection &conn,
+                          const char *c, octave_value &ov, int nb)
+{
+  return 1;
+}
+
+int to_octave_bin_timetz (const octave_pq_connection &conn,
+                          const char *c, octave_value &ov, int nb)
+{
+  Cell tp (dim_vector (2, 1));
+
+  tp(0) = time_8byte_to_octave (c, conn.get_integer_datetimes ());
+
+  c += 8;
+
+  tp(1) = octave_value (octave_int32 (int32_t (be32toh (*((int32_t *) c)))));
+
+  ov = octave_value (tp);
+
+  return 0;
+}
+
+int from_octave_str_timetz (const octave_pq_connection &conn,
+                            const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  return 1;
+}
+
+int from_octave_bin_timetz (const octave_pq_connection &conn,
+                            const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  Cell iv = ov.cell_value ();
+  if (error_state || iv.numel () != 2)
+    {
+      error ("timetz: can not convert octave_value to cell with 2 elements");
+      return 1;
+    }
+
+  if (time_8byte_from_octave (iv(0), val, conn.get_integer_datetimes ()))
+    return 1;
+
+  int32_t i4 = iv(1).int_value ();
+
+  if (error_state)
+    {
+      error ("timetz: can not convert octave_value to int4 value");
+      return 1;
+    }
+
+  OCT_PQ_PUT(val, int32_t, htobe32 (i4))
+
+  return 0;
+}
+
+oct_pq_conv_t conv_timetz = {0,
+                             0,
+                             oct_pq_el_oids_t (),
+                             oct_pq_conv_cache_t (),
+                             false,
+                             false,
+                             false,
+                             "timetz",
+                             &to_octave_str_timetz,
+                             &to_octave_bin_timetz,
+                             &from_octave_str_timetz,
+                             &from_octave_bin_timetz};
+
+/* end type timetz */
+
+/* type date */
+
+int to_octave_str_date (const octave_pq_connection &conn,
+                        const char *c, octave_value &ov, int nb)
+{
+  return 1;
+}
+
+int to_octave_bin_date (const octave_pq_connection &conn,
+                        const char *c, octave_value &ov, int nb)
+{
+  ov = octave_value (octave_int32 (int32_t (be32toh (*((int32_t *) c)))));
+
+  return 0;
+}
+
+int from_octave_str_date (const octave_pq_connection &conn,
+                          const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  return 1;
+}
+
+int from_octave_bin_date (const octave_pq_connection &conn,
+                          const octave_value &ov, oct_pq_dynvec_t &val)
+{
+  int32_t i4 = ov.int_value ();
+
+  if (error_state)
+    {
+      error ("date: can not convert octave_value to int4 value");
+      return 1;
+    }
+
+  OCT_PQ_PUT(val, int32_t, htobe32 (i4))
+
+  return 0;
+}
+
+oct_pq_conv_t conv_date = {0,
+                           0,
+                           oct_pq_el_oids_t (),
+                           oct_pq_conv_cache_t (),
+                           false,
+                           false,
+                           false,
+                           "date",
+                           &to_octave_str_date,
+                           &to_octave_bin_date,
+                           &from_octave_str_date,
+                           &from_octave_bin_date};
+
+
+/* end type date */
+
 oct_pq_conv_t *t_conv_ptrs[OCT_PQ_NUM_CONVERTERS] = {&conv_bool,
                                                      &conv_oid,
                                                      &conv_float8,
@@ -820,6 +1067,10 @@
                                                      &conv_int8,
                                                      &conv_money,
                                                      &conv_timestamp,
-                                                     &conv_timestamptz};
+                                                     &conv_timestamptz,
+                                                     &conv_interval,
+                                                     &conv_time,
+                                                     &conv_timetz,
+                                                     &conv_date};
 
 oct_pq_conv_ptrs_t conv_ptrs (OCT_PQ_NUM_CONVERTERS, t_conv_ptrs);
--- a/main/database/src/converters.h	Sun Apr 28 09:40:08 2013 +0000
+++ b/main/database/src/converters.h	Sun Apr 28 15:16:11 2013 +0000
@@ -32,7 +32,7 @@
 
 #include "wrap_endian.h"
 
-#define OCT_PQ_NUM_CONVERTERS 15
+#define OCT_PQ_NUM_CONVERTERS 19
 
 typedef std::vector<char> oct_pq_dynvec_t;