Date: Tuesday, January 17, 2006 @ 14:51:14
  Author: csaba
    Path: /cvsroot/carob/libmysequoia/src

Modified: Utils.cpp (1.19 -> 1.20)

Date and time conversion functions.


-----------+
 Utils.cpp |  196 ++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 158 insertions(+), 38 deletions(-)


Index: libmysequoia/src/Utils.cpp
diff -u libmysequoia/src/Utils.cpp:1.19 libmysequoia/src/Utils.cpp:1.20
--- libmysequoia/src/Utils.cpp:1.19     Tue Jan 17 09:38:38 2006
+++ libmysequoia/src/Utils.cpp  Tue Jan 17 14:51:14 2006
@@ -32,6 +32,7 @@
 
 #include <climits>
 #include <cmath>
+#include <ctime>
 #include <sstream>
 
 using namespace CarobNS;
@@ -252,6 +253,64 @@
   s << t;
   return !(s >> o).fail();
 }
+/**
+ * Convert to MYSQL datetime structure
+ */
+void convertTime(void *data, MYSQL_TIME *mt, enum enum_mysql_timestamp_type 
type)
+{
+  int time = *(int *)data;
+
+  memset(mt, 0, sizeof(MYSQL_TIME));
+  mt->time_type = type;
+
+  mt->second_part = (time % 1000) * 1000;
+  time = time / 1000;
+  mt->second = time % 60;
+  time = time / 60;
+  mt->minute = time % 60;
+  time = time / 60;
+  mt->hour = time;
+
+  if (type != MYSQL_TIMESTAMP_TIME)
+  {
+    mt->day = mt->hour / 24;
+    mt->hour = mt->hour % 24;
+  }
+  
+  if (type == MYSQL_TIMESTAMP_DATE)
+  {
+    mt->hour = mt->minute = mt->second = mt->second_part = 0;
+  }
+}
+
+void convertDateTime(void *data, MYSQL_TIME *mt, enum 
enum_mysql_timestamp_type type, bool is_nano)
+{
+  int64_t date = *(int64_t *)data/1000;
+  tm tm;
+  
+  tm = *(localtime((time_t *)&date));
+  memset(mt, 0, sizeof(MYSQL_TIME));
+  mt->time_type = type;
+  
+  if (type <= MYSQL_TIMESTAMP_DATETIME)
+  {
+    mt->year = tm.tm_year + 1900;
+    mt->month = tm.tm_mon + 1;
+    mt->day = tm.tm_mday;      
+  }
+  
+  if (type >= MYSQL_TIMESTAMP_DATETIME)
+  {
+    int micro = is_nano ? (*(int *)((char *)data + sizeof(int64_t))) / 1000: 
((*(int64_t *)data) % 1000) * 1000;
+    mt->hour = tm.tm_hour;
+    mt->minute = tm.tm_min;
+    mt->second = tm.tm_sec;
+    mt->second_part = micro;
+  
+    if (type == MYSQL_TIMESTAMP_TIME)
+      mt->hour += tm.tm_mday * 24;
+  }
+}
 
 /**
  * return true if the value is not in the range (min, max)
@@ -266,6 +325,8 @@
 {
   bool is_unsigned = (field->flags & UNSIGNED_FLAG) == UNSIGNED_FLAG;
     
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -293,6 +354,13 @@
       *(double *)bind->buffer = is_unsigned ? *(unsigned char *)data : *(char 
*)data;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%hhd", 
is_unsigned ? *(unsigned char *)data : *(char *)data);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
@@ -316,6 +384,8 @@
 {
   bool is_unsigned = (field->flags & UNSIGNED_FLAG) == UNSIGNED_FLAG;
 
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -344,6 +414,13 @@
       *(double *)bind->buffer = is_unsigned ? *(unsigned short int *)data : 
*(short int *)data;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%hd", 
is_unsigned ? *(unsigned short int *)data : *(short int *)data);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
@@ -367,6 +444,8 @@
 {
   bool is_unsigned = (field->flags & UNSIGNED_FLAG) == UNSIGNED_FLAG;
 
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -396,6 +475,13 @@
       *(double *)bind->buffer = is_unsigned ? *(unsigned int *)data : *(int 
*)data;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%d", 
is_unsigned ? *(unsigned int *)data : *(int *)data);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
@@ -419,6 +505,8 @@
 {
   bool is_unsigned = (field->flags & UNSIGNED_FLAG) == UNSIGNED_FLAG;
 
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -442,13 +530,22 @@
       break;
 
     case MYSQL_TYPE_FLOAT:
+      // TODO check overflow
       *(float *)bind->buffer = is_unsigned ? *(unsigned long long int *)data : 
*(long long int *)data;
       break;
 
     case MYSQL_TYPE_DOUBLE:
+      // TODO check overflow
       *(double *)bind->buffer = is_unsigned ? *(unsigned long long int *)data 
: *(long long int *)data;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%lld", 
is_unsigned ? *(unsigned long long int *)data : *(long long int *)data);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
@@ -472,6 +569,8 @@
 {
   float f = *(float *)data;
   float ff = f < 0 ? -floorf(-f) : floorf(f);
+
+  *bind->error = 0;
   
   switch (bind->buffer_type)
   {
@@ -503,7 +602,15 @@
       *(double *)bind->buffer = *(float *)data;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
+      // TODO precision
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%.*f", 
(int)field->decimals, f);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
       break;
@@ -527,6 +634,8 @@
   double d = *(double *)data;
   double df = d < 0 ? -floor(-d) : floorf(d);
 
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -558,6 +667,13 @@
       *(double *)bind->buffer = d;
       break;
       
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       int len = snprintf((char *)bind->buffer, bind->buffer_length, "%.*f", 
(int)field->decimals, d);
       *bind->error = (len < 0) || ((unsigned int)len >= bind->buffer_length);
@@ -585,6 +701,8 @@
   float f;
   double d;
 
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -674,6 +792,8 @@
 
 void getFromTime(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -694,17 +814,27 @@
     case MYSQL_TYPE_DOUBLE:
       break;
 
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       break;
 
     case MYSQL_TYPE_TIME:
-      *(MYSQL_TIME *)bind->buffer = *(MYSQL_TIME *)data;
+      convertTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_TIME);
       break;
 
     case MYSQL_TYPE_DATE:
+      convertTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_DATE);
+      *bind->error = true;
       break;
 
     case MYSQL_TYPE_DATETIME:
+      convertTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_DATETIME);
       break;
 
     default:
@@ -714,6 +844,8 @@
 
 void getFromDate(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -734,17 +866,27 @@
     case MYSQL_TYPE_DOUBLE:
       break;
 
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       break;
 
     case MYSQL_TYPE_TIME:
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_TIME, 
false);
+      *bind->error = true;
       break;
 
     case MYSQL_TYPE_DATE:
-      *(MYSQL_TIME *)bind->buffer = *(MYSQL_TIME *)data;
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_DATE, 
false);
       break;
 
     case MYSQL_TYPE_DATETIME:
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, 
MYSQL_TIMESTAMP_DATETIME, false);
       break;
 
     default:
@@ -754,6 +896,8 @@
 
 void getFromDateTime(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
+  *bind->error = 0;
+
   switch (bind->buffer_type)
   {
     case MYSQL_TYPE_TINY:
@@ -774,17 +918,28 @@
     case MYSQL_TYPE_DOUBLE:
       break;
 
+    case MYSQL_TYPE_DECIMAL:
+    case MYSQL_TYPE_NEWDECIMAL:
+    case MYSQL_TYPE_TINY_BLOB:
+    case MYSQL_TYPE_MEDIUM_BLOB:
+    case MYSQL_TYPE_LONG_BLOB:
+    case MYSQL_TYPE_BLOB:
+    case MYSQL_TYPE_VAR_STRING:
     case MYSQL_TYPE_STRING:
       break;
 
     case MYSQL_TYPE_TIME:
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_TIME, 
true);
+      *bind->error = true;
       break;
 
     case MYSQL_TYPE_DATE:
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, MYSQL_TIMESTAMP_DATE, 
true);
+      *bind->error = true;
       break;
 
     case MYSQL_TYPE_DATETIME:
-      *(MYSQL_TIME *)bind->buffer = *(MYSQL_TIME *)data;
+      convertDateTime(data, (MYSQL_TIME *)bind->buffer, 
MYSQL_TIMESTAMP_DATETIME, true);
       break;
 
     default:
@@ -795,10 +950,6 @@
 /* 
  * TODO Not yet implemented conversion functions!!!
  * 
-void getFromNull(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
 void getFromTimestamp(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
 }
@@ -815,18 +966,10 @@
 {
 }
 
-void getFromVarchar(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
 void getFromBit(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
 }
 
-void getFromNewDecimal(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
 void getFromEnum(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
 }
@@ -834,32 +977,9 @@
 void getFromSet(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
 }
-      
-void getFromTinyBlob(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
-void getFromMediumBlob(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
-void getFromLongBlob(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
-void getFromBlob(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
-
-void getFromVarstring(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
 
 void getFromGeometry(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
 {
 }
 
-void getFromDecimal(MYSQL_BIND *bind, MYSQL_FIELD *field, void *data)
-{
-}
 */

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to