📄 sqlora.c
字号:
#ifdef ENABLE_THREADS if (!_init_mux_initialized) _init_init_mux(); return (_mutex_lock(&_init_mux) );#else return (OCI_SUCCESS);#endif}/*-------------------------------------------------------------------------*//** * Unlock @ref _init_mux. * @return OCI status */#if CC_PRAGMA_INLINE#pragma INLINE _init_unlock#endifstatic inline intDEFUN_VOID(_init_unlock){#ifdef ENABLE_THREADS return (_mutex_unlock(&_init_mux) );#else return (OCI_SUCCESS);#endif}/*---------------------------------------------------------------------------*//** * Returns the thread id of this thread. * @return The thread id */static inline sqlo_thread_tDEFUN_VOID(_get_thread_id) {#ifdef ENABLE_THREADS# ifdef ENABLE_PTHREADS return (pthread_self());# elif ENABLE_ORATHREADS sqlo_thread_t tid = NULL; OCIThreadIdInit(_oci_envhp, _oci_errhp, &tid); OCIThreadIdGet(_oci_envhp, _oci_errhp, tid); return (tid);# elif ENABLE_WINTHREADS return (0);# endif#endif return (0);}/*-------------------------------------------------------------------------*//** * Sets the attribute PREFETCH_ROWS. * @param stp I - statement pointer * @param nrows I - Number of rows to prefetch * @return OCI status code (dbp->status) */#if CC_PRAGMA_INLINE#pragma INLINE _set_prefetch_rows#endifstatic inline intDEFUN(_set_prefetch_rows, (stp, nrows), sqlo_stmt_struct_ptr_t stp AND unsigned int nrows){ unsigned int prefetch_rows = nrows; sqlo_db_struct_ptr_t dbp; assert( stp != NULL ); assert( stp->dbp != NULL ); assert( stp->stmthp != NULL ); assert( stp->dbp->errhp != NULL ); dbp = stp->dbp; dbp->status = OCIAttrSet( (dvoid*)stp->stmthp, (ub4)OCI_HTYPE_STMT, &prefetch_rows, (ub4) sizeof(prefetch_rows), (ub4)OCI_ATTR_PREFETCH_ROWS, dbp->errhp ); CHECK_OCI_STATUS_RETURN( dbp, dbp->status, "_get_stmt_state", "OCIAttrGet"); return ( dbp->status );}/*-------------------------------------------------------------------------*//** * Gets the statement text and puts it into stp->stmt. * * @param stp I - statement pointer * @return OCI status code */#if CC_PRAGMA_INLINE#pragma INLINE _get_stmt_string#endifstatic inline const char *DEFUN(_get_stmt_string, (stp), sqlo_stmt_struct_ptr_t stp ){ static const char *nostmt = "_get_stmt_string: No statement avalailable"; if (!stp->stmt) return ( nostmt ); return stp->stmt;}/*-------------------------------------------------------------------------*//** * Gets the statement state * * @param stp I - statement pointer * @return The statement state or -1 on error. */#if CC_PRAGMA_INLINE#pragma INLINE _get_stmt_state#endifstatic inline intDEFUN(_get_stmt_state, (stp), sqlo_stmt_struct_ptr_t stp ){ ub4 st = 0; sqlo_db_struct_ptr_t dbp; assert( stp != NULL ); assert( stp->dbp != NULL ); assert( dbp->stmthp != NULL ); assert( dbp->errhp != NULL ); dbp = stp->dbp; /* OCI_ATTR_STMT_STATE is not defined in Oracle < 9i * I define this here, to make it compile with Oracle 8,8i, * but nobody should call this * function */#ifndef OCI_ATTR_STMT_STATE#define OCI_ATTR_STMT_STATE 182#endif dbp->status = OCIAttrGet( (dvoid*)stp->stmthp, (ub4)OCI_HTYPE_STMT, (dvoid *)&st, (ub4 *) 0, (ub4)OCI_ATTR_STMT_STATE, dbp->errhp ); CHECK_OCI_STATUS_RETURN( dbp, dbp->status, "_get_stmt_state", "OCIAttrGet"); return ((int)st);}/*-------------------------------------------------------------------------*//** * Looks for a free entry in _dbv. * First we try to find an already allocated but unused one. If this * failes, free_idx points to the first free, but unallocated entry. * @param free_idxp O - Returns the index where we can put in our new * db. * @return SQLO_SUCCESS if free_idx contains the new index, SQLO_ERROR if * no free slots are available anymore. */static intDEFUN(_find_free_dbv_entry, (free_idx), unsigned int * free_idxp){ register unsigned int dbv_idx; int first_null_idx; /* first null slot */ bool_t found_unused; int status; assert(free_idxp); *free_idxp = 0; found_unused = FALSE; first_null_idx = -1; /* -1 marks the variable as not set */ /* Scan the _dbv array until we found an unused slot */ for (dbv_idx = 0; dbv_idx < _dbv_size && found_unused == FALSE; ++dbv_idx) { if ((_dbv[ dbv_idx ]) && (! _dbv[ dbv_idx ]->used)) { /* free slot found */ *free_idxp = dbv_idx; found_unused = TRUE; } else if ( -1 == first_null_idx && !_dbv[ dbv_idx ]) { first_null_idx = dbv_idx; } } /* either we found an unused slot (found_unused == TRUE), or * we have and empty slot (first_null_idx >= 0) or we found * nothing */ if ( TRUE == found_unused || first_null_idx >= 0 ) { status = SQLO_SUCCESS; /* return the index of the first null slot if no unused one was found */ if ( FALSE == found_unused ) *free_idxp = first_null_idx; } else { status = SQLO_ERROR; } TRACE(4, fprintf(_trace_fp, "_find_free_dbv_entry: status=%d, free_idx=%u " "first_null_idx=%d\n", status, *free_idxp, first_null_idx);); return status;}/*-------------------------------------------------------------------------*//** Allocates and initializes a new entry in _dbv[ dbv_idx ] * @param dbv_idx I - the index in _dbv where we allocate the new db structure * @return SQLO_SUCCESS or SQLO_ERROR on memory allocation error. */static intDEFUN(_db_alloc, (dbv_idx), unsigned int dbv_idx){ /* allocate a new entry at free_idx position */ TRACE(3, fprintf(_trace_fp,"_db_alloc: Add new handle at %u\n", dbv_idx);); _dbv[ dbv_idx ] = MALLOC(sizeof(sqlo_db_struct_t)) ; if (_dbv[ dbv_idx ]) { TRACE(4, fprintf(_trace_fp,"_db_add: Allocated %d bytes\n", (int) sizeof(sqlo_db_struct_t));); /* initialize the db structure */ memset(_dbv[ dbv_idx ], 0, sizeof(sqlo_db_struct_t)) ; } return _dbv[ dbv_idx ] != NULL ? SQLO_SUCCESS : SQLO_ERROR;}/*-------------------------------------------------------------------------*//** Allocates and initializes the stmt vector _dbv[ dbv_idx ]->stmtv * * @param dbv_idx I - the index in _dbv where we allocate the * new db structure * * @return SQLO_SUCCESS or SQLO_ERRMALLOC on memory allocation error. */static intDEFUN(_stmtv_alloc, (dbv_idx), unsigned int dbv_idx){ /* allocate the stmtv arrays in the _dbv */ if ( NULL == _dbv[ dbv_idx ]->stmtv ) { TRACE(4, fprintf(_trace_fp,"_stmtv_alloc: Alloc stmtv for %d cursors at %u\n", _max_cursors, dbv_idx);); ERRMALLOC( _dbv[ dbv_idx], _dbv[ dbv_idx ]->stmtv, _max_cursors * sizeof(sqlo_stmt_struct_t), "_stmtv_alloc", SQLO_ERRMALLOC); _dbv[ dbv_idx ]->stmtv_size = _max_cursors; /* init the memory */ memset( _dbv[ dbv_idx ]->stmtv, 0, _max_cursors * sizeof(sqlo_stmt_struct_t)); } return SQLO_SUCCESS;}/*-------------------------------------------------------------------------*//** * Adds a new database entry to @ref _dbv[]. * Alllocates a new entry entry if necessary, or reuses an old one. * @return The pointer to the entry. The entry is marked used. */static sqlo_db_struct_ptr_t DEFUN_VOID(_db_add){ unsigned int free_idx; int status; TRACE(4, fprintf(_trace_fp, "_db_add starts _dbv_size=%u\n", _dbv_size);); /* check for initialization */ if ((! _sqlo_init) || (_dbv_size <= 0)) return (NULL) ; EXEC_WHEN_THREADING(_dbv_lock();); /* start of critical section */ status = _find_free_dbv_entry(&free_idx); if (status != SQLO_SUCCESS) { /* no more slots available? */ EXEC_WHEN_THREADING(_dbv_unlock();); /* end of critical section */ return NULL; } /* If the slot is not allocated yet, we do it first */ if ( !_dbv[ free_idx ] ) { if (SQLO_SUCCESS != _db_alloc(free_idx) ) { EXEC_WHEN_THREADING(_dbv_unlock();); /* end of critical section */ return NULL; } } /* Mark the slot as used */ TRACE(3, fprintf(_trace_fp, "_db_add: Using db handle %u\n", free_idx);); _dbv[ free_idx ]->dbh = free_idx; _dbv[ free_idx ]->used = TRUE ; /* set the in use flag *before* releasing the mutex */ EXEC_WHEN_THREADING( _dbv_unlock(); ); /* end of critical section */ EXEC_WHEN_THREADING( _dbv[ free_idx ]->thread_id = _get_thread_id(); ); /* allocate the stmtv arrays in the _dbv */ if ( NULL == _dbv[ free_idx ]->stmtv ) { if (SQLO_SUCCESS != _stmtv_alloc(free_idx)) { return (NULL); } } return (_dbv[ free_idx ]) ;}/*-------------------------------------------------------------------------*//** * Releases a statement. * Release is done by setting used to 0. */#ifdef CC_PRAGMA_INLINE#define PRAGMA INLINE _stmt_release#endifstatic inline voidDEFUN(_stmt_release, (stp), sqlo_stmt_struct_ptr_t stp){ if (stp) { stp->used = FALSE; stp->opened = FALSE; stp->prepared = FALSE; stp->still_executing = FALSE; stp->num_executions = 0; }}/*-------------------------------------------------------------------------*//** * Release a database in _dbv[]. * Marks it as unused, frees all allocated handles and marks all sths as free. * * @param dbp - I The pointer to the database structure */static voidDEFUN(_db_release, (dbp), sqlo_db_struct_ptr_t dbp){ unsigned int i; if (!dbp) return; TRACE(2, fprintf(_get_trace_fp(dbp), "_db_release[%u] starts\n", dbp->dbh); );#ifdef ENABLE_ORATHREADS OCIThreadIdDestroy(dbp->envhp, dbp->errhp, &dbp->thread_id);#endif if (dbp->srvhp) (void)OCIHandleFree((dvoid *) dbp->srvhp, OCI_HTYPE_SERVER); if (dbp->svchp) (void)OCIHandleFree((dvoid *) dbp->svchp, OCI_HTYPE_SVCCTX); if (dbp->errhp) (void)OCIHandleFree((dvoid *) dbp->errhp, OCI_HTYPE_ERROR); if (dbp->authp) (void)OCIHandleFree((dvoid *) dbp->authp, OCI_HTYPE_SESSION); if (dbp->envhp) (void)OCIHandleFree((dvoid *) dbp->envhp, OCI_HTYPE_ENV); dbp->srvhp = NULL; dbp->svchp = NULL; dbp->errhp = NULL; dbp->authp = NULL; dbp->envhp = NULL; FREE(dbp->tnsname); /* close the trace file */ if (TRACE_ENABLED && _trace_level > 0) _close_session_trace_file(dbp); dbp->thread_id = 0; dbp->errcode = 0; *(dbp->errmsg) = '\0'; dbp->status = 0; /* release all sths */ for (i = 0; i < dbp->stmtv_size; ++i) { _stmt_release( &(dbp->stmtv[ i ]) ); } EXEC_WHEN_THREADING( _dbv_lock(); ); /* start of critical section */ dbp->attached = FALSE; dbp->session_created = FALSE; dbp->used = FALSE; EXEC_WHEN_THREADING( _dbv_unlock(); ); /* end of critical section */ }/*-------------------------------------------------------------------------*//** * Resets the number of bindpv entries. */static voidDEFUN(_bindpv_reset, (stp), sqlo_stmt_struct_ptr_t stp){ unsigned int bindp_idx; if (stp) { for (bindp_idx = 0; bindp_idx < stp->num_bindpv; ++bindp_idx) { stp->bindpv[ bindp_idx ] = NULL; } stp->num_bindpv = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -