/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (C) 2014 Regents of the University of California.
 * See COPYING for copyright and distribution information.
 */

#include "sqlite-handle.hpp"

namespace repo {

SqliteHandle::SqliteHandle(const string& dbPath)
  : StorageHandle(STORAGE_METHOD_SQLITE)
{
  if (dbPath.empty()) {
    std::cerr << "Create db file in local location [" << dbPath << "]. " << std::endl
              << "You can assign the path using -d option" << std::endl;
    m_dbPath = string("ndn_repo.db");
  }
  else {
    m_dbPath = dbPath + "/ndn_repo.db";
  }
  initializeRepo();
}

void
SqliteHandle::initializeRepo()
{
  char* errMsg = 0;
  int rc = sqlite3_open(m_dbPath.c_str(), &m_db);
  if (rc == SQLITE_OK) {
    sqlite3_exec(m_db, "CREATE TABLE NDN_REPO ("
                      "name BLOB PRIMARY KEY, "
                      "data BLOB, "
                      "parentName BLOB, "
                      "nChildren INTEGER);\n"
                      "CREATE INDEX NdnRepoparentName ON NDN_REPO (parentName)", 0, 0, &errMsg);
    // Ignore errors (when database already exists, errors are expected)
  }
  else {
    std::cerr << "Database file open failure rc:" << rc << std::endl;
    throw Error("Database file open failure");
  }

  Name rootName;
  string sql = string("SELECT * FROM NDN_REPO WHERE name = ?;");

  sqlite3_stmt* queryStmt = 0;

  rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc == SQLITE_OK) {
    if (sqlite3_bind_blob(queryStmt, 1, rootName.wireEncode().wire(),
                          rootName.wireEncode().size(), 0) == SQLITE_OK) {
      rc = sqlite3_step(queryStmt);
      if (rc == SQLITE_ROW) {
        std::cerr << "root has been created" << std::endl;
      }
      else if (rc == SQLITE_DONE) {
        sqlite3_stmt* p2Stmt = 0;
        sql = string("INSERT INTO NDN_REPO (name, data, parentName, nChildren) "
                     " VALUES (?,    ?,    ?,     ?);");
        rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &p2Stmt, 0);
        if (rc == SQLITE_OK) {
          if (sqlite3_bind_blob(p2Stmt, 1, rootName.wireEncode().wire(),
                                rootName.wireEncode().size(), 0) == SQLITE_OK &&
              sqlite3_bind_null(p2Stmt, 2) == SQLITE_OK &&
              sqlite3_bind_null(p2Stmt, 3) == SQLITE_OK &&
              sqlite3_bind_int(p2Stmt, 4, 0) == SQLITE_OK) {
            rc = sqlite3_step(p2Stmt);;
            if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
              std::cerr << "Root name insert failure rc:" << rc << std::endl;
              sqlite3_finalize(p2Stmt);
              throw Error("Root name insert failure");
            }
          }
          else {
            std::cerr << "bind blob failure rc:" << rc << std::endl;
            sqlite3_finalize(p2Stmt);
            throw Error("bind blob failure");
          }
        }
        else {
          std::cerr << "p2Stmt prepared rc:" << rc << std::endl;
          sqlite3_finalize(p2Stmt);
          throw Error("p2Stmt prepared");
        }
        sqlite3_finalize(p2Stmt);
      }
      else {
        std::cerr << "Database query failure rc:" << rc << std::endl;
        sqlite3_finalize(queryStmt);
        throw Error("Database query failure");
      }
    }
    sqlite3_finalize(queryStmt);
  }
  sqlite3_exec(m_db, "PRAGMA synchronous = OFF", 0, 0, &errMsg);
  sqlite3_exec(m_db, "PRAGMA journal_mode = WAL", 0, 0, &errMsg);
}

SqliteHandle::~SqliteHandle()
{
  sqlite3_close(m_db);
}


