// // Copyright (C) 2007 Refractions Research, Inc. // // This library is free software; you can redistribute it and/or // modify it under the terms of version 2.1 of the GNU Lesser // General Public License as published by the Free Software Foundation. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA // #include #include #include #include #include #include #include #include #include "fdoutility.h" using namespace fdoutility; int main(int argc, char* argv[]) { try { std::wstring provider(L"OSGeo.PostGIS.3.2"); // Connection string with datastore specified std::wstring ds(L"public"); std::wstring connStr(L"service=test@localhost;username=mloskot;password=pantera;"); // Create connection FdoPtr conn = fdoutility::create_connection(provider); // Configure connection connStr += L"datastore=" + ds; conn->SetConnectionString(connStr.c_str()); // Open connection FdoConnectionState state = conn->Open(); std::wcout << "Connection state: " << state << std::endl; assert(FdoConnectionState_Open == state); // Create SQL command FdoPtr cmd; cmd = static_cast(conn->CreateCommand(FdoCommandType_SQLCommand)); assert(0 != cmd); // // Run sample DDL statements // FdoInt32 tuples = 0; std::wstring sql; //// Drop table if exists //sql = L"DROP TABLE IF EXISTS city"; //cmd->SetSQLStatement(sql.c_str()); //tuples = cmd->ExecuteNonQuery(); //std::cout << "SQL (" << tuples << ") : " << sql << std::endl; //// Create a normal non-spatial table //sql = L"CREATE TABLE city (id serial, name varchar(60), capital boolean)"; //cmd->SetSQLStatement(sql.c_str()); //tuples = cmd->ExecuteNonQuery(); //std::cout << "SQL (" << tuples << ") : " << sql << std::endl; //// Add a spatial column to the table //sql = L"SELECT AddGeometryColumn('city','geom',4326,'POINT',2)"; //cmd->SetSQLStatement(sql.c_str()); //tuples = cmd->ExecuteNonQuery(); //std::cout << "SQL (" << tuples << ") : " << sql << std::endl; //// //// Insert sample records with spatial data //// // //sql = L"INSERT INTO city (id,name,capital,geom) VALUES (1,'Warsaw',TRUE,GeomFromText('POINT(22.01 52.12)',4326))"; //cmd->SetSQLStatement(sql.c_str()); //tuples = cmd->ExecuteNonQuery(); //std::cout << "SQL (" << tuples << ") : " << sql << std::endl; //sql = L"INSERT INTO city (id,name,capital,geom) VALUES (2,'Poznan',FALSE,GeomFromText('POINT(18.45 53.23)',4326))"; //cmd->SetSQLStatement(sql.c_str()); //tuples = cmd->ExecuteNonQuery(); //std::cout << "SQL (" << tuples << ") : " << sql << std::endl; // // Run sample SELECT statements // // // Input parameters // FdoPtr params = cmd->GetParameterValues(); //// Integer //FdoPtr v1 = FdoDataValue::Create(3); //FdoPtr p1 = FdoParameterValue::Create(L"$2"); //p1->SetValue(v1); //params->Add(p1); //// NULL //FdoPtr v2 = FdoDataValue::Create(FdoDataType_Int32); //FdoPtr p2 = FdoParameterValue::Create(L"$1"); //p2->SetValue(v2); //params->Add(p2); //// Bool //FdoPtr v3 = FdoDataValue::Create(true); //FdoPtr p3 = FdoParameterValue::Create(L"$1"); //p3->SetValue(v3); //params->Add(p3); //// DateTime FdoDateTime dtNew(FdoInt16(2007), 4, 11); FdoPtr v4 = FdoDataValue::Create(dtNew); FdoPtr p4 = FdoParameterValue::Create(L"$1"); p4->SetValue(v4); //params->Add(p4); //sql = L"SELECT id, date1 FROM mytypes WHERE date1 = $1"; sql = L"SELECT date1,timestamp1 FROM mytypes"; cmd->SetSQLStatement(sql.c_str()); FdoPtr reader = cmd->ExecuteReader(); FdoInt32 fields = reader->GetColumnCount(); std::cout << "\nNumber of columns: " << fields << "\n\n"; //for (FdoInt32 i = 0; i < fields; i++) //{ // FdoString* fname = reader->GetColumnName(i); // std::wcout << i << ".\tName: " << fname; // std::wcout << "\n\tType: "; // FdoPropertyType propType = reader->GetPropertyType(fname); // if (FdoPropertyType_DataProperty == propType) // std::wcout << reader->GetColumnType(fname); // else if (FdoPropertyType_GeometricProperty == propType) // std::wcout << "geometry"; // else // std::wcout << "(Unknown)"; // std::wcout << std::endl; //} FdoPtr geomFactory(FdoFgfGeometryFactory::GetInstance()); assert(NULL != geomFactory); FdoDateTime dt; FdoSize listed = 0; while (reader->ReadNext()) { dt = reader->GetDateTime(L"date1"); std::cout << boost::format("%d-%d-%d %d:%d:%f\n") % dt.year % static_cast(dt.month) % static_cast(dt.day) % static_cast(dt.hour) % static_cast(dt.minute) % dt.seconds; dt = reader->GetDateTime(L"timestamp1"); std::cout << boost::format("%d-%d-%d %d:%d:%f\n") % dt.year % static_cast(dt.month) % static_cast(dt.day) % static_cast(dt.hour) % static_cast(dt.minute) % dt.seconds; /* FdoPtr fgf = reader->GetGeometry(L"geom"); FdoPtr geom = geomFactory->CreateGeometryFromFgf(fgf); std::wcout << "geom = " << geom->GetText() << "\n\n"; */ ++listed; } std::cout << "Read " << listed << " tuples\n"; // Close reader and connection (usually optional) reader->Close(); conn->Close(); return 0; } catch (FdoException* ex) { std::wcout << L"*** FDO Error:\n"; int i = 5; FdoException* nex = ex; while (NULL != nex) { const wchar_t* msg = nex->GetExceptionMessage(); if (NULL == msg) { msg = L"NO MESSAGE"; } std::wcout << std::setw(++i) << L"*** " << msg << std::endl; FdoException* p = nex; nex = nex->GetCause(); p->Release(); } } catch(std::exception& ex) { std::cout << "*** Std Error: " << ex.what() << std::endl; } catch(...) { std::cout << "*** Undefined error!\n" << std::endl; } return 0; }