char* and size_t length.
- The doer handler now accepts a zend_string* instead of char* + size_t
pair for the SQL statement.
+ - The last_id handler now returns a zend_string* instead of returning a
+ char* and the length as an out param, and accepts a zend_string* instead
+ of char* for the optional sequence/table name.
}
/* }}} */
+/* TODO Refactor */
static const char digit_vec[] = "0123456789";
-PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64) /* {{{ */
+PDO_API zend_string *php_pdo_int64_to_str(pdo_int64_t i64) /* {{{ */
{
char buffer[65];
char outbuf[65] = "";
zend_long long_val;
char *dst = outbuf;
+ if (i64 == 0) {
+ return ZSTR_CHAR('0');
+ }
+
if (i64 < 0) {
i64 = -i64;
*dst++ = '-';
}
- if (i64 == 0) {
- *dst++ = '0';
- *dst++ = '\0';
- return estrdup(outbuf);
- }
-
p = &buffer[sizeof(buffer)-1];
*p = '\0';
while ((*dst++ = *p++) != 0)
;
*dst = '\0';
- return estrdup(outbuf);
+ return zend_string_init(outbuf, strlen(outbuf), 0);
}
/* }}} */
PHP_METHOD(PDO, lastInsertId)
{
pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
- char *name = NULL;
- size_t namelen;
+ zend_string *name = NULL;
+ zend_string *last_id = NULL;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
- Z_PARAM_STRING_OR_NULL(name, namelen)
+ Z_PARAM_STR_OR_NULL(name)
ZEND_PARSE_PARAMETERS_END();
PDO_CONSTRUCT_CHECK;
if (!dbh->methods->last_id) {
pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()");
RETURN_FALSE;
- } else {
- size_t id_len;
- char *id;
- id = dbh->methods->last_id(dbh, name, &id_len);
- if (!id) {
- PDO_HANDLE_DBH_ERR();
- RETURN_FALSE;
- } else {
- //??? use zend_string ?
- RETVAL_STRINGL(id, id_len);
- efree(id);
- }
}
+ last_id = dbh->methods->last_id(dbh, name);
+ if (!last_id) {
+ PDO_HANDLE_DBH_ERR();
+ RETURN_FALSE;
+ }
+ RETURN_STR(last_id);
}
/* }}} */
typedef long long int pdo_int64_t;
typedef unsigned long long int pdo_uint64_t;
#endif
-PDO_API char *php_pdo_int64_to_str(pdo_int64_t i64);
+PDO_API zend_string *php_pdo_int64_to_str(pdo_int64_t i64);
#ifndef TRUE
# define TRUE 1
* Return true on success and false in case of failure */
typedef bool (*pdo_dbh_set_attr_func)(pdo_dbh_t *dbh, zend_long attr, zval *val);
-/* return last insert id. NULL indicates error condition, otherwise, the return value
- * MUST be an emalloc'd NULL terminated string. */
-typedef char *(*pdo_dbh_last_id_func)(pdo_dbh_t *dbh, const char *name, size_t *len);
+/* return last insert id. NULL indicates error condition.
+ * name MIGHT be NULL */
+typedef zend_string *(*pdo_dbh_last_id_func)(pdo_dbh_t *dbh, const zend_string *name);
/* Fetch error information.
* If stmt is not null, fetch information pertaining to the statement,
return pdo_dblib_transaction_cmd("ROLLBACK TRANSACTION", dbh);
}
-char *dblib_handle_last_id(pdo_dbh_t *dbh, const char *name, size_t *len)
+zend_string *dblib_handle_last_id(pdo_dbh_t *dbh, const zend_string *name)
{
pdo_dblib_db_handle *H = (pdo_dblib_db_handle *)dbh->driver_data;
RETCODE ret;
char *id = NULL;
+ size_t len;
+ zend_string *ret_id;
/*
* Would use scope_identity() but it's not implemented on Sybase
}
id = emalloc(32);
- *len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
-
+ len = dbconvert(NULL, (dbcoltype(H->link, 1)) , (dbdata(H->link, 1)) , (dbdatlen(H->link, 1)), SQLCHAR, (BYTE *)id, (DBINT)-1);
dbcancel(H->link);
- return id;
+
+ ret_id = zend_string_init(id, len, 0);
+ efree(id);
+ return ret_id;
}
static bool dblib_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
/* }}} */
/* {{{ pdo_mysql_last_insert_id */
-static char *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
+static zend_string *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
{
pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
- char *id = php_pdo_int64_to_str(mysql_insert_id(H->server));
PDO_DBG_ENTER("pdo_mysql_last_insert_id");
- *len = strlen(id);
- PDO_DBG_RETURN(id);
+ PDO_DBG_RETURN(php_pdo_int64_to_str(mysql_insert_id(H->server)));
}
/* }}} */
oci_handle_commit,
oci_handle_rollback,
oci_handle_set_attribute,
- NULL,
+ NULL, /* last_id not supported */
pdo_oci_fetch_error_func,
oci_handle_get_attribute,
pdo_oci_check_liveness, /* check_liveness */
return quoted_str;
}
-static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
+static zend_string *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
- char *id = NULL;
+ zend_string *id = NULL;
PGresult *res;
ExecStatusType status;
res = PQexec(H->server, "SELECT LASTVAL()");
} else {
const char *q[1];
- q[0] = name;
+ q[0] = ZSTR_VAL(name);
res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
}
status = PQresultStatus(res);
if (res && (status == PGRES_TUPLES_OK)) {
- id = estrdup((char *)PQgetvalue(res, 0, 0));
- *len = PQgetlength(res, 0, 0);
+ id = zend_string_init((char *)PQgetvalue(res, 0, 0), PQgetlength(res, 0, 0), 0);
} else {
pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
}
}
}
-static char *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
+static zend_string *pdo_sqlite_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
{
pdo_sqlite_db_handle *H = (pdo_sqlite_db_handle *)dbh->driver_data;
- char *id;
- id = php_pdo_int64_to_str(sqlite3_last_insert_rowid(H->db));
- *len = strlen(id);
- return id;
+ return php_pdo_int64_to_str(sqlite3_last_insert_rowid(H->db));
}
/* NB: doesn't handle binary strings... use prepared stmts for that */