Author: wyoung
Date: Sat Dec 22 00:00:29 2007
New Revision: 2012

URL: http://svn.gna.org/viewcvs/mysqlpp?rev=2012&view=rev
Log:
- If you create a DateTime with its default ctor now, it will become the
  SQL function "NOW()" when inserted into an ostream.  If you
  subsequently assign a new value to it, you get the given values.
- Added a ctor to DateTime taking all six discrete y/m/d h:m:s values.
- Added a test covering all these cases and more.

Added:
    trunk/test/test_datetime.cpp
Modified:
    trunk/Wishlist
    trunk/lib/datetime.cpp
    trunk/lib/datetime.h
    trunk/mysql++.bkl

Modified: trunk/Wishlist
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/Wishlist?rev=2012&r1=2011&r2=2012&view=diff
==============================================================================
--- trunk/Wishlist (original)
+++ trunk/Wishlist Sat Dec 22 00:00:29 2007
@@ -9,9 +9,6 @@
 
 v3.0 Plan
 ---------
-    o Add syntactic sugar to the Date and Time classes.  A way to
-      represent SQL's NOW() function would be especially helpful.
-
     o Add userman chapter on connection options
 
     o Create examples/vstudio/threads to test new thread-related

Modified: trunk/lib/datetime.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.cpp?rev=2012&r1=2011&r2=2012&view=diff
==============================================================================
--- trunk/lib/datetime.cpp (original)
+++ trunk/lib/datetime.cpp Sat Dec 22 00:00:29 2007
@@ -74,9 +74,14 @@
 
 std::ostream& operator <<(std::ostream& os, const DateTime& dt)
 {
-       operator <<(os, Date(dt));
-       os << ' ';
-       return operator <<(os, Time(dt));
+       if (dt.now) {
+               return os << "NOW()";
+       }
+       else {
+               operator <<(os, Date(dt));
+               os << ' ';
+               return operator <<(os, Time(dt));
+       }
 }
 
 
@@ -147,6 +152,8 @@
        hour = t.hour;
        minute = t.minute;
        second = t.second;
+
+       now = false;
        
        return str;
 }
@@ -170,29 +177,43 @@
 
 int DateTime::compare(const DateTime& other) const
 {
-       Date d(*this), od(other);
-       Time t(*this), ot(other);
-
-       if (int x = d.compare(od)) {
-               return x;
+       if (now && other.now) {
+               return 0;
        }
        else {
-               return t.compare(ot);
+               Date d(*this), od(other);
+               Time t(*this), ot(other);
+
+               if (int x = d.compare(od)) {
+                       return x;
+               }
+               else {
+                       return t.compare(ot);
+               }
        }
 }
 
 DateTime::operator time_t() const
 {
-       struct tm tm;
-       tm.tm_sec = second;
-       tm.tm_min = minute;
-       tm.tm_hour = hour;
-       tm.tm_mday = day;
-       tm.tm_mon = month - 1;
-       tm.tm_year = year - 1900;
-       tm.tm_isdst = -1;
-
-       return mktime(&tm);
+       if (now) {
+               // Many factors combine to make it almost impossible for this
+               // case to return the same value as you'd get if you used this
+               // in a query.  But, you gotta better idea than to return the
+               // current time for an object initialized with the value "now"?
+               return time(0);
+       }
+       else {
+               struct tm tm;
+               tm.tm_sec = second;
+               tm.tm_min = minute;
+               tm.tm_hour = hour;
+               tm.tm_mday = day;
+               tm.tm_mon = month - 1;
+               tm.tm_year = year - 1900;
+               tm.tm_isdst = -1;
+
+               return mktime(&tm);
+       }
 };
 
 DateTime::DateTime(time_t t)
@@ -220,6 +241,8 @@
        hour = tm.tm_hour;
        minute = tm.tm_min;
        second = tm.tm_sec;
+
+       now = false;
 }
 
 } // end namespace mysqlpp

Modified: trunk/lib/datetime.h
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/lib/datetime.h?rev=2012&r1=2011&r2=2012&view=diff
==============================================================================
--- trunk/lib/datetime.h (original)
+++ trunk/lib/datetime.h Sat Dec 22 00:00:29 2007
@@ -26,7 +26,7 @@
  USA
 ***********************************************************************/
 
-#ifndef MYSQLPP_DATETIME_H
+#if !defined(MYSQLPP_DATETIME_H)
 #define MYSQLPP_DATETIME_H
 
 #include "common.h"
@@ -102,6 +102,13 @@
 ///
 /// Objects of this class can be inserted into streams, and
 /// initialized from MySQL DATETIME strings.
