Exemple #1
0
// Gracefully stop the app
void terminate(int exitcode) {
    if(opts.db != 0) {
        if(opts.verbose) printf("Closing DB connection\n");
        sqlite3_close_v2(opts.db);
    }
    exit(exitcode);
}
static pse_op_error_t sqlite_open_db(sqlite3 **db)
{
    int rc = 0;
    pse_op_error_t ret = OP_SUCCESS;
    char vmc_db_path[MAX_PATH] = {0};

    pse_vmc_db_state = PSE_VMC_DB_STATE_WORKABLE;

    if(aesm_get_cpathname(FT_PERSISTENT_STORAGE, VMC_DATABASE_FID, vmc_db_path, MAX_PATH) != AE_SUCCESS) 
    {
        *db = NULL;
        return OP_ERROR_INTERNAL;
    }

    rc = sqlite3_open_v2(vmc_db_path, db, SQLITE_OPEN_READWRITE, NULL);
    if( SQLITE_OK != rc )
    {
        // Can't open database
        sqlite3_close_v2(*db);
        *db = NULL;
        if(SQLITE_ERROR == rc || SQLITE_CORRUPT == rc || SQLITE_CANTOPEN == rc)
        {
            ret = OP_ERROR_DATABASE_FATAL;
        }
        else
        {
            ret = OP_ERROR_SQLITE_INTERNAL;
        }

        pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
    }

    return ret;
}
Exemple #3
0
void free_region_table(region_table_t* regions) {
    // Destroy prepared statements
    sqlite3_finalize(regions->insert_region_stmt);
    sqlite3_finalize(regions->find_exact_region_stmt);
    sqlite3_finalize(regions->find_exact_region_type_stmt);
    sqlite3_finalize(regions->find_region_stmt);
    sqlite3_finalize(regions->find_region_type_stmt);
    sqlite3_finalize(regions->remove_exact_region_stmt);
    sqlite3_finalize(regions->remove_region_stmt);
    sqlite3_finalize(regions->get_chromosome_stmt);
    sqlite3_finalize(regions->count_in_chromosome_stmt);

    // Close database
    sqlite3_close_v2(regions->storage);
    
    // Free ordering array
    char **ordering = regions->ordering;
    for (int i = 0; i < regions->max_chromosomes; i++) {
        free(ordering[i]);
    }
    free(ordering);

    // Destroy the chunks hashtable
    kh_destroy(stats_chunks, regions->chunks);

    free(regions);
}
Exemple #4
0
  int wrap_close(sqlite3* dbh) {
#if SQLITE_VERSION_NUMBER >= 3007014
    return sqlite3_close_v2(dbh);
#else
    return sqlite3_close(dbh);
#endif
  }
Exemple #5
0
int
CSQLiteDE::CloseDatabase(bool bNoIndexes)
{
int TblIdx;
int Rslt = 0;
tsDEStmSQL *pStms;
pStms = m_StmSQL;
if(m_pDB != NULL)
	{
	if(!bNoIndexes)
		{
		for(TblIdx = 0; TblIdx < 4; TblIdx++,pStms++)
			{
			if(pStms->pPrepInsert == NULL)
				continue;
			sqlite3_finalize(pStms->pPrepInsert);
			pStms->pPrepInsert = NULL;
			}
		}
	Rslt = sqlite3_close_v2(m_pDB);
	sqlite3_shutdown();
	m_pDB = NULL;
	}
return(Rslt);
}
Exemple #6
0
static void
destruct_esqlite_connection(ErlNifEnv *env, void *arg)
{
    esqlite_connection *db = (esqlite_connection *) arg;
    esqlite_command *cmd = command_create();

    /* Send the stop command
     */
    cmd->type = cmd_stop;
    queue_push(db->commands, cmd);

    /* Wait for the thread to finish
     */
    enif_thread_join(db->tid, NULL);

    enif_thread_opts_destroy(db->opts);

    /* The thread has finished... now remove the command queue, and close
     * the database (if it was still open).
     */
    while(queue_has_item(db->commands)) {
        command_destroy(queue_pop(db->commands));
    }
    queue_destroy(db->commands);

    sqlite3_close_v2(db->db);
    db->db = NULL;
}
Exemple #7
0
static int sqlite_handle_closer(pdo_dbh_t *dbh) /* {{{ */
{
	pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;

	if (H) {
		pdo_sqlite_error_info *einfo = &H->einfo;

		pdo_sqlite_cleanup_callbacks(H);
		if (H->db) {
#ifdef HAVE_SQLITE3_CLOSE_V2
			sqlite3_close_v2(H->db);
#else
			sqlite3_close(H->db);
#endif
			H->db = NULL;
		}
		if (einfo->errmsg) {
			pefree(einfo->errmsg, dbh->is_persistent);
			einfo->errmsg = NULL;
		}
		pefree(H, dbh->is_persistent);
		dbh->driver_data = NULL;
	}
	return 0;
}
Exemple #8
0
static ERL_NIF_TERM
do_open(ErlNifEnv *env, esqlite_connection *db, const ERL_NIF_TERM arg)
{
    char filename[MAX_PATHNAME];
    unsigned int size;
    int rc;
    ERL_NIF_TERM error;

    size = enif_get_string(env, arg, filename, MAX_PATHNAME, ERL_NIF_LATIN1);
    if(size <= 0)
        return make_error_tuple(env, "invalid_filename");

    /* Open the database.
     */
    rc = sqlite3_open(filename, &db->db);
    if(rc != SQLITE_OK) {
	    error = make_sqlite3_error_tuple(env, rc, db->db);
	    sqlite3_close_v2(db->db);
	    db->db = NULL;

	    return error;
    }

    sqlite3_busy_timeout(db->db, 2000);

    return make_atom(env, "ok");
}
 void SqliteDeleter::operator()(sqlite3* p)
 {
   if (p != nullptr)
   {
     sqlite3_close_v2(p);
   }
 }
