From 1021b792b740fad3c8a324ce1c329808f6a29320 Mon Sep 17 00:00:00 2001 From: aleks-f Date: Wed, 3 Apr 2013 19:19:29 -0500 Subject: [PATCH] change RowFormatter* to RowFormatterPtr Modified all uses of RowFormatter pointer to SharedPtr (RowFormatterPtr) --- Data/include/Poco/Data/RecordSet.h | 24 ++++++++++++++++--- Data/include/Poco/Data/RowFormatter.h | 9 ++++--- Data/include/Poco/Data/Statement.h | 8 +++---- .../samples/RowFormatter/src/RowFormatter.cpp | 14 +++++++---- Data/src/RecordSet.cpp | 6 ++--- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Data/include/Poco/Data/RecordSet.h b/Data/include/Poco/Data/RecordSet.h index 296904276..30ace7a9c 100644 --- a/Data/include/Poco/Data/RecordSet.h +++ b/Data/include/Poco/Data/RecordSet.h @@ -98,21 +98,39 @@ public: static const std::size_t UNKNOWN_TOTAL_ROW_COUNT; explicit RecordSet(const Statement& rStatement, - RowFormatter* pRowFormatter = 0); + RowFormatterPtr pRowFormatter = 0); /// Creates the RecordSet. explicit RecordSet(Session& rSession, const std::string& query, - RowFormatter* pRowFormatter = 0); + RowFormatterPtr pRowFormatter = 0); /// Creates the RecordSet. + explicit RecordSet(Session& rSession, + const std::string& query, + const RowFormatter& rowFormatter); + /// Creates the RecordSet. + + template + RecordSet(Session& rSession, const std::string& query, const RF& rowFormatter): + Statement((rSession << query, now)), + _currentRow(0), + _pBegin(new RowIterator(this, 0 == rowsExtracted())), + _pEnd(new RowIterator(this, true)), + _pFilter(0), + _totalRowCount(UNKNOWN_TOTAL_ROW_COUNT) + /// Creates the RecordSet. + { + setRowFormatter(Keywords::format(rowFormatter)); + } + RecordSet(const RecordSet& other); /// Copy-creates the recordset. ~RecordSet(); /// Destroys the RecordSet. - void setRowFormatter(RowFormatter* pRowFormatter); + void setRowFormatter(RowFormatterPtr pRowFormatter); /// Assigns the row formatter to the statement and all recordset rows. Statement& operator = (const Statement& stmt); diff --git a/Data/include/Poco/Data/RowFormatter.h b/Data/include/Poco/Data/RowFormatter.h index 0e5486dac..87258577f 100644 --- a/Data/include/Poco/Data/RowFormatter.h +++ b/Data/include/Poco/Data/RowFormatter.h @@ -232,13 +232,15 @@ inline void RowFormatter::setMode(Mode mode) } +typedef SharedPtr RowFormatterPtr; + + namespace Keywords { template -inline T* format(const T& formatter) +inline RowFormatterPtr format(const T& formatter) /// Utility function used to pass formatter to the statement. - /// Statement takes the ownership of the formatter. { return new T(formatter); } @@ -247,9 +249,6 @@ inline T* format(const T& formatter) } // namespace Keywords -typedef SharedPtr RowFormatterPtr; - - } } // namespace Poco::Data diff --git a/Data/include/Poco/Data/Statement.h b/Data/include/Poco/Data/Statement.h index d983dda05..3f801d800 100644 --- a/Data/include/Poco/Data/Statement.h +++ b/Data/include/Poco/Data/Statement.h @@ -250,7 +250,7 @@ public: /// /// Set per default to zero to Limit::LIMIT_UNLIMITED, which disables the limit. - Statement& operator , (RowFormatter* pRowFformatter); + Statement& operator , (RowFormatterPtr pRowFformatter); /// Sets the row formatter for the statement. Statement& operator , (const Range& extrRange); @@ -401,7 +401,7 @@ public: /// Returns false if the current data set index points to the last /// data set. Otherwise, it returns true. - void setRowFormatter(RowFormatter* pRowFormatter); + void setRowFormatter(RowFormatterPtr pRowFormatter); /// Sets the row formatter for this statement. /// Statement takes the ownership of the formatter. @@ -545,7 +545,7 @@ inline void Data_API reset(Statement& statement) // inlines // -inline Statement& Statement::operator , (RowFormatter* pRowFformatter) +inline Statement& Statement::operator , (RowFormatterPtr pRowFformatter) { _pRowFormatter = pRowFformatter; return *this; @@ -811,7 +811,7 @@ inline bool Statement::isAsync() const } -inline void Statement::setRowFormatter(RowFormatter* pRowFormatter) +inline void Statement::setRowFormatter(RowFormatterPtr pRowFormatter) { _pRowFormatter = pRowFormatter; } diff --git a/Data/samples/RowFormatter/src/RowFormatter.cpp b/Data/samples/RowFormatter/src/RowFormatter.cpp index 48af049ee..132f0e4ae 100644 --- a/Data/samples/RowFormatter/src/RowFormatter.cpp +++ b/Data/samples/RowFormatter/src/RowFormatter.cpp @@ -49,6 +49,7 @@ using Poco::Data::Session; using Poco::Data::Statement; using Poco::Data::RecordSet; using Poco::Data::RowFormatter; +using Poco::Data::RowFormatterPtr; class HTMLTableFormatter : public RowFormatter @@ -103,15 +104,18 @@ public: int main(int argc, char** argv) { + // register SQLite connector + Poco::Data::SQLite::Connector::registerConnector(); + // create a session Session session("SQLite", "sample.db"); // drop sample table, if it exists session << "DROP TABLE IF EXISTS Simpsons", now; - + // (re)create table session << "CREATE TABLE Simpsons (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3), Birthday DATE)", now; - + // insert some rows DateTime hd(1956, 3, 1); session << "INSERT INTO Simpsons VALUES('Homer Simpson', 'Springfield', 42, ?)", use(hd), now; @@ -121,7 +125,7 @@ int main(int argc, char** argv) session << "INSERT INTO Simpsons VALUES('Bart Simpson', 'Springfield', 12, ?)", use(hd), now; hd.assign(1982, 5, 9); session << "INSERT INTO Simpsons VALUES('Lisa Simpson', 'Springfield', 10, ?)", use(hd), now; - + // create a statement and print the column names and data as HTML table HTMLTableFormatter tf; Statement stmt = (session << "SELECT * FROM Simpsons", format(tf), now); @@ -129,8 +133,8 @@ int main(int argc, char** argv) std::cout << rs << std::endl; // Note: The code above is divided into individual steps for clarity purpose. - // The four lines can be reduced to the following single line of code: - std::cout << RecordSet(session, "SELECT * FROM Simpsons", new HTMLTableFormatter); + // The four lines can be reduced to the following single line: + std::cout << RecordSet(session, "SELECT * FROM Simpsons", HTMLTableFormatter()); // simple formatting example (uses the default SimpleRowFormatter provided by framework) std::cout << std::endl << "Simple formatting:" << std::endl << std::endl; diff --git a/Data/src/RecordSet.cpp b/Data/src/RecordSet.cpp index c0ff05ffc..8b52d1843 100644 --- a/Data/src/RecordSet.cpp +++ b/Data/src/RecordSet.cpp @@ -55,7 +55,7 @@ const std::size_t RecordSet::UNKNOWN_TOTAL_ROW_COUNT = std::numeric_limitssetTotalRowCount(static_cast(getTotalRowCount())); Statement::setRowFormatter(pRowFormatter);