⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgsql.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
		if (Z_TYPE_PP(row) != IS_NULL) { 			convert_to_long_ex(row);			pgsql_row = Z_LVAL_PP(row);			pg_result->row = pgsql_row;			if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld", Z_LVAL_PP(row), Z_LVAL_PP(result));				RETURN_FALSE;			}		} else {			/* If 2nd param is NULL, use internal row counter to access next row */			pgsql_row = pg_result->row;			if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {				RETURN_FALSE;			}			pg_result->row++;		}	}	array_init(return_value);	for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {		if (PQgetisnull(pgsql_result, pgsql_row, i)) {			if (result_type & PGSQL_NUM) {				add_index_null(return_value, i);			}			if (result_type & PGSQL_ASSOC) {				field_name = PQfname(pgsql_result, i);				add_assoc_null(return_value, field_name);			}		} else {			element = PQgetvalue(pgsql_result, pgsql_row, i);			element_len = (element ? strlen(element) : 0);			if (element) {				char *data;				int data_len;				int should_copy=0;				if (PG(magic_quotes_runtime)) {					data = php_addslashes(element, element_len, &data_len, 0 TSRMLS_CC);				} else {					data = safe_estrndup(element, element_len);					data_len = element_len;				}							if (result_type & PGSQL_NUM) {					add_index_stringl(return_value, i, data, data_len, should_copy);					should_copy=1;				}							if (result_type & PGSQL_ASSOC) {					field_name = PQfname(pgsql_result, i);					add_assoc_stringl(return_value, field_name, data, data_len, should_copy);				}			}		}	}}/* }}} *//* {{{ proto array pg_fetch_row(resource result [, int row [, int result_type]])   Get a row as an enumerated array */ PHP_FUNCTION(pg_fetch_row){	php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM);}/* }}} *//* {{{ proto array pg_fetch_assoc(resource result [, int row])   Fetch a row as an assoc array */PHP_FUNCTION(pg_fetch_assoc){	/* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when	   there is 3rd parameter */	if (ZEND_NUM_ARGS() > 2)		WRONG_PARAM_COUNT;	php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC);}/* }}} *//* {{{ proto array pg_fetch_array(resource result [, int row [, int result_type]])   Fetch a row as an array */PHP_FUNCTION(pg_fetch_array){	php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH);}/* }}} *//* {{{ proto object pg_fetch_object(resource result [, int row])   Fetch a row as an object */PHP_FUNCTION(pg_fetch_object){	/* pg_fetch_object() allowed result_type used to be. 3rd parameter	   must be allowed for compatibility */	php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC);	if (Z_TYPE_P(return_value)==IS_ARRAY) {		object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));	}}/* }}} *//* {{{ proto array pg_fetch_all(resource result)   Fetch all rows into array */PHP_FUNCTION(pg_fetch_all){	zval *result;	PGresult *pgsql_result;	pgsql_result_handle *pg_result;	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r",							  &result) == FAILURE) {		return;	}	ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);	pgsql_result = pg_result->result;	array_init(return_value);	if (php_pgsql_result2array(pgsql_result, return_value TSRMLS_CC) == FAILURE) {		zval_dtor(return_value);		RETURN_FALSE;	}}/* }}} *//* {{{ proto bool pg_result_seek(resource result, int offset)   Set internal row offset */PHP_FUNCTION(pg_result_seek){	zval *result;	long row;	pgsql_result_handle *pg_result;	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &row) == FAILURE) {		return;	}	ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);	if (row < 0 || row >= PQntuples(pg_result->result)) {		RETURN_FALSE;	}		/* seek to offset */	pg_result->row = row;	RETURN_TRUE;}/* }}} */#define PHP_PG_DATA_LENGTH 1#define PHP_PG_DATA_ISNULL 2/* {{{ php_pgsql_data_info */static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type){	zval **result, **row, **field;	PGresult *pgsql_result;	pgsql_result_handle *pg_result;	int field_offset, pgsql_row;		if ((ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &result, &row, &field)==FAILURE) &&	    (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &result, &field)==FAILURE)) {		WRONG_PARAM_COUNT;	}		ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);	pgsql_result = pg_result->result;	if (ZEND_NUM_ARGS() == 2) {		if (pg_result->row < 0)			pg_result->row = 0;		pgsql_row = pg_result->row;		if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {			RETURN_FALSE;		}	} else {		convert_to_long_ex(row);		pgsql_row = Z_LVAL_PP(row);		if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld", Z_LVAL_PP(row), Z_LVAL_PP(result));			RETURN_FALSE;		}	}		switch(Z_TYPE_PP(field)) {		case IS_STRING:			convert_to_string_ex(field);			field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));			break;		default:			convert_to_long_ex(field);			field_offset = Z_LVAL_PP(field);			break;	}	if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");		RETURN_FALSE;	}		switch (entry_type) {		case PHP_PG_DATA_LENGTH:			Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset);			break;		case PHP_PG_DATA_ISNULL:			Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset);			break;	}	Z_TYPE_P(return_value) = IS_LONG;}/* }}} *//* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number)   Returns the printed length */PHP_FUNCTION(pg_field_prtlen){	php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);}/* }}} *//* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number)   Test if a field is NULL */PHP_FUNCTION(pg_field_is_null){	php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);}/* }}} *//* {{{ proto bool pg_free_result(resource result)   Free result memory */PHP_FUNCTION(pg_free_result){	zval **result;	pgsql_result_handle *pg_result;		if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {		WRONG_PARAM_COUNT;	}	ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);	if (Z_LVAL_PP(result) == 0) {		RETURN_FALSE;	}	zend_list_delete(Z_LVAL_PP(result));	RETURN_TRUE;}/* }}} *//* {{{ proto string pg_last_oid(resource result)   Returns the last object identifier */PHP_FUNCTION(pg_last_oid){	zval **result;	PGresult *pgsql_result;	pgsql_result_handle *pg_result;#ifdef HAVE_PQOIDVALUE	Oid oid;#endif	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) {		WRONG_PARAM_COUNT;	}		ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result);	pgsql_result = pg_result->result;#ifdef HAVE_PQOIDVALUE	oid = PQoidValue(pgsql_result);	if (oid == InvalidOid) {		RETURN_FALSE;	}	PGSQL_RETURN_OID(oid);#else	Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result);	if (Z_STRVAL_P(return_value)) {		RETURN_STRING(Z_STRVAL_P(return_value), 1);	}	RETURN_STRING(empty_string, 0);#endif}/* }}} *//* {{{ proto bool pg_trace(string filename [, string mode [, resource connection]])   Enable tracing a PostgreSQL connection */PHP_FUNCTION(pg_trace){	zval **z_filename, **z_mode, **pgsql_link = NULL;	int id = -1;	PGconn *pgsql;	char *mode = "w";	FILE *fp = NULL;	php_stream *stream;	id = PGG(default_link);	switch (ZEND_NUM_ARGS()) {		case 1:			if (zend_get_parameters_ex(1, &z_filename)==FAILURE) {				RETURN_FALSE;			}			CHECK_DEFAULT_LINK(id);			break;		case 2:			if (zend_get_parameters_ex(2, &z_filename, &z_mode)==FAILURE) {				RETURN_FALSE;			}			CHECK_DEFAULT_LINK(id);			convert_to_string_ex(z_mode);			mode = Z_STRVAL_PP(z_mode);			break;		case 3:			if (zend_get_parameters_ex(3, &z_filename, &z_mode, &pgsql_link)==FAILURE) {				RETURN_FALSE;			}			convert_to_string_ex(z_mode);			mode = Z_STRVAL_PP(z_mode);			break;		default:			ZEND_WRONG_PARAM_COUNT();			break;	}	if (pgsql_link == NULL && id == -1) {		RETURN_FALSE;	}		ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);	convert_to_string_ex(z_filename);	stream = php_stream_open_wrapper(Z_STRVAL_PP(z_filename), mode, ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL);	if (!stream) {		RETURN_FALSE;	}	if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)fp, REPORT_ERRORS))	{		php_stream_close(stream);		RETURN_FALSE;	}	php_stream_auto_cleanup(stream);	PQtrace(pgsql, fp);	RETURN_TRUE;}/* }}} *//* {{{ proto bool pg_untrace([resource connection])   Disable tracing of a PostgreSQL connection */PHP_FUNCTION(pg_untrace){	zval **pgsql_link = NULL;	int id = -1;	PGconn *pgsql;	switch (ZEND_NUM_ARGS()) {		case 0:			id = PGG(default_link);			CHECK_DEFAULT_LINK(id);			break;		case 1:			if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {				RETURN_FALSE;			}			break;		default:			ZEND_WRONG_PARAM_COUNT();			break;	}	if (pgsql_link == NULL && id == -1) {		RETURN_FALSE;	}		ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);	PQuntrace(pgsql);	RETURN_TRUE;}/* }}} *//* {{{ proto int pg_lo_create([resource connection])   Create a large object */PHP_FUNCTION(pg_lo_create){  	zval **pgsql_link = NULL;	PGconn *pgsql;	Oid pgsql_oid;	int id = -1;	switch(ZEND_NUM_ARGS()) {		case 0:			id = PGG(default_link);			CHECK_DEFAULT_LINK(id);			break;		case 1:			if (zend_get_parameters_ex(1, &pgsql_link)==FAILURE) {				RETURN_FALSE;			}			break;		default:			WRONG_PARAM_COUNT;			break;	}	if (pgsql_link == NULL && id == -1) {		RETURN_FALSE;	}			ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink);		/* NOTE: Archive modes not supported until I get some more data. Don't think anybody's	   using it anyway. I believe it's also somehow related to the 'time travel' feature of	   PostgreSQL, that's on the list of features to be removed... Create modes not supported.	   What's the use of an object that can be only written to, but not read from, and vice	   versa? Beats me... And the access type (r/w) must be specified again when opening	   the object, probably (?) overrides this. (Jouni) 	*/	if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");		RETURN_FALSE;	}	PGSQL_RETURN_OID(pgsql_oid);	}/* }}} *//* {{{ proto bool pg_lo_unlink([resource connection,] string large_object_oid)   Delete a large object */PHP_FUNCTION(pg_lo_unlink){	zval *pgsql_link = NULL;	long oid_long;	char *oid_string, *end_ptr;	int oid_strlen;	PGconn *pgsql;	Oid oid;	int id = -1;	int argc = ZEND_NUM_ARGS();	/* accept string type since Oid type is unsigned int */	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,								 "rs", &pgsql_link, &oid_string, &oid_strlen) == SUCCESS) {		oid = (Oid)strtoul(oid_string, &end_ptr, 10);		if ((oid_string+oid_strlen) != end_ptr) {			/* wrong integer format */			php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");			RETURN_FALSE;		}	}	else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,								 "rl", &pgsql_link, &oid_long) == SUCCESS) {		if (oid_long <= InvalidOid) {			php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");			RETURN_FALSE;		}		oid = (Oid)oid_long;	}	else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,								 "s", &oid_string, &oid_strlen) == SUCCESS) {		oid = (Oid)strtoul(oid_string, &end_ptr, 10);		if ((oid_string+oid_strlen) != end_ptr) {			/* wrong integer format */			php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");			RETURN_FALSE;		}		id = PGG(default_link);		CHECK_DEFAULT_LINK(id);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -