Fix DB insertion issue of mis-counting binding index 91/118891/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 10:44:48 +0000 (19:44 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 10:44:48 +0000 (19:44 +0900)
Change-Id: I8819b9ca9df4b83ff264867e5a0dca4365e77f99
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/database/Database.cpp
src/shared/Tuple.cpp

index 5bec68e..54cdfdb 100644 (file)
@@ -241,6 +241,8 @@ Database::Insertions::Insertions(Database* database, const std::string& tableNam
 
        query.back() = ')';
 
+       _D("%s", query.c_str());
+
        int error = sqlite3_prepare_v2(__dbHandle, query.c_str(), -1, &__stmt, NULL);
        IF_FAIL_VOID_TAG(error == SQLITE_OK, _E, "Preparation failed");
 }
@@ -253,6 +255,7 @@ Database::Insertions::~Insertions()
 bool Database::Insertions::add(Tuple* tuple, Tuple* tupleExt)
 {
        IF_FAIL_RETURN(tuple, false);
+
        __values.push_back(std::make_pair(tuple, tupleExt));
        return true;
 }
@@ -266,6 +269,7 @@ int Database::Insertions::__bind(Tuple* tuple, int idx)
        double dbl = 0;
 
        for (unsigned int i = 0; i < tuple->size(); ++i) {
+               ++idx;
                if (tuple->getAt(i, &str)) {
                        if (sqlite3_bind_text(__stmt, idx, str, -1, SQLITE_STATIC) != SQLITE_OK)
                                return E_FAILED;
@@ -279,7 +283,6 @@ int Database::Insertions::__bind(Tuple* tuple, int idx)
                        _W("Unknown attribute type");
                        return E_FAILED;
                }
-               ++idx;
        }
 
        return idx;
@@ -293,7 +296,6 @@ bool Database::Insertions::__execute()
 
                if (__bind(tupleExt, __bind(tuple, 0)) != __dimension) {
                        _E("Binding failed");
-                       sqlite3_reset(__stmt);
                        return false;
                }
 
@@ -301,6 +303,8 @@ bool Database::Insertions::__execute()
                        _E("Execution failed");
                        return false;
                }
+
+               sqlite3_reset(__stmt);
        }
 
        return true;
index a8e2f16..e668ff3 100644 (file)
@@ -69,10 +69,8 @@ bool Tuple::__verify(unsigned int idx, const GVariantType* type)
        IF_FAIL_RETURN_TAG(__parse(), false, _E, "Parsing failed");
        IF_FAIL_RETURN_TAG(idx < __numElements, false, _W, "Index out of range");
 
-       if (!g_variant_is_of_type(__elements[idx], type)) {
-               _W("Type mismatched");
+       if (!g_variant_is_of_type(__elements[idx], type))
                return false;
-       }
 
        return true;
 }
@@ -162,6 +160,7 @@ std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
                        tuples.push_back(tuple);
                } else {
                        _E("Memory allocation failed");
+                       g_variant_unref(gVar);
                }
        } else if (g_variant_is_of_type(gVar, G_VARIANT_TYPE_ARRAY)) {
                GVariantIter iter;
@@ -170,6 +169,7 @@ std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
                while ((child = g_variant_iter_next_value(&iter))) {
                        if (!g_variant_is_of_type(child, G_VARIANT_TYPE_TUPLE)) {
                                _E("Invalid data");
+                               g_variant_unref(gVar);
                                continue;
                        }
                        tuple = new(std::nothrow) Tuple(child);
@@ -180,11 +180,12 @@ std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
                                g_variant_unref(child);
                        }
                }
+               g_variant_unref(gVar);
        } else {
                _E("Invalid data");
+               g_variant_unref(gVar);
        }
 
-       g_variant_unref(gVar);
        return tuples;
 }