Beispiel #1
0
/*
 * smb_nic_dbcreate
 *
 * Creates the host database based on the defined SQL statement.
 * It also initializes db_info table.
 */
static int
smb_nic_dbcreate(void)
{
	sqlite *db = NULL;
	char *errmsg = NULL;
	int rc, err = SMB_NIC_SUCCESS;

	(void) unlink(SMB_NIC_DB_NAME);

	db = sqlite_open(SMB_NIC_DB_NAME, 0600, &errmsg);
	if (db == NULL) {
		syslog(LOG_ERR, "Failed to create host database (%s).",
		    NULL_MSGCHK(errmsg));
		sqlite_freemem(errmsg);
		return (SMB_NIC_DBOPEN_FAILED);
	}

	sqlite_busy_timeout(db, SMB_NIC_DB_TIMEOUT);
	rc = sqlite_exec(db, "BEGIN TRANSACTION", NULL, NULL, &errmsg);
	if (rc != SQLITE_OK) {
		syslog(LOG_ERR, "Failed to create host database.  Unable to " \
		    "begin database transaction (%s).", NULL_MSGCHK(errmsg));
		sqlite_freemem(errmsg);
		sqlite_close(db);
		return (SMB_NIC_DBEXEC_FAILED);
	}

	if (sqlite_exec(db, SMB_NIC_DB_SQL, NULL, NULL, &errmsg) == SQLITE_OK) {
		rc = sqlite_exec(db, "COMMIT TRANSACTION", NULL, NULL,
		    &errmsg);
		if (rc == SQLITE_OK)
			err = smb_nic_dbsetinfo(db);
		if (err != SMB_NIC_SUCCESS)
			rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL,
			    &errmsg);
	} else {
		syslog(LOG_ERR, "Failed to create host database.  Unable to " \
		    "initialize host database (%s).", NULL_MSGCHK(errmsg));
		sqlite_freemem(errmsg);
		rc = sqlite_exec(db, "ROLLBACK TRANSACTION", NULL, NULL,
		    &errmsg);
	}

	if (rc != SQLITE_OK) {
		/* this is bad - database may be left in a locked state */
		syslog(LOG_ERR, "Failed to create host database.  Unable to " \
		    "close a transaction (%s).", NULL_MSGCHK(errmsg));
		sqlite_freemem(errmsg);
		err = SMB_NIC_DBINIT_FAILED;
	}

	(void) sqlite_close(db);
	return (err);
}
static void sqlite_close_conn(void *conn)
{
    if (conn == NULL)
        return;

    sqlite_close((sqlite*) conn);
}
Beispiel #3
0
/*
 * smb_nic_dbclose
 *
 * Closes the given database handle
 */
