Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-726743: [HTAP] LAST QUERY ID - Driver API for PHP/PDO #336

Merged
2 commits merged into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pdo_snowflake.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ static PHP_MINIT_FUNCTION(pdo_snowflake) {
"SNOWFLAKE_ATTR_SSL_VERIFY_CERTIFICATE_REVOCATION_STATUS",
(zend_long) PDO_SNOWFLAKE_ATTR_SSL_VERIFY_CERTIFICATE_REVOCATION_STATUS);

REGISTER_PDO_CLASS_CONST_LONG(
"SNOWFLAKE_ATTR_QUERY_ID",
(zend_long) PDO_SNOWFLAKE_ATTR_QUERY_ID);

return php_pdo_register_driver(&pdo_snowflake_driver);
}
/* }}} */
Expand Down
4 changes: 3 additions & 1 deletion php_pdo_snowflake_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ typedef struct {

typedef struct {
SF_CONNECT *server;
char last_qid[SF_UUID4_LEN];
} pdo_snowflake_db_handle;

typedef struct {
Expand Down Expand Up @@ -74,7 +75,8 @@ extern void _pdo_snowflake_user_dealloc(void* ptr);
enum {
PDO_SNOWFLAKE_ATTR_SSL_CAPATH = PDO_ATTR_DRIVER_SPECIFIC,
PDO_SNOWFLAKE_ATTR_SSL_VERSION,
PDO_SNOWFLAKE_ATTR_SSL_VERIFY_CERTIFICATE_REVOCATION_STATUS
PDO_SNOWFLAKE_ATTR_SSL_VERIFY_CERTIFICATE_REVOCATION_STATUS,
PDO_SNOWFLAKE_ATTR_QUERY_ID
};

#define PDO_SNOWFLAKE_CONN_ATTR_HOST_IDX 0
Expand Down
18 changes: 17 additions & 1 deletion snowflake_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ snowflake_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
{
PDO_LOG_ENTER("snowflake_handle_doer");
int ret = 0;
SF_STATUS query_status;
const char * qid;
pdo_snowflake_db_handle *H = (pdo_snowflake_db_handle *) dbh->driver_data;
PDO_LOG_DBG("sql: %.*s, len: %d", sql_len, sql, sql_len);
SF_STMT *sfstmt = snowflake_stmt(H->server);
Expand All @@ -239,7 +241,16 @@ snowflake_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
snowflake_stmt_set_attr(sfstmt, SF_STMT_USER_REALLOC_FUNC,
_pdo_snowflake_user_realloc);

if (snowflake_query(sfstmt, sql, sql_len) == SF_STATUS_SUCCESS) {
query_status = snowflake_query(sfstmt, sql, sql_len);

//save query id if available
qid = snowflake_sfqid(sfstmt);
if (qid && (strlen(qid) > 0))
{
strncpy(H->last_qid, qid, sizeof(H->last_qid) - 1);
}

if (query_status == SF_STATUS_SUCCESS) {
int64 rows = snowflake_affected_rows(sfstmt);
if (rows == -1) {
snowflake_propagate_error(H->server, sfstmt);
Expand Down Expand Up @@ -409,6 +420,10 @@ pdo_snowflake_get_attribute(pdo_dbh_t *dbh, zend_long attr,
ZVAL_STRINGL(return_value, PDO_SNOWFLAKE_VERSION, strlen(PDO_SNOWFLAKE_VERSION));
PDO_LOG_RETURN(1);
break;
case PDO_SNOWFLAKE_ATTR_QUERY_ID:
ZVAL_STRINGL(return_value, H->last_qid, strlen(H->last_qid));
PDO_LOG_RETURN(1);
break;
default:
/**/
PDO_LOG_RETURN(0);
Expand Down Expand Up @@ -573,6 +588,7 @@ pdo_snowflake_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
sizeof(struct pdo_data_src_parser));

H = pecalloc(1, sizeof(pdo_snowflake_db_handle), dbh->is_persistent);
H->last_qid[0] = '\0';

//TODO set error stuff

Expand Down
70 changes: 61 additions & 9 deletions snowflake_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,22 @@ static int pdo_snowflake_stmt_execute_prepared(pdo_stmt_t *stmt) /* {{{ */
{
PDO_LOG_ENTER("pdo_snowflake_stmt_execute_prepared");
int i;
SF_STATUS query_status;
const char * qid;
pdo_snowflake_stmt *S = stmt->driver_data;
pdo_snowflake_db_handle *H = S->H;

/* execute */
if (snowflake_execute(S->stmt) != SF_STATUS_SUCCESS) {
query_status = snowflake_execute(S->stmt);

//save query id if available
qid = snowflake_sfqid(S->stmt);
if (qid && (strlen(qid) > 0))
{
strncpy(H->last_qid, qid, sizeof(H->last_qid) - 1);
}

if (query_status != SF_STATUS_SUCCESS) {
pdo_snowflake_error_stmt(stmt);
PDO_LOG_RETURN(0);
}
Expand Down Expand Up @@ -141,6 +153,8 @@ static int pdo_snowflake_stmt_execute(pdo_stmt_t *stmt) /* {{{ */
{
pdo_snowflake_stmt *S = (pdo_snowflake_stmt *) stmt->driver_data;
pdo_snowflake_db_handle *H = S->H;
SF_STATUS query_status;
const char * qid;
PDO_LOG_ENTER("pdo_snowflake_stmt_execute");

if (S->stmt) {
Expand All @@ -150,14 +164,21 @@ static int pdo_snowflake_stmt_execute(pdo_stmt_t *stmt) /* {{{ */

// TODO: stmt->active_query_stringlen should be specified.
#if (PHP_VERSION_ID >= 80100)
if (snowflake_query(S->stmt, ZSTR_VAL(stmt->active_query_string),
ZSTR_LEN(stmt->active_query_string)) !=
SF_STATUS_SUCCESS) {
query_status = snowflake_query(S->stmt, ZSTR_VAL(stmt->active_query_string),
ZSTR_LEN(stmt->active_query_string));
#else
if (snowflake_query(S->stmt, stmt->active_query_string,
stmt->active_query_stringlen) !=
SF_STATUS_SUCCESS) {
query_status = snowflake_query(S->stmt, stmt->active_query_string,
stmt->active_query_stringlen);
#endif

//save query id if available
qid = snowflake_sfqid(S->stmt);
if (qid && (strlen(qid) > 0))
{
strncpy(H->last_qid, qid, sizeof(H->last_qid) - 1);
}

if (query_status != SF_STATUS_SUCCESS) {
PDO_LOG_RETURN(0);
}
PDO_LOG_RETURN(1);
Expand Down Expand Up @@ -643,6 +664,37 @@ static int pdo_snowflake_stmt_cursor_closer(pdo_stmt_t *stmt) /* {{{ */

/* }}} */

/**
* Maps to the PDOStatement::getAttribute. Gets the value of a given attribute on a statement.
*
* @param stmt Current statement for which the attribute value is requested.
* @param attr Represents any valid set of attribute constants supported by this driver.
* @param return_value Attribute value.
* @return 1 if success or 0 if error occurs
*/
int pdo_snowflake_stmt_get_attr(pdo_stmt_t *stmt, zend_long attr, zval *return_value )
{
PDO_LOG_ENTER("pdo_snowflake_stmt_get_attr");

if (!stmt) {
PDO_LOG_RETURN(0);
}
pdo_snowflake_stmt *S = (pdo_snowflake_stmt *) stmt->driver_data;

PDO_LOG_DBG("stmt=%p", stmt);
PDO_LOG_DBG("attr=%l", attr);
switch (attr) {
case PDO_SNOWFLAKE_ATTR_QUERY_ID:
ZVAL_STRINGL(return_value, snowflake_sfqid(S->stmt), strlen(snowflake_sfqid(S->stmt)));
PDO_LOG_RETURN(1);
break;
default:
/**/
PDO_LOG_RETURN(0);
}
PDO_LOG_RETURN(0);
}

#if (PHP_VERSION_ID < 80100)
struct pdo_stmt_methods snowflake_stmt_methods = {
pdo_snowflake_stmt_dtor,
Expand All @@ -652,7 +704,7 @@ struct pdo_stmt_methods snowflake_stmt_methods = {
pdo_snowflake_stmt_get_col,
pdo_snowflake_stmt_param_hook,
NULL, /* set_attr */
NULL, /* get_attr */
pdo_snowflake_stmt_get_attr,
pdo_snowflake_stmt_col_meta,
pdo_snowflake_stmt_next_rowset,
pdo_snowflake_stmt_cursor_closer
Expand Down Expand Up @@ -686,7 +738,7 @@ struct pdo_stmt_methods snowflake_stmt_methods = {
pdo_snowflake_stmt_get_col_newif,
pdo_snowflake_stmt_param_hook,
NULL, /* set_attr */
NULL, /* get_attr */
pdo_snowflake_stmt_get_attr,
pdo_snowflake_stmt_col_meta,
pdo_snowflake_stmt_next_rowset,
pdo_snowflake_stmt_cursor_closer
Expand Down
67 changes: 67 additions & 0 deletions tests/getattribute.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,69 @@ pdo_snowflake.cacert=libsnowflakeclient/cacert.pem
else {
echo "Failed get driver version.\n";
}

// query id with prepared query
$qid = $dbh->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
$sth = $dbh->prepare("select 1");
$sqid = $sth->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
if (!empty($qid) || !empty($sqid)) {
echo "query id is not empty before any query executed: " . $qid . ", " . $sqid . "\n";
}
else {
echo "query id is empty before any query executed.\n";
}
$sth->execute();
$qid = $dbh->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
$sqid = $sth->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
if ($qid != $sqid)
{
echo "query id from connecton is different from statement: " . $qid . ", " . $sqid . "\n";
}
else if (empty($qid)) {
echo "query id is empty after query executed.\n";
}
else {
echo "query id is valid after query executed.\n";
}

// query id with directly executed query
$sth = $dbh->query("create temporary table tb1(c1 varchar)");
$qid1 = $dbh->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
$sqid1 = $sth->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);
if ($qid == $qid1)
{
echo "query id is not updated after another query executed.\n";
}
if ($qid1 != $sqid1)
{
echo "query id from connecton is different from statement: " . $qid . ", " . $sqid . "\n";
}
else if (empty($qid1)) {
echo "query id is empty after query executed.\n";
}
else {
echo "query id is valid after query executed.\n";
}

// query id with failed query
$sth = $dbh->query("select * from table_not_exists");
if ($sth !== false)
{
echo "unexpected query success.\n";
}

$qid2 = $dbh->getAttribute(PDO::SNOWFLAKE_ATTR_QUERY_ID);

if ($qid2 == $qid1)
{
echo "query id is not updated with failed query.\n";
}
if (empty($qid2)) {
echo "query id is empty with failed query.\n";
}
else {
echo "query id is valid with failed query.\n";
}
?>
===DONE===
<?php exit(0); ?>
Expand All @@ -57,4 +120,8 @@ PDO::ATTR_AUTOCOMMIT: 1
PDO::ATTR_AUTOCOMMIT: 0
0 0 0 0 0
Successfully get driver version
query id is empty before any query executed.
query id is valid after query executed.
query id is valid after query executed.
query id is valid with failed query.
===DONE===
Loading