#include <utility>
#include <string>
#include <vector>
+#include <Tuple.h>
#include <ContextTypes.h>
#define COL_INT64 "x"
namespace ctx {
- class Tuple;
-
class EXPORT_API Database {
public:
class Insertions {
bool open();
void close();
- bool execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<Tuple*>* queryResult);
- bool execute(const std::string& query, std::vector<Tuple*>* queryResult);
+ bool execute(const std::string& query, const std::string& columnTypes, std::vector<std::string>* columnNames, std::vector<std::shared_ptr<Tuple>>* queryResult);
+ bool execute(const std::string& query, std::vector<std::shared_ptr<Tuple>>* queryResult);
// insert() does not consume the input tuples
bool insert(const std::string& tableName, const std::string& columns, Tuple* tuple, Tuple* tupleExt = NULL);
- bool insert(const std::string& tableName, const std::string& columns, std::vector<Tuple*>& tuples);
+ bool insert(const std::string& tableName, const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples);
bool insert(Insertions& insertions);
void beginTransaction();
#ifndef __CONTEXT_TUPLE_H__
#define __CONTEXT_TUPLE_H__
+#include <memory>
#include <vector>
#include <string>
#include <ContextTypes.h>
class EXPORT_API Tuple {
public:
+ Tuple(GVariant* gVar);
~Tuple();
unsigned int size();
bool getAt(unsigned int idx, std::string* val);
bool getAt(unsigned int idx, const char** val);
- static Tuple* duplicate(Tuple& tuple);
+ static std::shared_ptr<Tuple> duplicate(std::shared_ptr<Tuple> tuple);
- static GVariant* toGVariant(Tuple* tuple);
- static GVariant* toGVariant(std::vector<Tuple*>& tuples);
+ static GVariant* toGVariant(std::shared_ptr<Tuple> tuple);
+ static GVariant* toGVariant(std::vector<std::shared_ptr<Tuple>>& tuples);
- static std::vector<Tuple*> buildFrom(GVariant* gVar);
+ static std::vector<std::shared_ptr<Tuple>> buildFrom(GVariant* gVar);
private:
- Tuple(GVariant* gVar);
-
bool __parse();
bool __verify(unsigned int idx, const GVariantType* type);
Builder& add(double val);
Builder& add(const std::string& val);
- Tuple* build();
+ std::shared_ptr<Tuple> build();
private:
bool __init();
#include <algorithm>
#include <sqlite3.h>
-#include <Tuple.h>
#include <Database.h>
using namespace ctx;
+using std::shared_ptr;
Database::Database(const std::string& dbPath) :
__dbHandle(NULL),
struct ExecutionCbData {
const std::string* columnTypes;
std::vector<std::string>* columnNames;
- std::vector<Tuple*>* queryResult;
- ExecutionCbData(const std::string* types, std::vector<std::string>* names, std::vector<Tuple*>* result) :
+ std::vector<shared_ptr<Tuple>>* queryResult;
+ ExecutionCbData(const std::string* types, std::vector<std::string>* names, std::vector<shared_ptr<Tuple>>* result) :
columnTypes(types),
columnNames(names),
queryResult(result)
}
}
- Tuple* tuple = builder.build();
- IF_FAIL_RETURN_TAG(tuple, E_NO_MEM, _E, "Memory allocation failed");
+ try {
+ cbData->queryResult->push_back(builder.build());
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
+ return E_FAILED;
+ }
- cbData->queryResult->push_back(tuple);
return E_NONE;
}
bool Database::execute(const std::string& query, const std::string& columnTypes,
- std::vector<std::string>* columnNames, std::vector<Tuple*>* queryResult)
+ std::vector<std::string>* columnNames, std::vector<shared_ptr<Tuple>>* queryResult)
{
IF_FAIL_RETURN_TAG(__dbHandle, false, _E, "Not opened");
_SD("%s", query.c_str());
return true;
}
-bool Database::execute(const std::string& query, std::vector<Tuple*>* result)
+bool Database::execute(const std::string& query, std::vector<shared_ptr<Tuple>>* result)
{
return execute(query, EMPTY_STR, NULL, result);
}
return insertions.__execute();
}
-bool Database::insert(const std::string& tableName, const std::string& columns, std::vector<Tuple*>& tuples)
+bool Database::insert(const std::string& tableName, const std::string& columns, std::vector<shared_ptr<Tuple>>& tuples)
{
Insertions insertions(this, tableName, columns);
for (auto& tuple : tuples) {
- if (!insertions.add(tuple, NULL))
+ if (!insertions.add(tuple.get(), NULL))
return false;
}
return insert(insertions);
#include <Tuple.h>
using namespace ctx;
+using std::string;
+using std::vector;
+using std::shared_ptr;
Tuple::Tuple(GVariant* gVar) :
__gVar(gVar),
return true;
}
-Tuple* Tuple::duplicate(Tuple& tuple)
+shared_ptr<Tuple> Tuple::duplicate(shared_ptr<Tuple> tuple)
{
- gchar* printed = g_variant_print(tuple.__gVar, TRUE);
+ gchar* printed = g_variant_print(tuple->__gVar, TRUE);
GVariant* gv = g_variant_parse(NULL, printed, NULL, NULL, NULL);
g_free(printed);
- return new(std::nothrow) Tuple(gv);
-}
+ try {
+ return std::make_shared<Tuple>(gv);
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
+ g_variant_unref(gv);
+ throw;
+ }
+}
-GVariant* Tuple::toGVariant(Tuple* tuple)
+GVariant* Tuple::toGVariant(shared_ptr<Tuple> tuple)
{
GVariant* gv = tuple->__gVar;
tuple->__gVar = NULL;
- delete tuple;
return gv;
}
-GVariant* Tuple::toGVariant(std::vector<Tuple*>& tuples)
+GVariant* Tuple::toGVariant(vector<shared_ptr<Tuple>>& tuples)
{
IF_FAIL_RETURN_TAG(!tuples.empty(), NULL, _W, "Empty");
return g_variant_builder_end(&builder);
}
-std::vector<Tuple*> Tuple::buildFrom(GVariant* gVar)
+vector<shared_ptr<Tuple>> Tuple::buildFrom(GVariant* gVar)
{
- std::vector<Tuple*> tuples;
- Tuple* tuple = NULL;
+ vector<shared_ptr<Tuple>> tuples;
if (g_variant_is_of_type(gVar, G_VARIANT_TYPE_TUPLE)) {
- tuple = new(std::nothrow) Tuple(gVar);
- if (tuple) {
- tuples.push_back(tuple);
- } else {
- _E("Memory allocation failed");
+ try {
+ tuples.push_back(std::make_shared<Tuple>(gVar));
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
g_variant_unref(gVar);
}
} else if (g_variant_is_of_type(gVar, G_VARIANT_TYPE_ARRAY)) {
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);
+ g_variant_unref(child);
continue;
}
- tuple = new(std::nothrow) Tuple(child);
- if (tuple) {
- tuples.push_back(tuple);
- } else {
- _E("Memory allocation failed");
+ try {
+ tuples.push_back(std::make_shared<Tuple>(child));
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
g_variant_unref(child);
}
}
return *this;
}
-Tuple* Tuple::Builder::build()
+shared_ptr<Tuple> Tuple::Builder::build()
{
- GVariant* gVar = g_variant_builder_end(__gvBuilder);
- IF_FAIL_RETURN_TAG(gVar, NULL, _E, "Memory allocation failed");
-
- Tuple* tuple = new(std::nothrow) Tuple(gVar);
- if (!tuple) {
- _E("Memory allocation failed");
- g_variant_unref(gVar);
- return NULL;
- }
+ if (!__gvBuilder)
+ throw std::runtime_error("Tuple: building failed");
+ GVariant* gVar = g_variant_builder_end(__gvBuilder);
g_variant_builder_unref(__gvBuilder);
__gvBuilder = NULL;
- return tuple;
+ if (!gVar)
+ throw std::runtime_error("Tuple: building failed");
+
+ try {
+ return std::make_shared<Tuple>(gVar);
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
+ g_variant_unref(gVar);
+ throw;
+ }
}