RowFormatter.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // RecordSet.cpp
  3. //
  4. // $Id: //poco/Main/Data/samples/RecordSet/src/RowFormatter.cpp#2 $
  5. //
  6. // This sample demonstrates the Data library recordset formatting
  7. // capabilities.
  8. //
  9. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  10. // All rights reserved.
  11. //
  12. // This is unpublished proprietary source code of Applied Informatics
  13. // Software Engineering GmbH.
  14. // The contents of this file may not be disclosed to third parties,
  15. // copied or duplicated in any form, in whole or in part, without
  16. // prior written permission from Applied Informatics.
  17. //
  18. #include "Poco/SharedPtr.h"
  19. #include "Poco/Data/SessionFactory.h"
  20. #include "Poco/Data/Session.h"
  21. #include "Poco/Data/Statement.h"
  22. #include "Poco/Data/RecordSet.h"
  23. #include "Poco/Data/RowFormatter.h"
  24. #include "Poco/Data/SQLite/Connector.h"
  25. #include <iostream>
  26. using namespace Poco::Data::Keywords;
  27. using Poco::Data::Session;
  28. using Poco::Data::Statement;
  29. using Poco::Data::RecordSet;
  30. using Poco::Data::RowFormatter;
  31. class HTMLTableFormatter : public RowFormatter
  32. {
  33. public:
  34. HTMLTableFormatter()
  35. {
  36. std::ostringstream os;
  37. os << "<TABLE border=\"1\" cellspacing=\"0\">" << std::endl;
  38. setPrefix(os.str());
  39. os.str("");
  40. os << "</TABLE>" << std::endl;
  41. setPostfix(os.str());
  42. }
  43. std::string& formatNames(const NameVecPtr pNames, std::string& formattedNames) const
  44. {
  45. std::ostringstream str;
  46. str << "\t<TR>" << std::endl;
  47. NameVec::const_iterator it = pNames->begin();
  48. NameVec::const_iterator end = pNames->end();
  49. for (; it != end; ++it) str << "\t\t<TH align=\"center\">" << *it << "</TH>" << std::endl;
  50. str << "\t</TR>" << std::endl;
  51. return formattedNames = str.str();
  52. }
  53. std::string& formatValues(const ValueVec& vals, std::string& formattedValues) const
  54. {
  55. std::ostringstream str;
  56. str << "\t<TR>" << std::endl;
  57. ValueVec::const_iterator it = vals.begin();
  58. ValueVec::const_iterator end = vals.end();
  59. for (; it != end; ++it)
  60. {
  61. if (it->isNumeric())
  62. str << "\t\t<TD align=\"right\">";
  63. else
  64. str << "\t\t<TD align=\"left\">";
  65. str << it->convert<std::string>() << "</TD>" << std::endl;
  66. }
  67. str << "\t</TR>" << std::endl;
  68. return formattedValues = str.str();
  69. }
  70. };
  71. int main(int argc, char** argv)
  72. {
  73. // register SQLite connector
  74. Poco::Data::SQLite::Connector::registerConnector();
  75. // create a session
  76. Session session("SQLite", "sample.db");
  77. // drop sample table, if it exists
  78. session << "DROP TABLE IF EXISTS Simpsons", now;
  79. // (re)create table
  80. session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now;
  81. // insert some rows
  82. session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42)", now;
  83. session << "INSERT INTO Simpsons VALUES('Marge Simpson', 'Springfield', 38)", now;
  84. session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12)", now;
  85. session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10)", now;
  86. // create a statement and print the column names and data as HTML table
  87. HTMLTableFormatter tf;
  88. Statement stmt = (session << "SELECT * FROM Simpsons", format(tf), now);
  89. RecordSet rs(stmt);
  90. std::cout << rs << std::endl;
  91. // Note: The code above is divided into individual steps for clarity purpose.
  92. // The four lines can be reduced to the following single line of code:
  93. std::cout << RecordSet(session, "SELECT * FROM Simpsons", new HTMLTableFormatter);
  94. // simple formatting example (uses the default SimpleRowFormatter provided by framework)
  95. std::cout << std::endl << "Simple formatting:" << std::endl << std::endl;
  96. std::cout << RecordSet(session, "SELECT * FROM Simpsons");
  97. return 0;
  98. }