mirror of
https://github.com/bdnugget/php-src.git
synced 2026-11-18 06:26:37 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
553c09e438 | ||
|
|
361f0489c8 |
No files matched your search
+364
@@ -0,0 +1,364 @@
|
||||
#include "TSRM.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct _tsrm_tls_entry tsrm_tls_entry;
|
||||
|
||||
struct _tsrm_tls_entry {
|
||||
void **storage;
|
||||
int count;
|
||||
THREAD_T thread_id;
|
||||
tsrm_tls_entry *next;
|
||||
};
|
||||
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
void (*ctor)(void *resource);
|
||||
void (*dtor)(void *resource);
|
||||
} tsrm_resource_type;
|
||||
|
||||
|
||||
/* The memory manager table */
|
||||
static tsrm_tls_entry **tsrm_tls_table;
|
||||
static int tsrm_tls_table_size;
|
||||
static ts_rsrc_id id_count;
|
||||
|
||||
/* The resource sizes table */
|
||||
static tsrm_resource_type *resource_types_table;
|
||||
static int resource_types_table_size;
|
||||
|
||||
|
||||
static MUTEX_T tsmm_mutex; /* thread-safe memory manager mutex */
|
||||
|
||||
/* Debug support */
|
||||
static int tsrm_debug(const char *format, ...);
|
||||
static int tsrm_debug_status;
|
||||
|
||||
|
||||
/* Startup TSRM (call once for the entire process) */
|
||||
int tsrm_startup(int expected_threads, int expected_resources, int debug_status)
|
||||
{
|
||||
tsrm_tls_table_size = expected_threads;
|
||||
tsrm_tls_table = (tsrm_tls_entry **) calloc(tsrm_tls_table_size, sizeof(tsrm_tls_entry *));
|
||||
if (!tsrm_tls_table) {
|
||||
return 0;
|
||||
}
|
||||
id_count=0;
|
||||
|
||||
resource_types_table_size = expected_resources;
|
||||
resource_types_table = (tsrm_resource_type *) calloc(resource_types_table_size, sizeof(tsrm_resource_type));
|
||||
if (!resource_types_table) {
|
||||
free(tsrm_tls_table);
|
||||
return 0;
|
||||
}
|
||||
|
||||
tsmm_mutex = tsrm_mutex_alloc();
|
||||
|
||||
tsrm_debug_status = debug_status;
|
||||
|
||||
tsrm_debug("Started up TSRM, %d expected threads, %d expected resources\n", expected_threads, expected_resources);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* Shutdown TSRM (call once for the entire process) */
|
||||
void tsrm_shutdown()
|
||||
{
|
||||
int i;
|
||||
|
||||
if (tsrm_tls_table) {
|
||||
for (i=0; i<tsrm_tls_table_size; i++) {
|
||||
tsrm_tls_entry *p = tsrm_tls_table[i], *next_p;
|
||||
|
||||
while (p) {
|
||||
int j;
|
||||
|
||||
next_p = p->next;
|
||||
for (j=0; j<id_count; j++) {
|
||||
free(p->storage[j]);
|
||||
}
|
||||
free(p->storage);
|
||||
free(p);
|
||||
p = next_p;
|
||||
}
|
||||
}
|
||||
free(tsrm_tls_table);
|
||||
}
|
||||
if (resource_types_table) {
|
||||
free(resource_types_table);
|
||||
}
|
||||
tsrm_mutex_free(tsmm_mutex);
|
||||
tsrm_debug("Shutdown TSRM\n");
|
||||
}
|
||||
|
||||
|
||||
/* allocates a new thread-safe-resource id */
|
||||
TSRM_FUNC ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource))
|
||||
{
|
||||
ts_rsrc_id new_id;
|
||||
int i;
|
||||
|
||||
tsrm_debug("Obtaining a new resource id, %d bytes\n", size);
|
||||
|
||||
tsrm_mutex_lock(tsmm_mutex);
|
||||
|
||||
/* obtain a resource id */
|
||||
new_id = id_count++;
|
||||
tsrm_debug("Obtained resource id %d\n", new_id);
|
||||
|
||||
/* store the new resource type in the resource sizes table */
|
||||
if (resource_types_table_size < id_count) {
|
||||
resource_types_table = (tsrm_resource_type *) realloc(resource_types_table, sizeof(tsrm_resource_type)*id_count);
|
||||
if (!resource_types_table) {
|
||||
return -1;
|
||||
}
|
||||
resource_types_table_size = id_count;
|
||||
}
|
||||
resource_types_table[new_id].size = size;
|
||||
resource_types_table[new_id].ctor = ctor;
|
||||
resource_types_table[new_id].dtor = dtor;
|
||||
|
||||
/* enlarge the arrays for the already active threads */
|
||||
for (i=0; i<tsrm_tls_table_size; i++) {
|
||||
tsrm_tls_entry *p = tsrm_tls_table[i];
|
||||
|
||||
while (p) {
|
||||
if (p->count < id_count) {
|
||||
int j;
|
||||
|
||||
p->storage = realloc(p->storage, sizeof(void *)*id_count);
|
||||
for (j=p->count; j<id_count; j++) {
|
||||
p->storage[j] = (void *) malloc(resource_types_table[j].size);
|
||||
}
|
||||
p->count = id_count;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
tsrm_mutex_unlock(tsmm_mutex);
|
||||
|
||||
tsrm_debug("Successfully allocated new resource id %d\n", new_id);
|
||||
return new_id;
|
||||
}
|
||||
|
||||
|
||||
static void allocate_new_resource(tsrm_tls_entry **thread_resources_ptr, THREAD_T thread_id)
|
||||
{
|
||||
int i;
|
||||
|
||||
(*thread_resources_ptr) = (tsrm_tls_entry *) malloc(sizeof(tsrm_tls_entry));
|
||||
(*thread_resources_ptr)->storage = (void **) malloc(sizeof(void *)*id_count);
|
||||
(*thread_resources_ptr)->count = id_count;
|
||||
(*thread_resources_ptr)->thread_id = thread_id;
|
||||
(*thread_resources_ptr)->next = NULL;
|
||||
for (i=0; i<id_count; i++) {
|
||||
(*thread_resources_ptr)->storage[i] = (void *) malloc(resource_types_table[i].size);
|
||||
resource_types_table[i].ctor((*thread_resources_ptr)->storage[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* fetches the requested resource for the current thread */
|
||||
void *ts_resource(ts_rsrc_id id)
|
||||
{
|
||||
THREAD_T thread_id = tsrm_thread_id();
|
||||
int hash_value;
|
||||
tsrm_tls_entry *thread_resources;
|
||||
void *resource;
|
||||
|
||||
tsrm_debug("Fetching resource id %d for thread %ld\n", id, (long) thread_id);
|
||||
tsrm_mutex_lock(tsmm_mutex);
|
||||
|
||||
hash_value = THREAD_HASH_OF(thread_id, tsrm_tls_table_size);
|
||||
thread_resources = tsrm_tls_table[hash_value];
|
||||
|
||||
if (!thread_resources) {
|
||||
allocate_new_resource(&tsrm_tls_table[hash_value], thread_id);
|
||||
thread_resources = tsrm_tls_table[hash_value];
|
||||
} else {
|
||||
do {
|
||||
if (thread_resources->thread_id == thread_id) {
|
||||
break;
|
||||
}
|
||||
if (thread_resources->next) {
|
||||
thread_resources = thread_resources->next;
|
||||
} else {
|
||||
allocate_new_resource(&thread_resources->next, thread_id);
|
||||
thread_resources = thread_resources->next;
|
||||
break;
|
||||
}
|
||||
} while (thread_resources);
|
||||
}
|
||||
|
||||
resource = thread_resources->storage[id];
|
||||
|
||||
tsrm_mutex_unlock(tsmm_mutex);
|
||||
|
||||
tsrm_debug("Successfully fetched resource id %d for thread id %ld - %x\n", id, (long) thread_id, (long) resource);
|
||||
return resource;
|
||||
}
|
||||
|
||||
|
||||
/* frees all resources allocated for the current thread */
|
||||
void ts_free_thread()
|
||||
{
|
||||
THREAD_T thread_id = tsrm_thread_id();
|
||||
int hash_value;
|
||||
tsrm_tls_entry *thread_resources;
|
||||
tsrm_tls_entry *last=NULL;
|
||||
|
||||
tsrm_mutex_lock(tsmm_mutex);
|
||||
hash_value = THREAD_HASH_OF(thread_id, tsrm_tls_table_size);
|
||||
thread_resources = tsrm_tls_table[hash_value];
|
||||
|
||||
while (thread_resources) {
|
||||
if (thread_resources->thread_id == thread_id) {
|
||||
int i;
|
||||
|
||||
for (i=0; i<thread_resources->count; i++) {
|
||||
resource_types_table[i].dtor(thread_resources->storage[i]);
|
||||
free(thread_resources->storage[i]);
|
||||
}
|
||||
free(thread_resources->storage);
|
||||
if (last) {
|
||||
last->next = thread_resources->next;
|
||||
} else {
|
||||
tsrm_tls_table[hash_value]=NULL;
|
||||
}
|
||||
free(thread_resources);
|
||||
break;
|
||||
}
|
||||
if (thread_resources->next) {
|
||||
last = thread_resources;
|
||||
thread_resources = thread_resources->next;
|
||||
}
|
||||
}
|
||||
tsrm_mutex_unlock(tsmm_mutex);
|
||||
}
|
||||
|
||||
|
||||
/* deallocates all occurrences of a given id */
|
||||
void ts_free_id(ts_rsrc_id id)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Utility Functions
|
||||
*/
|
||||
|
||||
/* Obtain the current thread id */
|
||||
TSRM_FUNC THREAD_T tsrm_thread_id(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return GetCurrentThreadId();
|
||||
#elif defined(PTHREADS)
|
||||
return pthread_self();
|
||||
#elif defined(NSAPI)
|
||||
return systhread_current();
|
||||
#elif defined(PI3WEB)
|
||||
return PIThread_getCurrent();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Allocate a mutex */
|
||||
TSRM_FUNC MUTEX_T tsrm_mutex_alloc( void )
|
||||
{
|
||||
MUTEX_T mutexp;
|
||||
|
||||
#ifdef WIN32
|
||||
mutexp = CreateMutex(NULL,FALSE,NULL);
|
||||
#elif defined(PTHREADS)
|
||||
mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
|
||||
pthread_mutex_init(mutexp,NULL);
|
||||
#elif defined(NSAPI)
|
||||
mutexp = crit_init();
|
||||
#elif defined(PI3WEB)
|
||||
mutexp = PIPlatform_allocLocalMutex();
|
||||
#endif
|
||||
#ifdef THR_DEBUG
|
||||
printf("Mutex created thread: %d\n",mythreadid());
|
||||
#endif
|
||||
return( mutexp );
|
||||
}
|
||||
|
||||
|
||||
/* Free a mutex */
|
||||
TSRM_FUNC void tsrm_mutex_free( MUTEX_T mutexp )
|
||||
{
|
||||
if (mutexp) {
|
||||
#ifdef WIN32
|
||||
CloseHandle(mutexp);
|
||||
#elif defined(PTHREADS)
|
||||
free(mutexp);
|
||||
#elif defined(NSAPI)
|
||||
crit_terminate(mutexp);
|
||||
#elif defined(PI3WEB)
|
||||
PISync_delete(mutexp)
|
||||
#endif
|
||||
}
|
||||
#ifdef THR_DEBUG
|
||||
printf("Mutex freed thread: %d\n",mythreadid());
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Lock a mutex */
|
||||
TSRM_FUNC int tsrm_mutex_lock( MUTEX_T mutexp )
|
||||
{
|
||||
//tsrm_debug("Mutex locked thread: %ld\n",tsrm_thread_id());
|
||||
#ifdef WIN32
|
||||
return WaitForSingleObject(mutexp,1000);
|
||||
#elif defined(PTHREADS)
|
||||
return pthread_mutex_lock(mutexp);
|
||||
#elif defined(NSAPI)
|
||||
return crit_enter(mutexp);
|
||||
#elif defined(PI3WEB)
|
||||
return PISync_lock(mutexp);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/* Unlock a mutex */
|
||||
TSRM_FUNC int tsrm_mutex_unlock( MUTEX_T mutexp )
|
||||
{
|
||||
//tsrm_debug("Mutex unlocked thread: %ld\n",tsrm_thread_id());
|
||||
#ifdef WIN32
|
||||
return ReleaseMutex(mutexp);
|
||||
#elif defined(PTHREADS)
|
||||
return pthread_mutex_unlock(mutexp);
|
||||
#elif defined(NSAPI)
|
||||
return crit_exit(mutexp);
|
||||
#elif defined(PI3WEB)
|
||||
return PISync_unlock(mutexp);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Debug support
|
||||
*/
|
||||
|
||||
static int tsrm_debug(const char *format, ...)
|
||||
{
|
||||
if (tsrm_debug_status) {
|
||||
va_list args;
|
||||
int size;
|
||||
|
||||
va_start(args, format);
|
||||
size = vprintf(format, args);
|
||||
va_end(args);
|
||||
return size;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void tsrm_debug_set(int status)
|
||||
{
|
||||
tsrm_debug_status = status;
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
# Microsoft Developer Studio Project File - Name="TSRM" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Static Library" 0x0104
|
||||
|
||||
CFG=TSRM - Win32 Debug
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "TSRM.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "TSRM.mak" CFG="TSRM - Win32 Debug"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "TSRM - Win32 Release" (based on "Win32 (x86) Static Library")
|
||||
!MESSAGE "TSRM - Win32 Debug" (based on "Win32 (x86) Static Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "TSRM - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "Release"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
|
||||
# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_MBCS" /D "_LIB" /YX /FD /c
|
||||
# ADD BASE RSC /l 0x40d /d "NDEBUG"
|
||||
# ADD RSC /l 0x40d /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LIB32=link.exe -lib
|
||||
# ADD BASE LIB32 /nologo
|
||||
# ADD LIB32 /nologo
|
||||
|
||||
!ELSEIF "$(CFG)" == "TSRM - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "Debug"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "C:\Projects\TSRM" /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /YX /FD /GZ /c
|
||||
# ADD BASE RSC /l 0x40d /d "_DEBUG"
|
||||
# ADD RSC /l 0x40d /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LIB32=link.exe -lib
|
||||
# ADD BASE LIB32 /nologo
|
||||
# ADD LIB32 /nologo
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "TSRM - Win32 Release"
|
||||
# Name "TSRM - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\TSRM.c
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=.\TSRM.h
|
||||
# End Source File
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#ifndef _TSRM_H
|
||||
#define _TSRM_H
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
typedef int ts_rsrc_id;
|
||||
|
||||
|
||||
/* Define TSRM_FUNC */
|
||||
#ifdef __cplusplus
|
||||
#define TSRM_FUNC extern "C"
|
||||
#else
|
||||
#define TSRM_FUNC
|
||||
#endif
|
||||
|
||||
|
||||
/* Define THREAD_T and MUTEX_T */
|
||||
#if defined(WIN32)
|
||||
# define THREAD_T DWORD
|
||||
# define MUTEX_T void *
|
||||
#elif defined(PTHREADS)
|
||||
# define THREAD_T pthread_t
|
||||
# define MUTEX_T pthread_mutex_t *
|
||||
#elif defined(NSAPI)
|
||||
# define THREAD_T SYS_THREAD
|
||||
# define MUTEX_T CRITICAL
|
||||
#elif defined(PI3WEB)
|
||||
# define THREAD_T PIThread *
|
||||
# define MUTEX_T PISync *
|
||||
#endif
|
||||
|
||||
|
||||
#define THREAD_HASH_OF(thr,ts) thr%ts
|
||||
|
||||
|
||||
/* startup/shutdown */
|
||||
TSRM_FUNC int tsrm_startup(int expected_threads, int expected_resources, int debug_status);
|
||||
TSRM_FUNC void tsrm_shutdown();
|
||||
|
||||
/* allocates a new thread-safe-resource id */
|
||||
TSRM_FUNC ts_rsrc_id ts_allocate_id(size_t size, void (*ctor)(void *resource), void (*dtor)(void *resource));
|
||||
|
||||
/* fetches the requested resource for the current thread */
|
||||
TSRM_FUNC void *ts_resource(ts_rsrc_id id);
|
||||
|
||||
/* frees all resources allocated for the current thread */
|
||||
TSRM_FUNC void ts_free_thread();
|
||||
|
||||
/* deallocates all occurrences of a given id */
|
||||
TSRM_FUNC void ts_free_id(ts_rsrc_id id);
|
||||
|
||||
|
||||
/* Debug support */
|
||||
TSRM_FUNC void tsrm_debug_set(int status);
|
||||
|
||||
/* utility functions */
|
||||
TSRM_FUNC THREAD_T tsrm_thread_id(void);
|
||||
TSRM_FUNC MUTEX_T tsrm_mutex_alloc(void);
|
||||
TSRM_FUNC void tsrm_mutex_free(MUTEX_T mutexp);
|
||||
TSRM_FUNC int tsrm_mutex_lock(MUTEX_T mutexp);
|
||||
TSRM_FUNC int tsrm_mutex_unlock(MUTEX_T mutexp);
|
||||
|
||||
#endif /* _TSRM_H */
|
||||
@@ -0,0 +1 @@
|
||||
Work on the PDO specification to live here
|
||||
-423
@@ -1,423 +0,0 @@
|
||||
<?php
|
||||
//
|
||||
// +----------------------------------------------------------------------+
|
||||
// | PHP version 4.0 |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Copyright (c) 1997, 1998, 1999 The PHP Group |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | This source file is subject to version 2.0 of the PHP license, |
|
||||
// | that is bundled with this package in the file LICENSE, and is |
|
||||
// | available at through the world-wide-web at |
|
||||
// | http://www.php.net/license/2_0.txt. |
|
||||
// | If you did not receive a copy of the PHP license and are unable to |
|
||||
// | obtain it through the world-wide-web, please send a note to |
|
||||
// | license@php.net so we can mail you a copy immediately. |
|
||||
// +----------------------------------------------------------------------+
|
||||
// | Authors: Stig Bakken <ssb@fast.no> |
|
||||
// | |
|
||||
// +----------------------------------------------------------------------+
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Database independent query interface.
|
||||
//
|
||||
|
||||
// {{{ Database independent error codes.
|
||||
|
||||
/*
|
||||
* The method mapErrorCode in each DB_dbtype implementation maps
|
||||
* native error codes to one of these.
|
||||
*
|
||||
* If you add an error code here, make sure you also add a textual
|
||||
* version of it in DB::errorMessage().
|
||||
*/
|
||||
define("DB_OK", 0);
|
||||
define("DB_ERROR", -1);
|
||||
define("DB_ERROR_SYNTAX", -2);
|
||||
define("DB_ERROR_CONSTRAINT", -3);
|
||||
define("DB_ERROR_NOT_FOUND", -4);
|
||||
define("DB_ERROR_ALREADY_EXISTS", -5);
|
||||
define("DB_ERROR_UNSUPPORTED", -6);
|
||||
define("DB_ERROR_MISMATCH", -7);
|
||||
define("DB_ERROR_INVALID", -8);
|
||||
define("DB_ERROR_NOT_CAPABLE", -9);
|
||||
define("DB_ERROR_TRUNCATED", -10);
|
||||
define("DB_ERROR_INVALID_NUMBER", -11);
|
||||
define("DB_ERROR_INVALID_DATE", -12);
|
||||
define("DB_ERROR_DIVZERO", -13);
|
||||
define("DB_ERROR_NODBSELECTED", -14);
|
||||
define("DB_ERROR_CANNOT_CREATE", -15);
|
||||
define("DB_ERROR_CANNOT_DELETE", -16);
|
||||
define("DB_ERROR_CANNOT_DROP", -17);
|
||||
define("DB_ERROR_NOSUCHTABLE", -18);
|
||||
define("DB_ERROR_NOSUCHFIELD", -19);
|
||||
|
||||
// }}}
|
||||
// {{{ Prepare/execute parameter types
|
||||
|
||||
/*
|
||||
* These constants are used when storing information about prepared
|
||||
* statements (using the "prepare" method in DB_dbtype).
|
||||
*
|
||||
* The prepare/execute model in DB is borrowed from the ODBC extension,
|
||||
* in a query the "?" character means a scalar parameter, and a "*"
|
||||
* character means an opaque parameter. An opaque parameter is simply
|
||||
* a file name, the real data are in that file (useful for large blob
|
||||
* operations).
|
||||
*/
|
||||
define("DB_PARAM_SCALAR", 1);
|
||||
define("DB_PARAM_OPAQUE", 2);
|
||||
|
||||
// }}}
|
||||
// {{{ Binary data modes
|
||||
|
||||
/*
|
||||
* These constants define different ways of returning binary data
|
||||
* from queries. Again, this model has been borrowed from the ODBC
|
||||
* extension.
|
||||
*
|
||||
* DB_BINMODE_PASSTHRU sends the data directly through to the browser
|
||||
* when data is fetched from the database.
|
||||
* DB_BINMODE_RETURN lets you return data as usual.
|
||||
* DB_BINMODE_CONVERT returns data as well, only it is converted to
|
||||
* hex format, for example the string "123" would become "313233".
|
||||
*/
|
||||
define("DB_BINMODE_PASSTHRU", 1);
|
||||
define("DB_BINMODE_RETURN", 2);
|
||||
define("DB_BINMODE_CONVERT", 3);
|
||||
|
||||
// }}}
|
||||
|
||||
/**
|
||||
* The main "DB" class is simply a container class with some static
|
||||
* methods for creating DB objects as well as some utility functions
|
||||
* common to all parts of DB.
|
||||
*
|
||||
* The object model of DB is as follows (indentation means inheritance):
|
||||
*
|
||||
* DB The main DB class. This is simply a utility class
|
||||
* with some "static" methods for creating DB objects as
|
||||
* well as common utility functions for other DB classes.
|
||||
*
|
||||
* DB_common The base for each DB implementation. Provides default
|
||||
* | implementations (some would say virtual methods) for
|
||||
* | the actual DB implementations as well as a bunch of
|
||||
* | query utility functions.
|
||||
* |
|
||||
* +-DB_mysql The DB implementation for MySQL. Inherits DB_common.
|
||||
* When calling DB::factory or DB::connect for MySQL
|
||||
* connections, the object returned is an instance of this
|
||||
* class.
|
||||
*
|
||||
* @version 1.00
|
||||
* @author Stig Bakken <ssb@fast.no>
|
||||
* @since PHP 4.0
|
||||
*/
|
||||
class DB {
|
||||
// {{{ factory()
|
||||
|
||||
/**
|
||||
* Create a new DB object for the specified database type.
|
||||
*
|
||||
* @param $type database type
|
||||
*
|
||||
* @return object a newly created DB object, or a DB error code on
|
||||
* error
|
||||
*/
|
||||
function factory($type) {
|
||||
global $USED_PACKAGES;
|
||||
// "include" should be replaced with "use" once PHP gets it
|
||||
$pkgname = 'DB/' . $type;
|
||||
if (!is_array($USED_PACKAGES) || !$USED_PACKAGES[$pkgname]) {
|
||||
if (!@include("DB/${type}.php")) {
|
||||
return DB_ERROR_NOT_FOUND;
|
||||
} else {
|
||||
$USED_PACKAGES[$pkgname] = true;
|
||||
}
|
||||
}
|
||||
$classname = 'DB_' . $type;
|
||||
$obj = new $classname;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ connect()
|
||||
|
||||
/**
|
||||
* Create a new DB object and connect to the specified database.
|
||||
*
|
||||
* @param $dsn "data source name", see the parseDSN method for a
|
||||
* description of the dsn format.
|
||||
*
|
||||
* @param $persistent (optional) whether this connection should be
|
||||
* persistent. Ignored if the backend extension does not support
|
||||
* persistent connections.
|
||||
*
|
||||
* @return object a newly created DB object, or a DB error code on
|
||||
* error
|
||||
*/
|
||||
function connect($dsn, $persistent = false) {
|
||||
global $USED_PACKAGES;
|
||||
|
||||
$dsninfo = DB::parseDSN($dsn);
|
||||
$type = $dsninfo['phptype'];
|
||||
// "include" should be replaced with "use" once PHP gets it
|
||||
$pkgname = 'DB/' . $type;
|
||||
if (!is_array($USED_PACKAGES) || !$USED_PACKAGES[$pkgname]) {
|
||||
if (!@include($pkgname . '.php')) {
|
||||
return DB_ERROR_NOT_FOUND;
|
||||
} else {
|
||||
$USED_PACKAGES[$pkgname] = true;
|
||||
}
|
||||
}
|
||||
$classname = 'DB_' . $type;
|
||||
$obj = new $classname;
|
||||
$err = $obj->connect(&$dsninfo, $persistent);
|
||||
if (DB::isError($err)) {
|
||||
return $err;
|
||||
}
|
||||
return $obj; // XXX ADDREF
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ apiVersion()
|
||||
|
||||
/**
|
||||
* Return the DB API version.
|
||||
*
|
||||
* @return double the DB API version number
|
||||
*/
|
||||
function apiVersion() {
|
||||
return 1.00;
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ isError()
|
||||
|
||||
/**
|
||||
* Tell whether a result code from a DB method is an error.
|
||||
*
|
||||
* @param $code result code
|
||||
*
|
||||
* @return bool whether $code is an error
|
||||
*/
|
||||
function isError($code) {
|
||||
return is_int($code) && ($code < 0);
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ errorMessage()
|
||||
|
||||
/**
|
||||
* Return a textual error message for a DB error code.
|
||||
*
|
||||
* @param $code error code
|
||||
*
|
||||
* @return string error message, or false if the error code was
|
||||
* not recognized
|
||||
*/
|
||||
function errorMessage($code) {
|
||||
if (!is_array($errorMessages)) {
|
||||
$errorMessages = array(
|
||||
DB_OK => "no error",
|
||||
DB_ERROR => "unknown error",
|
||||
DB_ERROR_SYNTAX => "syntax error",
|
||||
DB_ERROR_CONSTRAINT => "constraint violation",
|
||||
DB_ERROR_NOT_FOUND => "not found",
|
||||
DB_ERROR_ALREADY_EXISTS => "already exists",
|
||||
DB_ERROR_UNSUPPORTED => "not supported",
|
||||
DB_ERROR_MISMATCH => "mismatch",
|
||||
DB_ERROR_INVALID => "invalid",
|
||||
DB_ERROR_NOT_CAPABLE => "DB implementation not capable",
|
||||
DB_ERROR_INVALID_NUMBER => "invalid number",
|
||||
DB_ERROR_INVALID_DATE => "invalid date or time",
|
||||
DB_ERROR_DIVZERO => "division by zero",
|
||||
DB_ERROR_NODBSELECTED => "no database selected",
|
||||
DB_ERROR_CANNOT_CREATE => "can not create",
|
||||
DB_ERROR_CANNOT_DELETE => "can not delete",
|
||||
DB_ERROR_CANNOT_DROP => "can not drop",
|
||||
DB_ERROR_NOSUCHTABLE => "no such table",
|
||||
DB_ERROR_NOSUCHFIELD => "no such field"
|
||||
);
|
||||
}
|
||||
return $errorMessages[$code];
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ parseDSN()
|
||||
|
||||
/**
|
||||
* Parse a data source name and return an associative array with
|
||||
* the following keys:
|
||||
* phptype Database backend used in PHP (mysql, odbc etc.)
|
||||
* dbsyntax Database used with regards to SQL syntax etc.
|
||||
* protocol Communication protocol to use (tcp, unix etc.)
|
||||
* hostspec Host specification (hostname[:port])
|
||||
* database Database to use on the DBMS server
|
||||
* username User name for login
|
||||
* password Password for login
|
||||
*
|
||||
* The format of the supplied DSN is in its fullest form:
|
||||
* phptype(dbsyntax)://username:password@protocol+hostspec/database
|
||||
* Most variations are allowed:
|
||||
* phptype://username:password@protocol+hostspec/database
|
||||
* phptype://username:password@hostspec/database
|
||||
* phptype://username:password@hostspec
|
||||
* phptype://hostspec/database
|
||||
* phptype://hostspec
|
||||
* phptype(dbsyntax)
|
||||
* phptype
|
||||
*
|
||||
* FALSE is returned on error.
|
||||
*
|
||||
* @param $dsn Data Source Name to be parsed
|
||||
*/
|
||||
function parseDSN($dsn) {
|
||||
$parsed = array(
|
||||
'phptype' => false,
|
||||
'dbsyntax' => false,
|
||||
'protocol' => false,
|
||||
'hostspec' => false,
|
||||
'database' => false,
|
||||
'username' => false,
|
||||
'password' => false
|
||||
);
|
||||
if (preg_match('|^([^:]+)://|', $dsn, &$arr)) {
|
||||
$dbtype = $arr[1];
|
||||
$dsn = preg_replace('|^[^:]+://|', '', $dsn);
|
||||
// match "phptype(dbsyntax)"
|
||||
if (preg_match('|^([^\(]+)\((.+)\)$|', $dbtype, &$arr)) {
|
||||
$parsed['phptype'] = $arr[1];
|
||||
$parsed['dbsyntax'] = $arr[2];
|
||||
} else {
|
||||
$parsed['phptype'] = $dbtype;
|
||||
}
|
||||
} else {
|
||||
// match "phptype(dbsyntax)"
|
||||
if (preg_match('|^([^\(]+)\((.+)\)$|', $dsn, &$arr)) {
|
||||
$parsed['phptype'] = $arr[1];
|
||||
$parsed['dbsyntax'] = $arr[2];
|
||||
} else {
|
||||
$parsed['phptype'] = $dsn;
|
||||
}
|
||||
return $parsed; // XXX ADDREF
|
||||
}
|
||||
|
||||
if (preg_match('|^(.*)/([^/]+)/?$|', $dsn, &$arr)) {
|
||||
$parsed['database'] = $arr[2];
|
||||
$dsn = $arr[1];
|
||||
}
|
||||
|
||||
if (preg_match('|^([^:]+):([^@]+)@?(.*)$|', $dsn, &$arr)) {
|
||||
$parsed['username'] = $arr[1];
|
||||
$parsed['password'] = $arr[2];
|
||||
$dsn = $arr[3];
|
||||
} elseif (preg_match('|^([^:]+)@(.*)$|', $dsn, &$arr)) {
|
||||
$parsed['username'] = $arr[1];
|
||||
$dsn = $arr[3];
|
||||
}
|
||||
|
||||
if (preg_match('|^([^\+]+)\+(.*)$|', $dsn, &$arr)) {
|
||||
$parsed['protocol'] = $arr[1];
|
||||
$dsn = $arr[2];
|
||||
}
|
||||
|
||||
if (!$parsed['database'])
|
||||
$dsn = preg_replace('|/+$|', '', $dsn);
|
||||
|
||||
$parsed['hostspec'] = $dsn;
|
||||
|
||||
if (!$parsed['dbsyntax']) {
|
||||
$parsed['dbsyntax'] = $parsed['phptype'];
|
||||
}
|
||||
|
||||
return $parsed; // XXX ADDREF
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
/**
|
||||
* This class implements a wrapper for a DB result set.
|
||||
* A new instance of this class will be returned by the DB implementation
|
||||
* after processing a query that returns data.
|
||||
*/
|
||||
class DB_result {
|
||||
// {{{ properties
|
||||
|
||||
var $dbh;
|
||||
var $result;
|
||||
|
||||
// }}}
|
||||
// {{{ DB_result()
|
||||
|
||||
/**
|
||||
* DB_result constructor.
|
||||
* @param $dbh DB object reference
|
||||
* @param $result result resource id
|
||||
*/
|
||||
function DB_result(&$dbh, $result) {
|
||||
$this->dbh = &$dbh;
|
||||
$this->result = $result;
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ fetchRow()
|
||||
|
||||
/**
|
||||
* Fetch and return a row of data.
|
||||
* @return array a row of data, or false on error
|
||||
*/
|
||||
function fetchRow() {
|
||||
return $this->dbh->fetchRow($this->result);
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ fetchInto()
|
||||
|
||||
/**
|
||||
* Fetch a row of data into an existing array.
|
||||
*
|
||||
* @param $arr reference to data array
|
||||
* @return int error code
|
||||
*/
|
||||
function fetchInto(&$arr) {
|
||||
return $this->dbh->fetchInto($this->result, &$arr);
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ numCols()
|
||||
|
||||
/**
|
||||
* Get the the number of columns in a result set.
|
||||
*
|
||||
* @return int the number of columns, or a DB error code
|
||||
*/
|
||||
function numCols() {
|
||||
return $this->dbh->numCols($this->result);
|
||||
}
|
||||
|
||||
// }}}
|
||||
// {{{ free()
|
||||
|
||||
/**
|
||||
* Frees the resources allocated for this result set.
|
||||
* @return int error code
|
||||
*/
|
||||
function free() {
|
||||
$err = $this->dbh->freeResult($this->result);
|
||||
if (DB::isError($err)) {
|
||||
return $err;
|
||||
}
|
||||
$this->dbh = $this->result = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
// Local variables:
|
||||
// tab-width: 4
|
||||
// c-basic-offset: 4
|
||||
// End:
|
||||
?>
|
||||
@@ -1,11 +0,0 @@
|
||||
## Process this file with automake to produce Makefile.in
|
||||
|
||||
peardir=$(prefix)/lib/php
|
||||
pear_DBdir=$(prefix)/lib/php/DB
|
||||
|
||||
install-data-local:
|
||||
-$(mkinstalldirs) $(peardir) $(pear_DBdir) && \
|
||||
$(INSTALL_DATA) $(srcdir)/DB.php $(peardir) && \
|
||||
$(INSTALL_DATA) $(srcdir)/DB/common.php $(pear_DBdir) && \
|
||||
$(INSTALL_DATA) $(srcdir)/DB/odbc.php $(pear_DBdir) && \
|
||||
$(INSTALL_DATA) $(srcdir)/DB/mysql.php $(pear_DBdir)
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
PEAR - PHP Extension and Add-on Repository
|
||||
==========================================
|
||||
Dedicated to Malin Bakken, born 1999-11-21
|
||||
|
||||
WHAT IS PEAR?
|
||||
|
||||
PEAR is a code repository for PHP extensions and PHP library code
|
||||
similar to TeX's CTAN and Perl's CPAN.
|
||||
|
||||
The intention behind PEAR is to provide a means for library code
|
||||
authors to organize their code in a defined way shared by other
|
||||
developers, and to give the PHP community a single source for such
|
||||
code.
|
||||
|
||||
|
||||
ADMINISTRATION
|
||||
|
||||
This section will describe the rules for how files are structured and
|
||||
how functions and classes should be named.
|
||||
|
||||
|
||||
TOOLS
|
||||
|
||||
This section will describe the tools provided to PEAR users.
|
||||
|
||||
|
||||
CODING RULES AND GUIDELINES
|
||||
|
||||
* All PHP code must use <?php ?>. This is the only really portable tag.
|
||||
|
||||
* Before adding a new top-level directory ("DB" is one), discuss your
|
||||
intentions on php4beta@lists.php.net.
|
||||
Reference in new issue
Block a user