Skip to content

Commit 517ea62

Browse files
authored
Change SpecialColumns to return correct information for unique indexes (#97)
* test with primary and unique keys * Send query directly and set result for best ROWID
1 parent 151feaa commit 517ea62

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

info.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,6 +2993,7 @@ PGAPI_SpecialColumns(HSTMT hstmt,
29932993
appendPQExpBufferStr(&columns_query, ", c.relhasoids");
29942994
else
29952995
appendPQExpBufferStr(&columns_query, ", 0 as relhasoids");
2996+
29962997
appendPQExpBufferStr(&columns_query, " from pg_catalog.pg_namespace u,"
29972998
" pg_catalog.pg_class c where "
29982999
"u.oid = c.relnamespace");
@@ -3042,25 +3043,32 @@ PGAPI_SpecialColumns(HSTMT hstmt,
30423043
goto cleanup;
30433044
}
30443045

3046+
// check to see if the relation has rules
30453047
result = PGAPI_BindCol(col_stmt, 1, internal_asis_type,
30463048
relhasrules, sizeof(relhasrules), NULL);
30473049
if (!SQL_SUCCEEDED(result))
30483050
{
30493051
goto cleanup;
30503052
}
30513053

3054+
// bind to relkind to check if a view or not
30523055
result = PGAPI_BindCol(col_stmt, 2, internal_asis_type,
30533056
relkind, sizeof(relkind), NULL);
30543057
if (!SQL_SUCCEEDED(result))
30553058
{
30563059
goto cleanup;
30573060
}
3058-
relhasoids[0] = '1';
3059-
result = PGAPI_BindCol(col_stmt, 3, internal_asis_type,
3060-
relhasoids, sizeof(relhasoids), NULL);
3061-
if (!SQL_SUCCEEDED(result))
3061+
3062+
// check to see if the relation has oids, this is only done for versions less than 12
3063+
if (PG_VERSION_LT(conn, 12.0))
30623064
{
3063-
goto cleanup;
3065+
relhasoids[0] = '1';
3066+
result = PGAPI_BindCol(col_stmt, 3, internal_asis_type,
3067+
relhasoids, sizeof(relhasoids), NULL);
3068+
if (!SQL_SUCCEEDED(result))
3069+
{
3070+
goto cleanup;
3071+
}
30643072
}
30653073

30663074
result = PGAPI_Fetch(col_stmt);
@@ -3092,6 +3100,7 @@ PGAPI_SpecialColumns(HSTMT hstmt,
30923100
if (relisaview)
30933101
{
30943102
/* there's no oid for views */
3103+
// TODO: this may still work for views since we don't really have to rely on oid for best rowid
30953104
if (fColType == SQL_BEST_ROWID)
30963105
{
30973106
ret = SQL_SUCCESS;
@@ -3112,7 +3121,7 @@ PGAPI_SpecialColumns(HSTMT hstmt,
31123121
set_tuplefield_int4(&tuple[SPECOLS_BUFFER_LENGTH], PGTYPE_ATTR_BUFFER_LENGTH(conn, the_type, atttypmod));
31133122
set_tuplefield_int2(&tuple[SPECOLS_DECIMAL_DIGITS], PGTYPE_ATTR_DECIMAL_DIGITS(conn, the_type, atttypmod));
31143123
set_tuplefield_int2(&tuple[SPECOLS_PSEUDO_COLUMN], SQL_PC_NOT_PSEUDO);
3115-
MYLOG(DETAIL_LOG_LEVEL, "Add ctid\n");
3124+
MYLOG(DETAIL_LOG_LEVEL, "Add ctid\n");
31163125
}
31173126
}
31183127
else
@@ -3181,21 +3190,15 @@ WHERE a.attnum > 0 and c.relname like 'testuktab';
31813190

31823191

31833192
if (szTableQualifier != NULL)
3184-
appendPQExpBuffer(&columns_query, " and c.relnamespace = %s" , szSchemaName);
3193+
appendPQExpBuffer(&columns_query, " and c.relnamespace = %s" , szSchemaName);
31853194

3186-
result = PGAPI_ExecDirect(stmt, (SQLCHAR *) columns_query.data, SQL_NTS, PODBC_RDONLY);
3187-
if (!SQL_SUCCEEDED(result))
3195+
if (res = CC_send_query(conn, columns_query.data, NULL, READ_ONLY_QUERY, stmt), !QR_command_maybe_successful(res))
31883196
{
3189-
/*
3190-
* "Couldn't execute index query (w/SQLExecDirect) in
3191-
* SpecialColumns";
3192-
*/
3193-
SC_full_error_copy(stmt, stmt, FALSE);
3197+
SC_set_error(stmt, STMT_EXEC_ERROR, "PGAPI_Special query error", func);
31943198
goto cleanup;
31953199
}
3196-
res = SC_get_Result(stmt);
3200+
SC_set_Result(stmt, res);
31973201
}
3198-
31993202
}
32003203
else if (fColType == SQL_ROWVER)
32013204
{

test/expected/catalogfunctions.out

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ contrib_regression public testtab1 id UPDATE YES
107107
contrib_regression public testtab1 id SELECT YES
108108
contrib_regression public testtab1 id REFERENCES YES
109109
contrib_regression public testtab1 id INSERT YES
110-
Check for SQLSpecialColumns
110+
Check for SQLSpecialColumns ROWVER
111111
Result set metadata:
112112
SCOPE: SMALLINT(5) digits: 0, nullable
113113
COLUMN_NAME: VARCHAR(128) digits: 0, nullable
@@ -119,16 +119,16 @@ DECIMAL_DIGITS: SMALLINT(5) digits: 0, nullable
119119
PSEUDO_COLUMN: SMALLINT(5) digits: 0, nullable
120120
Result set:
121121
NULL xmin 4 xid 10 4 0 2
122-
Check for SQLSpecialColumns
122+
Check for SQLSpecialColumns ROWID
123123
Result set metadata:
124124
SCOPE: LONGVARCHAR(8190) digits: 0, nullable
125-
SCHEMA_NAME: VARCHAR(63) digits: 0, nullable
126-
TABLE_NAME: VARCHAR(63) digits: 0, nullable
127-
COLUMN_NAME: VARCHAR(63) digits: 0, nullable
128-
DATA_TYPE: VARCHAR(63) digits: 0, nullable
129-
TYPE_NAME: VARCHAR(63) digits: 0, nullable
130-
COLUMN_SIZE: SMALLINT(5) digits: 0, nullable
131-
BUFFER_LENGTH: SMALLINT(5) digits: 0, nullable
125+
SCHEMA_NAME: VARCHAR(63) digits: 0, not nullable
126+
TABLE_NAME: VARCHAR(63) digits: 0, not nullable
127+
COLUMN_NAME: VARCHAR(63) digits: 0, not nullable
128+
DATA_TYPE: VARCHAR(63) digits: 0, not nullable
129+
TYPE_NAME: VARCHAR(63) digits: 0, not nullable
130+
COLUMN_SIZE: SMALLINT(5) digits: 0, not nullable
131+
BUFFER_LENGTH: SMALLINT(5) digits: 0, not nullable
132132
DECIMAL_DIGITS: INTEGER(10) digits: 0, nullable
133133
PSEUDO_COLUMN: INTEGER(10) digits: 0, nullable
134134
Result set:

0 commit comments

Comments
 (0)