Permalink
Browse files

SQLite dialect: add support for OFTDateTime/OFTDate/OFTTime

git-svn-id: https://svn.osgeo.org/gdal/trunk@24833 f0d54148-0727-0410-94bb-9a71ac55c965
  • Loading branch information...
rouault committed Aug 23, 2012
1 parent c4dfada commit 536a403e3afa04504d661cef900475059a97ea51
Showing with 85 additions and 26 deletions.
  1. +21 −6 autotest/ogr/ogr_sql_sqlite.py
  2. +64 −20 gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitevirtualogr.cpp
@@ -62,9 +62,15 @@ def ogr_sql_sqlite_1():
lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('nullablefield', ogr.OFTInteger)
lyr.CreateField(field_defn)
+ field_defn = ogr.FieldDefn('datetimefield', ogr.OFTDateTime)
+ lyr.CreateField(field_defn)
+ field_defn = ogr.FieldDefn('datefield', ogr.OFTDate)
+ lyr.CreateField(field_defn)
+ field_defn = ogr.FieldDefn('timefield', ogr.OFTTime)
+ lyr.CreateField(field_defn)
# Test INSERT
- sql_lyr = ds.ExecuteSQL( "INSERT INTO my_layer (intfield, nullablefield, doublefield, strfield, binaryfield) VALUES (1,NULL,2.34,'foo',x'0001FF')", dialect = 'SQLite' )
+ sql_lyr = ds.ExecuteSQL( "INSERT INTO my_layer (intfield, nullablefield, doublefield, strfield, binaryfield, datetimefield, datefield, timefield) VALUES (1,NULL,2.34,'foo',x'0001FF', '2012-08-23 21:24', '2012-08-23', '21:24')", dialect = 'SQLite' )
ds.ReleaseResultSet( sql_lyr )
lyr.ResetReading()
@@ -73,21 +79,27 @@ def ogr_sql_sqlite_1():
feat.GetField('nullablefield') is not None or \
feat.GetField('doublefield') != 2.34 or \
feat.GetField('strfield') != 'foo' or \
- feat.GetField('binaryfield') != '0001FF':
+ feat.GetField('binaryfield') != '0001FF' or \
+ feat.GetField('datetimefield') != '2012/08/23 21:24:00' or \
+ feat.GetField('datefield') != '2012/08/23' or \
+ feat.GetField('timefield') != '21:24:00':
gdaltest.post_reason('failure')
feat.DumpReadable()
return 'fail'
feat = None
# Test UPDATE
- sql_lyr = ds.ExecuteSQL( "UPDATE my_layer SET intfield = 2, doublefield = 3.45, strfield = 'bar' WHERE ROWID = 0", dialect = 'SQLite' )
+ sql_lyr = ds.ExecuteSQL( "UPDATE my_layer SET intfield = 2, doublefield = 3.45, strfield = 'bar', timefield = '12:34' WHERE ROWID = 0", dialect = 'SQLite' )
ds.ReleaseResultSet( sql_lyr )
lyr.ResetReading()
feat = lyr.GetNextFeature()
if feat.GetField('intfield') != 2 or \
- feat.GetField('doublefield') != 3.45 or \
- feat.GetField('strfield') != 'bar' :
+ feat.GetField('doublefield') != 3.45 or \
+ feat.GetField('strfield') != 'bar' or \
+ feat.GetField('datetimefield') != '2012/08/23 21:24:00' or \
+ feat.GetField('datefield') != '2012/08/23' or \
+ feat.GetField('timefield') != '12:34:00':
gdaltest.post_reason('failure')
feat.DumpReadable()
return 'fail'
@@ -103,7 +115,10 @@ def ogr_sql_sqlite_1():
if feat.GetField('intfield') != 2 or \
feat.GetField('nullablefield') is not None or \
feat.GetField('doublefield') != 3.45 or \
- feat.GetField('strfield') != 'bar' :
+ feat.GetField('strfield') != 'bar' or \
+ feat.GetField('datetimefield') != '2012/08/23 21:24:00' or \
+ feat.GetField('datefield') != '2012/08/23' or \
+ feat.GetField('timefield') != '12:34:00':
gdaltest.post_reason('failure')
feat.DumpReadable()
return 'fail'
@@ -361,23 +361,9 @@ int OGR2SQLITE_ConnectCreate(sqlite3* hDB, void *pAux,
osSQL += OGRSQLiteEscapeName(poFieldDefn->GetNameRef());
osSQL += "\"";
osSQL += " ";
- switch( poFieldDefn->GetType() )
- {
- case OFTInteger: osSQL += "INTEGER"; break;
- case OFTReal : osSQL += "DOUBLE"; break;
- case OFTBinary : osSQL += "BLOB"; break;
- case OFTString :
- {
- if( poFieldDefn->GetWidth() > 0 )
- osSQL += CPLSPrintf("VARCHAR(%d)", poFieldDefn->GetWidth());
- else
- osSQL += "VARCHAR";
- break;
- }
- default: osSQL += "VARCHAR"; break;
- }
+ osSQL += OGRSQLiteFieldDefnToSQliteFieldDefn(poFieldDefn);
}
-
+
if( bAddComma )
osSQL += ",";
bAddComma = TRUE;
@@ -792,6 +778,46 @@ int OGR2SQLITE_Column(sqlite3_vtab_cursor* pCursor,
break;
}
+ case OFTDateTime:
+ {
+ int nYear, nMonth, nDay, nHour, nMinute, nSecond, nTZ;
+ poFeature->GetFieldAsDateTime(nCol, &nYear, &nMonth, &nDay,
+ &nHour, &nMinute, &nSecond, &nTZ);
+ char szBuffer[64];
+ sprintf(szBuffer, "%04d-%02d-%02dT%02d:%02d:%02d",
+ nYear, nMonth, nDay, nHour, nMinute, nSecond);
+ sqlite3_result_text(pContext,
+ szBuffer,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+
+ case OFTDate:
+ {
+ int nYear, nMonth, nDay, nHour, nMinute, nSecond, nTZ;
+ poFeature->GetFieldAsDateTime(nCol, &nYear, &nMonth, &nDay,
+ &nHour, &nMinute, &nSecond, &nTZ);
+ char szBuffer[64];
+ sprintf(szBuffer, "%04d-%02d-%02dT", nYear, nMonth, nDay);
+ sqlite3_result_text(pContext,
+ szBuffer,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+
+ case OFTTime:
+ {
+ int nYear, nMonth, nDay, nHour, nMinute, nSecond, nTZ;
+ poFeature->GetFieldAsDateTime(nCol, &nYear, &nMonth, &nDay,
+ &nHour, &nMinute, &nSecond, &nTZ);
+ char szBuffer[64];
+ sprintf(szBuffer, "%02d:%02d:%02d", nHour, nMinute, nSecond);
+ sqlite3_result_text(pContext,
+ szBuffer,
+ -1, SQLITE_TRANSIENT);
+ break;
+ }
+
default:
sqlite3_result_text(pContext,
poFeature->GetFieldAsString(nCol),
@@ -857,16 +883,17 @@ static OGRFeature* OGR2SQLITE_FeatureFromArgs(OGRLayer* poLayer,
int argc,
sqlite3_value **argv)
{
- int nFieldCount = poLayer->GetLayerDefn()->GetFieldCount();
- int bHasGeomField = (poLayer->GetLayerDefn()->GetGeomType() != wkbNone);
+ OGRFeatureDefn* poLayerDefn = poLayer->GetLayerDefn();
+ int nFieldCount = poLayerDefn->GetFieldCount();
+ int bHasGeomField = (poLayerDefn->GetGeomType() != wkbNone);
if( argc != 2 + nFieldCount + 1 + bHasGeomField)
{
CPLDebug("OGR2SQLITE", "Did not get expect argument count : %d, %d", argc,
2 + nFieldCount + 1 + bHasGeomField);
return NULL;
}
- OGRFeature* poFeature = new OGRFeature(poLayer->GetLayerDefn());
+ OGRFeature* poFeature = new OGRFeature(poLayerDefn);
int i;
for(i = 0; i < nFieldCount; i++)
{
@@ -879,8 +906,25 @@ static OGRFeature* OGR2SQLITE_FeatureFromArgs(OGRLayer* poLayer,
poFeature->SetField(i, sqlite3_value_double(argv[2 + i]));
break;
case SQLITE_TEXT:
- poFeature->SetField(i, (const char*) sqlite3_value_text(argv[2 + i]));
+ {
+ const char* pszValue = (const char*) sqlite3_value_text(argv[2 + i]);
+ switch( poLayerDefn->GetFieldDefn(i)->GetType() )
+ {
+ case OFTDate:
+ case OFTTime:
+ case OFTDateTime:
+ {
+ if( !OGRSQLITEStringToDateTimeField( poFeature, i, pszValue ) )
+ poFeature->SetField(i, pszValue);
+ break;
+ }
+
+ default:
+ poFeature->SetField(i, pszValue);
+ break;
+ }
break;
+ }
case SQLITE_BLOB:
{
GByte* paby = (GByte *) sqlite3_value_blob (argv[2 + i]);

0 comments on commit 536a403

Please sign in to comment.