mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-17 03:03:23 +02:00
step, date, time
This commit is contained in:
@@ -114,6 +114,12 @@ public:
|
||||
void bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir);
|
||||
/// Binds a BLOB.
|
||||
|
||||
void bind(std::size_t pos, const Date& val, Direction dir);
|
||||
/// Binds a Date.
|
||||
|
||||
void bind(std::size_t pos, const Time& val, Direction dir);
|
||||
/// Binds a Time.
|
||||
|
||||
void bind(std::size_t pos, const DateTime& val, Direction dir);
|
||||
/// Binds a DateTime.
|
||||
|
||||
|
@@ -120,6 +120,12 @@ public:
|
||||
bool extract(std::size_t pos, Poco::Data::BLOB& val);
|
||||
/// Extracts a BLOB.
|
||||
|
||||
bool extract(std::size_t pos, Poco::Data::Date& val);
|
||||
/// Extracts a Date.
|
||||
|
||||
bool extract(std::size_t pos, Poco::Data::Time& val);
|
||||
/// Extracts a Time.
|
||||
|
||||
bool extract(std::size_t pos, Poco::DateTime& val);
|
||||
/// Extracts a DateTime.
|
||||
|
||||
@@ -248,6 +254,20 @@ private:
|
||||
val = b;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_DATE:
|
||||
{
|
||||
Date d;
|
||||
ret = extract(pos, d);
|
||||
val = d;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_TIME:
|
||||
{
|
||||
Time t;
|
||||
ret = extract(pos, t);
|
||||
val = t;
|
||||
break;
|
||||
}
|
||||
case MetaColumn::FDT_TIMESTAMP:
|
||||
{
|
||||
DateTime dt;
|
||||
|
@@ -77,8 +77,8 @@ protected:
|
||||
bool hasNext();
|
||||
/// Returns true if a call to next() will return data.
|
||||
|
||||
void next();
|
||||
/// Retrieves the next row from the resultset.
|
||||
Poco::UInt32 next();
|
||||
/// Retrieves the next row from the resultset and returns 1.
|
||||
/// Will throw, if the resultset is empty.
|
||||
|
||||
bool canBind() const;
|
||||
|
@@ -57,6 +57,9 @@ class SQLite_API Utility
|
||||
/// Various utility functions for SQLite, mostly return code handling
|
||||
{
|
||||
public:
|
||||
static const std::string SQLITE_DATE_FORMAT;
|
||||
static const std::string SQLITE_TIME_FORMAT;
|
||||
|
||||
static std::string lastError(sqlite3* pDb);
|
||||
/// Retreives the last error code from sqlite and converts it to a string
|
||||
|
||||
|
@@ -36,6 +36,8 @@
|
||||
|
||||
#include "Poco/Data/SQLite/Binder.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
@@ -103,6 +105,23 @@ void Binder::bind(std::size_t pos, const Poco::Data::BLOB& val, Direction dir)
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Date& val, Direction dir)
|
||||
{
|
||||
DateTime dt(val.year(), val.month(), val.day());
|
||||
std::string str(DateTimeFormatter::format(dt, Utility::SQLITE_DATE_FORMAT));
|
||||
bind(pos, str, dir);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const Time& val, Direction dir)
|
||||
{
|
||||
DateTime dt;
|
||||
dt.assign(dt.year(), dt.month(), dt.day(), val.hour(), val.minute(), val.second());
|
||||
std::string str(DateTimeFormatter::format(dt, Utility::SQLITE_TIME_FORMAT));
|
||||
bind(pos, str, dir);
|
||||
}
|
||||
|
||||
|
||||
void Binder::bind(std::size_t pos, const DateTime& val, Direction dir)
|
||||
{
|
||||
std::string dt(DateTimeFormatter::format(val, DateTimeFormat::ISO8601_FORMAT));
|
||||
|
@@ -36,6 +36,8 @@
|
||||
|
||||
#include "Poco/Data/SQLite/Extractor.h"
|
||||
#include "Poco/Data/SQLite/Utility.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Data/DataException.h"
|
||||
#include "Poco/DateTimeParser.h"
|
||||
@@ -205,6 +207,28 @@ bool Extractor::extract(std::size_t pos, char& val)
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Date& val)
|
||||
{
|
||||
std::string str;
|
||||
extract(pos, str);
|
||||
int tzd;
|
||||
DateTime dt = DateTimeParser::parse(Utility::SQLITE_DATE_FORMAT, str, tzd);
|
||||
val = dt;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, Time& val)
|
||||
{
|
||||
std::string str;
|
||||
extract(pos, str);
|
||||
int tzd;
|
||||
DateTime dt = DateTimeParser::parse(Utility::SQLITE_TIME_FORMAT, str, tzd);
|
||||
val = dt;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool Extractor::extract(std::size_t pos, DateTime& val)
|
||||
{
|
||||
std::string dt;
|
||||
|
@@ -196,7 +196,7 @@ bool SQLiteStatementImpl::hasNext()
|
||||
}
|
||||
|
||||
|
||||
void SQLiteStatementImpl::next()
|
||||
Poco::UInt32 SQLiteStatementImpl::next()
|
||||
{
|
||||
if (SQLITE_ROW == _nextResponse)
|
||||
{
|
||||
@@ -222,6 +222,8 @@ void SQLiteStatementImpl::next()
|
||||
int rc = _nextResponse;
|
||||
Utility::throwException(rc, std::string("Iterator Error: trying to access the next value"));
|
||||
}
|
||||
|
||||
return 1u;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -49,6 +49,10 @@ namespace Data {
|
||||
namespace SQLite {
|
||||
|
||||
|
||||
const std::string Utility::SQLITE_DATE_FORMAT = "%Y-%m-%d";
|
||||
const std::string Utility::SQLITE_TIME_FORMAT = "%H:%M:%S";
|
||||
|
||||
|
||||
std::string Utility::lastError(sqlite3 *pDB)
|
||||
{
|
||||
return std::string(sqlite3_errmsg(pDB));
|
||||
@@ -76,6 +80,8 @@ MetaColumn::ColumnDataType Utility::getColumnType(sqlite3_stmt* pStmt, std::size
|
||||
return MetaColumn::FDT_DOUBLE;
|
||||
else if (sqliteType.npos != sqliteType.find("BLOB"))
|
||||
return MetaColumn::FDT_BLOB;
|
||||
else if (sqliteType.npos != sqliteType.find("DATE"))
|
||||
return MetaColumn::FDT_TIMESTAMP;
|
||||
|
||||
throw Poco::NotFoundException();
|
||||
}
|
||||
|
@@ -34,6 +34,8 @@
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/Data/Common.h"
|
||||
#include "Poco/Data/Date.h"
|
||||
#include "Poco/Data/Time.h"
|
||||
#include "Poco/Data/BLOB.h"
|
||||
#include "Poco/Data/Statement.h"
|
||||
#include "Poco/Data/RecordSet.h"
|
||||
@@ -42,6 +44,8 @@
|
||||
#include "Poco/Data/TypeHandler.h"
|
||||
#include "Poco/Tuple.h"
|
||||
#include "Poco/Any.h"
|
||||
#include "Poco/DynamicAny.h"
|
||||
#include "Poco/DateTime.h"
|
||||
#include "Poco/Exception.h"
|
||||
#include <iostream>
|
||||
|
||||
@@ -51,6 +55,7 @@ using Poco::Tuple;
|
||||
using Poco::Any;
|
||||
using Poco::AnyCast;
|
||||
using Poco::DynamicAny;
|
||||
using Poco::DateTime;
|
||||
using Poco::InvalidAccessException;
|
||||
using Poco::RangeException;
|
||||
using Poco::BadCastException;
|
||||
@@ -1499,6 +1504,42 @@ void SQLiteTest::testTupleVector1()
|
||||
}
|
||||
|
||||
|
||||
void SQLiteTest::testDateTime()
|
||||
{
|
||||
Session tmp (SessionFactory::instance().create(SQLite::Connector::KEY, "dummy.db"));
|
||||
tmp << "DROP TABLE IF EXISTS DateTimes", now;
|
||||
tmp << "CREATE TABLE DateTimes (dt0 DATE)", now;
|
||||
|
||||
DateTime dt(1965, 6, 18, 5, 35, 1);
|
||||
tmp << "INSERT INTO DateTimes VALUES (?)", use(dt), now;
|
||||
|
||||
DateTime rdt;
|
||||
assert (rdt != dt);
|
||||
tmp << "SELECT * FROM DateTimes", into(rdt), now;
|
||||
assert (rdt == dt);
|
||||
|
||||
tmp << "DELETE FROM DateTimes", now;
|
||||
|
||||
Date d(dt);
|
||||
tmp << "INSERT INTO DateTimes VALUES (?)", use(d), now;
|
||||
|
||||
Date rd;
|
||||
assert (rd != d);
|
||||
tmp << "SELECT * FROM DateTimes", into(rd), now;
|
||||
assert (rd == d);
|
||||
|
||||
tmp << "DELETE FROM DateTimes", now;
|
||||
|
||||
Time t(dt);
|
||||
tmp << "INSERT INTO DateTimes VALUES (?)", use(t), now;
|
||||
|
||||
Time rt;
|
||||
assert (rt != t);
|
||||
tmp << "SELECT * FROM DateTimes", into(rt), now;
|
||||
assert (rt == t);
|
||||
}
|
||||
|
||||
|
||||
void SQLiteTest::testInternalExtraction()
|
||||
{
|
||||
Session tmp (SessionFactory::instance().create(SQLite::Connector::KEY, "dummy.db"));
|
||||
@@ -1901,6 +1942,7 @@ CppUnit::Test* SQLiteTest::suite()
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTupleVector2);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTuple1);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testTupleVector1);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testDateTime);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testInternalExtraction);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testPrimaryKeyConstraint);
|
||||
CppUnit_addTest(pSuite, SQLiteTest, testNull);
|
||||
|
@@ -107,6 +107,8 @@ public:
|
||||
void testTuple10();
|
||||
void testTupleVector10();
|
||||
|
||||
void testDateTime();
|
||||
|
||||
void testInternalExtraction();
|
||||
void testPrimaryKeyConstraint();
|
||||
void testNull();
|
||||
|
Reference in New Issue
Block a user