Browse Source

[1061] Unify the interface with #1062

The interface of #1062 is trough array of strings, so we do it too to be
a little bit closer to it. We don't pass the count of columns, but say
there should be 4 in the interface (passing array of wrong size is as
easy as passing array of wrong size and providing the correct size).
Michal 'vorner' Vaner 13 years ago
parent
commit
f429202995

+ 8 - 5
src/lib/datasrc/database.cc

@@ -120,8 +120,7 @@ public:
             ready_ = false;
             ready_ = false;
             return (ConstRRsetPtr());
             return (ConstRRsetPtr());
         }
         }
-        string nameStr(name_), rtypeStr(rtype_);
-        int ttl(ttl_);
+        string nameStr(name_), rtypeStr(rtype_), ttl(ttl_);
         Name name(nameStr);
         Name name(nameStr);
         RRType rtype(rtypeStr);
         RRType rtype(rtypeStr);
         RRsetPtr rrset(new RRset(name, class_, rtype, RRTTL(ttl)));
         RRsetPtr rrset(new RRset(name, class_, rtype, RRTTL(ttl)));
@@ -138,7 +137,12 @@ public:
 private:
 private:
     // Load next row of data
     // Load next row of data
     void getData() {
     void getData() {
-        data_ready_ = context_->getNext(name_, rtype_, ttl_, rdata_);
+        string data[4];
+        data_ready_ = context_->getNext(data);
+        name_ = data[0];
+        rtype_ = data[1];
+        ttl_ = data[2];
+        rdata_ = data[3];
     }
     }
     // The context
     // The context
     const DatabaseAbstraction::IteratorContextPtr context_;
     const DatabaseAbstraction::IteratorContextPtr context_;
@@ -147,8 +151,7 @@ private:
     // Status
     // Status
     bool ready_, data_ready_;
     bool ready_, data_ready_;
     // Data of the next row
     // Data of the next row
-    string name_, rtype_, rdata_;
-    int ttl_;
+    string name_, rtype_, rdata_, ttl_;
 };
 };
 
 
 }
 }

+ 4 - 10
src/lib/datasrc/database.h

@@ -106,19 +106,13 @@ public:
          * must not be interlieved with any other RRs (eg. RRsets must be
          * must not be interlieved with any other RRs (eg. RRsets must be
          * "together").
          * "together").
          *
          *
-         * \param name The name of the RR will be returned here.
-         * \param rtype The string representation of RRType will be returned
-         *     through this parameter.
-         * \param ttl The time to live output parameter.
-         * \param data This is where the string representation of data will be
-         *     put.
-         * \return If there was RR returned. Once it returns false, the zone
-         *     was iterated to its end.
+         * \param data The data are to be returned by this parameter. They are
+         *     (in order) the name, rrtype, TTL and the rdata.
+         * \todo Unify with the interface in #1062 eventually.
          * \todo Do we consider databases where it is stored in binary blob
          * \todo Do we consider databases where it is stored in binary blob
          *     format?
          *     format?
          */
          */
