date/time parts added for sqlite
This commit is contained in:
parent
3cee4dae4f
commit
e539a1da75
|
|
@ -218,8 +218,6 @@ QString SqliteGenerator::createConditionalPhrase(const PhraseData *d) const
|
|||
}
|
||||
|
||||
if (d->type == PhraseData::WithVariant) {
|
||||
QString part;
|
||||
|
||||
switch (op) {
|
||||
case PhraseData::AddYears:
|
||||
case PhraseData::AddMonths:
|
||||
|
|
@ -256,6 +254,37 @@ QString SqliteGenerator::createConditionalPhrase(const PhraseData *d) const
|
|||
}
|
||||
}
|
||||
}
|
||||
if (d->type == PhraseData::WithoutOperand) {
|
||||
switch (op) {
|
||||
case PhraseData::DatePartYear:
|
||||
return QString("CAST(strftime('%Y', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
case PhraseData::DatePartMonth:
|
||||
return QString("CAST(strftime('%m', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
case PhraseData::DatePartDay:
|
||||
return QString("CAST(strftime('%d', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
case PhraseData::DatePartHour:
|
||||
return QString("CAST(strftime('%H', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
case PhraseData::DatePartMinute:
|
||||
return QString("CAST(strftime('%M', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
case PhraseData::DatePartSecond:
|
||||
return QString("CAST(strftime('%S', %1) AS INT)")
|
||||
.arg(createConditionalPhrase(d->left));
|
||||
|
||||
// case PhraseData::DatePartMilisecond:
|
||||
// return QString("CAST(strftime('%Y', %1) AS INT)")
|
||||
// .arg(createConditionalPhrase(d->left));
|
||||
}
|
||||
}
|
||||
|
||||
return SqlGeneratorBase::createConditionalPhrase(d);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ do { \
|
|||
db.sampleTables()->append(s); \
|
||||
db.saveChanges(); \
|
||||
auto count = db.sampleTables()->query() \
|
||||
->where(SampleTable::dtField().command(n) == datetime.command(n)); \
|
||||
->where(SampleTable::dtField().command(n) == datetime.command(n)) \
|
||||
->count(); \
|
||||
QTEST_ASSERT(count); \
|
||||
} while (false)
|
||||
|
|
@ -78,7 +78,7 @@ do { \
|
|||
db.sampleTables()->append(s); \
|
||||
db.saveChanges(); \
|
||||
auto count = db.sampleTables()->query() \
|
||||
->where(SampleTable::dtField().command(n) == datetime.addSecs(num)); \
|
||||
->where(SampleTable::dtField().command(n) == datetime.addSecs(num)) \
|
||||
->count(); \
|
||||
QTEST_ASSERT(count); \
|
||||
} while (false)
|
||||
|
|
@ -134,6 +134,74 @@ void DateTimeTest::dateTimeAdd()
|
|||
TEST_TIME2(dt, addSeconds, -10, -10);
|
||||
}
|
||||
|
||||
void DateTimeTest::datePart()
|
||||
{
|
||||
db.sampleTables()->query()->remove();
|
||||
|
||||
QDate d = QDate::currentDate();
|
||||
auto s = Nut::create<SampleTable>();
|
||||
s->setD(d);
|
||||
db.sampleTables()->append(s);
|
||||
db.saveChanges();
|
||||
|
||||
int count;
|
||||
|
||||
count = db.sampleTables()->query()->where(SampleTable::dField().year() == d.year())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dField().month() == d.month())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dField().day() == d.day())->count();
|
||||
QTEST_ASSERT(count);
|
||||
|
||||
}
|
||||
|
||||
void DateTimeTest::timePart()
|
||||
{
|
||||
db.sampleTables()->query()->remove();
|
||||
|
||||
QTime t = QTime::currentTime();
|
||||
auto s = Nut::create<SampleTable>();
|
||||
s->setT(t);
|
||||
db.sampleTables()->append(s);
|
||||
db.saveChanges();
|
||||
|
||||
int count;
|
||||
|
||||
count = db.sampleTables()->query()->where(SampleTable::tField().hour() == t.hour())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::tField().minute() == t.minute())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::tField().second() == t.second())->count();
|
||||
QTEST_ASSERT(count);
|
||||
}
|
||||
|
||||
void DateTimeTest::dateTimePart()
|
||||
{
|
||||
db.sampleTables()->query()->remove();
|
||||
|
||||
QDateTime dt = QDateTime::currentDateTime();
|
||||
auto s = Nut::create<SampleTable>();
|
||||
s->setDT(dt);
|
||||
db.sampleTables()->append(s);
|
||||
db.saveChanges();
|
||||
|
||||
int count;
|
||||
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().year() == dt.date().year())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().month() == dt.date().month())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().day() == dt.date().day())->count();
|
||||
QTEST_ASSERT(count);
|
||||
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().hour() == dt.time().hour())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().minute() == dt.time().minute())->count();
|
||||
QTEST_ASSERT(count);
|
||||
count = db.sampleTables()->query()->where(SampleTable::dtField().second() == dt.time().second())->count();
|
||||
QTEST_ASSERT(count);
|
||||
}
|
||||
|
||||
void DateTimeTest::cleanupTestCase()
|
||||
{
|
||||
db.sampleTables()->query()->remove();
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ private slots:
|
|||
void dateAdd();
|
||||
void timeAdd();
|
||||
void dateTimeAdd();
|
||||
void datePart();
|
||||
void timePart();
|
||||
void dateTimePart();
|
||||
void cleanupTestCase();
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue