Exemplo n.º 1
0
		ref_ptr<statement> connection::get_prepared_statement(std::string const &q)
		{
			ref_ptr<statement> st;
			if(!cache_.active()) {
				st = prepare_statement(q);
				return st;
			}
			st = cache_.fetch(q);
			if(!st)
				st = prepare_statement(q);
			st->cache(&cache_);
			return st;
		}
		size_t connection::last_insert_id(){
			std::string statement;
			switch(_handle->type){
				case ODBC_Type::MySQL:
					statement = "SELECT LAST_INSERT_ID()"; break;
				case ODBC_Type::TSQL:
					statement = "SELECT SCOPE_IDENTITY"; break;
				case ODBC_Type::SQLite3:
					statement = "SELECT last_insert_rowid()"; break;
				case ODBC_Type::PostgreSQL:
					statement = "SELECT LASTVAL()"; break;
				default:
					throw sqlpp::exception("Can't get last insert id for ODBC_Type "+std::to_string(static_cast<int>(_handle->type)));
			}
			auto prepared_statement = prepare_statement(*_handle, statement);
			execute_statement(prepared_statement->stmt);
			int64_t ret;
			bool is_null;
			bind_result_t result(prepared_statement);
			if(!result.next_impl()){
				std::cerr << "ODBC warning: next_impl failed!" << std::endl;
				return 0;
			}
			result._bind_integral_result(0, &ret, &is_null);
			if(is_null) {
				std::cerr << "ODBC warning: NULL returned from " << statement << std::endl;
				return 0;
			}
			return ret;
		}
		size_t connection::insert_impl(const std::string& statement)
		{
			auto prepared = prepare_statement(*_handle, statement);
			execute_statement(*_handle, prepared);

			return sqlite3_last_insert_rowid(_handle->sqlite);
		}
		bind_result_t connection::select_impl(const std::string& statement) {
			auto prepared = prepare_statement(*_handle, statement);
			if(!prepared || !*prepared) {
				throw sqlpp::exception("ODBC error: Could not store result set");
			}
			execute_statement(prepared->stmt);
			return bind_result_t(prepared);
		}
Exemplo n.º 5
0
	void SQLiteQuery::Reset(const char* query)
	{
		copy_query(query);
		if (stmt) {
			sqlite3_finalize(stmt);
			stmt = NULL;
		}
		prepare_statement();
	}
		void connection::commit_transaction()
		{
			if (not _transaction_active)
			{
				throw sqlpp::exception("Sqlite3 error: Cannot commit a finished or failed transaction");
			}
			_transaction_active = false;
			auto prepared = prepare_statement(*_handle, "COMMIT");
			execute_statement(*_handle, prepared);
		}
		void connection::start_transaction()
		{
			if (_transaction_active)
			{
				throw sqlpp::exception("Sqlite3 error: Cannot have more than one open transaction per connection");
			}
			auto prepared = prepare_statement(*_handle, "BEGIN");
			execute_statement(*_handle, prepared);
			_transaction_active = true;
		}
		void connection::rollback_transaction(bool report)
		{
			if (not _transaction_active)
			{
				throw sqlpp::exception("Sqlite3 error: Cannot rollback a finished or failed transaction");
			}
			if (report)
			{
				std::cerr << "Sqlite3 warning: Rolling back unfinished transaction" << std::endl;
			}
			_transaction_active = false;
			auto prepared = prepare_statement(*_handle, "ROLLBACK");
			execute_statement(*_handle, prepared);
		}
Exemplo n.º 9
0
svn_error_t *
svn_sqlite__read_schema_version(int *version,
                                svn_sqlite__db_t *db,
                                apr_pool_t *scratch_pool)
{
  svn_sqlite__stmt_t *stmt;

  SVN_ERR(prepare_statement(&stmt, db, "PRAGMA user_version;", scratch_pool));
  SVN_ERR(svn_sqlite__step_row(stmt));

  *version = svn_sqlite__column_int(stmt, 0);

  return svn_error_trace(svn_sqlite__finalize(stmt));
}
Exemplo n.º 10
0
int evaluate(shell_state_t *state, evaluation_queue_t *queue,
        const cypher_quick_parse_segment_t *segment)
{
    size_t n;
    const char *s = cypher_quick_parse_segment_get_text(segment, &n);
    if (n == 0)
    {
        return 0;
    }

    struct cypher_input_range range =
            cypher_quick_parse_segment_get_range(segment);

    if (cypher_quick_parse_segment_is_command(segment))
    {
        // drain queue before running commands
        int err = finalize(state, queue, queue->depth);
        if (err)
        {
            return err;
        }
        return evaluate_command(state, s, n, range.start);
    }

    assert(queue->depth <= queue->capacity);
    if ((queue->depth >= queue->capacity) && finalize(state, queue, 1))
    {
        neo4j_perror(state->err, errno, "unexpected error");
        return -1;
    }
    assert(queue->depth < queue->capacity);

    evaluation_continuation_t *continuation =
            prepare_statement(state, s, n, range.start);
    if (continuation == NULL)
    {
        neo4j_perror(state->err, errno, "unexpected error");
        return -1;
    }

    unsigned int i = queue->next + queue->depth;
    if (i >= queue->capacity)
    {
        i -= queue->capacity;
    }
    queue->continuations[i] = continuation;
    ++(queue->depth);
    return 0;
}
Exemplo n.º 11
0
char *
sound_hash(const char *str, int len, enum sound_hash_type type)
{
  sqlite3 *sqldb = get_shared_db();
  sqlite3_stmt *hasher;
  char *utf8, *result = NULL;
  int ulen;
  int status;

  switch (type) {
  case HASH_SOUNDEX:
    /* Classic Penn soundex turns a leading ph into f. This makes
       sense but isn't typical. */
    hasher = prepare_statement(sqldb,
                               "VALUES (soundex(CASE WHEN ?1 LIKE 'ph%' THEN "
                               "printf('f%s', substr(?1, 3)) ELSE ?1 END))",
                               "hash.soundex");
    break;
  case HASH_PHONE:
    hasher =
      prepare_statement(sqldb, "VALUES (spellfix1_phonehash(?))", "hash.phone");
    break;
  default:
    return NULL;
  }

  utf8 = latin1_to_utf8(str, len, &ulen, "string");
  sqlite3_bind_text(hasher, 1, utf8, ulen, free_string);
  status = sqlite3_step(hasher);
  if (status == SQLITE_ROW) {
    result =
      mush_strdup((const char *) sqlite3_column_text(hasher, 0), "string");
  }
  sqlite3_reset(hasher);
  return result;
}
Exemplo n.º 12
0
/* Like svn_sqlite__get_statement but gets an internal statement.

   All internal statements that use this api are executed with step_done(),
   so we don't need the fallback reset handling here or in the pool cleanup */