-        virtual bool getNext(std::string& name, std::string& rtype, int& ttl,
-                             std::string& data) = 0;
+        virtual bool getNext(std::string data[4]) = 0;
     };
     };
     typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
     typedef boost::shared_ptr<IteratorContext> IteratorContextPtr;
     /**
     /**

+ 8 - 7
src/lib/datasrc/sqlite3_database.cc

@@ -18,6 +18,8 @@
 #include <datasrc/logger.h>
 #include <datasrc/logger.h>
 #include <datasrc/data_source.h>
 #include <datasrc/data_source.h>
 
 
+#include <boost/lexical_cast.hpp>
+
 namespace isc {
 namespace isc {
 namespace datasrc {
 namespace datasrc {
 
 
@@ -345,15 +347,14 @@ public:
                       " to SQL statement (iterate)");
                       " to SQL statement (iterate)");
         }
         }
     }
     }
-    bool getNext(std::string& name, std::string& rtype, int& ttl,
-                 std::string& data)
-    {
+    bool getNext(std::string data[4]) {
         // If there's another row, get it
         // If there's another row, get it
         if (sqlite3_step(statement) == SQLITE_ROW) {
         if (sqlite3_step(statement) == SQLITE_ROW) {
-            name = getstr(sqlite3_column_text(statement, 0));
-            rtype = getstr(sqlite3_column_text(statement, 1));
-            ttl = sqlite3_column_int(statement, 2);
-            data = getstr(sqlite3_column_text(statement, 3));
+            data[0] = getstr(sqlite3_column_text(statement, 0));
+            data[1] = getstr(sqlite3_column_text(statement, 1));
+            data[2] = boost::lexical_cast<std::string>(
+                sqlite3_column_int(statement, 2));
+            data[3] = getstr(sqlite3_column_text(statement, 3));
             return (true);
             return (true);
         }
         }
         return (false);
         return (false);

+ 31 - 35
src/lib/datasrc/tests/database_unittest.cc

@@ -66,40 +66,38 @@ private:
         MockIteratorContext() :
         MockIteratorContext() :
             step(0)
             step(0)
         { }
         { }
-        virtual bool getNext(string& name, string& rtype, int& ttl,
-                             string& data)
-        {
+        virtual bool getNext(string data[4]) {
             switch (step ++) {
             switch (step ++) {
                 case 0:
                 case 0:
-                    name = "example.org";
-                    rtype = "SOA";
-                    ttl = 300;
-                    data = "ns1.example.org. admin.example.org. "
+                    data[0] = "example.org";
+                    data[1] = "SOA";
+                    data[2] = "300";
+                    data[3] = "ns1.example.org. admin.example.org. "
                         "1234 3600 1800 2419200 7200";
                         "1234 3600 1800 2419200 7200";
                     return (true);
                     return (true);
                 case 1:
                 case 1:
-                    name = "x.example.org";
-                    rtype = "A";
-                    ttl = 300;
-                    data = "192.0.2.1";
+                    data[0] = "x.example.org";
+                    data[1] = "A";
+                    data[2] = "300";
+                    data[3] = "192.0.2.1";
                     return (true);
                     return (true);
                 case 2:
                 case 2:
-                    name = "x.example.org";
-                    rtype = "A";
-                    ttl = 300;
-                    data = "192.0.2.2";
+                    data[0] = "x.example.org";
+                    data[1] = "A";
+                    data[2] = "300";
+                    data[3] = "192.0.2.2";
                     return (true);
                     return (true);
                 case 3:
                 case 3:
-                    name = "x.example.org";
-                    rtype = "AAAA";
-                    ttl = 300;
-                    data = "2001:db8::1";
+                    data[0] = "x.example.org";
+                    data[1] = "AAAA";
+                    data[2] = "300";
+                    data[3] = "2001:db8::1";
                     return (true);
                     return (true);
                 case 4:
                 case 4:
-                    name = "x.example.org";
-                    rtype = "AAAA";
-                    ttl = 300;
-                    data = "2001:db8::2";
+                    data[0] = "x.example.org";
+                    data[1] = "AAAA";
+                    data[2] = "300";
+                    data[3] = "2001:db8::2";
                     return (true);
                     return (true);
                 default:
                 default:
                     ADD_FAILURE() <<
                     ADD_FAILURE() <<
@@ -111,7 +109,7 @@ private:
     };
     };
     class EmptyIteratorContext : public IteratorContext {
     class EmptyIteratorContext : public IteratorContext {
     public:
     public:
-        virtual bool getNext(string&, string&, int&, string&) {
+        virtual bool getNext(string[4]) {
             return (false);
             return (false);
         }
         }
     };
     };
@@ -122,21 +120,19 @@ private:
         BadIteratorContext() :
         BadIteratorContext() :
             step(0)
             step(0)
         { }
         { }
-        virtual bool getNext(string& name, string& rtype, int& ttl,
-                             string& data)
-        {
+        virtual bool getNext(string data[4]) {
             switch (step ++) {
             switch (step ++) {
                 case 0:
                 case 0:
-                    name = "x.example.org";
-                    rtype = "A";
-                    ttl = 300;
-                    data = "192.0.2.1";
+                    data[0] = "x.example.org";
+                    data[1] = "A";
+                    data[2] = "300";
+                    data[3] = "192.0.2.1";
                     return (true);
                     return (true);
                 case 1:
                 case 1:
-                    name = "x.example.org";
-                    rtype = "A";
-                    ttl = 301;
-                    data = "192.0.2.2";
+                    data[0] = "x.example.org";
+                    data[1] = "A";
+                    data[2] = "301";
+                    data[3] = "192.0.2.2";
                     return (true);
                     return (true);
                 default:
                 default:
                     ADD_FAILURE() <<
                     ADD_FAILURE() <<

+ 7 - 8
src/lib/datasrc/tests/sqlite3_database_unittest.cc

@@ -111,17 +111,16 @@ TEST_F(SQLite3Conn, iterator) {
     ASSERT_NE(DatabaseAbstraction::IteratorContextPtr(),
     ASSERT_NE(DatabaseAbstraction::IteratorContextPtr(),
               context);
               context);
 
 
-    std::string name, rtype, data;
-    int ttl;
+    std::string data[4];
     // Get and check the first and only record
     // Get and check the first and only record
-    EXPECT_TRUE(context->getNext(name, rtype, ttl, data));
-    EXPECT_EQ("example2.com.", name);
-    EXPECT_EQ("SOA", rtype);
+    EXPECT_TRUE(context->getNext(data));
+    EXPECT_EQ("example2.com.", data[0]);
+    EXPECT_EQ("SOA", data[1]);
     EXPECT_EQ("master.example2.com. admin.example2.com. "
     EXPECT_EQ("master.example2.com. admin.example2.com. "
-              "1234 3600 1800 2419200 7200", data);
-    EXPECT_EQ(3600, ttl);
+              "1234 3600 1800 2419200 7200", data[3]);
+    EXPECT_EQ("3600", data[2]);
     // Check there's no other
     // Check there's no other
-    EXPECT_FALSE(context->getNext(name, rtype, ttl, data));
+    EXPECT_FALSE(context->getNext(data));
 }
 }
 
 
 }
 }