//Temporarily assigned the datatype of every component. needs to be further discussed

bool
SqliteHandle::insertData(const Data& data)
{
  Name name = data.getName();

  if (name.empty()) {
    std::cerr << "name is empty" << std::endl;
    return false;
  }

  int rc = 0;

  string updateSql2 = string("UPDATE NDN_REPO SET data = ? WHERE name = ?;");
  //std::cerr << "update" << std::endl;
  sqlite3_stmt* update2Stmt = 0;
  if (sqlite3_prepare_v2(m_db, updateSql2.c_str(), -1, &update2Stmt, 0) != SQLITE_OK) {
    sqlite3_finalize(update2Stmt);
    std::cerr << "update sql2 not prepared" << std::endl;
    throw Error("update sql2 not prepared");
  }
  if (sqlite3_bind_blob(update2Stmt, 1,
                        data.wireEncode().wire(),
                        data.wireEncode().size(), 0) == SQLITE_OK &&
      sqlite3_bind_blob(update2Stmt, 2,
                        name.wireEncode().wire(),
                        name.wireEncode().size(), 0) == SQLITE_OK) {
    rc = sqlite3_step(update2Stmt);
    sqlite3_finalize(update2Stmt);
    if (rc != SQLITE_DONE) {
      return false;
    }
    //what error??
    //std::cerr << "update rc:" << rc << std::endl;
    /// \todo Do something with rc
  }
  int changeCount = sqlite3_changes(m_db);
  //std::cerr << "changeCount: " << changeCount << std::endl;
  if (changeCount > 0) {
    return true;
  }

  sqlite3_stmt* insertStmt = 0;
  sqlite3_stmt* updateStmt = 0;
  string insertSql = string("INSERT INTO NDN_REPO (name, data, parentName, nChildren) "
                            "VALUES (?, ?, ?, ?)");
  string updateSql = string("UPDATE NDN_REPO SET nChildren = nChildren + 1 WHERE name = ?");

  Name rootName;


  if (sqlite3_prepare_v2(m_db, insertSql.c_str(), -1, &insertStmt, 0) != SQLITE_OK) {
    sqlite3_finalize(insertStmt);
    std::cerr << "insert sql not prepared" << std::endl;
  }
  if (sqlite3_prepare_v2(m_db, updateSql.c_str(), -1, &updateStmt, 0) != SQLITE_OK) {
    sqlite3_finalize(updateStmt);
    std::cerr << "update sql not prepared" << std::endl;
    throw Error("update sql not prepared");
  }

  //Insert and read the prefix
  Name parentName = name;
  Name grandName;
  do {
    parentName = parentName.getPrefix(-1);
    if (!hasName(parentName)) {
      grandName = parentName.getPrefix(-1);
      if (sqlite3_bind_blob(insertStmt, 1,
                            parentName.wireEncode().wire(),
                            parentName.wireEncode().size(), 0) == SQLITE_OK &&
          sqlite3_bind_null(insertStmt, 2) == SQLITE_OK &&
          sqlite3_bind_blob(insertStmt, 3,
                            grandName.wireEncode().wire(),
                            grandName.wireEncode().size(), 0) == SQLITE_OK &&
          sqlite3_bind_int(insertStmt, 4, 1) == SQLITE_OK) {
        rc = sqlite3_step(insertStmt);
        if (rc == SQLITE_CONSTRAINT) {
          std::cerr << "Insert parent prefix failed" << std::endl;
          sqlite3_finalize(insertStmt);
          throw Error("Insert parent prefix failed");
        }
        sqlite3_reset(insertStmt);
      }
    }
    else {
      break;
    }
  } while (!parentName.empty());

  //The existed parent nChildren + 1

  if (sqlite3_bind_blob(updateStmt, 1, parentName.wireEncode().wire(),
                        parentName.wireEncode().size(), 0) == SQLITE_OK) {
    rc = sqlite3_step(updateStmt);
    if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
      std::cerr << "update error rc:" << rc << std::endl;
      sqlite3_finalize(updateStmt);
      sqlite3_finalize(insertStmt);
      throw Error("update error");
    }
    sqlite3_reset(updateStmt);
  }

  //Insert the name and the data, if this data name exists update, else insert data

  parentName = name.getPrefix(-1);
  sqlite3_reset(insertStmt);
  if (sqlite3_bind_blob(insertStmt, 1,
                        name.wireEncode().wire(),
                        name.wireEncode().size(), 0) == SQLITE_OK &&
      sqlite3_bind_blob(insertStmt, 2,
                        data.wireEncode().wire(),
                        data.wireEncode().size(), 0) == SQLITE_OK &&
      sqlite3_bind_blob(insertStmt, 3,
                        parentName.wireEncode().wire(),
                        parentName.wireEncode().size(), 0) == SQLITE_OK &&
      sqlite3_bind_int(insertStmt, 4, 0) == SQLITE_OK) {
    rc = sqlite3_step(insertStmt);
    //std::cerr << "insert rc:" << rc << std::endl;
    //std::cerr << "insert the data: " << data.wireEncode().wire() << std::endl;
    if (rc == SQLITE_CONSTRAINT) {
      std::cerr << "The name of the data has existed!" << std::endl;
      sqlite3_finalize(insertStmt);
      return false;
    }
  }

  sqlite3_finalize(updateStmt);
  sqlite3_finalize(insertStmt);
  return true;
}

bool
SqliteHandle::deleteData(const Name& name)
{
  sqlite3_stmt* queryStmt = 0;
  sqlite3_stmt* deleteStmt = 0;
  sqlite3_stmt* updateStmt = 0;
  sqlite3_stmt* update2Stmt = 0;

  string querySql = string("SELECT * from NDN_REPO where name = ?;");
  string deleteSql = string("DELETE from NDN_REPO where name = ?;");

  string updateSql = string("UPDATE NDN_REPO SET nChildren = nChildren - 1 WHERE name = ?;");
  string updateSql2 = string("UPDATE NDN_REPO SET data = NULL WHERE name = ?;");

  int rc = sqlite3_prepare_v2(m_db, querySql.c_str(), -1, &queryStmt, 0);
  Name tmpName = name;
  int nChildren = -1;
  if (sqlite3_prepare_v2(m_db, deleteSql.c_str(), -1, &deleteStmt, 0) != SQLITE_OK) {
    sqlite3_finalize(deleteStmt);
    std::cerr << "delete statement prepared failed" << std::endl;
    throw Error("delete statement prepared failed");
  }
  if (sqlite3_prepare_v2(m_db, updateSql.c_str(), -1, &updateStmt, 0) != SQLITE_OK) {
    sqlite3_finalize(updateStmt);
    std::cerr << "delete update prepared failed" << std::endl;
    throw Error("delete update prepared failed");
  }
  if (rc == SQLITE_OK) {
    if (sqlite3_bind_blob(queryStmt, 1,
                          tmpName.wireEncode().wire(),
                          tmpName.wireEncode().size(), 0) == SQLITE_OK) {
      rc = sqlite3_step(queryStmt);
      if (rc == SQLITE_ROW) {
        nChildren = sqlite3_column_int(queryStmt, 3);
      }
      else {
        std::cerr << "Database query no such name or failure rc:" << rc << std::endl;
        sqlite3_finalize(queryStmt);
        return false;
      }
    }
    if (nChildren > 0) {
      //update internal node, so just update and return
      if (sqlite3_prepare_v2(m_db, updateSql2.c_str(), -1, &update2Stmt, 0) != SQLITE_OK) {
        sqlite3_finalize(update2Stmt);
        std::cerr << "delete update prepared failed" << std::endl;
        throw Error("delete update prepared failed");
      }
      if (sqlite3_bind_blob(update2Stmt, 1,
                            tmpName.wireEncode().wire(),
                            tmpName.wireEncode().size(), 0) == SQLITE_OK) {
        rc = sqlite3_step(update2Stmt);
        std::cerr << "deleteData update" << std::endl;
      }
      else {
        std::cerr << "delete bind error" << std::endl;
        sqlite3_finalize(update2Stmt);
        throw Error("delete bind error");
      }
      return true;
    }
    else {
      //Delete the leaf node
      if (sqlite3_bind_blob(deleteStmt, 1,
                            tmpName.wireEncode().wire(),
                            tmpName.wireEncode().size(), 0) == SQLITE_OK) {
        rc = sqlite3_step(deleteStmt);
        if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
          std::cerr << "leaf node delete error rc:" << rc << std::endl;
          sqlite3_finalize(deleteStmt);
          throw Error("leaf node delete error");
        }
      }
      else {
        std::cerr << "delete bind error" << std::endl;
        sqlite3_finalize(deleteStmt);
        throw Error("delete bind error");
      }
      sqlite3_reset(deleteStmt);
    }
    queryStmt = 0;
    rc = sqlite3_prepare_v2(m_db, querySql.c_str(), -1, &queryStmt, 0);
    if (rc != SQLITE_OK) {
      std::cerr << "prepare error" << std::endl;
      sqlite3_finalize(queryStmt);
      throw Error("prepare error");
    }
    //read prefix if nChildren is 0 and data is 0
    int dataSize = 0;
    do {
      tmpName = tmpName.getPrefix(-1);
      if (sqlite3_bind_blob(queryStmt, 1,
                            tmpName.wireEncode().wire(),
                            tmpName.wireEncode().size(), 0) == SQLITE_OK) {
        rc = sqlite3_step(queryStmt);
        if (rc == SQLITE_ROW) {
          nChildren = sqlite3_column_int(queryStmt, 3);
          dataSize = sqlite3_column_bytes(queryStmt, 1);
        }
        else {
          std::cerr << "Database query no such name or failure rc:" << rc << std::endl;
          sqlite3_finalize(queryStmt);
          return false;
        }
        if (nChildren == 1 && !tmpName.empty() && dataSize == 0) {
          //Delete this internal node
          if (sqlite3_bind_blob(deleteStmt, 1,
                                tmpName.wireEncode().wire(),
                                tmpName.wireEncode().size(), 0) == SQLITE_OK) {
            rc = sqlite3_step(deleteStmt);
            if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
              std::cerr << "internal node delete error rc:" << rc << std::endl;
              sqlite3_finalize(deleteStmt);
              throw Error("internal node delete error");
            }
          }
          else {
            std::cerr << "delete bind error" << std::endl;
            sqlite3_finalize(deleteStmt);
            throw Error("delete bind error");
          }
          sqlite3_reset(deleteStmt);
        }
        else {
          //nChildren - 1
          if (sqlite3_bind_blob(updateStmt, 1,
                                tmpName.wireEncode().wire(),
                                tmpName.wireEncode().size(), 0) == SQLITE_OK) {
            rc = sqlite3_step(updateStmt);
            if (rc != SQLITE_DONE && rc !=SQLITE_ROW) {
              std::cerr << "internal node nChildren update error rc:" << rc << std::endl;
              sqlite3_finalize(updateStmt);
              throw Error("internal node nChildren update error");
            }
          }
          else {
            std::cerr << "update bind error" << std::endl;
            sqlite3_finalize(updateStmt);
            throw Error("update bind error");
          }
          sqlite3_reset(updateStmt);
          break;
        }
      }
      else {
        std::cerr << "query bind error" << std::endl;
        sqlite3_finalize(queryStmt);
        throw Error("query bind error");
      }
      sqlite3_reset(queryStmt);
    } while (!tmpName.empty());

  }
  else {
    std::cerr << "query prepared failure rc:" << rc << std::endl;
    sqlite3_finalize(queryStmt);
    throw Error("query prepared failure");
  }
  return true;
}

