From 34d10931e2bef87b43a6ca39510e99c308daa9bc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 19 Sep 2004 16:58:13 +0000 Subject: [PATCH] Add "no permission" error code. Add a uri: psuedo driver; it specifies the name of a resource that contains, as its first line, the actual data source to connect to. The resource can be a local file, or it can be any resource for which PHP has a wrapper. // loads connection data from the file "myapp" $d = new PDO('uri:myapp'); // lets say that public.db.com has a read-only db open for the public // their connection data is also published via the web: // (not so great to resolve this on each request though...) $d = new PDO('uri:http://public.db.com/pdo-connection-data'); --- ext/pdo/TODO | 1 - ext/pdo/pdo_dbh.c | 24 ++++++++++++++++++++++++ ext/pdo/php_pdo_driver.h | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ext/pdo/TODO b/ext/pdo/TODO index 43da3fe9a6..b30eab16c2 100755 --- a/ext/pdo/TODO +++ b/ext/pdo/TODO @@ -14,7 +14,6 @@ Low-level: - LOB support via Streams API - persistent handles - iterator support -- "lazy" fetch support Not-so-low-level: - hash key case folding for portabilitiy, ala sqlite.assoc_case, but not using an ini option. diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 23503a7303..09a96c39bc 100755 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -63,6 +63,7 @@ void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC) case PDO_ERR_MISMATCH: msg = "Mismatch"; break; case PDO_ERR_TRUNCATED: msg = "Truncated"; break; case PDO_ERR_DISCONNECTED: msg = "Disconnected"; break; + case PDO_ERR_NO_PERM: msg = "No permission"; break; default: msg = "<>"; } @@ -126,6 +127,19 @@ void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt TSRMLS_DC) } } +static char *dsn_from_uri(char *uri TSRMLS_DC) +{ + php_stream *stream; + char *dsn = NULL; + + stream = php_stream_open_wrapper(uri, "rb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL); + if (stream) { + dsn = php_stream_get_line(stream, NULL, 0, NULL); + php_stream_close(stream); + } + return dsn; +} + /* {{{ proto object PDO::__construct(string dsn, string username, string passwd [, array driver_opts]) */ static PHP_FUNCTION(dbh_constructor) @@ -156,6 +170,16 @@ static PHP_FUNCTION(dbh_constructor) return; } + if (!strncmp(data_source, "uri:", sizeof("uri:")-1)) { + /* the specified URI holds connection details */ + data_source = dsn_from_uri(data_source TSRMLS_CC); + if (!data_source) { + zend_throw_exception_ex(php_pdo_get_exception(), PDO_ERR_SYNTAX TSRMLS_CC, "invalid data source URI"); + ZVAL_NULL(object); + return; + } + } + driver = pdo_find_driver(data_source, colon - data_source); if (!driver) { diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index 2acd860b92..f80efd2d8f 100755 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -95,6 +95,7 @@ enum pdo_error_type { PDO_ERR_MISMATCH, PDO_ERR_TRUNCATED, PDO_ERR_DISCONNECTED, + PDO_ERR_NO_PERM, }; enum pdo_error_mode { -- 2.50.1