Exemple #10
0
Cache::~Cache() {
    if (db) {
        int retVal = sqlite3_close_v2(db);
        if( SQLITE_OK != retVal ) {
            std::cerr << "Oops: failed to properly close database: " << sqlite3_errstr(retVal) << std::endl;
        }
    }
}
Exemple #11
0
CSQLiteDE::~CSQLiteDE(void)
{
if(m_pDB != NULL)
	{
	sqlite3_close_v2(m_pDB);
	sqlite3_shutdown();
	m_pDB = NULL;
	}
}
 void object::close ()
 {
     if (context)
     {
         error = sqlite3_close_v2 (context);
         if (error != SQLITE_OK)
             LOG_ERROR("database close error (%s)", sqlite3_errstr (error));
     }
 }
void SqliteDatabaseBackend::closeWithoutException()
{
    if (databaseHandle) {
        int resultCode = sqlite3_close_v2(databaseHandle);
        databaseHandle = nullptr;
        if (resultCode != SQLITE_OK)
            qWarning() << "SqliteDatabaseBackend::closeWithoutException: Unexpected error at closing the database!";
    }
}
static void
vmod_free(void *priv)
{
	struct vmod_sqlite3 *v;

	CAST_OBJ_NOTNULL(v, priv, VMOD_SQLITE3_MAGIC);
	sqlite3_close_v2(v->db);
	free(v->d);
	FREE_OBJ(v);
}
int main(int argc, char* argv[])
{
	sqlite3 *db;
	int return_code;


	const char *db_name;
	if (argc == 2) {
		db_name = argv[1];
	} else {
		db_name = "test.db";
	}

	return_code = sqlite3_open(db_name, &db);

	if (return_code != SQLITE_OK) {
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}


	const char *sql = add_record;
	sqlite3_stmt *statement;
	return_code = sqlite3_prepare_v2(db, sql, -1, &statement, 0);
	if (return_code != SQLITE_OK) {
		printf("Problem with SQL statement - can't prepare it?\n");
		sqlite3_close_v2(db);
		return -1;
	}

	return_code = sqlite3_step(statement);

	if (return_code != SQLITE_DONE)
		printf("Something went wrong: error code %d\n", return_code);
	else
		printf("Insert successful:\n%s\n", sql);

	sqlite3_finalize(statement);
	sqlite3_close_v2(db);
	return 0;
}
Exemple #16
0
static ERL_NIF_TERM
do_close(ErlNifEnv *env, esqlite_connection *conn, const ERL_NIF_TERM arg)
{
    int rc;

    rc = sqlite3_close_v2(conn->db);
    if(rc != SQLITE_OK)
	    return make_sqlite3_error_tuple(env, rc, conn->db);

    conn->db = NULL;
    return make_atom(env, "ok");
}
Exemple #17
0
Database::~Database() {
    if ( _db ) {
        sqlite3_close_v2( _db );
        _db = nullptr;
    }

    #ifdef DEV
        string log_str;
        cout << "DATABASE LOG: " << endl;
        while(getline(_log, log_str))
            cout << "\t" << log_str << endl << endl;
    #endif
}
Exemple #18
0
SQLite3DB::~SQLite3DB() {
	if (db) {
		// close db
		int rc;
		rc=sqlite3_close_v2(db);
		if (rc!=SQLITE_OK) {
	    proxy_debug(PROXY_DEBUG_SQLITE, 1, "SQLITE: Error on sqlite3_close_v2(): %s\n", sqlite3_errmsg(db));	
			if (assert_on_error) {
				assert(rc==0);
			}
		}
	}
	if (url) {free(url); url=NULL;}
}
  int database_handle::close() {
    std::set<function_handle*>::iterator i = function_.begin();
    std::set<function_handle*>::iterator end = function_.end();
    for (; i != end; ++i) {
      delete *i;
    }
    function_.clear();

    sqlite3* dbh = dbh_;
    dbh_ = 0;
#if SQLITE_VERSION_NUMBER >= 3007014
    return sqlite3_close_v2(dbh);
#else
    return sqlite3_close(dbh);
#endif
  }
Exemple #20
0
NMBLSEXPORT void handleUnLoadLibrary(void)
{
    done = true;
    sign.post();

    // Currently throwing: what():  boost: mutex lock failed in pthread_mutex_lock: Invalid argument
    // But given we are using an atomic done flag - is this needed?
    // worker_thread.join_all();

    for ( auto it = dbhandles.begin(); it != dbhandles.end(); it++)
    {
        sqlite3_close_v2(it->second);
    }

    std::cout << "Oppertunity to clean up..." << std::endl;
}
int
duf_sqlite_close( void )
{
  int r3 = 0;

/*										*/ DEBUG_START(  );
  if ( pDb )
    r3 = sqlite3_close_v2( pDb );
  pDb = NULL;
  DUF_TRACE( action, 0, "DB Close %s (%d)", r3 == SQLITE_OK ? "OK" : "FAIL", r3 );

  r3 = sqlite3_shutdown(  );
  DUF_TRACE( action, 0, "DB Shutdown %s (%d)", r3 == SQLITE_OK ? "OK" : "FAIL", r3 );

/*										*/ DEBUG_END(  );
  return ( r3 );
}
void SqliteWorkHorse::run()
{

	initDatabase();

	std::stringstream sql_stream;

	DM::Logger(DM::Debug) << "running chunk " << id << "/" << this->plow->getTotalChunks();
	sql_stream.str("");
	sql_stream.clear();
	sql_stream << this->plow->GetWorkerQuery() << " limit " << start << "," << end;

	this->execute_query(sql_stream.str().c_str(), true);

	sqlite3_close_v2(db);
	this->plow->register_results(result_vector);
	DM::Logger(DM::Debug) << "done chunk " << id << "/" << this->plow->getTotalChunks();
}
Exemple #23
0
void ServerApplication::Quit() {
	std::cout << "Shutting down" << std::endl;

	//save the server state
	SaveServerState();

	//close the managers
	accountMgr.UnloadAll();
	characterMgr.UnloadAll();
	clientMgr.UnloadAll();
	roomMgr.UnloadAll();

	//APIs
	lua_close(luaState);
	sqlite3_close_v2(database);
	network.Close();
	SDLNet_Quit();
	SDL_Quit();

	std::cout << "Clean exit" << std::endl;
}
Exemple #24
0
void sqliteworker(void)
{
    while(!done)
    {
        sign.wait();
        int nexttoworkon;
        while (workqueue.pop(nexttoworkon))
        {
            auto workingon = requests.begin() + nexttoworkon;
            std::string sqlfile = workingon->hostroot +  "data.sqlite";
            std::string sqlinitfile = workingon->hostroot +  "data.init";

            void (*addJsonRowFunction)(std::ostringstream &output, int row, sqlite3_stmt *stmt) = addJsonTableRow;
            void (*finiJsonFunction)(std::ostringstream &output, bool haverows) = finiJsonTableRow;

            try
            {
                workingon->params.at("formatdatatable");
                addJsonRowFunction = addGDataTableRow;
                finiJsonFunction = finiGDataTable;
            }
            catch (const std::out_of_range &x)
            {

            }

            boost::posix_time::ptime starttime;
            measureuSTime(starttime, is_running_as_debug);

            sqlite3 *dbhandle = NULL;
            try
            {
                dbhandle = dbhandles.at(workingon->host);
            }
            catch(const std::out_of_range &x)
            {
                bool needscreating = false;
                if ( !boost::filesystem::is_regular_file(sqlfile) )
                {
                    needscreating = true;
                }

                std::cout << "Need to open db" << std::endl;
                if ( SQLITE_OK == sqlite3_open_v2(sqlfile.c_str(),  &dbhandle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))
                {
                    dbhandles[workingon->host] = dbhandle;
                    dbhandle = dbhandle;

                    if (needscreating)
                    {
                        char *errmsg = 0;
                        std::ifstream insql(sqlinitfile);
                        if ( !insql.fail())
                        {
                            std::string createsql(static_cast<std::stringstream const&>(std::stringstream() << insql.rdbuf()).str());
                            int rc = sqlite3_exec(dbhandle, createsql.c_str(), NULL, 0, &errmsg);
                            if( rc != SQLITE_OK )
                            {
                                std::cerr << "Error running SQL: " <<  errmsg <<std::endl;

                                std::ostringstream doc;
                                genHttpDoc("", doc);

                                workingon->postResponceCallback("500", errmsg, doc.str());
                                sqlite3_free(errmsg);

                                sqlite3_close_v2(dbhandle);
                                backlog--;
                                continue;
                            }

                            measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_exec - init)");
                        }
                    }
                }
            }

            if ( NULL != dbhandle )
            {
                measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_open_v2)");

                try
                {
                    std::string insql;
                    readFile(workingon->filename, insql);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (read sql)");

                    sqlite3_stmt *stmt;
                    const char *sqlltail;
                    int rc = sqlite3_prepare_v2(dbhandle, insql.c_str(), insql.size(), &stmt, &sqlltail);

                    measureuSTime(starttime, is_running_as_debug, "Time taken (sqlite3_prepare_v2)");

                    if( rc != SQLITE_OK )
                    {
                        std::string errormsg = "{error: \"";
                        errormsg += "Error preparing SQL statement";

                        std::cerr << "Error preparing: " << insql << std::endl;

                        errormsg +=  "\"}";

                        std::ostringstream doc;
                        genHttpDoc("", doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                    else
                    {
                        getAndBindParamsFromSql(insql, stmt, workingon);

                        measureuSTime(starttime, is_running_as_debug, "Time taken (bind)");

                        std::ostringstream jsondoc;
                        bool haverows = false;

                        // Have some sencible limit
                        for ( int row = 0; row < 10000; row++)
                        {
                            int step_result = sqlite3_step(stmt);
                            if ( SQLITE_ROW != step_result )
                            {
                                // Probably SQLITE_DONE
                                break;
                            }

                            haverows = true;
                            addJsonRowFunction(jsondoc, row, stmt);
                            //addGDataTableRow(jsondoc, row, stmt);
                        }
                        sqlite3_finalize(stmt);

                        //finiGDataTable(jsondoc, haverows);
                        finiJsonFunction(jsondoc, haverows);

                        std::ostringstream doc;
                        genHttpDoc(jsondoc.str(), doc);

                        workingon->postResponceCallback("200", "OK", doc.str());
                    }
                }
                catch (std::ifstream::failure e)
                {
                    std::cerr << "Exception opening/reading/closing file: " << workingon->filename << std::endl;
                }
            }
            else
            {
                workingon->postResponceCallback("404", "Not found", "Something bad just happened...");
            }

            //sqlite3_close_v2(dbhandle);
            backlog--;
        }
    }
}
Exemple #25
0
int db_sqlite_connect(struct my_con* ptr)
{
	sqlite3* con;
	char* errmsg;
	struct db_sqlite_extension_list *iter;

	/* if connection already in use, close it first*/
	if (ptr->init)
		sqlite3_close_v2(ptr->con);

	ptr->init = 1;

	memcpy(url_buf, ptr->id->url.s+sizeof(SQLITE_ID)-1,
				ptr->id->url.len - (sizeof(SQLITE_ID)-1));
	url_buf[ptr->id->url.len - (sizeof(SQLITE_ID)-1)] = '\0';

	if (sqlite3_open(url_buf, &con) != SQLITE_OK) {
		LM_ERR("Can't open database: %s\n", sqlite3_errmsg((sqlite3*)ptr->con));
		return -1;
	}

	/* trying to load extensions */
	if (extension_list) {
		if (sqlite3_enable_load_extension(con, 1)) {
			LM_ERR("failed to enable extension loading\n");
			return -1;
		}

		iter=extension_list;
		for (iter=extension_list; iter; iter=iter->next) {
			if (sqlite3_load_extension(con, iter->ldpath,
						iter->entry_point, &errmsg)) {
				LM_ERR("failed to load!"
						"Extension [%s]! Entry point [%s]!"
						"Errmsg [%s]!\n",
						iter->ldpath, iter->entry_point,
						errmsg);
				goto out_free;
			}
			LM_DBG("Extension [%s] loaded!\n", iter->ldpath);
		}

		if (sqlite3_enable_load_extension(con, 0)) {
			LM_ERR("failed to enable extension loading\n");
			return -1;
		}
	}



	ptr->con = con;

	return 0;

out_free:
	while (extension_list) {
		iter=extension_list;
		extension_list=extension_list->next;
		pkg_free(iter);
	}
	return -1;
}
pse_op_error_t sqlite_get_empty_leafnode(int* leaf_node_id, sgx_measurement_t* mr_signer)
{
    PROFILE_START("sqlite_get_empty_leafnode");

    char mrsigner[65] = {0};
    char sql_sentence[512] = {0};
    int counter = 0;
    pse_op_error_t ret = OP_SUCCESS;
    sqlite3 *db = NULL;

    if(!leaf_node_id || !mr_signer)
    {
        PROFILE_END("sqlite_get_empty_leafnode");
        return OP_ERROR_INVALID_PARAMETER;
    }

    // convert mr_signer to hex string
    for(uint32_t i=0; i < sizeof(sgx_measurement_t); i++)
    {
        char tmp[3];
        if(_snprintf_s(tmp, sizeof(tmp), "%02x", ((uint8_t*)mr_signer)[i]) < 0)
        {
            ret = OP_ERROR_INTERNAL;
            goto clean_up;
        }

        if(0 != strncat_s(mrsigner, sizeof(mrsigner), tmp, sizeof(tmp)))
        {
            ret = OP_ERROR_INTERNAL;
            goto clean_up;
        }

    }

    ret = sqlite_open_db(&db);
    if(OP_SUCCESS != ret)
    {
        pse_vmc_db_state = PSE_VMC_DB_STATE_DOWN;
        PROFILE_END("sqlite_get_empty_leafnode");
        return ret;
    }

    // check QUOTA
    if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select COUNTER from VMC_QUOTA_TABLE where MRSIGNER='%s';", mrsigner) < 0)
    {
        ret = OP_ERROR_INTERNAL;
        goto clean_up;
    }

    ret = sqlite_query_int_value(db, sql_sentence, &counter);
    if(OP_SUCCESS != ret && OP_ERROR_SQLITE_NOT_FOUND != ret)
    {
        goto clean_up;
    }

    if(PSE_VMC_QUOTA_SIZE <= counter)
    {
        // exceeds quota, return error.
        ret = OP_ERROR_DATABASE_OVER_QUOTA;
        goto clean_up;
    }

    // the specified MR SIGNER doesn't exceed quota.
    *leaf_node_id = 0;
    if (_snprintf_s(sql_sentence, sizeof(sql_sentence), "select min(ID) from HASH_TREE_NODE_TABLE where USED=0 and ID>%d and ID<%d;", 
        INIT_MIN_LEAF_NODE_ID-1, 
        INIT_MAX_LEAF_NODE_ID+1) < 0)
    {
        ret = OP_ERROR_INTERNAL;
        goto clean_up;
    }
    ret = sqlite_query_int_value(db, sql_sentence, leaf_node_id);
    if(OP_ERROR_SQLITE_NOT_FOUND == ret)
    {
        ret = OP_ERROR_DATABASE_FULL;
        goto clean_up;
    }

clean_up:
    if(db)
    {
        sqlite3_close_v2(db);
    }

    PROFILE_END("sqlite_get_empty_leafnode");
    return ret;
}
Exemple #27
0
sqlite3 *
CSQLiteDE::CreateDatabase(bool bSafe,				// true if sourcing from input CSV of unknown origin which may contain duplicates etc..
				char *pszDatabase)		// database to create (any existing database is deleted then clean created)
{
tsDEStmSQL *pStms;
int TblIdx;
int sqlite_error;
// note if database already exists in case bReplace is requested
struct stat TargStat;
int StatRslt = stat(pszDatabase,&TargStat);
if(StatRslt >= 0)
	remove(pszDatabase);

m_bSafe = bSafe;

// try creating the database
if((sqlite_error = sqlite3_open_v2(pszDatabase, &m_pDB,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,NULL))!=SQLITE_OK)
	{
	gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - can't open database: %s", sqlite3_errmsg(m_pDB));
	sqlite3_shutdown();
	m_pDB = NULL;
	return(NULL);
	}

// create all tables
pStms = m_StmSQL;
for(TblIdx = 0; TblIdx < 4; TblIdx++,pStms++)
	{
	pStms->pPrepInsert = NULL;
	if(pStms->pszCreateTbl == NULL)
		continue;
	if((sqlite_error = sqlite3_exec(m_pDB,pStms->pszCreateTbl,0,0,0))!=SQLITE_OK)
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - can't create table %s : %s", pStms->pTblName,sqlite3_errmsg(m_pDB));
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - statement: %s",pStms->pszCreateTbl);   
		sqlite3_close_v2(m_pDB);
		sqlite3_shutdown();
		m_pDB = NULL;
		return(NULL);
		}
	}