static svn_error_t *
get_internal_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
                       int stmt_idx)
{
  /* The internal statements are stored after the registered statements */
  int prep_idx = db->nbr_statements + stmt_idx;
  SVN_ERR_ASSERT(stmt_idx < STMT_INTERNAL_LAST);

  if (db->prepared_stmts[prep_idx] == NULL)
    SVN_ERR(prepare_statement(&db->prepared_stmts[prep_idx], db,
                              internal_statements[stmt_idx],
                              db->state_pool));

  *stmt = db->prepared_stmts[prep_idx];

  return SVN_NO_ERROR;
}
Exemplo n.º 13
0
svn_error_t *
svn_sqlite__get_statement(svn_sqlite__stmt_t **stmt, svn_sqlite__db_t *db,
                          int stmt_idx)
{
  SVN_ERR_ASSERT(stmt_idx < db->nbr_statements);

  if (db->prepared_stmts[stmt_idx] == NULL)
    SVN_ERR(prepare_statement(&db->prepared_stmts[stmt_idx], db,
                              db->statement_strings[stmt_idx],
                              db->state_pool));

  *stmt = db->prepared_stmts[stmt_idx];

  if ((*stmt)->needs_reset)
    return svn_error_trace(svn_sqlite__reset(*stmt));

  return SVN_NO_ERROR;
}
Exemplo n.º 14
0
 prepared_statement_t connection::prepare_impl(const std::string& statement,
                                               size_t no_of_parameters,
                                               size_t no_of_columns)
 {
   return prepare_statement(*_handle, statement, no_of_parameters, no_of_columns);
 }
		prepared_statement_t connection::prepare_impl(const std::string& statement)
		{
			return { std::unique_ptr<detail::prepared_statement_handle_t>(new detail::prepared_statement_handle_t(prepare_statement(*_handle, statement))) };
		}
Exemplo n.º 16
0
		ref_ptr<statement> connection::get_prepared_uncached_statement(std::string const &q)
		{
			ref_ptr<statement> st = prepare_statement(q);
			return st;
		}
		size_t connection::remove_impl(const std::string& statement)
		{
			auto prepared = prepare_statement(*_handle, statement);
			execute_statement(*_handle, prepared);
			return sqlite3_changes(_handle->sqlite);
		}
Exemplo n.º 18
0
	//-----------------------------------------------------------------------------------------
	// Class: SQLiteQuery
	// Purpose: Provide an interface for executing prepared queries
	// 
	SQLiteQuery::SQLiteQuery(SQLite& parent, const char* query)
		: SQLiteObject(parent), query(NULL), stmt(NULL)
	{
		copy_query(query);
		prepare_statement();
	}
		bind_result_t connection::select_impl(const std::string& statement)
		{
			std::unique_ptr<detail::prepared_statement_handle_t> prepared(new detail::prepared_statement_handle_t(prepare_statement(*_handle, statement)));
			if (!prepared)
			{
				throw sqlpp::exception("Sqlite3 error: Could not store result set");
			}

			return {std::move(prepared)};
		}
		size_t connection::remove_impl(const std::string& statement) {
			auto prepared = prepare_statement(*_handle, statement);
			execute_statement(prepared->stmt);
			return odbc_affected(prepared->stmt);
		}
		prepared_statement_t connection::prepare_impl(const std::string& statement) {
			return prepared_statement_t(prepare_statement(*_handle, statement));
		}
		size_t connection::insert_impl(const std::string& statement) {
			auto prepared = prepare_statement(*_handle, statement);
			execute_statement(prepared->stmt);
			
			return last_insert_id();
		}
Exemplo n.º 23
0
/**
 * Get internal handle for a prepared statement.  This function should rarely
 * be used, and if, with caution!  On failures during the interaction with
 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'!
 *
 * @param sh prepared statement to introspect
 * @return MySQL statement handle, NULL on error
 */
MYSQL_STMT *
GNUNET_MYSQL_statement_get_stmt (struct GNUNET_MYSQL_StatementHandle *sh)
{
  (void) prepare_statement (sh);
  return sh->statement;
}