[fdo-internals] RE: [fdo-commits] r4971 - branches/3.4/Providers/PostGIS/Src/Provider

Greg Boone greg.boone at autodesk.com
Mon Sep 28 10:20:00 EDT 2009


Will these changes be submitted to the trunk as well?

-----Original Message-----
From: fdo-commits-bounces at lists.osgeo.org [mailto:fdo-commits-bounces at lists.osgeo.org] On Behalf Of svn_fdo at osgeo.org
Sent: Monday, September 28, 2009 4:46 AM
To: fdo-commits at lists.osgeo.org
Subject: [fdo-commits] r4971 - branches/3.4/Providers/PostGIS/Src/Provider

Author: scroux
Date: 2009-09-28 04:45:46 -0400 (Mon, 28 Sep 2009)
New Revision: 4971

Modified:
   branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.h
   branches/3.4/Providers/PostGIS/Src/Provider/CommandCapabilities.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/Connection.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/Connection.h
   branches/3.4/Providers/PostGIS/Src/Provider/ConnectionCapabilities.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/DescribeSchemaCommand.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/ExpressionProcessor.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/GeometryCapabilities.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/InsertCommand.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.h
   branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.h
   branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.vcproj
   branches/3.4/Providers/PostGIS/Src/Provider/SQLDataReader.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.cpp
   branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.h
Log:
bruno made by bruno


Modified: branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.cpp  2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.cpp  2009-09-28 08:45:46 UTC (rev 4971)
@@ -174,6 +174,8 @@
             case FdoSchemaElementState_Detached:
                 break;
             case FdoSchemaElementState_Modified:
+                AlterTable(classDef);
+                break;
             case FdoSchemaElementState_Unchanged:
                 break;
             }
@@ -260,6 +262,82 @@
     return !hasData;
 }

+void ApplySchemaCommand::AlterTable(FdoPtr<FdoClassDefinition> classDef) const
+{
+    FDOLOG_MARKER("ApplySchemaCommand::-AlterTable");
+
+    int nbPropModified=0;
+    ov::ClassDefinition* phClass;
+    phClass = this->GetClassDefinition(classDef->GetName());
+    if (NULL == phClass)
+    {
+      CreateTable(classDef);
+      return;
+      /*
+        FdoStringP msg = FdoStringP::Format(
+            L"[PostGIS] ApplySchemaCommand: Alter class '%s' error! ClassDefinition not fount!",
+            classDef->GetName());
+        FDOLOG_WRITE("ERROR: %s", static_cast<FdoString*>(msg));
+        throw FdoCommandException::Create(msg);
+        */
+    }
+
+    std::string schemaName(static_cast<char const*>(phClass->GetSchemaName()));
+    std::string tableName (static_cast<char const*>(phClass->GetTableName()));
+
+    std::string sqlAlter("ALTER TABLE ");
+    sqlAlter += tableName;
+
+    FdoPtr<FdoPropertyDefinitionCollection> props(classDef->GetProperties());
+    if (NULL != props && props->GetCount() > 0)
+    {
+        //
+        // Read properties and parse details: name, data type, size.
+        //
+        FdoInt32 const propsSize = props->GetCount();
+        for (FdoInt32 j = 0; j < propsSize; j++)
+        {
+            FdoPtr<FdoPropertyDefinition> propDef(props->GetItem(j));
+            if (FdoPropertyType_DataProperty == propDef->GetPropertyType())
+            {
+                FdoSchemaElementState propState = propDef->GetElementState();
+                FdoStringP propName(propDef->GetName());
+                std::string sqlType(details::PgTypeFromFdoProperty(propDef));
+                if (sqlType.empty())
+                {
+                    throw FdoCommandException::Create(L"ApplySchemaCommand::Execute: Unkown data property type");
+                }
+                switch(propState)
+                {
+                  case FdoSchemaElementState_Added:
+                    if(nbPropModified > 0) sqlAlter += ',';
+                    sqlAlter += " ADD COLUMN ";
+                    sqlAlter += static_cast<char const*>(propName);
+                    sqlAlter += " " + sqlType;
+                    nbPropModified++;
+                    break;
+                  case FdoSchemaElementState_Deleted:
+                    if(nbPropModified > 0) sqlAlter += ',';
+                    sqlAlter += " DROP COLUMN ";
+                    sqlAlter += static_cast<char const*>(propName);
+                    nbPropModified++;
+                    break;
+                }
+
+            }
+        }
+    }
+    if(nbPropModified)
+    {
+      FDOLOG_WRITE("SQL:\n\t%s", sqlAlter.c_str());
+      mConn->PgExecuteCommand(sqlAlter.c_str());
+      //
+      // Update a Logical & Physical mapping of the feature class
+      //
+      mConn->ResetSchema();
+    }
+}
+
 void ApplySchemaCommand::CreateTable(FdoPtr<FdoClassDefinition> classDef) const
 {
     FDOLOG_MARKER("ApplySchemaCommand::-CreateTable");
@@ -407,28 +485,14 @@
             {
                 CreateSequence(tableName, propId);
             }
+            InsertDummyRecord(tableName, propId);
         }

         // TODO: Add class description as a COMMENT

