php_monetdb.c
来自「这个是内存数据库的客户端」· C语言 代码 · 共 2,237 行 · 第 1/5 页
C
2,237 行
{ zval *result; long row; php_monetdb_result_handle *monetdb_result_h; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &row) == FAILURE) { return; } ZEND_FETCH_RESOURCE(monetdb_result_h, php_monetdb_result_handle *, &result, -1, "MonetDB result", le_result); if (row < 0 || row >= mapi_get_row_count(monetdb_result_h->result)) { RETURN_FALSE; } /* seek to offset */ monetdb_result_h->row = row; RETURN_TRUE;}/* }}} */#define PHP_MONETDB_DATA_LENGTH 1#define PHP_MONETDB_DATA_ISNULL 2/* {{{ php_monetdb_data_info */static void php_monetdb_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type){ zval **result, **row, **field; Mresult *monetdb_result; php_monetdb_result_handle *monetdb_result_h; int field_offset, monetdb_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(monetdb_result_h, php_monetdb_result_handle *, result, -1, "MonetDB result", le_result); monetdb_result = monetdb_result_h->result; if (ZEND_NUM_ARGS() == 2) { if (monetdb_result_h->row < 0) monetdb_result_h->row = 0; monetdb_row = monetdb_result_h->row; if (monetdb_row < 0 || monetdb_row >= mapi_get_row_count(monetdb_result)) { RETURN_FALSE; } } else { convert_to_long_ex(row); monetdb_row = Z_LVAL_PP(row); if (monetdb_row < 0 || monetdb_row >= mapi_get_row_count(monetdb_result)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on MonetDB result index %ld", Z_LVAL_PP(row), Z_LVAL_PP(result)); RETURN_FALSE; } } switch(Z_TYPE_PP(field)) { case IS_STRING: { int i; convert_to_string_ex(field); field_offset = -1; for (i = 0; i < mapi_get_field_count(monetdb_result); i++) { if (strcmp(mapi_get_name(monetdb_result, i), Z_STRVAL_PP(field)) == 0) { field_offset = i; break; } } } break; default: convert_to_long_ex(field); field_offset = Z_LVAL_PP(field); break; } if (field_offset < 0 || field_offset >= mapi_get_row_count(monetdb_result)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified"); RETURN_FALSE; } switch (entry_type) { case PHP_MONETDB_DATA_LENGTH: Z_LVAL_P(return_value) = mapi_get_len(monetdb_result, field_offset); break; case PHP_MONETDB_DATA_ISNULL: /* get the data in the field */ if (mapi_seek_row(monetdb_result, monetdb_row, MAPI_SEEK_SET) != MOK) { PHP_MONETDB_ERROR_RESULT("Can't jump to row: %s", monetdb_result_h); RETURN_FALSE; } if (mapi_fetch_row(monetdb_result) == 0 && mapi_error(monetdb_result_h->conn) != 0) { PHP_MONETDB_ERROR("Can't get row: %s", monetdb_result_h->conn); RETURN_FALSE; } Z_LVAL_P(return_value) = (mapi_fetch_field(monetdb_result, field_offset) == NULL); if (mapi_error(monetdb_result_h->conn) != 0) { PHP_MONETDB_ERROR("Can't fetch field: %s", monetdb_result_h->conn); RETURN_FALSE; } break; } Z_TYPE_P(return_value) = IS_LONG;}/* }}} *//* {{{ proto int monetdb_field_prtlen(resource result, [int row,] mixed field_name_or_number) Returns the printed length */PHP_FUNCTION(monetdb_field_prtlen){ php_monetdb_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MONETDB_DATA_LENGTH);}/* }}} *//* {{{ proto int monetdb_field_is_null(resource result, [int row,] mixed field_name_or_number) Test if a field is NULL */PHP_FUNCTION(monetdb_field_is_null){ php_monetdb_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MONETDB_DATA_ISNULL);}/* }}} *//* {{{ proto bool monetdb_free_result(resource result) Free result memory */PHP_FUNCTION(monetdb_free_result){ zval **result; php_monetdb_result_handle *monetdb_result_h; if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &result)==FAILURE) { WRONG_PARAM_COUNT; } ZEND_FETCH_RESOURCE(monetdb_result_h, php_monetdb_result_handle *, result, -1, "MonetDB result", le_result); if (Z_LVAL_PP(result) == 0) { RETURN_FALSE; } zend_list_delete(Z_LVAL_PP(result)); RETURN_TRUE;}/* }}} *//* {{{ proto string monetdb_escape_string(string data) Escape string for text/char type */PHP_FUNCTION(monetdb_escape_string){ char *from = NULL, *to = NULL, *ret = NULL; int to_len; int from_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) { return; } to = mapi_quote(from, from_len); to_len = strlen(to); ret = estrdup(to); free(to); /* mapi allocated a new string using malloc, don't leak it */ RETURN_STRINGL(ret, to_len, 0);}/* }}} *//* {{{ proto int monetdb_connection_status(resource connnection) Get connection status */PHP_FUNCTION(monetdb_connection_status){ zval *monetdb_link = NULL; int id = -1; Mconn *monetdb_conn; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &monetdb_link) == FAILURE) { RETURN_FALSE; } ZEND_FETCH_RESOURCE2(monetdb_conn, Mconn *, &monetdb_link, id, "MonetDB link", le_link, le_plink); RETURN_LONG(mapi_is_connected(monetdb_conn) ? MONETDB_CONNECTION_OK : MONETDB_CONNECTION_BAD);}/* }}} *//* {{{ proto bool monetdb_connection_reset(resource connection) Reset connection (reconnect) */PHP_FUNCTION(monetdb_connection_reset){ zval *monetdb_link; int id = -1; Mconn *monetdb_conn; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &monetdb_link) == FAILURE) { RETURN_FALSE; } ZEND_FETCH_RESOURCE2(monetdb_conn, Mconn *, &monetdb_link, id, "MonetDB link", le_link, le_plink); if (mapi_reconnect(monetdb_conn) != MOK) { RETURN_FALSE; } RETURN_TRUE;}/* }}} */#ifdef I_FEEL_LIKE_IMPLEMENTING_COPY_INTO_PROPERLY#define COPYBUFSIZ 8192/* {{{ proto bool monetdb_put_line([resource connection,] string query) Send null-terminated string to backend server */PHP_FUNCTION(monetdb_put_line){ zval **query, **pgsql_link = NULL; int id = -1; Mconn *pgsql; int result = 0; switch(ZEND_NUM_ARGS()) { case 1: if (zend_get_parameters_ex(1, &query)==FAILURE) { RETURN_FALSE; } id = MG(default_link); CHECK_DEFAULT_LINK(id); break; case 2: if (zend_get_parameters_ex(2, &pgsql_link, &query)==FAILURE) { RETURN_FALSE; } break; default: WRONG_PARAM_COUNT; break; } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; } ZEND_FETCH_RESOURCE2(pgsql, Mconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); convert_to_string_ex(query); result = PQputline(pgsql, Z_STRVAL_PP(query)); if (result==EOF) { PHP_PQ_ERROR("Query failed: %s", pgsql); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto bool monetdb_end_copy([resource connection]) Sync with backend. Completes the Copy command */PHP_FUNCTION(monetdb_end_copy){ zval **pgsql_link = NULL; int id = -1; Mconn *pgsql; int result = 0; switch(ZEND_NUM_ARGS()) { case 0: id = MG(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, Mconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); result = PQendcopy(pgsql); if (result!=0) { PHP_PQ_ERROR("Query failed: %s", pgsql); RETURN_FALSE; } RETURN_TRUE;}/* }}} *//* {{{ proto array monetdb_copy_to(resource connection, string table_name [, string delimiter [, string null_as]]) Copy table to array */PHP_FUNCTION(monetdb_copy_to){ zval *pgsql_link; char *table_name, *pg_delim = NULL, *pg_null_as = NULL; int table_name_len, pg_delim_len, pg_null_as_len; char *query; char *query_template = "COPY \"\" TO STDOUT DELIMITERS ':' WITH NULL AS ''"; int id = -1; Mconn *pgsql; Mresult *pgsql_result; ExecStatusType status; int copydone = 0;#if !HAVE_PQGETCOPYDATA char copybuf[COPYBUFSIZ];#endif char *csv = (char *)NULL; int ret; int argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rs|ss", &pgsql_link, &table_name, &table_name_len, &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) { return; } if (!pg_delim) { pg_delim = "\t"; } if (!pg_null_as) { pg_null_as = safe_estrdup("\\\\N"); } ZEND_FETCH_RESOURCE2(pgsql, Mconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); query = (char *)emalloc(strlen(query_template) + strlen(table_name) + strlen(pg_null_as) + 1); sprintf(query, "COPY \"%s\" TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, *pg_delim, pg_null_as); while ((pgsql_result = MgetResult(pgsql))) { Mclear(pgsql_result); } pgsql_result = PQexec(pgsql, query); efree(pg_null_as); efree(query); if (pgsql_result) { status = PQresultStatus(pgsql_result); } else { status = (ExecStatusType) PQstatus(pgsql); } switch (status) { case PGRES_COPY_OUT: if (pgsql_result) { Mclear(pgsql_result); array_init(return_value);#if HAVE_PQGETCOPYDATA while (!copydone) { ret = PQgetCopyData(pgsql, &csv, 0); switch (ret) { case -1: copydone = 1; break; case 0: case -2: PHP_PQ_ERROR("getline failed: %s", pgsql); RETURN_FALSE; break; default: add_next_index_string(return_value, csv, 1); PQfreemem(csv); break; } }#else while (!copydone) { if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) { PHP_PQ_ERROR("getline failed: %s", pgsql); RETURN_FALSE; } if (copybuf[0] == '\\' && copybuf[1] == '.' && copybuf[2] == '\0') { copydone = 1; } else { if (csv == (char *)NULL) { csv = estrdup(copybuf); } else { csv = (char *)erealloc(csv, strlen(csv) + sizeof(char)*(COPYBUFSIZ+1)); strcat(csv, copybuf); } switch (ret) { case EOF: copydone = 1; case 0: add_next_index_string(return_value, csv, 1); efree(csv); csv = (char *)NULL; break; case 1: break; } } } if (PQendcopy(pgsql)) { PHP_PQ_ERROR("endcopy failed: %s", pgsql); RETURN_FALSE; }#endif while ((pgsql_result = MgetResult(pgsql))) { Mclear(pgsql_result); } } else { Mclear(pgsql_result); RETURN_FALSE; } break; default: Mclear(pgsql_result); PHP_PQ_ERROR("Copy command failed: %s", pgsql); RETURN_FALSE; break; }}/* }}} *//* {{{ proto bool monetdb_copy_from(resource connection, string table_name , array rows [, string delimiter [, string null_as]]) Copy table from array */PHP_FUNCTION(monetdb_copy_from){ zval *pgsql_link = NULL, *pg_rows; zval **tmp; char *table_name, *pg_delim = NULL, *pg_null_as = NULL; int table_name_len, pg_delim_len, pg_null_as_len; int pg_null_as_free = 0; char *query; char *query_template = "COPY \"\" FROM STDIN DELIMITERS ':' WITH NULL AS ''"; HashPosition pos; int id = -1; Mconn *pgsql; Mresult *pgsql_result; ExecStatusType status; int argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rs/a|ss", &pgsql_link, &table_name, &table_name_len, &pg_rows, &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILUR
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?