pStms = m_StmSQL;
if(bSafe)
	{
	for(TblIdx = 0; TblIdx < 4; TblIdx++,pStms++)
		{
		if(pStms->pszOpenCreateSafeIndexes == NULL)
			continue;
		if((sqlite_error = sqlite3_exec(m_pDB,pStms->pszOpenCreateSafeIndexes,0,0,0))!=SQLITE_OK)
			{
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - can't create safe indexes on table %s : %s", pStms->pTblName,sqlite3_errmsg(m_pDB));
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - statement: %s",pStms->pszOpenCreateIndexes);   
			sqlite3_close_v2(m_pDB);
			sqlite3_shutdown();
			m_pDB = NULL;
			return(NULL);
			}
		}
	}
else
	{
	for(TblIdx = 0; TblIdx < 4; TblIdx++,pStms++)
		{
		if(pStms->pszOpenCreateIndexes == NULL)
			continue;
		if((sqlite_error = sqlite3_exec(m_pDB,pStms->pszOpenCreateIndexes,0,0,0))!=SQLITE_OK)
			{
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - can't create indexes on table %s : %s", pStms->pTblName,sqlite3_errmsg(m_pDB));
			gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - statement: %s",pStms->pszOpenCreateIndexes);   
			sqlite3_close_v2(m_pDB);
			sqlite3_shutdown();
			m_pDB = NULL;
			return(NULL);
			}
		}
	}

