Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCI <=> NAS interoperability changes #9

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gdal/ogr/ogrsf_frmts/nas/nashandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ void NASHandler::endElement(const XMLCh* const uri,
{
int iId = poState->m_poFeature->GetClass()->GetPropertyIndex( "gml_id" );
const GMLProperty *poIdProp = poState->m_poFeature->GetProperty(iId);
#if DEBUG_VERBOSE
#ifdef DEBUG_VERBOSE
char *pszOldGeom = CPLSerializeXMLTree( poState->m_poFeature->GetGeometryList()[0] );

CPLDebug("NAS", "Overwriting other geometry (%s; replace:%s; with:%s)",
Expand Down
15 changes: 0 additions & 15 deletions gdal/ogr/ogrsf_frmts/nas/nasreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,21 +625,6 @@ void NASReader::SetFeaturePropertyDirectly( const char *pszElement,
}
return;
}
#if 0
else if( strcmp(poClass->GetProperty(iProperty)->GetName(),"signaturnummer") == 0 ||
strcmp(poClass->GetProperty(iProperty)->GetName(),"anlass") == 0 )
{
if( isalpha( pszValue[0] ) && isalpha( pszValue[1] ) && isdigit( pszValue[2] ) )
{
poFeature->SetPropertyDirectly( iProperty, CPLStrdup( pszValue+2 ) );
#ifdef DEBUG_VERBOSE
CPLDebug( "NAS", "Skipping two letter prefix of '%s'", pszValue );
#endif
CPLFree(pszValue);
return;
}
}
#endif
else if( strcmp(poClass->GetProperty(iProperty)->GetName(),"kartendarstellung") == 0 ||
strcmp(poClass->GetProperty(iProperty)->GetName(),"rechtsbehelfsverfahren") == 0 )
{
Expand Down
3 changes: 3 additions & 0 deletions gdal/ogr/ogrsf_frmts/oci/ogr_oci.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ class OGROCITableLayer : public OGROCIWritableLayer

// following methods are not base class overrides
int IsValid() { return bValidTable; }

int GetMaxFID();
};

/************************************************************************/
Expand Down Expand Up @@ -516,6 +518,7 @@ class OGROCIDataSource : public OGRDataSource
const char *GetName() { return pszName; }
int GetLayerCount() { return nLayers; }
OGRLayer *GetLayer( int );
OGRLayer *GetLayerByName(const char * pszName);

virtual OGRErr DeleteLayer(int);
virtual OGRLayer *CreateLayer( const char *,
Expand Down
48 changes: 47 additions & 1 deletion gdal/ogr/ogrsf_frmts/oci/ogrocidatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ void OGROCIDataSource::DeleteLayer( const char *pszLayerName )
if( iLayer == nLayers )
{
CPLDebug( "OCI", "DeleteLayer: %s not found in layer list." \
" Layer * not* deleted.", pszLayerName );
" Layer *not* deleted.", pszLayerName );
return;
}

Expand Down Expand Up @@ -964,3 +964,49 @@ int OGROCIDataSource::FetchSRSId( OGRSpatialReference * poSRS )
else
return nSRSId;
}


/************************************************************************/
/* GetLayerByName() */
/************************************************************************/

OGRLayer *OGROCIDataSource::GetLayerByName( const char *pszName )

{
OGROCILayer *poLayer;
int i, count;

if ( !pszName )
return NULL;

count = GetLayerCount();

/* first a case sensitive check */
for( i = 0; i < count; i++ )
{
poLayer = papoLayers[i];

if( strcmp( pszName, poLayer->GetName() ) == 0 )
{
return poLayer;
}
}

char *pszSafeLayerName = CPLStrdup( pszName );
poSession->CleanName( pszSafeLayerName );

/* then case insensitive and laundered */
for( i = 0; i < count; i++ )
{
poLayer = papoLayers[i];

if( EQUAL( pszSafeLayerName, poLayer->GetName() ) )
{
break;
}
}

CPLFree( pszSafeLayerName );

return i < count ? poLayer : NULL;
}
123 changes: 83 additions & 40 deletions gdal/ogr/ogrsf_frmts/oci/ogrocitablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ OGROCITableLayer::OGROCITableLayer( OGROCIDataSource *poDSIn,
bNewLayer = bNewLayerIn;

iNextShapeId = 0;
iNextFIDToWrite = 1;
iNextFIDToWrite = -1;

bValidTable = FALSE;
if( bNewLayerIn )
Expand Down Expand Up @@ -154,6 +154,9 @@ OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition( const char * pszTable )

poDefn->Reference();

CPLString osQuotedTableName;
osQuotedTableName.Printf( "\"%s\"", pszTable );

/* -------------------------------------------------------------------- */
/* Split out the owner if available. */
/* -------------------------------------------------------------------- */
Expand All @@ -175,20 +178,20 @@ OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition( const char * pszTable )
OCIParam *hAttrList = NULL;

nStatus =
OCIDescribeAny( poSession->hSvcCtx, poSession->hError,
(dvoid *) pszTable, strlen(pszTable), OCI_OTYPE_NAME,
OCIDescribeAny( poSession->hSvcCtx, poSession->hError,
(dvoid *) osQuotedTableName.c_str(), osQuotedTableName.length(), OCI_OTYPE_NAME,
OCI_DEFAULT, OCI_PTYPE_TABLE, poSession->hDescribe );
if( poSession->Failed( nStatus, "OCIDescribeAny" ) )
{
CPLErrorReset();
nStatus =
OCIDescribeAny(poSession->hSvcCtx, poSession->hError,
(dvoid *)pszTable, strlen(pszTable), OCI_OTYPE_NAME,
(dvoid *) osQuotedTableName.c_str(), osQuotedTableName.length(), OCI_OTYPE_NAME,
OCI_DEFAULT, OCI_PTYPE_VIEW, poSession->hDescribe );
if( poSession->Failed( nStatus, "OCIDescribeAny" ) )
return poDefn;
}

if( poSession->Failed(
OCIAttrGet( poSession->hDescribe, OCI_HTYPE_DESCRIBE,
&hAttrParam, 0, OCI_ATTR_PARAM, poSession->hError ),
Expand Down Expand Up @@ -264,13 +267,13 @@ OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition( const char * pszTable )
char **papszResult;
int iDim = -1;

oDimCmd.Append( "SELECT COUNT(*) FROM ALL_SDO_GEOM_METADATA u," );
oDimCmd.Append( " TABLE(u.diminfo) t" );
oDimCmd.Append( " WHERE u.table_name = '" );
oDimCmd.Append( osTableName );
oDimCmd.Append( "' AND u.column_name = '" );
oDimCmd.Append( pszGeomName );
oDimCmd.Append( "'" );
oDimCmd.Append( "SELECT COUNT(*) FROM ALL_SDO_GEOM_METADATA u," );
oDimCmd.Append( " TABLE(u.diminfo) t" );
oDimCmd.Append( " WHERE u.table_name = '" );
oDimCmd.Append( osTableName );
oDimCmd.Append( "' AND u.column_name = '" );
oDimCmd.Append( pszGeomName );
oDimCmd.Append( "'" );

oDimStatement.Execute( oDimCmd.GetString() );

Expand All @@ -283,7 +286,7 @@ OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition( const char * pszTable )
char **papszResult2;

CPLErrorReset();

oDimCmd2.Appendf( 1024,
"select m.sdo_index_dims\n"
"from all_sdo_index_metadata m, all_sdo_index_info i\n"
Expand All @@ -302,8 +305,8 @@ OGRFeatureDefn *OGROCITableLayer::ReadTableDefinition( const char * pszTable )
}
else
{
// we want to clear any errors to avoid confusing the application.
CPLErrorReset();
// we want to clear any errors to avoid confusing the application.
CPLErrorReset();
}
}
else
Expand Down Expand Up @@ -770,7 +773,7 @@ OGRErr OGROCITableLayer::CreateFeature( OGRFeature *poFeature )

poFeature->GetGeometryRef()->getEnvelope( &sThisExtent );

if( !sExtent.Contains( sThisExtent ) )
if( !sExtent.Contains( sThisExtent ) )
{
sExtent.Merge( sThisExtent );
bExtentUpdated = true;
Expand Down Expand Up @@ -807,7 +810,7 @@ OGRErr OGROCITableLayer::UnboundCreateFeature( OGRFeature *poFeature )
/* -------------------------------------------------------------------- */
/* Form the INSERT command. */
/* -------------------------------------------------------------------- */
sprintf( pszCommand, "INSERT INTO %s (", poFeatureDefn->GetName() );
sprintf( pszCommand, "INSERT INTO \"%s\"(\"", poFeatureDefn->GetName() );

if( poFeature->GetGeometryRef() != NULL )
{
Expand All @@ -818,7 +821,7 @@ OGRErr OGROCITableLayer::UnboundCreateFeature( OGRFeature *poFeature )
if( pszFIDName != NULL )
{
if( bNeedComma )
strcat( pszCommand, ", " );
strcat( pszCommand, "\",\"" );

strcat( pszCommand, pszFIDName );
bNeedComma = TRUE;
Expand All @@ -833,13 +836,13 @@ OGRErr OGROCITableLayer::UnboundCreateFeature( OGRFeature *poFeature )
if( !bNeedComma )
bNeedComma = TRUE;
else
strcat( pszCommand, ", " );
strcat( pszCommand, "\",\"" );

sprintf( pszCommand + strlen(pszCommand), "\"%s\"",
sprintf( pszCommand + strlen(pszCommand), "%s",
poFeatureDefn->GetFieldDefn(i)->GetNameRef() );
}

strcat( pszCommand, ") VALUES (" );
strcat( pszCommand, "\") VALUES (" );

CPLAssert( strlen(pszCommand) < nCommandBufSize );

Expand Down Expand Up @@ -1337,12 +1340,12 @@ void OGROCITableLayer::UpdateLayerExtents()
" FROM ALL_SDO_GEOM_METADATA m, table(m.diminfo) d"
" where m.table_name = UPPER('%s') and m.COLUMN_NAME = UPPER('%s')",
osTableName.c_str(), pszGeomName );

if( osOwner != "" )
{
oCommand.Appendf(500, " AND OWNER = UPPER('%s')", osOwner.c_str() );
}

oCommand.Append(" ) ");

OGROCISession *poSession = poDS->GetSession();
Expand Down Expand Up @@ -1439,7 +1442,7 @@ void OGROCITableLayer::UpdateLayerExtents()

}
else
{
{
/* -------------------------------------------------------------------- */
/* Prepare dimension update statement. */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -1585,19 +1588,32 @@ int OGROCITableLayer::AllocAndBindForWrite(int eType)
/* Collect the INSERT statement. */
/* -------------------------------------------------------------------- */
OGROCIStringBuf oCmdBuf;
oCmdBuf.Append( "INSERT INTO " );

oCmdBuf.Append( "INSERT INTO \"" );
oCmdBuf.Append( poFeatureDefn->GetName() );
oCmdBuf.Append( "\"(\"" );
oCmdBuf.Append( pszFIDName );

if (eType == wkbNone)
oCmdBuf.Append( " VALUES ( :fid" );
else
oCmdBuf.Append( " VALUES ( :fid, :geometry" );
if (eType != wkbNone)
{
oCmdBuf.Append( "\",\"" );
oCmdBuf.Append( pszGeomName );
}

for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )
{
oCmdBuf.Append( ", " );
oCmdBuf.Append( "\",\"" );
oCmdBuf.Append( poFeatureDefn->GetFieldDefn(i)->GetNameRef() );
}

oCmdBuf.Append( "\") VALUES ( :fid " );

if (eType != wkbNone)
oCmdBuf.Append( ", :geometry" );

for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ )
{
oCmdBuf.Append( ", " );
oCmdBuf.Appendf( 20, " :field_%d", i );
}

Expand Down Expand Up @@ -1760,16 +1776,16 @@ OGRErr OGROCITableLayer::BoundCreateFeature( OGRFeature *poFeature )
if( nWriteCacheMax == 0 )
{
int eType;
if( poFeature->GetGeometryRef() == NULL )
{
eType = wkbNone;
}
else
{
if( poFeature->GetGeometryRef() == NULL )
{
eType = wkbNone;
}
else
{
eType = 1; /* PJH: properly, this should be the gType from the geometry */
/* but the actual value does not matter, so long as it is */
/* not wkbNone */
}
/* but the actual value does not matter, so long as it is */
/* not wkbNone */
}
if( !AllocAndBindForWrite(eType) )
return OGRERR_FAILURE;
}
Expand Down Expand Up @@ -1882,6 +1898,7 @@ OGRErr OGROCITableLayer::BoundCreateFeature( OGRFeature *poFeature )
}
}

psInd->sdo_gtype = OCI_IND_NOTNULL;
OCINumberFromInt( poSession->hError, &nGType,
(uword)sizeof(int), OCI_NUMBER_SIGNED,
&(psGeom->sdo_gtype) );
Expand All @@ -1891,7 +1908,14 @@ OGRErr OGROCITableLayer::BoundCreateFeature( OGRFeature *poFeature )
/* Set the FID. */
/* -------------------------------------------------------------------- */
if( poFeature->GetFID() == OGRNullFID )
{
if( iNextFIDToWrite < 0 )
{
iNextFIDToWrite = GetMaxFID() + 1;
}

poFeature->SetFID( iNextFIDToWrite++ );
}

panWriteFIDs[iCache] = poFeature->GetFID();

Expand Down Expand Up @@ -2075,3 +2099,22 @@ void OGROCITableLayer::CreateSpatialIndex()
}
}
}

int OGROCITableLayer::GetMaxFID()
{
if( pszFIDName == NULL )
return 0;

OGROCIStringBuf sCmd;
OGROCIStatement oSelect( poDS->GetSession() );

sCmd.Appendf( 10000, "SELECT MAX(\"%s\") FROM \"%s\"",
pszFIDName,
poFeatureDefn->GetName()
);

oSelect.Execute( sCmd.GetString() );

char **papszResult = oSelect.SimpleFetchRow();
return CSLCount(papszResult) == 1 ? atoi( papszResult[0] ) : 0;
}