// // 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 "fdoutility.h" using namespace fdoutility; using namespace std; 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=poland@localhost;username=mloskot;password=pantera;"); // // Connect // FdoPtr conn = fdoutility::create_connection(provider); connStr += L"datastore=" + ds; conn->SetConnectionString(connStr.c_str()); FdoConnectionState state = conn->Open(); cout << "Connection state: " << state << endl; // // Create command // FdoPtr cmd; cmd = static_cast(conn->CreateCommand(FdoCommandType_SelectAggregates)); assert(0 != cmd); // // Configure command // cout << "Executing FdoISelectAggregates command...\n"; // 1. Set Feature Class FdoStringP className("FdoPostGIS:"); className += ds.c_str(); className += L"~"; className += L"districts49"; cmd->SetFeatureClassName(static_cast(className)); // 2. Add identifiers for SELECT // Column: region FdoPtr ids = cmd->GetPropertyNames(); FdoPtr id = FdoIdentifier::Create(L"region"); ids->Add(id); // Aggregate function: AVG(pow_ha) FdoPtr argsArea = FdoExpressionCollection::Create(); FdoPtr argArea = FdoIdentifier::Create(L"pow_ha"); argsArea ->Add(argArea); FdoPtr exprAvgArea = FdoFunction::Create(L"AVG", argsArea); FdoPtr idAvgArea = FdoComputedIdentifier::Create(L"AvgArea", exprAvgArea); ids->Add(idAvgArea); // Aggregate function: COUNT(region) FdoPtr argsCount = FdoExpressionCollection::Create(); FdoPtr argCount = FdoIdentifier::Create(L"region"); argsCount->Add(argCount); FdoPtr exprCount = FdoFunction::Create(L"COUNT", argsCount); FdoPtr idCountDistricts = FdoComputedIdentifier::Create(L"CountDistricts", exprCount); ids->Add(idCountDistricts); // Non-function computed identifier: multiplication // NOTE: Using aliases this way is forbidden: AvgArea * CountDistricts //FdoPtr lhs = FdoIdentifier::Create(L"AvgArea"); //FdoPtr rhs = FdoIdentifier::Create(L"CountDistricts"); // NOTE: So, the allowed syntax is: AVG(pow_ha) * COUNT(region) FdoPtr lhs = exprAvgArea; FdoPtr rhs = exprCount; FdoPtr exprStats = FdoBinaryExpression::Create(lhs, FdoBinaryOperations_Multiply, rhs); FdoPtr idStats = FdoComputedIdentifier::Create(L"StatsSum", exprStats); ids->Add(idStats); // 3. Add identifiers for GROUP BY clause FdoPtr grpIds = cmd->GetGrouping(); FdoPtr grpId = FdoIdentifier::Create(L"region"); grpIds->Add(grpId); // 4. Add grouping filter for HAVING clause // Execute command FdoPtr reader = cmd->Execute(); std::cout << " Region \n------------\n"; while (reader->ReadNext()) { for (FdoInt32 i = 0; i < reader->GetPropertyCount(); i++) { FdoStringP name(reader->GetPropertyName(i)); std::wcout << name << L" = "; if (FdoDataType_Int16 == reader->GetDataType(name)) std::cout << reader->GetInt16(name); if (FdoDataType_Int32 == reader->GetDataType(name)) std::cout << reader->GetInt32(name); if (FdoDataType_Int64 == reader->GetDataType(name)) std::cout << reader->GetInt64(name); else if (FdoDataType_Double == reader->GetDataType(name) || FdoDataType_Decimal == reader->GetDataType(name)) std::cout << std::fixed << std::showpoint << std::setprecision(6) << reader->GetDouble(name); else if (FdoDataType_String == reader->GetDataType(name)) std::wcout << reader->GetString(name); std::cout << std::endl; } std::cout << "---------------------------------\n"; } // Close 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; }