-        // Physical mapping for the feature class
-        if (NULL == phClass) {
-          ov::PhysicalSchemaMapping *schemaMapping=mConn->GetPhysicalSchemaMapping();
-          if (schemaMapping) {
-            ov::ClassCollection::Ptr phClasses(schemaMapping->GetClasses());
-            ov::ClassDefinition::Ptr classDef = ov::ClassDefinition::Create();
-            classDef->SetName(className);
-            classDef->SetSchemaName(FdoStringP(schemaName.c_str()));
-            try {
-              phClasses->Add(classDef);
-            }
-            catch (FdoException* e)
-            {
-              FDOLOG_WRITE("Error Do not append ClassDefinition / Schema Mapping for class");
-              e->Release();
-            }
-          }
-        }
+        // Update Logical & Physical mapping for the feature class
+        //mConn->ResetSchema();
+
     } // if (NULL != props && props->GetCount() > 0)
 }

@@ -557,6 +621,18 @@
     mConn->PgExecuteCommand(sql.c_str());
 }

+void ApplySchemaCommand::InsertDummyRecord(std::string const& table,
+                                        FdoPtr<FdoDataPropertyDefinition> prop) const
+{
+    FDOLOG_MARKER("ApplySchemaCommand::-InsertDummyRecord");
+
+    assert(!table.empty());
+    std::string column(static_cast<char const*>(FdoStringP(prop->GetName()).Lower()));
+
+    std::string sql("INSERT INTO " + table + "(" + column + ") values(0)");
+    mConn->PgExecuteCommand(sql.c_str());
+}
+
 void ApplySchemaCommand::DropTable(FdoPtr<FdoClassDefinition> classDef) const
 {
     assert(NULL != mFeatureSchema);
@@ -633,8 +709,9 @@
     }

     //
-    // TODO: Remove a Physical mapping of the feature class
+    // Update a Logical & Physical mapping of the feature class
     //
+    mConn->ResetSchema();

 }


Modified: branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.h
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.h    2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/ApplySchemaCommand.h    2009-09-28 08:45:46 UTC (rev 4971)
@@ -154,6 +154,10 @@
     //
     void DropTable(FdoPtr<FdoClassDefinition> classDef) const;

+    // This function alter a feature table, support only add and drop column
+    // classDef - definition of feature class
+    //
+    void AlterTable(FdoPtr<FdoClassDefinition> classDef) const;

     // This function registers geometry column in PostGIS meta-schema.
     // It is a simple proxy around SQL command:
@@ -175,6 +179,7 @@
     void CreateSpatialIndex(std::string const& table, FdoPtr<FdoGeometricPropertyDefinition> prop) const;

     void CreateSequence(std::string const& table, FdoPtr<FdoDataPropertyDefinition> prop) const;
+    void InsertDummyRecord(std::string const& table, FdoPtr<FdoDataPropertyDefinition> prop) const;

     ov::ClassDefinition* GetClassDefinition(FdoStringP className) const;


Modified: branches/3.4/Providers/PostGIS/Src/Provider/CommandCapabilities.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/CommandCapabilities.cpp 2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/CommandCapabilities.cpp 2009-09-28 08:45:46 UTC (rev 4971)
@@ -62,7 +62,8 @@

         // Other commands
         FdoCommandType_SQLCommand,
-        FdoCommandType_GetSpatialContexts
+        FdoCommandType_GetSpatialContexts,
+        FdoCommandType_CreateSpatialContext
     };

     size = (sizeof(commands) / sizeof(FdoCommandType));
@@ -72,7 +73,7 @@

 bool CommandCapabilities::SupportsParameters()
 {
-    return false;
+    return true;
 }

 bool CommandCapabilities::SupportsTimeout()
@@ -102,8 +103,7 @@

 bool CommandCapabilities::SupportsSelectGrouping()
 {
-    // TODO: SelectAggregate commands is under construction
-    return false;
+    return true;
 }

 }} // namespace fdo::postgis

Modified: branches/3.4/Providers/PostGIS/Src/Provider/Connection.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/Connection.cpp  2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/Connection.cpp  2009-09-28 08:45:46 UTC (rev 4971)
@@ -37,6 +37,7 @@
 #include "ApplySchemaCommand.h"
 #include "DescribeSchemaCommand.h"
 #include "GetSpatialContextsCommand.h"
+#include "CreateSpatialContextCommand.h"
 #include "InsertCommand.h"
 #include "UpdateCommand.h"
 #include "DeleteCommand.h"
@@ -423,6 +424,9 @@
     case FdoCommandType_GetSpatialContexts:
         cmd = new GetSpatialContextsCommand(this);
         break;
+    case FdoCommandType_CreateSpatialContext:
+        cmd = new CreateSpatialContextCommand(this);
+        break;
     default:
         {
             FDOLOG_WRITE("ERROR: Unsupported command requested.");
@@ -472,6 +476,18 @@
     return sc->GetLogicalSchemas();
 }

+void Connection::ResetSchema()
+{
+    FDOLOG_MARKER("Connection::+ResetSchema");
+
+    if (NULL != mSchemaDesc)
+    {
+      mSchemaDesc->ResetSchema();
+      FDO_SAFE_RELEASE(mSchemaDesc.p);
+    }
+
+}
+
 ov::PhysicalSchemaMapping* Connection::GetPhysicalSchemaMapping()
 {
     FDOLOG_MARKER("Connection::+GetLogicalSchema");
@@ -783,7 +799,7 @@

     if (0 >= mSoftTransactionLevel)
     {
-        FDOLOG_WRITE("No active transaction to commit");
+        FDOLOG_WRITE("No active transaction to rollback");
     }
     else
     {
@@ -821,7 +837,7 @@
 {
     FDOLOG_MARKER("Connection::-DescribeSchema");

-    if (NULL == mSchemaDesc)
+    if (NULL == mSchemaDesc || mSchemaDesc->IsDescribed() == false)
     {
         // TODO: Add support of describing selected schema instead of all

@@ -1095,4 +1111,74 @@
     return schemaName;
 }

+std::string Connection::GetPgNextVal(std::string sequence)
+{
+    FDOLOG_MARKER("Connection::-GetPgNextVal");
+
+    std::string sql("select nextval(\'" + sequence + "\')");
+
+    return PgQueryOneValue(sql);
+
+}
+
+std::string Connection::PgQueryOneValue(std::string sql)
+{
+    FDOLOG_MARKER("Connection::-PgQueryOneValue");
+    boost::shared_ptr<PGresult> pgRes(PgExecuteQuery(sql.c_str()), PQclear);
+    std::string value;
+    value = PQgetvalue(pgRes.get(), 0, 0);
+    FDOLOG_WRITE(L"Value : %s", value.c_str());
+    return value;
+}
+
+bool Connection::GetCoordinateSystemWkt(std::string sridText,std::string& csName,std::string& csWkt)
+{
+    std::string sql("SELECT srtext FROM spatial_ref_sys WHERE srid = " + sridText);
+    boost::shared_ptr<PGresult> pgRes(PgExecuteQuery(sql.c_str()), PQclear);
+    if (PGRES_TUPLES_OK != PQresultStatus(pgRes.get()) || PQntuples(pgRes.get()) < 1)
+    {
+        FDOLOG_WRITE("ERROR: The Spatial Reference System for SRID=%s not found",sridText.c_str());
+        /*
+        FdoStringP tmp = sridText.c_str();
+        FdoStringP msg = FdoStringP::Format(L"The Spatial Reference System for SRID=%s not found.",
+            static_cast<FdoString const*>(tmp));
+        throw FdoException::Create(static_cast<FdoString*>(msg));
+        */
+        return false;
+    }
+
+    int const nfield = PQfnumber(pgRes.get(), "srtext");
+    std::string wkt(PQgetvalue(pgRes.get(), 0, nfield));
+    assert(!wkt.empty());
+    csWkt = wkt;
+
+    // Use substring between first quotes ("") as the SRS name
+    std::string wktName("UNKNOWN");
+    std::string::size_type pos1 = wkt.find_first_of('"') + 1;
+    std::string::size_type pos2 = wkt.find_first_of(',') - 1;
+    if (pos1 != std::string::npos && pos2 != std::string::npos)
+    {
+        wktName = wkt.substr(pos1, pos2 - pos1);
+    }
+    csName = wktName;
+    return true;
+}
+
+bool Connection::GetSrid(std::string csName,std::string& sridText)
+{
+    std::string sql("SELECT srid FROM spatial_ref_sys WHERE srtext like '%" + csName + "%'");
+    boost::shared_ptr<PGresult> pgRes(PgExecuteQuery(sql.c_str()), PQclear);
+    if (PGRES_TUPLES_OK != PQresultStatus(pgRes.get()) || PQntuples(pgRes.get()) < 1)
+    {
+        FDOLOG_WRITE("ERROR: The SRID not found for csName=%s",csName.c_str());
+        return false;
+    }
+
+    int const nfield = PQfnumber(pgRes.get(), "srid");
+    std::string srid(PQgetvalue(pgRes.get(), 0, nfield));
+    assert(!srid.empty());
+    sridText = srid;
+    return true;
+}
+
 }} // namespace fdo::postgis

Modified: branches/3.4/Providers/PostGIS/Src/Provider/Connection.h
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/Connection.h    2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/Connection.h    2009-09-28 08:45:46 UTC (rev 4971)
@@ -158,6 +158,9 @@
     /// \todo To be documented.
     FdoFeatureSchemaCollection* GetLogicalSchema();

+    /// clears all cached schema information, next access will recache.
+    void ResetSchema();
+
     /// \todo To be documented.
     ov::PhysicalSchemaMapping* GetPhysicalSchemaMapping();

@@ -210,6 +213,17 @@
     /// Force unwinding of any active transaction with commit execution.
     void PgFlushSoftTransaction();

+    /// retreive the next value giving a sequence name.
+    std::string GetPgNextVal(std::string sequence);
+    /// Execute a query and return a single value.
+    std::string PgQueryOneValue(std::string sql);
+    ///Return the csName and csWkt associated to the postgis srid number
+    bool GetCoordinateSystemWkt(std::string sridText,std::string& csName,std::string& csWkt);
+    ///try to find a srid number giving a coordsystem name
+    bool GetSrid(std::string csName,std::string& sridText);
+
+
+
 protected:

     /// Destructor.

Modified: branches/3.4/Providers/PostGIS/Src/Provider/ConnectionCapabilities.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/ConnectionCapabilities.cpp      2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/ConnectionCapabilities.cpp      2009-09-28 08:45:46 UTC (rev 4971)
@@ -46,12 +46,14 @@
     // TODO: Verify threading model

     return FdoThreadCapability_PerConnectionThreaded;
+    //return FdoThreadCapability_PerCommandThreaded;
 }

 FdoSpatialContextExtentType* ConnectionCapabilities::GetSpatialContextTypes(FdoInt32& size)
 {
     static FdoSpatialContextExtentType types[] = {
-        FdoSpatialContextExtentType_Static
+        //FdoSpatialContextExtentType_Static
+        FdoSpatialContextExtentType_Dynamic
     };

     size = (sizeof(types) / sizeof(FdoSpatialContextExtentType));
@@ -120,7 +122,7 @@

 bool ConnectionCapabilities::SupportsWrite()
 {
-    return false;
+    return true;
 }

 bool ConnectionCapabilities::SupportsMultiUserWrite()

Modified: branches/3.4/Providers/PostGIS/Src/Provider/DescribeSchemaCommand.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/DescribeSchemaCommand.cpp       2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/DescribeSchemaCommand.cpp       2009-09-28 08:45:46 UTC (rev 4971)
@@ -146,8 +146,18 @@

         FDOLOG_WRITE("Number of schema elements fetched: %d", logicalSchema->GetCount());

-        FDO_SAFE_ADDREF(logicalSchema.p);
-        return logicalSchema.p;
+        // need to create a copy to return beacuse of issue in MapGuide
+        // ( when decribe scchema command is running than MG serialize and temporary removes (and returns back )
+        // class from this schema.
+        // and if multisession's (multhi threading) application is running and if they share this same schema
+        // then it got's exception.
+        // If I create a copy and for every request return copy than it is OK.
+        // and some other callers (FME) may change it and that is nt ok for us then
+
+        FdoFeatureSchemaCollection* ret = FdoCommonSchemaUtil::DeepCopyFdoFeatureSchemas(logicalSchema);
+        return ret;
+        //FDO_SAFE_ADDREF(logicalSchema.p);
+        //return logicalSchema.p;
     }
     catch (FdoException* e)
     {

Modified: branches/3.4/Providers/PostGIS/Src/Provider/ExpressionProcessor.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/ExpressionProcessor.cpp 2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/ExpressionProcessor.cpp 2009-09-28 08:45:46 UTC (rev 4971)
@@ -166,11 +166,23 @@
     }
     else if (0 == name.ICompare(FDO_FUNCTION_SPATIALEXTENTS))
     {
+        /*
         processArgs = false;
         mBuffer.append("GeomFromEwkb(AsEwkb(Extent(");
         FdoPtr<FdoExpression> geomExpr(args->GetItem(0));
         geomExpr->Process(this);
         mBuffer.append(")))");
+        */
+
+        //Coalesce( GeomFromEwkb(AsEwkb(Extent(geometry_1))) , geomfromtext('POLYGON((0 0,0 0,0 0,0 0,0 0))'))
+
+        processArgs = false;
+        mBuffer.append("Coalesce(GeomFromEwkb(AsEwkb(Extent(");
+        FdoPtr<FdoExpression> geomExpr(args->GetItem(0));
+        geomExpr->Process(this);
+        mBuffer.append("))),geomfromtext('POLYGON((0 0,0 50,50 50,50 0,0 0))'))");
+
+
     }
     else if (0 == name.ICompare(FDO_FUNCTION_AREA2D))
     {

Modified: branches/3.4/Providers/PostGIS/Src/Provider/GeometryCapabilities.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/GeometryCapabilities.cpp        2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/GeometryCapabilities.cpp        2009-09-28 08:45:46 UTC (rev 4971)
@@ -89,7 +89,7 @@

 FdoInt32 GeometryCapabilities::GetDimensionalities()
 {
-    return (FdoDimensionality_XY | FdoDimensionality_Z | FdoDimensionality_M);
+    return (FdoDimensionality_XY );//| FdoDimensionality_Z | FdoDimensionality_M);
 }



Modified: branches/3.4/Providers/PostGIS/Src/Provider/InsertCommand.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/InsertCommand.cpp       2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/InsertCommand.cpp       2009-09-28 08:45:46 UTC (rev 4971)
@@ -96,7 +96,12 @@

 FdoBatchParameterValueCollection* InsertCommand::GetBatchParameterValues()
 {
-    return GetCollection(mBatchParameters);
+   if( !mBatchParameters.p )
+   {
+     mBatchParameters = FdoBatchParameterValueCollection::Create();
+   }
+
+   return FDO_SAFE_ADDREF( mBatchParameters.p );
 }

 FdoIFeatureReader* InsertCommand::Execute()
@@ -128,11 +133,23 @@
         throw FdoCommandException::Create(L"[PostGIS] InsertCommand can not find class definition or class mapping!");
     }

+    if( mBatchParameters.p && mBatchParameters->GetCount() != 0 )
+    {
+        // Collect bind parameters
+        details::pgexec_params_t params;
+        Base::PgGenerateExecParams(params);
+
+        // Execute SQL statement
+        //mConn->PgExecuteCommand(sql.c_str(), params, cmdTuples);
+    }

     std::string sep;
     std::string columns;
     std::string values;
     std::string sequence;
+    std::string nextValue;
+    bool pkColumnFound = false;
+    FdoDataType pkDataType = FdoDataType_Int32;

     ExpressionProcessor::Ptr expProc(new ExpressionProcessor());

@@ -146,16 +163,20 @@
     if (1 == propsIdentity->GetCount())
     {
         FdoPtr<FdoDataPropertyDefinition> prop(propsIdentity->GetItem(0));
-        if (prop->GetIsAutoGenerated()
-            && (FdoDataType_Int16 == prop->GetDataType()
-            || FdoDataType_Int32 == prop->GetDataType()
-            || FdoDataType_Int64 == prop->GetDataType()))
+        if (prop->GetIsAutoGenerated())
         {
-            pkColumn = prop->GetName();
+          pkDataType = prop->GetDataType();
+          if(  FdoDataType_Int16 == pkDataType
+            || FdoDataType_Int32 == pkDataType
+            || FdoDataType_Int64 == pkDataType)
+          {
+              pkColumn = prop->GetName();

-            std::string table(static_cast<char const*>(phClass->GetTableName()));
-            std::string column(static_cast<char const*>(pkColumn));
-            sequence = details::MakeSequenceName(table, column);
+              std::string table(static_cast<char const*>(phClass->GetTableName()));
+              std::string column(static_cast<char const*>(pkColumn));
+              sequence = details::MakeSequenceName(table, column);
+              nextValue = mConn->GetPgNextVal(sequence);
+          }
         }
     }

@@ -182,7 +203,8 @@

         if (0 == pkColumn.ICompare(pName))
         {
-            values += sep + "nextval(\'" + sequence + "\')";
+            values += sep + nextValue;
+            pkColumnFound = true;
         }
         else
         {
@@ -239,6 +261,29 @@
         sep = ",";
     }

+    if(pkColumn.GetLength() && !pkColumnFound)
+    {
+      FdoPtr<FdoDataValue> newval;
+      values += sep + nextValue;
+      columns += sep + static_cast<char const*>(pkColumn);;
+
+      switch(pkDataType)
+      {
+        case FdoDataType_Int16:
+          newval = FdoDataValue::Create((FdoInt16) boost::lexical_cast<FdoInt16>(nextValue));
+          break;
+        case FdoDataType_Int32:
+          newval = FdoDataValue::Create((FdoInt32) boost::lexical_cast<FdoInt32>(nextValue));
+          break;
+        case FdoDataType_Int64:
+          newval = FdoDataValue::Create((FdoInt64) boost::lexical_cast<FdoInt64>(nextValue));
+          break;
+      }
+
+      FdoPtr<FdoPropertyValue> propval = FdoPropertyValue::Create(pkColumn,newval);
+      mProperties->Insert(0,propval);
+    }
+
     //
     // Build and execute SQL command
     //

Modified: branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.cpp      2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.cpp      2009-09-28 08:45:46 UTC (rev 4971)
@@ -225,15 +225,18 @@

             // Estimate bounding box of geometries in given column
             FdoPtr<FdoEnvelopeImpl> bbox;
-            bbox = EstimateColumnExtent(static_cast<char const*>(name));
-            if (bbox->GetIsEmpty())
+            if(IsEstimateColumnExtentAvailable(static_cast<char const*>(name)))
             {
+              bbox = EstimateColumnExtent(static_cast<char const*>(name));
+            }
+            else
+            {
               bbox = SelectColumnExtent(static_cast<char const*>(name));
-              if (bbox->GetIsEmpty())
-              {
-                bbox = FdoEnvelopeImpl::Create(0.0, 0.0, 0.0, 0.0);
-              }
             }
+            if (bbox->GetIsEmpty())
+            {
+              bbox = FdoEnvelopeImpl::Create(0.0, 0.0, 0.0, 0.0);
+            }

             // Describe geometry column and add to the collection
             PgGeometryColumn::Ptr col(new PgGeometryColumn(name, type, dim, srid, bbox));
@@ -315,6 +318,47 @@
     }
 }

+bool PgTablesReader::IsEstimateColumnExtentAvailable(
+    std::string const& column) const
+{
+    FDOLOG_MARKER("PgTablesReader::-IsEstimateColumnExtentAvailable");
+    assert(!mCurrentSchema.empty() && !mTableCached.empty());
+
+    if (!mTableSpatialCached) {
+      return false;
+    }
+
+    std::string sql("SELECT count(*) FROM pg_statistic s, pg_class c, pg_attribute a, pg_namespace n WHERE c.relname = '"
+                    + mTableCached + "' AND a.attrelid = c.oid AND a.attname = '"
+                    + column + "' AND n.nspname = '"
+                    + mCurrentSchema + "' AND c.relnamespace = n.oid AND s.starelid=c.oid AND s.staattnum = a.attnum AND staattnum = attnum");
+    try
+    {
+        boost::shared_ptr<PGresult> pgRes(mConn->PgExecuteQuery(sql.c_str()), PQclear);
+        assert(PGRES_TUPLES_OK == PQresultStatus(pgRes.get()));
+        assert(1 == PQntuples(pgRes.get()));
+
+        bool    nullVal = false;
+        char const* cval = NULL;
+        cval = PQgetvalue(pgRes.get(), 0, 0);
+        if (!cval || !strlen(cval)) return false;
+        int nbStats = StringConv<int>(cval);
+        if(nbStats > 0) return true;
+        return false;
+    }
+    catch (FdoException* e)
+    {
+      FDOLOG_WRITE(L"Warning! estimated_extent is empty! use VACUUM or ANALYZE");
+      e->Release();
+      return false;
+    }
+    catch (boost::bad_lexical_cast& e)
+    {
+      FDOLOG_WRITE("Extent coordinate value conversion failed: %s", e.what());
+      throw FdoException::Create(L"Error occured while reading coordinate of estimated extent");
+    }
+}
+
 FdoPtr<FdoEnvelopeImpl> PgTablesReader::EstimateColumnExtent(
     std::string const& column) const
 {
@@ -389,9 +433,13 @@
     }
     catch (FdoException* e)
     {
-      FDOLOG_WRITE(L"Error occured while reading coordinate of estimated extent");
-      throw FdoException::Create(L"Error occured while reading coordinate of estimated extent");
+      FdoPtr<FdoEnvelopeImpl> extent;
+      extent = FdoEnvelopeImpl::Create(); // create empty envelope!
+      FDOLOG_WRITE(L"Warning! estimated_extent is empty! use VACUUM or ANALYZE");
+      //throw FdoException::Create(L"Error occured while reading coordinate of estimated extent");
       e->Release();
+      FDO_SAFE_ADDREF(extent.p);
+      return extent.p;
     }
     catch (boost::bad_lexical_cast& e)
     {
@@ -425,14 +473,12 @@
                      + mCurrentSchema + "."+ mTableCached // TODO add mBaseName
                      + ") AS box");

-    // NOTE: The PgExecuteQuery throws on error, but if no exception occurs,
-    //       valid query result is assumed.
-
-    boost::shared_ptr<PGresult> pgRes(mConn->PgExecuteQuery(sql.c_str()), PQclear);
-    assert(PGRES_TUPLES_OK == PQresultStatus(pgRes.get()));
-    assert(1 == PQntuples(pgRes.get()));
     try
     {
+        boost::shared_ptr<PGresult> pgRes(mConn->PgExecuteQuery(sql.c_str()), PQclear);
+        assert(PGRES_TUPLES_OK == PQresultStatus(pgRes.get()));
+        assert(1 == PQntuples(pgRes.get()));
+
         bool    nullVal = false;
         char const* cval = NULL;

@@ -468,10 +514,19 @@
         FDO_SAFE_ADDREF(extent.p);
         return extent.p;
     }
+    catch (FdoException* e)
+    {
+      FdoPtr<FdoEnvelopeImpl> extent;
+      extent = FdoEnvelopeImpl::Create(); // create empty envelope!
+      FDOLOG_WRITE(L"Warning! SelectColumnExtent failed, table must be empty");
+      FDO_SAFE_ADDREF(extent.p);
+      e->Release();
+      return extent.p;
+    }
     catch (boost::bad_lexical_cast& e)
     {
-        FDOLOG_WRITE("Extent coordinate value conversion failed: %s", e.what());
-        throw FdoException::Create(L"Error occured while select coordinate of extent");
+      FDOLOG_WRITE("Extent coordinate value conversion failed: %s", e.what());
+      throw FdoException::Create(L"Error occured while select coordinate of extent");
     }
 }


Modified: branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.h
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.h        2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/PgTablesReader.h        2009-09-28 08:45:46 UTC (rev 4971)
@@ -131,6 +131,13 @@
     // Validates basic state of the reader instance.
     void ValidateConnectionState() const;

+    // Executes query to test if estimated_extent is available
+    // for the given geometry column in a table.
+    // Table is used with full path: schema.table.
+    bool IsEstimateColumnExtentAvailable(
+        std::string const& column) const;
+
+
     // Executes query to estimate spatial extent of features
     // in given geometry column in a table.
     // Table is used with full path: schema.table.

Modified: branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.h
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.h       2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.h       2009-09-28 08:45:46 UTC (rev 4971)
@@ -101,7 +101,7 @@

 const wchar_t SpatialContextDefaultName[8] = L"Default";
 const wchar_t SpatialContextDefaultDesc[50] = L"Defines default properties of coordinate system.";
-const wchar_t SpatialContextDefaultCoordSysName[8] = L"Unknown";
+const wchar_t SpatialContextDefaultCoordSysName[2] = L"";
 const wchar_t SpatialContextDefaulWkt[2] = L"";
 const double SpatialContextDefaultXYTolerance = 0.05;
 const double SpatialContextDefaultZTolerance = 0.05;

Modified: branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.vcproj
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.vcproj  2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/PostGisProvider.vcproj  2009-09-28 08:45:46 UTC (rev 4971)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="windows-1250"?>
 <VisualStudioProject
        ProjectType="Visual C++"
-       Version="9,00"
+       Version="9.00"
        Name="PostGisProvider"
        ProjectGUID="{81A1E411-2952-42B5-8430-B588A89D9BE2}"
        RootNamespace="Provider"