static void
smb_nic_dbclose(sqlite *db)
{
	if (db) {
		sqlite_close(db);
	}
}
Beispiel #4
0
static int unload_module(void)
{
	if (db)
		sqlite_close(db);
	ast_cdr_unregister(name);
	return 0;
}
int main(int argc, char **argv){
  int i;
  sqlite *db;
  char *zErr;
  int status;
  int parent = getpid();

  unlink("test.db");
  unlink("test.db-journal");
  db = sqlite_open("test.db", 0, &zErr);
  if( db==0 ){
    printf("Cannot initialize: %s\n", zErr);
    return 1;
  }
  sqlite_exec(db, "CREATE TABLE t1(a,b)", 0, 0, 0);
  sqlite_close(db);
  for(i=0; i<10000; i++){
    int pid = fork();
    if( pid==0 ){
      sched_yield();
      do_some_sql(parent);
      return 0;
    }
    printf("test %d, pid=%d\n", i, pid);
    usleep(rand()%10000 + 1000);
    kill(pid, SIGKILL);
    waitpid(pid, &status, 0);
  }
  return 0;
}
void cSQLiteDriver::close()
{
	if ( connection != 0 )
	{
		sqlite_close( ( sqlite * ) connection );
		connection = 0;
	}
}
Beispiel #7
0
static apr_status_t dbd_sqlite_close(apr_dbd_t * handle)
{
    if (handle->conn) {
        sqlite_close(handle->conn);
        handle->conn = NULL;
    }
    return APR_SUCCESS;
}
SmoothedCountPlugin::~SmoothedCountPlugin()
{
#if defined(HAVE_SQLITE3_H)
    sqlite3_close(db);
#elif defined(HAVE_SQLITE_H)
    sqlite_close(db);
#endif
}
Beispiel #9
0
void QSQLiteDriver::close()
{
    if (isOpen()) {
        sqlite_close(d->access);
        d->access = 0;
        setOpen(FALSE);
        setOpenError(FALSE);
    }
}
Beispiel #10
0
void QSQLite2Driver::close()
{
    if (isOpen()) {
        sqlite_close(d->access);
        d->access = 0;
        setOpen(false);
        setOpenError(false);
    }
}
void SqliteDatabaseConnector::closeDatabase()
{
    if (db) {
#if defined(HAVE_SQLITE3_H)
	sqlite3_close(db);
#elif defined(HAVE_SQLITE_H)
	sqlite_close(db);
#endif
    }
}
Beispiel #12
0
static int unload_module(void)
{
	if (ast_cdr_unregister(name)) {
		return -1;
	}

	if (db) {
		sqlite_close(db);
	}
	return 0;
}
int sqlite_unload_module(void)
{
	int res;
	if (db)
		sqlite_close(db);
	res = ast_unregister_application (dt_app);
	res |= ast_unregister_application (d_app);
	res |= ast_unregister_application (p_app);
	res |= ast_unregister_application (g_app);
	return res;
}
Beispiel #14
0
static void *worker_bee(void *pArg){
  const char *zFilename = (char*)pArg;
  char *azErr;
  int i, cnt;
  int t = atoi(zFilename);
  char **az;
  sqlite *db;

  pthread_mutex_lock(&lock);
  thread_cnt++;
  pthread_mutex_unlock(&lock);
  printf("%s: START\n", zFilename);
  fflush(stdout);
  for(cnt=0; cnt<10; cnt++){
    db = sqlite_open(&zFilename[2], 0, &azErr);
    if( db==0 ){
      fprintf(stdout,"%s: can't open\n", zFilename);
      Exit(1);
    }
    sqlite_busy_handler(db, db_is_locked, zFilename);
    db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
    for(i=1; i<=100; i++){
      db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
         t, i, i*2, i*i);
    }
    az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
    db_check(zFilename, "tX size", az, "100", 0);  
    az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
    db_check(zFilename, "tX avg", az, "101", 0);  
    db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
    az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
    db_check(zFilename, "tX avg2", az, "51", 0);
    for(i=1; i<=50; i++){
      char z1[30], z2[30];
      az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
      sprintf(z1, "%d", i*2);
      sprintf(z2, "%d", i*i);
      db_check(zFilename, "readback", az, z1, z2, 0);
    }
    db_execute(db, zFilename, "DROP TABLE t%d;", t);
    sqlite_close(db);
  }
  printf("%s: END\n", zFilename);
  /* unlink(zFilename); */
  fflush(stdout);
  pthread_mutex_lock(&lock);
  thread_cnt--;
  if( thread_cnt<=0 ){
    pthread_cond_signal(&sig);
  }
  pthread_mutex_unlock(&lock);
  return 0;
}
Beispiel #15
0
static int sqlite_odbx_unbind( odbx_t* handle )
{
	struct sconn* aux = (struct sconn*) handle->aux;

	if( handle->generic != NULL && aux != NULL )
	{
		sqlite_close( (sqlite*) handle->generic );

		handle->generic = NULL;
		aux->errmsg = NULL;

		return ODBX_ERR_SUCCESS;
	}

	return -ODBX_ERR_PARAM;
}
Beispiel #16
0
/*
** Usage:  sqlite_close DB
**
** Closes the database opened by sqlite_open.
*/
static int sqlite_test_close(
  void *NotUsed,
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int argc,              /* Number of arguments */
  char **argv            /* Text of each argument */
){
  sqlite *db;
  if( argc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
       " FILENAME\"", 0);
    return TCL_ERROR;
  }
  if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
  sqlite_close(db);
  return TCL_OK;
}
Beispiel #17
0
IoObject *IoSQLite_close(IoSQLite *self, IoObject *locals, IoMessage *m)
{
	/*doc SQLite close
	Closes the database if it is open. Returns self. 
	If the database is open when the open is garbage collected, 
	it will be automatically closed. 
	*/

	if (DATA(self)->db)
	{
		sqlite_close(DATA(self)->db);
		DATA(self)->db = NULL;
	}

	return self;
}
Beispiel #18
0
static int SQLite2_connect (dbconn_t * c, const char * host, const char * database, const char * username, const char * password)
{    
	c->SQLite2.handle = sqlite_open(database, 0666, &c->SQLite2.errormsg);
	if (!c->SQLite2.handle) {
		sqlite_close(c->SQLite2.handle);
		return 0;
	}

	c->SQLite2.nrows = 0;
	c->SQLite2.ncolumns = 0;
	c->SQLite2.last_row = 0;
	c->SQLite2.step_res = 0;
	c->SQLite2.values = NULL;
	c->SQLite2.col_names = NULL;
	c->SQLite2.vm = NULL;
	return 1;
}
static int sqlite_load_module(void)
{
	char *zErr;
	char fn[PATH_MAX];
	int res;

	/* is the database there? */
	snprintf(fn, sizeof(fn), "%s/astdb.db", ast_config_AST_LOG_DIR);
	db = sqlite_open(fn, 0660, &zErr);
	if (!db) {
		ast_log(LOG_ERROR, "app_dbsqlite: %s\n", zErr);
		free(zErr);
		return -1;
	}

	/* is the table there? */
	res = sqlite_exec(db, "SELECT COUNT(Id) FROM astdb;", NULL, NULL, &zErr);
	if (res) {
		res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
		if (res) {
			ast_log(LOG_ERROR, "app_dbsqlite: Unable to create table 'astdb': %s\n", zErr);
			free(zErr);
			goto err;
		}

		/* TODO: here we should probably create an index */
	}
	res = ast_register_application (g_app, sqliteget_exec, g_synopsis, g_descrip);
	if (!res)
		res = ast_register_application (p_app, sqliteput_exec, p_synopsis, p_descrip);
	if (!res)
		res = ast_register_application (d_app, sqlitedel_exec, d_synopsis, d_descrip);
	if (!res)
		res = ast_register_application (dt_app, sqlitedeltree_exec, dt_synopsis, dt_descrip);

	if (res) {
		ast_log(LOG_ERROR, "Unable to register app_dbsqlite\n");
		return -1;
	}
	return 0;

err:
	if (db)
		sqlite_close(db);
	return -1;
}
Beispiel #20
0
static int load_module(void)
{
	char *zErr;
	char fn[PATH_MAX];
	int res;

	/* is the database there? */
	snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
	db = sqlite_open(fn, 0660, &zErr);
	if (!db) {
		ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
		free(zErr);
		return -1;
	}

	/* is the table there? */
	res = sqlite_exec(db, "SELECT COUNT(id) FROM cdr;", NULL, NULL, NULL);
	if (res) {
		res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
		if (res) {
			ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
			free(zErr);
			goto err;
		}
		/*res = sqlite_exec(db, sql_create_trigger, NULL, NULL, &zErr);
		if (res) {
			ast_log(LOG_ERROR, "cdr_sqlite: Unable to create maxentries trigger: %s\n", zErr);
			free(zErr);
			goto err;
		}*/

		/* TODO: here we should probably create an index */
	}
	
	res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
	if (res) {
		ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
		return -1;
	}
	return 0;

err:
	if (db)
		sqlite_close(db);
	return -1;
}
Beispiel #21
0
/*
** Close a Connection object.
*/
static int conn_close(lua_State *L)
{
	conn_data *conn = (conn_data *)luaL_checkudata(L, 1, LUASQL_CONNECTION_SQLITE);
	luaL_argcheck (L, conn != NULL, 1, LUASQL_PREFIX"connection expected");
	if (conn->closed) {
		lua_pushboolean(L, 0);
		return 1;
	}

	if (conn->cur_counter > 0)
		return luaL_error (L, LUASQL_PREFIX"there are open cursors");

	/* Nullify structure fields. */
	conn->closed = 1;
	luaL_unref(L, LUA_REGISTRYINDEX, conn->env);
    sqlite_close(conn->sql_conn);
	lua_pushboolean(L, 1);
	return 1;
}
Beispiel #22
0
/*
** Clean up and delete a VDBE after execution.  Return an integer which is
** the result code.  Write any error message text into *pzErrMsg.
*/
int sqliteVdbeFinalize(Vdbe *p, char **pzErrMsg){
  int rc;
  sqlite *db;

  if( p->magic!=VDBE_MAGIC_RUN && p->magic!=VDBE_MAGIC_HALT ){
    sqliteSetString(pzErrMsg, sqlite_error_string(SQLITE_MISUSE), (char*)0);
    return SQLITE_MISUSE;
  }
  db = p->db;
  rc = sqliteVdbeReset(p, pzErrMsg);
  sqliteVdbeDelete(p);
  if( db->want_to_close && db->pVdbe==0 ){
    sqlite_close(db);
  }
  if( rc==SQLITE_SCHEMA ){
    sqliteResetInternalSchema(db, 0);
  }
  return rc;
}
Beispiel #23
0
static int load_module(void)
{
	char *zErr;
	char fn[PATH_MAX];
	int res;

	ast_log(LOG_NOTICE, "This module has been marked deprecated in favor of "
		"using cdr_sqlite3_custom.\n");

	/* is the database there? */
	snprintf(fn, sizeof(fn), "%s/cdr.db", ast_config_AST_LOG_DIR);
	db = sqlite_open(fn, AST_FILE_MODE, &zErr);
	if (!db) {
		ast_log(LOG_ERROR, "cdr_sqlite: %s\n", zErr);
		ast_free(zErr);
		return AST_MODULE_LOAD_DECLINE;
	}

	/* is the table there? */
	res = sqlite_exec(db, "SELECT COUNT(AcctId) FROM cdr;", NULL, NULL, NULL);
	if (res) {
		res = sqlite_exec(db, sql_create_table, NULL, NULL, &zErr);
		if (res) {
			ast_log(LOG_ERROR, "cdr_sqlite: Unable to create table 'cdr': %s\n", zErr);
			ast_free(zErr);
			goto err;
		}

		/* TODO: here we should probably create an index */
	}

	res = ast_cdr_register(name, ast_module_info->description, sqlite_log);
	if (res) {
		ast_log(LOG_ERROR, "Unable to register SQLite CDR handling\n");
		return AST_MODULE_LOAD_DECLINE;
	}
	return AST_MODULE_LOAD_SUCCESS;

err:
	if (db)
		sqlite_close(db);
	return AST_MODULE_LOAD_DECLINE;
}
Beispiel #24
0
/*
** Called when the command is deleted.
*/
static void DbDeleteCmd(void *db){
  SqliteDb *pDb = (SqliteDb*)db;
  sqlite_close(pDb->db);
  while( pDb->pFunc ){
    SqlFunc *pFunc = pDb->pFunc;
    pDb->pFunc = pFunc->pNext;
    Tcl_Free((char*)pFunc);
  }
  if( pDb->zBusy ){
    Tcl_Free(pDb->zBusy);
  }
  if( pDb->zTrace ){
    Tcl_Free(pDb->zTrace);
  }
  if( pDb->zAuth ){
    Tcl_Free(pDb->zAuth);
  }
  Tcl_Free((char*)pDb);
}
Beispiel #25
0
int
_ds_shutdown_storage (DSPAM_CTX * CTX)
{
  struct _sqlite_drv_storage *s = (struct _sqlite_drv_storage *) CTX->storage;
  struct nt_node *node_nt;
  struct nt_c c_nt;

  if (s->dbh == NULL)
  {
    LOGDEBUG ("_ds_shutdown_storage: invalid database handle (NULL)");
    return EINVAL;
  }

  node_nt = c_nt_first (s->dir_handles, &c_nt);
  while (node_nt != NULL)
  {
    DIR *dir;
    dir = (DIR *) node_nt->ptr;
    closedir (dir);
    node_nt = c_nt_next (s->dir_handles, &c_nt);
  }
                                                                                
  nt_destroy (s->dir_handles);


  /* Store spam totals on shutdown */
  if (CTX->username != NULL && CTX->operating_mode != DSM_CLASSIFY)
  {
      _sqlite_drv_set_spamtotals (CTX);
  }

  if (!s->dbh_attached)
    sqlite_close(s->dbh);

  s->dbh = NULL;

  free(s);
  CTX->storage = NULL;

  return 0;
}
Beispiel #26
0
int main(int argc, const char *argv[])
{
    sqlite3 *db;
    char buff[256] = {0};
    char name[100] = {0};
    char info[256] = {0};
    int *len = NULL;

    struct socket_data sock_data;
    char *ptr = sock_data.buff;

    db = sqlite_init();

    sqlite_read(db, &sock_data);

    printf("id = %d\n",*((int *)ptr));
    ptr+=4;

    len = (int *)ptr;
    //printf("len of name is :%d \n", *len);
    ptr+=4;

    strncpy(name, ptr, *len);
    printf("name is :%s\n",name);
    ptr+=*len;

    printf("price = %d\n",*((int *)ptr));
    ptr+=4;

    len = (int *)ptr;
    //printf("len of info is :%d \n", *len);
    ptr+=4;

    strncpy(info, ptr, *len);
    printf("info is :%s\n",info);


    sqlite_close(db);
    
    return 0;
}
Beispiel #27
0
static int SQLite2_close (dbconn_t * c)
{
	if (c->SQLite2.errormsg)
		free(c->SQLite2.errormsg);

	if (c->SQLite2.vm)
		sqlite_finalize(c->SQLite2.vm, NULL);

	sqlite_close(c->SQLite2.handle);

	c->SQLite2.handle = 0;
	c->SQLite2.errormsg = 0;
	c->SQLite2.nrows = 0;
	c->SQLite2.ncolumns = 0;
	c->SQLite2.last_row = 0;
	c->SQLite2.step_res =0;
	c->SQLite2.vm = NULL;
	c->SQLite2.values = NULL;
	c->SQLite2.col_names = NULL;
	return 1;
}
Beispiel #28
0
void daccept(int socket_bind)
{
    int socket_client;
    int ret = 0;
    fd_set fds_set;
    int max_fds;
    struct timeval timeout = {3, 0};

    struct socket_data sock_data;
    int i = 0;

    int ret_thread = 0;
    pthread_t tid = 0;
    pthread_attr_t attr;
    int err = 0;

// 初始化sqlite
    sock_data.db = sqlite_init(); 

//初始化attr结构,是创建的线程为分离状态,退出时自动回收线程资源
    err = pthread_attr_init(&attr);
    if( !err ) 
        debug("pthread attr init error");
    err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    if( !err ) 
        debug("pthread attr set detachstate error"); 
    while(1)
    {
        FD_ZERO(&fds_set);
        FD_SET(socket_bind, &fds_set);
        max_fds = socket_bind;

        ret = select(max_fds+1, &fds_set, NULL , NULL, &timeout); //对socketserver进行select,等待链接时不阻塞。
        if(ret < 0) {
            debug("select error\n");
        } else if(ret == 0) {

        } else {
            debug("select ok\n");
            if(FD_ISSET(socket_bind, &fds_set)) {
                debug("accept one client\n");
                if ( (socket_client = accept(socket_bind, NULL, NULL)) < 0) {
                    debug("accept error\n");
                    exit(1);
                } else {
                    debug("accept ok\n");
                    printf("socket:%d\n",socket_client);
                }

                pthread_mutex_lock(&mutex);// 加入互斥锁,在线程中再解锁
                sock_data.socket = socket_client;
                ret_thread = pthread_create(&tid, &attr,(void *)thread_handler, &sock_data);
                if(ret_thread != 0) {
                    debug("pthread create error\n");
                } else {
                    debug("pthread create ok");
                }
                /* 新建线程,与client通信
                    pthread_creat(); //当有连接时,新建线程
                 *
                 *
                 *
                 */
            }
        }

        //if ( (sock_client = accept(sock_server, NULL, NULL)) < 0) {
        //printf("accept error\n");
        //exit(1);
        //} else {
        //printf("accept ok\n");
        //}
        //
        //pthread_cancle();  //等待线程结束,也不能阻塞。
    }

    sqlite_close(sock_data.db);

}
Beispiel #29
0
DatabaseSqlite::~DatabaseSqlite()
{
    if (mSqlite)
        sqlite_close(mSqlite);
}
Beispiel #30
0
/*
** Open a new SQLite database.  Construct an "sqlite" structure to define
** the state of this database and return a pointer to that structure.
**
** An attempt is made to initialize the in-memory data structures that
** hold the database schema.  But if this fails (because the schema file
** is locked) then that step is deferred until the first call to
** sqlite_exec().
*/
sqlite *sqlite_open(const char *zFilename, int mode, char **pzErrMsg){
  sqlite *db;
  int rc, i;

  /* Allocate the sqlite data structure */
  db = sqliteMalloc( sizeof(sqlite) );
  if( pzErrMsg ) *pzErrMsg = 0;
  if( db==0 ) goto no_mem_on_open;
  db->onError = OE_Default;
  db->priorNewRowid = 0;
  db->magic = SQLITE_MAGIC_BUSY;
  db->nDb = 2;
  db->aDb = db->aDbStatic;
  /* db->flags |= SQLITE_ShortColNames; */
  sqliteHashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
  for(i=0; i<db->nDb; i++){
    sqliteHashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
    sqliteHashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
    sqliteHashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
    sqliteHashInit(&db->aDb[i].aFKey, SQLITE_HASH_STRING, 1);
  }
  
  /* Open the backend database driver */
  if( zFilename[0]==':' && strcmp(zFilename,":memory:")==0 ){
    db->temp_store = 2;
  }
  rc = sqliteBtreeFactory(db, zFilename, 0, MAX_PAGES, &db->aDb[0].pBt);
  if( rc!=SQLITE_OK ){
    switch( rc ){
      default: {
        sqliteSetString(pzErrMsg, "unable to open database: ",
           zFilename, (char*)0);
      }
    }
    sqliteFree(db);
    sqliteStrRealloc(pzErrMsg);
    return 0;
  }
  db->aDb[0].zName = "main";
  db->aDb[1].zName = "temp";

  /* Attempt to read the schema */
  sqliteRegisterBuiltinFunctions(db);
  rc = sqliteInit(db, pzErrMsg);
  db->magic = SQLITE_MAGIC_OPEN;
  if( sqlite_malloc_failed ){
    sqlite_close(db);
    goto no_mem_on_open;
  }else if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
    sqlite_close(db);
    sqliteStrRealloc(pzErrMsg);
    return 0;
  }else if( pzErrMsg ){
    sqliteFree(*pzErrMsg);
    *pzErrMsg = 0;
  }

  /* Return a pointer to the newly opened database structure */
  return db;

no_mem_on_open:
  sqliteSetString(pzErrMsg, "out of memory", (char*)0);
  sqliteStrRealloc(pzErrMsg);
  return 0;
}