diff --git a/src/generators/sqlitegenerator.cpp b/src/generators/sqlitegenerator.cpp index 4ed07a7..0555c47 100644 --- a/src/generators/sqlitegenerator.cpp +++ b/src/generators/sqlitegenerator.cpp @@ -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); } diff --git a/test/tst_datetime/tst_datetime.cpp b/test/tst_datetime/tst_datetime.cpp index eb5119f..5f1fd53 100644 --- a/test/tst_datetime/tst_datetime.cpp +++ b/test/tst_datetime/tst_datetime.cpp @@ -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(); + 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(); + 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(); + 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(); diff --git a/test/tst_datetime/tst_datetime.h b/test/tst_datetime/tst_datetime.h index 960a47f..9f309c2 100644 --- a/test/tst_datetime/tst_datetime.h +++ b/test/tst_datetime/tst_datetime.h @@ -33,6 +33,9 @@ private slots: void dateAdd(); void timeAdd(); void dateTimeAdd(); + void datePart(); + void timePart(); + void dateTimePart(); void cleanupTestCase(); };