+///
+/// If you use the default constructor, it's sensible to use the object
+/// in a SQL query as of MySQL++ v3.0.  When converting the object to a
+/// string form for assembling the query, we convert to the SQL function
+/// "NOW()", allowing the database server to set the value.  In older
+/// versions of MySQL++, you'd get a "zero time" instead.
+
 struct DateTime : public DTbase<DateTime>
 {
        unsigned short year;    ///< the year, as a simple integer
@@ -110,6 +117,7 @@
        unsigned char hour;             ///< the hour, 0-23
        unsigned char minute;   ///< the minute, 0-59
        unsigned char second;   ///< the second, 0-59
+       bool now;                               ///< true if not given explicit 
value
 
        /// \brief Default constructor
        DateTime() :
@@ -119,7 +127,29 @@
        day(0),
        hour(0),
        minute(0),
-       second(0)
+       second(0),
+       now(true)
+       {
+       }
+
+       /// \brief Initialize object from discrete y/m/d h:m:s values.
+       ///
+       /// \param y year
+       /// \param mon month
+       /// \param d day of month
+       /// \param h hour
+       /// \param min minute
+       /// \param s second
+       DateTime(unsigned short y, unsigned char mon, unsigned char d,
+                       unsigned char h, unsigned char min, unsigned char s) :
+       DTbase<DateTime>(),
+       year(y),
+       month(mon),
+       day(d),
+       hour(h),
+       minute(min),
+       second(s),
+       now(false)
        {
        }
        
@@ -131,7 +161,8 @@
        day(other.day),
        hour(other.hour),
        minute(other.minute),
-       second(other.second)
+       second(other.second),
+       now(false)
        {
        }
 

Modified: trunk/mysql++.bkl
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/mysql%2B%2B.bkl?rev=2012&r1=2011&r2=2012&view=diff
==============================================================================
--- trunk/mysql++.bkl (original)
+++ trunk/mysql++.bkl Sat Dec 22 00:00:29 2007
@@ -195,6 +195,9 @@
 
        <!-- Define library testing programs' output targets, if enabled -->
        <if cond="BUILDTEST=='yes'">
+               <exe id="test_datetime" template="programs">
+                       <sources>test/test_datetime.cpp</sources>
+               </exe>
                <exe id="test_manip" template="programs">
                        <sources>test/test_manip.cpp</sources>
                </exe>

Added: trunk/test/test_datetime.cpp
URL: 
http://svn.gna.org/viewcvs/mysqlpp/trunk/test/test_datetime.cpp?rev=2012&view=auto
==============================================================================
--- trunk/test/test_datetime.cpp (added)
+++ trunk/test/test_datetime.cpp Sat Dec 22 00:00:29 2007
@@ -1,0 +1,130 @@
+/***********************************************************************
+ test_datetime.cpp - Tests the Date, DateTime, and Time classes.
+
+ Copyright (c) 2007 by Educational Technology Resources, Inc.
+ Others may also hold copyrights on code in this file.  See the
+ CREDITS file in the top directory of the distribution for details.
+
+ This file is part of MySQL++.
+
+ MySQL++ is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ (at your option) any later version.
+
+ MySQL++ 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 Lesser General Public
+ License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with MySQL++; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
+ USA
+***********************************************************************/
+
+#include <mysql++.h>
+
+#include <iostream>
+#include <sstream>
+
+#include <stdio.h>
+
+using namespace mysqlpp;
+using namespace std;
+
+
+// Compare the given string against the object inserted into an ostream.
+template <class T>
+static unsigned int
+test_ostream_form(const T& object, const char* expected, const char* what)
+{
+       ostringstream os;
+       os << object;
+       if (os.str().compare(expected) == 0) {
+               return 0;
+       }
+       else {
+               cerr << what << " '" << object << "' should be '" <<
+                               expected << "' in string form!" << endl;
+               return 1;
+       }
+}
+
+
+// Given a Date and a set of values we should expect to be in it,
+// compare its outputs against values we compute separately.
+static unsigned int
+test_date(const Date& d, int year, int month, int day)
+{
+       if (d.year == year && d.month == d.month && d.day == day) {
+               char ac[20];
+               snprintf(ac, sizeof(ac), "%04d-%02d-%02d",
+                               year, month, day);
+               return test_ostream_form(d, ac, "Date");
+       }
+       else {
+               cerr << "Date '" << d << "' values should be '" <<
+                               year << '-' << month << '-' << day << endl;
+               return 1;
+       }
+}
+
+
+// Given a Time and a set of values we should expect to be in it,
+// compare its outputs against values we compute separately.
+static unsigned int
+test_time(const Time& t, int hour, int minute, int second)
+{
+       if (t.hour == hour && t.minute == minute && t.second == second) {
+               char ac[20];
+               snprintf(ac, sizeof(ac), "%02d:%02d:%02d",
+                               hour, minute, second);
+               return test_ostream_form(t, ac, "Time");
+       }
+       else {
+               cerr << "Time '" << t << "' values should be '" <<
+                               hour << ':' << minute << ':' << second << endl;
+               return 1;
+       }
+}
+
+
+// Given a DateTime and a set of values we should expect to be in it,
+// compare its outputs against values we compute separately.
+static unsigned int
+test_datetime(const DateTime& dt,
+               int year, int month, int day,
+               int hour, int minute, int second)
+{
+       return  test_date(Date(dt), year, month, day) +
+                       test_time(Time(dt), hour, minute, second);
+}
+
+
+// Run tests above for the various types we support using the date and
+// time values given.
+static unsigned int
+test(int year, int month, int day, int hour, int minute, int second)
+{
+       unsigned int failures = 0;
+       failures += test_date(Date(year, month, day), year, month, day);
+       failures += test_datetime(
+                       DateTime(year, month, day, hour, minute, second),
+                       year, month, day, hour, minute, second);
+       failures += test_time(Time(hour, minute, second), hour, minute,
+                       second);
+       return failures;
+}
+
+
+int
+main()
+{
+       unsigned int failures = 0;
+       failures += test(0, 0, 0, 0, 0, 0);
+       failures += test(1, 2, 3, 4, 5, 6);
+       failures += test_ostream_form(DateTime(), "NOW()", "DateTime");
+       return failures;
+}
+


_______________________________________________
Mysqlpp-commits mailing list
[email protected]
https://mail.gna.org/listinfo/mysqlpp-commits

Reply via email to