// prepare all insert statements
pStms = m_StmSQL;
for(TblIdx = 0; TblIdx <4; TblIdx++,pStms++)
	{
	if(pStms->pszInsert == NULL)
		{
		pStms->pPrepInsert = NULL;
		continue;
		}
	if((sqlite_error = sqlite3_prepare_v2(m_pDB,pStms->pszInsert,-1,&pStms->pPrepInsert,NULL))!=SQLITE_OK)
		{
		gDiagnostics.DiagOut(eDLFatal,gszProcName,"sqlite - can't prepare insert statement on table %s: %s", pStms->pTblName, sqlite3_errmsg(m_pDB));
		while(TblIdx > 0)
			{
			pStms -= 1;
			if(pStms->pPrepInsert != NULL)
				{
				sqlite3_finalize(pStms->pPrepInsert);
				pStms->pPrepInsert = NULL;
				}
			}
		sqlite3_close_v2(m_pDB);
		sqlite3_shutdown();
		m_pDB = NULL;
		return(NULL);
		}
	}
return(m_pDB);
}
int main(int argc, char *argv[]) {
    sqlite3 *db = NULL;
    sqlite3_stmt *stmt = NULL;
    int flags = SQLITE_OPEN_READWRITE;
    int rc = 0;
    int idx = -1;
    /* Stringa SQL con i parametri nominativi - named parameters.
    A ogni stringa alfanumerica, usata nelle tre versioni supportate, viene
    associato dalla funzione sqlite3_prepare_v2() un indice numerico. */
    char *sql_str = "INSERT INTO addressbook (fullname, alias, email)"
                    "VALUES(:name, @aka, $mail)";

    if (argc != 5) {
        fprintf(stderr, "Usage: %s <DB>,<name>,<alias>,<email>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Le stringhe ricevute in input diventano costanti */
    const char *const str_fullname = (argc && argv[2]) ? argv[2] : "";
    const char *const str_alias = (argc && argv[3]) ? argv[3] : "";
    const char *const str_email = (argc && argv[4]) ? argv[4] : "";

    // Library initialization
    if (sqlite3_initialize() != SQLITE_OK) {
        fprintf(stderr, "Err. Unable to initialize the library.\n");
        exit(EXIT_FAILURE);
    }

    // Open database connection
    rc = sqlite3_open_v2(argv[1], &db, flags, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. can't create database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(EXIT_FAILURE);
    }

    // Prepare the statement
    rc = sqlite3_prepare_v2(db, sql_str,-1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Can't prepare the statement.\n");
        sqlite3_close(db);
        exit(EXIT_FAILURE);
    }

    /* Bind del primo parametro
    Si utilizza la funzione sqlite3_bind_text() per associare il parametro al
    valore che in questo caso e' 'text', poiche' trattasi di stringa.
    L'indice del parametro viene salvato nella variabile intera 'idx', ottenuto
    dal valore di ritorno della funzione sqlite3_bind_parameter_index(): */
    idx = sqlite3_bind_parameter_index(stmt, ":name");
    rc = sqlite3_bind_text(stmt, idx, str_fullname, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    // Bind del secondo parametro
    idx = sqlite3_bind_parameter_index(stmt, "@aka");
    rc = sqlite3_bind_text(stmt, idx, str_alias, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    // Bind del terzo parametro
    idx = sqlite3_bind_parameter_index(stmt, "$mail");
    rc = sqlite3_bind_text(stmt, idx, str_email, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Err. Binding the value (%i).\n", rc);
        return 1;
    }

    /* Stampa del totale dei parametri, del nome degli stessi e del 
    rispettivo indice, mediante le funzioni specifiche */
    printf("Total bound parameters: %d\n", sqlite3_bind_parameter_count(stmt));
    
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 1), \
            sqlite3_bind_parameter_index(stmt, ":name"));
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 2), \
            sqlite3_bind_parameter_index(stmt, "@aka"));
    printf("\'%5s\' index \'%d\'\n", sqlite3_bind_parameter_name(stmt, 3), \
            sqlite3_bind_parameter_index(stmt, "$mail"));

    // Execute the statement
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        fprintf(stderr, "Err. Stepping through the statement.\n");
    } else
        puts("... Statement successfully executed.");

    // Release prepared statement resources
    sqlite3_finalize(stmt);
    // Close database connection
    sqlite3_close_v2(db);
    // Shutdown library initialization
    sqlite3_shutdown();

    return(EXIT_SUCCESS);
}
Exemple #29
0
int SQLite::Close(sqlite3 *pDb) {
  BOOST_LOG_TRIVIAL(debug) << "database::SQLite::Close: Function call";
  return sqlite3_close_v2(pDb);
}
Exemple #30
0
int main()
{
  // Build an APD index.
  MapIndexHandler<unsigned char> APDIndex;
  for(unsigned char i = 152; i < 226; i++) {
    // Skip APD channels which have never been active.
    if(i == 178 or i == 191 or i == 205) continue;
    APDIndex.InsertKey(i);
  }

  // Build a lightmap object in memory.
  // This is where we specify the lightmap binning and choice of run ranges for gain.
  LightMap::PositionFunc posFunc;
  posFunc.SetAPDIndex(APDIndex);
  posFunc.SetBinning(-210, 15, 28, -210, 15, 28, -200, 10, 40); // 1.5cm in X/Y, 1cm in Z.
  LightMap::GainFunc gainFunc;
  gainFunc.InsertGain(APDIndex, 2401, 2423); // Old 09-28-11 APD biases
  gainFunc.InsertGain(APDIndex, 2424, 2690); // FEC voltage adjustment
  gainFunc.InsertGain(APDIndex, 2691, 2852); // Ebox 1 fan installed
  gainFunc.InsertGain(APDIndex, 2853, 2891); // Ebox 2 fan installed
  gainFunc.InsertGain(APDIndex, 2892, 3117); // Power outage here.
  gainFunc.InsertGain(APDIndex, 3118, 3326); // APD board swap
  gainFunc.InsertGain(APDIndex, 3327, 3700); // Utility power swap
  gainFunc.InsertGain(APDIndex, 3701, 3949);
  gainFunc.InsertGain(APDIndex, 3950, 4140); // Ralph's diode box installed
  gainFunc.InsertGain(APDIndex, 4141, 4579);
  gainFunc.InsertGain(APDIndex, 4580, 4779);
  gainFunc.InsertGain(APDIndex, 4780, 5197); // LC filters removed from FECs
  gainFunc.InsertGain(APDIndex, 5198, 5590); // Toward end of 5590 CR temps are elevated; APD gain impacted
  gainFunc.InsertGain(APDIndex, 5591, 5892); // Run2c ends.

  // Retrieve the indices of the x, y, and z coordinates.
  const IntervalIndexHandler& xIndex = posFunc.PosIndex().MajorIndex().MajorIndex();
  const IntervalIndexHandler& yIndex = posFunc.PosIndex().MajorIndex().MinorIndex();
  const IntervalIndexHandler& zIndex = posFunc.PosIndex().MinorIndex();

  // Initialize the gain function to be identically one.
  for(size_t i = 0; i < gainFunc.NumSnapshots(); i++) {
    LightMap::GainSnapshot& gain = gainFunc.GainAtIndex(i);
    for(size_t j = 0; j < gain.APDIndex().MaxIndex(); j++) gain.GetValAt(j) = 1;
  }

  // Create a connection to the database of thorium events.
  int ret;
  sqlite3* connection;
  ret = sqlite3_open_v2("Tmp/ThoriumLightmapEvents.db",
                        &connection,
                        SQLITE_OPEN_READONLY,
                        NULL);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to open database of events." << std::endl;
    exit(1);
  }

  // Build a comma-separated list of APD columns to retrieve in each case.
  std::ostringstream QueryColumns;
  QueryColumns << "apd_" << int(APDIndex.KeyForIndex(0)) << "_magnitude";
  for(size_t i = 1; i < APDIndex.MaxIndex(); i++) {
    QueryColumns << ", apd_" << int(APDIndex.KeyForIndex(i)) << "_magnitude";
  }
  QueryColumns << " FROM events WHERE ";

  // Prepare statements to retrieve by position or by run range.
  sqlite3_stmt *prep_stmt_pos, *prep_stmt_run;
  ret = sqlite3_prepare_v2(connection,
                           std::string("SELECT runNo, " + QueryColumns.str() +
                                       "? <= xpos AND xpos < ? AND " +
                                       "? <= ypos AND ypos < ? AND " +
                                       "? <= zpos AND zpos < ?").c_str(),
                           -1,
                           &prep_stmt_pos,
                           NULL);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to prepare the position select statement." << std::endl;
    exit(1);
  }
  ret = sqlite3_prepare_v2(connection,
                           std::string("SELECT xpos, ypos, zpos, " + QueryColumns.str() +
                                       "? <= runNo AND runNo <= ?").c_str(),
                           -1,
                           &prep_stmt_run,
                           NULL);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to prepare the run select statement." << std::endl;
    exit(1);
  }

  // Iteratively converge on an appropriate lightmap -- three iterations seem to be enough.
  for(size_t iteration = 0; iteration < 3; iteration++) {

    std::cout << "Computing the position function, holding gain fixed." << std::endl;

    for(size_t posBin = 0; posBin < posFunc.PosIndex().MaxIndex(); posBin++) {

      // Get information about this position bin.
      size_t xyBin = posFunc.PosIndex().MajorIndexForIndex(posBin);
      size_t xBin = posFunc.PosIndex().MajorIndex().MajorIndexForIndex(xyBin);
      size_t yBin = posFunc.PosIndex().MajorIndex().MinorIndexForIndex(xyBin);
      size_t zBin = posFunc.PosIndex().MinorIndexForIndex(posBin);
      double xmin = xIndex.KeyForIndex(xBin);
      double xmax = xIndex.KeyForIndex(xBin+1);
      double ymin = yIndex.KeyForIndex(yBin);
      double ymax = yIndex.KeyForIndex(yBin+1);
      double zmin = zIndex.KeyForIndex(zBin);
      double zmax = zIndex.KeyForIndex(zBin+1);

      // Bind these parameters to the query.
      bool success = (
        sqlite3_bind_double(prep_stmt_pos, 1, xmin) == SQLITE_OK and
        sqlite3_bind_double(prep_stmt_pos, 2, xmax) == SQLITE_OK and
        sqlite3_bind_double(prep_stmt_pos, 3, ymin) == SQLITE_OK and
        sqlite3_bind_double(prep_stmt_pos, 4, ymax) == SQLITE_OK and
        sqlite3_bind_double(prep_stmt_pos, 5, zmin) == SQLITE_OK and
        sqlite3_bind_double(prep_stmt_pos, 6, zmax) == SQLITE_OK);
      if(not success) {
        std::cerr << "Failed to bind values to a select statement." << std::endl;
        exit(1);
      }

      // Retrieve all events which fall in this bin.
      // For each APD, we wish to compute the weighted sum of magnitudes
      // and the sum of square weights.
      LightMap::FuncVsAPD WeightedSumMagnitudes;
      LightMap::FuncVsAPD SumSquareWeights;
      for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
        WeightedSumMagnitudes[i] = 0;
        SumSquareWeights[i] = 0;
      }
      while((ret = sqlite3_step(prep_stmt_pos)) == SQLITE_ROW) {
        // Handle a row.
        int runNo = sqlite3_column_int(prep_stmt_pos, 0);
        const LightMap::GainSnapshot& gain = gainFunc.GainForRun(runNo);
        for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
          // Handle one APD for that row.
          double weight = gain.GetValAt(i);
          double apd_magnitude = sqlite3_column_double(prep_stmt_pos, 1 + i);
          WeightedSumMagnitudes[i] += weight*apd_magnitude;
          SumSquareWeights[i] += weight*weight;
        }
      }
      if(ret != SQLITE_DONE) {
        std::cerr << "Query returned failure." << std::endl;
        exit(1);
      }
      ret = sqlite3_reset(prep_stmt_pos);
      if(ret != SQLITE_OK) {
        std::cerr << "Failed to reset a statement." << std::endl;
        exit(1);
      }

      // Update the lightmap values for this bin, overwriting the old ones.
      for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
        if(WeightedSumMagnitudes[i] <= 0) posFunc.GetValAt(posBin, i) = 0;
        else posFunc.GetValAt(posBin, i) = WeightedSumMagnitudes[i]/SumSquareWeights[i];
      }
    }

    std::cout << "Done computing the position function." << std::endl;
    std::cout << "Computing the gain function, holding the position function fixed." << std::endl;

    for(size_t gainBin = 0; gainBin < gainFunc.NumSnapshots(); gainBin++) {
      LightMap::GainSnapshot& gain = gainFunc.GainAtIndex(gainBin);

      // Bind the run range parameters to the query.
      bool success = (
        sqlite3_bind_int(prep_stmt_run, 1, gain.FirstRun()) == SQLITE_OK and
        sqlite3_bind_int(prep_stmt_pos, 2, gain.LastRun()) == SQLITE_OK);
      if(not success) {
        std::cerr << "Failed to bind values to a select statement." << std::endl;
        exit(1);
      }

      // Retrieve all events which fall in this bin.
      // For each APD, we wish to compute the weighted sum of magnitudes
      // and the sum of square weights.
      LightMap::FuncVsAPD WeightedSumMagnitudes;
      LightMap::FuncVsAPD SumSquareWeights;
      for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
        WeightedSumMagnitudes[i] = 0;
        SumSquareWeights[i] = 0;
      }
      while((ret = sqlite3_step(prep_stmt_run)) == SQLITE_ROW) {
        // Handle a row.
        double xpos = sqlite3_column_double(prep_stmt_run, 0);
        double ypos = sqlite3_column_double(prep_stmt_run, 1);
        double zpos = sqlite3_column_double(prep_stmt_run, 2);

        size_t posBin = posFunc.PosIndex().IndexForKey(std::make_pair(std::make_pair(xpos, ypos), zpos));
        if(posBin >= posFunc.PosIndex().MaxIndex()) continue; // Kills events out of boundary for lightmap.
        const LightMap::FuncVsAPD& posFuncAtBin = posFunc.GetAllValsAt(posBin);

        for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
          // Handle one APD for that row.
          double weight = posFuncAtBin[i];
          double apd_magnitude = sqlite3_column_double(prep_stmt_run, 3 + i);
          WeightedSumMagnitudes[i] += weight*apd_magnitude;
          SumSquareWeights[i] += weight*weight;
        }
      }
      if(ret != SQLITE_DONE) {
        std::cerr << "Query returned failure." << std::endl;
        exit(1);
      }
      ret = sqlite3_reset(prep_stmt_run);
      if(ret != SQLITE_OK) {
        std::cerr << "Failed to reset a statement." << std::endl;
        exit(1);
      }

      // Update the gain snapshot, overwriting the old ones.
      for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
        if(WeightedSumMagnitudes[i] <= 0) gain.GetValAt(i) = 0;
        else gain.GetValAt(i) = WeightedSumMagnitudes[i]/SumSquareWeights[i];
      }
    }
    std::cout << "Done computing the gain function." << std::endl;
  } // End loop over iterations.

  // Clean up sqlite resources.
  ret = sqlite3_finalize(prep_stmt_pos);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to finalize an sqlite statement." << std::endl;
    std::cerr << "We'll finish up because we (probably) can, but this should be fixed." << std::endl;
  }
  ret = sqlite3_finalize(prep_stmt_run);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to finalize an sqlite statement." << std::endl;
    std::cerr << "We'll finish up because we (probably) can, but this should be fixed." << std::endl;
  }
  ret = sqlite3_close_v2(connection);
  if(ret != SQLITE_OK) {
    std::cerr << "Failed to close the sqlite connection." << std::endl;
    std::cerr << "We'll finish up because we (probably) can, but this should be fixed." << std::endl;
  }

  // We know relative R(x) and S(t) -- fix them using [hard-coded] laser data.
  // The laser data used is from laser run 4540.
  // NOTE: by the time of run 4540, channel 163 was already disconnected, so the number here is made up.
  // NOTE: it would be great in the future to do a better correspondence between
  // absolute gain measurements from laser runs and relative gain measurements from thorium.
  for(size_t i = 0; i < APDIndex.MaxIndex(); i++) {
    double laser_gain;
    switch(APDIndex.KeyForIndex(i)) {
      case 152: laser_gain = 201.230438146; break;
      case 153: laser_gain = 178.750438779; break;
      case 154: laser_gain = 194.228589338; break;
      case 155: laser_gain = 183.33801615; break;
      case 156: laser_gain = 218.485999976; break;
      case 157: laser_gain = 222.139259152; break;
      case 158: laser_gain = 169.982559736; break;
      case 159: laser_gain = 140.385120552; break;
      case 160: laser_gain = 137.602725389; break;
      case 161: laser_gain = 197.78183714; break;
      case 162: laser_gain = 155.478773762; break;
      case 163: laser_gain = 200; break; // FICTITIOUS, but this channel was disconnected Feb 2012.  Better guess?
      case 164: laser_gain = 175.875067527; break;
      case 165: laser_gain = 160.014408865; break;
      case 166: laser_gain = 183.408055613; break;
      case 167: laser_gain = 189.600819126; break;
      case 168: laser_gain = 160.339214431; break;
      case 169: laser_gain = 168.547991045; break;
      case 170: laser_gain = 182.670039836; break;
      case 171: laser_gain = 205.567802982; break;
      case 172: laser_gain = 195.87450621; break;
      case 173: laser_gain = 224.956647122; break;
      case 174: laser_gain = 232.062359991; break;
      case 175: laser_gain = 241.822881767; break;
      case 176: laser_gain = 194.740435753; break;
      case 177: laser_gain = 189.867775084; break;
      // case 178: laser_gain = 0; // Bad channel, omitted.
      case 179: laser_gain = 206.755206938; break;
      case 180: laser_gain = 207.822617603; break;
      case 181: laser_gain = 207.501985741; break;
      case 182: laser_gain = 218.213137769; break;
      case 183: laser_gain = 234.369354843; break;
      case 184: laser_gain = 99.908111992; break;
      case 185: laser_gain = 238.381809313; break;
      case 186: laser_gain = 225.118270743; break;
      case 187: laser_gain = 199.078450518; break;
      case 188: laser_gain = 221.863823239; break;
      case 189: laser_gain = 177.032783679; break;
      case 190: laser_gain = 196.787332164; break;
      // case 191: laser_gain = 0; // Bad channel, omitted.
      case 192: laser_gain = 194.923448865; break;
      case 193: laser_gain = 197.027984846; break;
      case 194: laser_gain = 202.757086104; break;
      case 195: laser_gain = 194.432937658; break;
      case 196: laser_gain = 208.992809367; break;
      case 197: laser_gain = 224.762562055; break;
      case 198: laser_gain = 217.696006443; break;
      case 199: laser_gain = 222.380158829; break;
      case 200: laser_gain = 218.358804472; break;
      case 201: laser_gain = 209.573057132; break;
      case 202: laser_gain = 194.684536629; break;
      case 203: laser_gain = 182.543842783; break;
      case 204: laser_gain = 193.469930111; break;
      // case 205: laser_gain = 0; // Bad channel, omitted.
      case 206: laser_gain = 193.627191472; break;
      case 207: laser_gain = 196.073150574; break;
      case 208: laser_gain = 189.597962521; break;
      case 209: laser_gain = 198.824317108; break;
      case 210: laser_gain = 222.747770671; break;
      case 211: laser_gain = 216.928470825; break;
      case 212: laser_gain = 223.437239807; break;
      case 213: laser_gain = 224.316404923; break;
      case 214: laser_gain = 216.26783603; break;
      case 215: laser_gain = 209.612423384; break;
      case 216: laser_gain = 223.041660884; break;
      case 217: laser_gain = 202.642254512; break;
      case 218: laser_gain = 213.904993632; break;
      case 219: laser_gain = 221.988942321; break;
      case 220: laser_gain = 201.427174798; break;
      case 221: laser_gain = 196.689200146; break;
      case 222: laser_gain = 191.457656123; break;
      case 223: laser_gain = 186.183873541; break;
      case 224: laser_gain = 217.033080346; break;
      case 225: laser_gain = 205.858374653; break;
      default: laser_gain = 0; // Bad or non-existent channel.
    }

    double thorium_gain = gainFunc.GainForRun(4540).GetValAt(i);

    if(laser_gain == 0 or thorium_gain == 0) continue;

    double ratio_gains = laser_gain/thorium_gain;
    for(size_t j = 0; j < gainFunc.NumSnapshots(); j++) {
      gainFunc.GainAtIndex(j).GetValAt(i) *= ratio_gains;
    }
    for(size_t j = 0; j < posFunc.PosIndex().MaxIndex(); j++) {
      posFunc.GetValAt(j, i) /= ratio_gains; // To keep the product of R and S the same.
    }
  }

  // We've generated a good (hopefully) lightmap -- now write it to file.
  std::cout << "Finished generating lightmap -- write to Tmp/LightMap.hdf5." << std::endl;
  LightMapIO::WriteLightMap("Tmp/LightMap.hdf5", posFunc, gainFunc);
  std::cout << "Done writing the lightmap to file.  Have a nice day." << std::endl;
}