Skip to content
Permalink
Browse files

[oracle] Fix handling of date/time types

Also add test for Oracle default values

On behalf of Faunalia, sponsored by ENEL
  • Loading branch information
nyalldawson committed Jul 7, 2016
1 parent 612043c commit f9d839fac5569d5ceb743643873a08a124ddf101
@@ -159,6 +159,10 @@ QgsOracleProvider::QgsOracleProvider( QString const & uri )
<< QgsVectorDataProvider::NativeType( tr( "Text, fixed length (char)" ), "CHAR", QVariant::String, 1, 255 )
<< QgsVectorDataProvider::NativeType( tr( "Text, limited variable length (varchar2)" ), "VARCHAR2", QVariant::String, 1, 255 )
<< QgsVectorDataProvider::NativeType( tr( "Text, unlimited length (long)" ), "LONG", QVariant::String )

// date type
<< QgsVectorDataProvider::NativeType( tr( "Date" ), "DATE", QVariant::Date, 38, 38, 0, 0 )
<< QgsVectorDataProvider::NativeType( tr( "Date & Time" ), "TIMESTAMP(6)", QVariant::DateTime, 38, 38, 6, 6 )
;

QString key;
@@ -741,7 +745,15 @@ bool QgsOracleProvider::loadFields()
if ( !mIsQuery && !types.contains( field.name() ) )
continue;

mAttributeFields.append( QgsField( field.name(), field.type(), types.value( field.name() ), field.length(), field.precision(), comments.value( field.name() ) ) );
QVariant::Type type = field.type();

if ( types.value( field.name() ) == "DATE" )
{
// date types are incorrectly detected as datetime
type = QVariant::Date;
}

mAttributeFields.append( QgsField( field.name(), type, types.value( field.name() ), field.length(), field.precision(), comments.value( field.name() ) ) );
mDefaultValues.append( defvalues.value( field.name(), QVariant() ) );
}

@@ -2522,6 +2534,11 @@ bool QgsOracleProvider::convertField( QgsField &field )
break;

case QVariant::DateTime:
fieldType = "TIMESTAMP";
fieldPrec = -1;
break;


case QVariant::Time:
case QVariant::String:
fieldType = "VARCHAR2(2047)";
@@ -16,7 +16,7 @@

import os

from qgis.core import QgsVectorLayer, QgsFeatureRequest
from qgis.core import QgsVectorLayer, QgsFeatureRequest, NULL

from qgis.PyQt.QtCore import QSettings, QDate, QTime, QDateTime, QVariant

@@ -77,5 +77,31 @@ def uncompiledFilters(self):
'intersects($geometry,geom_from_wkt( \'Polygon ((-72.2 66.1, -65.2 66.1, -65.2 72.0, -72.2 72.0, -72.2 66.1))\'))'])
return filters

# HERE GO THE PROVIDER SPECIFIC TESTS
def testDateTimeTypes(self):
vl = QgsVectorLayer('%s table="QGIS"."DATE_TIMES" sql=' %
(self.dbconn), "testdatetimes", "oracle")
self.assertTrue(vl.isValid())

fields = vl.dataProvider().fields()
self.assertEqual(fields.at(fields.indexFromName(
'date_field')).type(), QVariant.Date)
self.assertEqual(fields.at(fields.indexFromName(
'datetime_field')).type(), QVariant.DateTime)

f = next(vl.getFeatures(QgsFeatureRequest()))

date_idx = vl.fieldNameIndex('date_field')
self.assertTrue(isinstance(f.attributes()[date_idx], QDate))
self.assertEqual(f.attributes()[date_idx], QDate(2004, 3, 4))
datetime_idx = vl.fieldNameIndex('datetime_field')
self.assertTrue(isinstance(f.attributes()[datetime_idx], QDateTime))
self.assertEqual(f.attributes()[datetime_idx], QDateTime(
QDate(2004, 3, 4), QTime(13, 41, 52)))

def testDefaultValue(self):
self.assertEqual(self.provider.defaultValue(1), NULL)
self.assertEqual(self.provider.defaultValue(2), "'qgis'")

if __name__ == '__main__':
unittest.main()
@@ -24,3 +24,9 @@ INSERT INTO user_sdo_geom_metadata (TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALU

CREATE INDEX some_poly_data_spatial_idx ON QGIS.SOME_POLY_DATA(GEOM) INDEXTYPE IS MDSYS.SPATIAL_INDEX;


CREATE TABLE QGIS.DATE_TIMES ( "id" INTEGER PRIMARY KEY, "date_field" DATE, "datetime_field" TIMESTAMP );

INSERT INTO QGIS.DATE_TIMES ("id", "date_field", "datetime_field" ) VALUES (1, DATE '2004-03-04', TIMESTAMP '2004-03-04 13:41:52' );

COMMIT;

0 comments on commit f9d839f

Please sign in to comment.
You can’t perform that action at this time.