bool
SqliteHandle::readData(const Interest& interest, Data& data)
{
  vector<Name> names;
  Name resultName;
  if (!interest.hasSelectors()) {
    return readDataPlain(interest.getName(), data);
  }
  else {
    if (readNameSelector(interest, names)) {
      if (!filterNameChild(interest.getName(), interest.getChildSelector(), names, resultName)) {
        return false;
      }
    }
    return readData(resultName, data);
  }
}

// This function is the first version of data read following longest prefix match.
// It will return the leftmost data
bool
SqliteHandle::readDataPlain(const Name& name, Data& data)
{
  vector<Name> names;
  Name resultName;
  readDataName(name, names);
  filterNameChild(name, 0, names, resultName);
  if (!resultName.empty()) {
    return readData(resultName, data);
  }
  else
  {
    return false;
  }
}

// retrieve all the leaf nodes of a subtree
bool
SqliteHandle::readDataName(const Name& name, vector<Name>& names) const
{
  if (name.empty()) {
    std::cerr << "The name is empty" << std::endl;
    return false;
  }
  Name tmpName = name;
  //This queue is for internal node;
  queue<Name> internalNames;

  // Step 1. Check if the requested name corresponds to a leaf (data is not NULL)
  string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
  sqlite3_stmt* queryStmt = 0;
  int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc != SQLITE_OK)
    throw Error("prepare error");

  if (sqlite3_bind_blob(queryStmt, 1,
                        tmpName.wireEncode().wire(),
                        tmpName.wireEncode().size(), 0) == SQLITE_OK) {
    rc = sqlite3_step(queryStmt);
    if (rc == SQLITE_ROW) {
      int nChildren = sqlite3_column_int(queryStmt, 3);
      Name elementName;
      elementName.wireDecode(Block(sqlite3_column_blob(queryStmt, 0),
                                   sqlite3_column_bytes(queryStmt, 0)));
      names.push_back(elementName);
      if (nChildren == 0) {
        sqlite3_finalize(queryStmt);
        return true;
      }
    }
    else if (rc == SQLITE_DONE) {
      // ignore
    }
    else {
      std::cerr << "read error rc:" << rc << std::endl;
      sqlite3_finalize(queryStmt);
      throw Error("read error");
    }
  }
  sqlite3_finalize(queryStmt);


  // Step 2. Recursively find all data packets with the specified prefix
  string psql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
  sqlite3_stmt* queryParentStmt = 0;
  rc = sqlite3_prepare_v2(m_db, psql.c_str(), -1, &queryParentStmt, 0);
  if (rc != SQLITE_OK)
    throw Error("prepare error");

  internalNames.push(tmpName);
  while (!internalNames.empty()) {
    tmpName = internalNames.front();
    internalNames.pop();
    if (sqlite3_bind_blob(queryParentStmt, 1,
                          tmpName.wireEncode().wire(),
                          tmpName.wireEncode().size(), 0) == SQLITE_OK) {
      while (true) {
        rc = sqlite3_step(queryParentStmt);
        if (rc == SQLITE_ROW) {
          Name elementName;
          elementName.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 0),
                                       sqlite3_column_bytes(queryParentStmt, 0)));
          int nChildren = sqlite3_column_int(queryParentStmt, 3);
          if (nChildren > 0) {
            internalNames.push(elementName);
          }
          if (sqlite3_column_type(queryParentStmt, 3) != SQLITE_NULL) {
            names.push_back(elementName);
          }
        }
        else if (rc == SQLITE_DONE) {
          break;
        }
        else {
          std::cerr << "read error rc:" << rc << std::endl;
          sqlite3_finalize(queryParentStmt);
          throw Error("read error");
        }
      }
      sqlite3_reset(queryParentStmt);
    }
    else {
      std::cerr << "bind error" << std::endl;
      sqlite3_finalize(queryParentStmt);
      throw Error("bind error");
    }
  }
  sqlite3_finalize(queryParentStmt);
  return true;
}