@@ -18,7 +18,7 @@
        <Configurations>
                <Configuration
                        Name="Debug|Win32"
-                       OutputDirectory="..\..\Bin\Win32\Debug"
+                       OutputDirectory="C:\Program Files\AutoCAD Map 3D 2010\FDO\bin"
                        IntermediateDirectory="..\..\Obj\Win32\Debug\Provider"
                        ConfigurationType="2"
                        InheritedPropertySheets="..\PostGis.vsprops"
@@ -100,7 +100,7 @@
                </Configuration>
                <Configuration
                        Name="Release|Win32"
-                       OutputDirectory="..\..\Bin\Win32\Release"
+                       OutputDirectory="C:\Program Files\AutoCAD Map 3D 2010\FDO\bin"
                        IntermediateDirectory="..\..\Obj\Win32\Release\Provider"
                        ConfigurationType="2"
                        InheritedPropertySheets="..\PostGis.vsprops"
@@ -177,7 +177,7 @@
                        <Tool
                                Name="VCPostBuildEventTool"
                                Description="Versioning PostGISProvider.dll"
-                               CommandLine="$(FDOTHIRDPARTY)\util\stampver\StampVer.exe -v&quot;..\..\..\SubDoc\Providers.version&quot; -f&quot;3.4.0.0%PROVIDERS_VERSION%&quot; $(OutDir)\PostGISProvider.dll"
+                               CommandLine="$(FDOTHIRDPARTY)\util\stampver\StampVer.exe -v&quot;..\..\..\SubDoc\Providers.version&quot; -f&quot;3.4.0.0%PROVIDERS_VERSION%&quot; &quot;$(OutDir)\PostGISProvider.dll&quot;"
                        />
                </Configuration>
        </Configurations>
@@ -446,6 +446,14 @@
                        Name="SpatialContext"
                        >
                        <File
+                               RelativePath=".\CreateSpatialContextCommand.cpp"
+                               >
+                       </File>
+                       <File
+                               RelativePath=".\CreateSpatialContextCommand.h"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\GetSpatialContextsCommand.cpp"
                                >
                        </File>

Modified: branches/3.4/Providers/PostGIS/Src/Provider/SQLDataReader.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/SQLDataReader.cpp       2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/SQLDataReader.cpp       2009-09-28 08:45:46 UTC (rev 4971)
@@ -416,7 +416,9 @@

     try
     {
-      if ((mCurrentTuple + 1 )>= mCursor->GetTuplesCount())
+      FdoSize nbTupples = mCursor->GetTuplesCount();
+      //if(nbTupples == 0) return eof;
+      if ((mCurrentTuple + 1 )>= nbTupples)
       {
           PgCursor::ResultPtr pgRes = mCursor->Fetch(mCoursorPageSize);
           if (PGRES_TUPLES_OK == PQresultStatus(pgRes))

Modified: branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.cpp
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.cpp   2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.cpp   2009-09-28 08:45:46 UTC (rev 4971)
@@ -69,6 +69,15 @@
     return mIsDescribed;
 }

+void SchemaDescription::ResetSchema()
+{
+
+  FDO_SAFE_RELEASE(mLogicalSchemas.p);
+  FDO_SAFE_RELEASE(mSchemaMapping.p);
+  FDO_SAFE_RELEASE(mSpatialContexts.p);
+  mIsDescribed = false;
+}
+
 void SchemaDescription::SetLogicalSchemas(FdoFeatureSchemaCollection* logicalSchemas)
 {
     mLogicalSchemas = logicalSchemas;

Modified: branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.h
===================================================================
--- branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.h     2009-09-28 07:48:00 UTC (rev 4970)
+++ branches/3.4/Providers/PostGIS/Src/Provider/SchemaDescription.h     2009-09-28 08:45:46 UTC (rev 4971)
@@ -53,9 +53,11 @@
     // default schema name: FdoPostGIS.
     //
     void DescribeSchema(Connection* conn, FdoString* schemaName);
-
+
     /// Get flag indicating if schema has been successfully described.
     bool IsDescribed() const;
+
+    void ResetSchema();

     void SetLogicalSchemas(FdoFeatureSchemaCollection* logicalSchemas);
     void SetSchemaMapping(ov::PhysicalSchemaMapping* phSchemaMapping);

_______________________________________________
fdo-commits mailing list
fdo-commits at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/fdo-commits


More information about the fdo-internals mailing list