Add Tuple::getType() to check the types of attributes 23/126723/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 25 Apr 2017 03:19:10 +0000 (12:19 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 25 Apr 2017 03:19:10 +0000 (12:19 +0900)
Database uses the type checker to strictly bind the attribute values to SQL queries.

Change-Id: I761cdaef508313de46c70ba4c2bf51d8e168fb72
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/Tuple.h
src/database/Database.cpp
src/shared/Tuple.cpp

index 728207f..bbe8e7c 100644 (file)
 
 namespace ctx {
 
+       enum class DataType {
+               UNKNOWN = 0,
+               INT64 = 1,
+               DOUBLE,
+               STRING
+       };
+
        class EXPORT_API Tuple {
        public:
                Tuple(GVariant* gVar);
@@ -31,6 +38,8 @@ namespace ctx {
 
                unsigned int size();
 
+               DataType getType(unsigned int idx);
+
                bool getAt(unsigned int idx, int64_t* val);
                bool getAt(unsigned int idx, double* val);
                bool getAt(unsigned int idx, std::string* val);
index 0a58e4b..1c29936 100644 (file)
@@ -273,13 +273,18 @@ int Database::Insertions::__bind(Tuple* tuple, int idx)
 
        for (unsigned int i = 0; i < tuple->size(); ++i) {
                ++idx;
-               if (tuple->getAt(i, &str)) {
+               DataType type = tuple->getType(i);
+
+               if (type == DataType::STRING) {
+                       tuple->getAt(i, &str);
                        if (sqlite3_bind_text(__stmt, idx, str, -1, SQLITE_STATIC) != SQLITE_OK)
                                return E_FAILED;
-               } else if (tuple->getAt(i, &i64)) {
+               } else if (type == DataType::INT64) {
+                       tuple->getAt(i, &i64);
                        if (sqlite3_bind_int64(__stmt, idx, i64) != SQLITE_OK)
                                return E_FAILED;
-               } else if (tuple->getAt(i, &dbl)) {
+               } else if (type == DataType::DOUBLE) {
+                       tuple->getAt(i, &dbl);
                        if (sqlite3_bind_double(__stmt, idx, dbl) != SQLITE_OK)
                                return E_FAILED;
                } else {
index 9863126..b1fc004 100644 (file)
@@ -85,6 +85,20 @@ unsigned int Tuple::size()
        return static_cast<unsigned int>(__numElements);
 }
 
+DataType Tuple::getType(unsigned int idx)
+{
+       if (__verify(idx, G_VARIANT_TYPE_INT64))
+               return DataType::INT64;
+
+       if (__verify(idx, G_VARIANT_TYPE_DOUBLE))
+               return DataType::DOUBLE;
+
+       if (__verify(idx, G_VARIANT_TYPE_STRING))
+               return DataType::STRING;
+
+       return DataType::UNKNOWN;
+}
+
 bool Tuple::getAt(unsigned int idx, int64_t* val)
 {
        if (__verify(idx, G_VARIANT_TYPE_INT64)) {