31a32 > #include "ext/standard/file.h" 498a500,1003 > /* {{{ proto string PDO::pgsqlIsInTransaction() > Returns the status of a transaction according to PDO::PGSQL_TRANSACTION_* constants */ > static PHP_METHOD(PDO, pgsqlIsInTransaction) > { > pdo_dbh_t *dbh; > pdo_pgsql_db_handle *H; > > dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > PDO_CONSTRUCT_CHECK; > > H = (pdo_pgsql_db_handle *)dbh->driver_data; > > RETURN_LONG(PQtransactionStatus(H->server)); > } > /* }}} */ > > /* {{{ proto string PDO::pgsqlCopyFromArray(string $table_name , array $rows [, string $delimiter [, string $null_as ] [, string $fields]) > Returns true if the copy worked fine or false if error */ > static PHP_METHOD(PDO, pgsqlCopyFromArray) > { > pdo_dbh_t *dbh; > pdo_pgsql_db_handle *H; > > zval *pg_rows; > zval **tmp; > char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL; > int table_name_len, pg_delim_len, pg_null_as_len, pg_fields_len; > char *query, *fields; > HashPosition pos; > int id = -1; > PGresult *pgsql_result; > ExecStatusType status; > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/a|sss", > &table_name, &table_name_len, &pg_rows, > &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) { > return; > } > > if (!pg_delim) { > pg_delim = "\t"; > } > if (pg_null_as_len<=0) { > pg_null_as = safe_estrdup("\\\\N"); > } > > if(pg_fields_len>0) { > spprintf(&query, 0, "COPY %s (%s) FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, *pg_delim, pg_null_as); > } > else { > spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as); > } > > // Obtain db Handler > dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > PDO_CONSTRUCT_CHECK; > > H = (pdo_pgsql_db_handle *)dbh->driver_data; > // H->server is the db handle for libPQ > > > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > pgsql_result = PQexec(H->server, query); > > if (pg_null_as_len<=0) { > efree(pg_null_as); > } > efree(query); > > if (pgsql_result) { > status = PQresultStatus(pgsql_result); > } else { > status = (ExecStatusType) PQstatus(H->server); > } > > switch (status) { > case PGRES_COPY_IN: > if (pgsql_result) { > int command_failed = 0; > PQclear(pgsql_result); > zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos); > while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) { > convert_to_string_ex(tmp); > query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2); > strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2); > if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') { > strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2); > } > if (PQputCopyData(H->server, query, strlen(query)) != 1) { > efree(query); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed"); > RETURN_FALSE; > } > efree(query); > zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos); > } > if (PQputCopyEnd(H->server, NULL) != 1) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed"); > RETURN_FALSE; > } > while ((pgsql_result = PQgetResult(H->server))) { > if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > command_failed = 1; > } > PQclear(pgsql_result); > } > if (command_failed) { > RETURN_FALSE; > } > } else { > PQclear(pgsql_result); > RETURN_FALSE; > } > RETURN_TRUE; > break; > default: > PQclear(pgsql_result); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > RETURN_FALSE; > break; > } > } > /* }}} */ > > /* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields]) > Returns true if the copy worked fine or false if error */ > static PHP_METHOD(PDO, pgsqlCopyFromFile) > { > pdo_dbh_t *dbh; > pdo_pgsql_db_handle *H; > > char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL; > int table_name_len, filename_len, pg_delim_len, pg_null_as_len, pg_fields_len; > char *query, *fields; > PGresult *pgsql_result; > ExecStatusType status; > zval *zcontext = NULL; > > php_stream_context *context = NULL; > > > size_t line_len = 0; > php_stream *stream; > char *buf = NULL; > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|sss", > &table_name, &table_name_len, &filename, &filename_len, > &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) { > return; > } > > if (!pg_delim) { > pg_delim = "\t"; > } > if (pg_null_as_len<=0) { > pg_null_as = safe_estrdup("\\\\N"); > } > > if(pg_fields_len>0) { > spprintf(&query, 0, "COPY %s (%s) FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, *pg_delim, pg_null_as); > } > else { > spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as); > } > > // Obtain db Handler > dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > PDO_CONSTRUCT_CHECK; > > H = (pdo_pgsql_db_handle *)dbh->driver_data; > // H->server is the db handle for libPQ > > > // Check filename > context = php_stream_context_from_zval(zcontext, 0); > > stream = php_stream_open_wrapper_ex(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); > if (!stream) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file"); > RETURN_FALSE; > } > > > > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > pgsql_result = PQexec(H->server, query); > > if (pg_null_as_len<=0) { > efree(pg_null_as); > } > efree(query); > > if (pgsql_result) { > status = PQresultStatus(pgsql_result); > } else { > status = (ExecStatusType) PQstatus(H->server); > } > > switch (status) { > case PGRES_COPY_IN: > if (pgsql_result) { > int command_failed = 0; > PQclear(pgsql_result); > buf = php_stream_get_line(stream, NULL, 0, &line_len); > while(buf != NULL) { > if (PQputCopyData(H->server, buf, strlen(buf)) != 1) { > efree(buf); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed"); > php_stream_close(stream); > RETURN_FALSE; > } > efree(buf); > buf = php_stream_get_line(stream, NULL, 0, &line_len); > } > > efree(buf); > > php_stream_close(stream); > > if (PQputCopyEnd(H->server, NULL) != 1) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed"); > RETURN_FALSE; > } > while ((pgsql_result = PQgetResult(H->server))) { > if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > command_failed = 1; > } > PQclear(pgsql_result); > } > if (command_failed) { > RETURN_FALSE; > } > } else { > PQclear(pgsql_result); > RETURN_FALSE; > } > RETURN_TRUE; > break; > default: > PQclear(pgsql_result); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > php_stream_close(stream); > RETURN_FALSE; > break; > } > } > /* }}} */ > > > /* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , $filename, [string $delimiter [, string $null_as [, string $fields]]]) > Returns true if the copy worked fine or false if error */ > static PHP_METHOD(PDO, pgsqlCopyToFile) > { > pdo_dbh_t *dbh; > pdo_pgsql_db_handle *H; > > char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL; > int table_name_len, pg_delim_len, pg_null_as_len, pg_fields_len, filename_len; > char *query; > int id = -1; > > PGresult *pgsql_result; > ExecStatusType status; > int copydone = 0; > char *csv = (char *)NULL; > int ret, bytes_written; > > zval *zcontext = NULL; > php_stream *stream; > > php_stream_context *context = NULL; > > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|sss", > &table_name, &table_name_len, &filename, &filename_len, > &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) { > return; > } > > > if (!pg_delim) { > pg_delim = "\t"; > } > > if (pg_null_as_len <= 0) { > pg_null_as = safe_estrdup("\\\\N"); > } > > if(pg_fields_len>0) { > spprintf(&query, 0, "COPY %s (%s) TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, pg_fields, *pg_delim, pg_null_as); > } > else { > spprintf(&query, 0, "COPY %s TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, *pg_delim, pg_null_as); > } > > // Obtain db Handler > dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > PDO_CONSTRUCT_CHECK; > > H = (pdo_pgsql_db_handle *)dbh->driver_data; > // H->server is the db handle for libPQ > > // Check and open filename for writing > context = php_stream_context_from_zval(zcontext, 0); > > stream = php_stream_open_wrapper_ex(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context); > if (!stream) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing"); > RETURN_FALSE; > } > > > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > pgsql_result = PQexec(H->server, query); > > > > if (pg_null_as_len <= 0) { > efree(pg_null_as); > } > > efree(query); > > if (pgsql_result) { > status = PQresultStatus(pgsql_result); > } else { > status = (ExecStatusType) PQstatus(H->server); > } > > > switch (status) { > case PGRES_COPY_OUT: > if (pgsql_result) { > PQclear(pgsql_result); > array_init(return_value); > > while (!copydone) > { > ret = PQgetCopyData(H->server, &csv, 0); > switch (ret) { > case -1: > copydone = 1; > break; > case 0: > case -2: > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed"); > php_stream_close(stream); > RETURN_FALSE; > break; > default: > bytes_written = php_stream_write(stream, csv, ret); > if(bytes_written != ret) { > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to write to file"); > PQfreemem(csv); > php_stream_close(stream); > RETURN_FALSE; > } > PQfreemem(csv); > break; > } > } > php_stream_close(stream); > > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > RETURN_TRUE; > } else { > php_stream_close(stream); > PQclear(pgsql_result); > RETURN_FALSE; > } > break; > default: > PQclear(pgsql_result); > php_stream_close(stream); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > RETURN_FALSE; > break; > } > } > /* }}} */ > > > > /* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]]) > Returns true if the copy worked fine or false if error */ > static PHP_METHOD(PDO, pgsqlCopyToArray) > { > pdo_dbh_t *dbh; > pdo_pgsql_db_handle *H; > > char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL; > int table_name_len, pg_delim_len, pg_null_as_len, pg_fields_len; > char *query; > int id = -1; > > PGresult *pgsql_result; > ExecStatusType status; > int copydone = 0; > char *csv = (char *)NULL; > int ret; > > if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss", > &table_name, &table_name_len, > &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) { > return; > } > > > if (!pg_delim) { > pg_delim = "\t"; > } > > if (pg_null_as_len <= 0) { > pg_null_as = safe_estrdup("\\\\N"); > } > > if(pg_fields_len>0) { > spprintf(&query, 0, "COPY %s (%s) TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, pg_fields, *pg_delim, pg_null_as); > } > else { > spprintf(&query, 0, "COPY %s TO STDOUT DELIMITERS '%c' WITH NULL AS '%s'", table_name, *pg_delim, pg_null_as); > } > > // Obtain db Handler > dbh = zend_object_store_get_object(getThis() TSRMLS_CC); > PDO_CONSTRUCT_CHECK; > > H = (pdo_pgsql_db_handle *)dbh->driver_data; > // H->server is the db handle for libPQ > > > > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > pgsql_result = PQexec(H->server, query); > > > > if (pg_null_as_len <= 0) { > efree(pg_null_as); > } > > efree(query); > > if (pgsql_result) { > status = PQresultStatus(pgsql_result); > } else { > status = (ExecStatusType) PQstatus(H->server); > } > > > switch (status) { > case PGRES_COPY_OUT: > if (pgsql_result) { > PQclear(pgsql_result); > array_init(return_value); > > while (!copydone) > { > ret = PQgetCopyData(H->server, &csv, 0); > switch (ret) { > case -1: > copydone = 1; > break; > case 0: > case -2: > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed"); > RETURN_FALSE; > break; > default: > add_next_index_string(return_value, csv, 1); > PQfreemem(csv); > break; > } > } > while ((pgsql_result = PQgetResult(H->server))) { > PQclear(pgsql_result); > } > } else { > PQclear(pgsql_result); > RETURN_FALSE; > } > break; > default: > PQclear(pgsql_result); > pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed"); > RETURN_FALSE; > break; > } > } > /* }}} */ > > 610a1116,1120 > PHP_ME(PDO, pgsqlIsInTransaction, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC) > PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)