bool
SqliteHandle::readNameSelector(const Interest& interest, vector<Name>& names) const
{
  if (interest.getName().empty()) {
    std::cerr << "The name of interest is empty" << std::endl;
    return false;
  }
  Name tmpName = interest.getName();
  //This queue is for internal node;
  queue<Name> internalNames;

  // Step 1. Check if the requested Data corresponds to a leaf (data is not NULL)
  sqlite3_stmt* queryStmt = 0;
  string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
  int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc != SQLITE_OK)
    throw Error("prepare error");

  if (sqlite3_bind_blob(queryStmt, 1,
                        tmpName.wireEncode().wire(),
                        tmpName.wireEncode().size(), 0) == SQLITE_OK) {
    rc = sqlite3_step(queryStmt);
    if (rc == SQLITE_ROW) {
      Data elementData;
      elementData.wireDecode(Block(sqlite3_column_blob(queryStmt, 1),
                                   sqlite3_column_bytes(queryStmt, 1)));
      if (interest.matchesData(elementData)) {
        names.push_back(elementData.getName());
      }

      int nChildren = sqlite3_column_int(queryStmt, 3);
      if (nChildren == 0) {
        sqlite3_finalize(queryStmt);
        return true;
      }
    }
    else if (rc == SQLITE_DONE) {
      // ignore
    }
    else {
      std::cerr << "read error rc:" << rc << std::endl;
      sqlite3_finalize(queryStmt);
      throw Error("read error");
    }
  }
  sqlite3_finalize(queryStmt);

  // Step 2. Recursively find all data packets that match the Interest
  internalNames.push(tmpName);
  sqlite3_stmt* queryParentStmt = 0;
  string psql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
  rc = sqlite3_prepare_v2(m_db, psql.c_str(), -1, &queryParentStmt, 0);
  if (rc != SQLITE_OK)
    throw Error("prepare error");

  while (!internalNames.empty()) {
    tmpName = internalNames.front();
    internalNames.pop();
    if (sqlite3_bind_blob(queryParentStmt, 1,
                          tmpName.wireEncode().wire(),
                          tmpName.wireEncode().size(), 0) == SQLITE_OK) {
      while (true) {
        rc = sqlite3_step(queryParentStmt);
        if (rc == SQLITE_ROW) {
          if (sqlite3_column_type(queryParentStmt, 1) != SQLITE_NULL) {
            Data elementData;
            elementData.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 1),
                                         sqlite3_column_bytes(queryParentStmt, 1)));
            if (interest.matchesData(elementData)) {
              names.push_back(elementData.getName());
            }
          }

          Name elementName;
          elementName.wireDecode(Block(sqlite3_column_blob(queryParentStmt, 0),
                                       sqlite3_column_bytes(queryParentStmt, 0)));

          int nChildren = sqlite3_column_int(queryParentStmt, 3);
          if (nChildren > 0) {
            internalNames.push(elementName);
          }
        }
        else if (rc == SQLITE_DONE) {
          break;
        }
        else {
          std::cerr << "read error rc:" << rc << std::endl;
          sqlite3_finalize(queryParentStmt);
          throw Error("read error");
        }
      }
      sqlite3_reset(queryParentStmt);
    }
    else {
      std::cerr << "bind error" << std::endl;
      sqlite3_finalize(queryParentStmt);
      throw Error("bind error");
    }
  }
  sqlite3_finalize(queryParentStmt);
  return true;
}

bool
SqliteHandle::filterNameChild(const Name& name, int childSelector,
                              const vector<Name>& names, Name& resultName)
{
  if (childSelector == 0) {
    if (!names.empty()) {
      resultName = *std::min_element(names.begin(), names.end());
    }
    else {
      return false;
    }
  }
  else if (childSelector == 1) {
    if (!names.empty()) {
      resultName = *std::max_element(names.begin(), names.end());
    }
    else {
      return false;
    }
  }
  else {
    return false;
  }
  return true;
}

bool
SqliteHandle::readNameAny(const Name& name, const Selectors& selectors, vector<Name>& names)
{
  if (selectors.empty()) {
    if (hasName(name)) {
      names.push_back(name);
    }
    return true;
  }
  else {
    Interest interest(name);
    interest.setSelectors(selectors);
    readNameSelector(interest, names);
    if (selectors.getChildSelector() >= 0) {
      Name resultName;
      if (!filterNameChild(name, selectors.getChildSelector(), names, resultName))
        return false;
      names.clear();
      names.push_back(resultName);
      return true;
    }
    else {
      return true;
    }
  }
}

bool
SqliteHandle::readData(const Name& name, Data& data)
{
  sqlite3_stmt* queryStmt = 0;
  string sql = string("SELECT * FROM NDN_REPO WHERE name = ? AND data IS NOT NULL;");
  int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc == SQLITE_OK) {
    if (sqlite3_bind_blob(queryStmt, 1,
                          name.wireEncode().wire(),
                          name.wireEncode().size(), 0) == SQLITE_OK) {
      rc = sqlite3_step(queryStmt);
      if (rc == SQLITE_ROW) {
        data.wireDecode(Block(sqlite3_column_blob(queryStmt, 1),
                              sqlite3_column_bytes(queryStmt, 1)));
        sqlite3_finalize(queryStmt);
        return true;
      }
      else if (rc == SQLITE_DONE) {
        return false;
      }
      else {
        std::cerr << "Database query failure rc:" << rc << std::endl;
        sqlite3_finalize(queryStmt);
        throw Error("Database query failure");
      }
    }
    sqlite3_finalize(queryStmt);
  }
  return true;
}


//This is the exact name query in database.
bool
SqliteHandle::hasName(const Name& name)
{
  sqlite3_stmt* queryStmt = 0;
  string sql = string("select * from NDN_REPO where name = ?;");
  int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc == SQLITE_OK) {
    if (sqlite3_bind_blob(queryStmt, 1,
                          name.wireEncode().wire(),
                          name.wireEncode().size(), 0) == SQLITE_OK) {
      rc = sqlite3_step(queryStmt);
      if (rc == SQLITE_ROW) {
        sqlite3_finalize(queryStmt);
        return true;
      }
      else if (rc == SQLITE_DONE) {
        sqlite3_finalize(queryStmt);
        return false;
      }
      else {
        std::cerr << "Database query failure rc:" << rc << std::endl;
        sqlite3_finalize(queryStmt);
        return false;
      }
    }
    sqlite3_finalize(queryStmt);
  }
  return true;
}

//This is the exact parent name query in database.
bool
SqliteHandle::hasParentName(const Name& parentName) const
{
  sqlite3_stmt* queryStmt = 0;
  string sql = string("SELECT * FROM NDN_REPO WHERE parentName = ?;");
  int rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &queryStmt, 0);
  if (rc == SQLITE_OK) {
    if (sqlite3_bind_blob(queryStmt, 1,
                          parentName.wireEncode().wire(),
                          parentName.wireEncode().size(), 0) == SQLITE_OK) {
      rc = sqlite3_step(queryStmt);
      if (rc == SQLITE_ROW) {
        sqlite3_finalize(queryStmt);
        return true;
      }
      else if (rc == SQLITE_DONE) {
        sqlite3_finalize(queryStmt);
        return false;
      }
      else {
        std::cerr << "Database query failure rc:" << rc << std::endl;
        sqlite3_finalize(queryStmt);
        return false;
      }
    }
    sqlite3_finalize(queryStmt);
  }
  return true;
}

} //namespace repo
