|
| 1 | +/* |
| 2 | + * Test that SQLPrimaryKeys excludes INCLUDE columns. |
| 3 | + * |
| 4 | + * A PRIMARY KEY with INCLUDE (PG 11+) should only return the actual |
| 5 | + * key columns, not the included columns. |
| 6 | + */ |
| 7 | +#include <stdio.h> |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +#include "common.h" |
| 11 | + |
| 12 | +int |
| 13 | +main(int argc, char **argv) |
| 14 | +{ |
| 15 | + SQLRETURN rc; |
| 16 | + HSTMT hstmt = SQL_NULL_HSTMT; |
| 17 | + |
| 18 | + test_connect(); |
| 19 | + |
| 20 | + rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt); |
| 21 | + CHECK_CONN_RESULT(rc, "failed to allocate stmt handle", conn); |
| 22 | + |
| 23 | + /* INCLUDE clause requires PG >= 11 */ |
| 24 | + if (server_version_lt(conn, 11, 0)) |
| 25 | + { |
| 26 | + printf("Server version < 11, skipping INCLUDE test\n"); |
| 27 | + test_disconnect(); |
| 28 | + return 0; |
| 29 | + } |
| 30 | + |
| 31 | + /* Create table with PK that has INCLUDE columns */ |
| 32 | + rc = SQLExecDirect(hstmt, |
| 33 | + (SQLCHAR *) "DROP TABLE IF EXISTS pk_include_test", SQL_NTS); |
| 34 | + CHECK_STMT_RESULT(rc, "DROP TABLE failed", hstmt); |
| 35 | + |
| 36 | + rc = SQLExecDirect(hstmt, |
| 37 | + (SQLCHAR *) "CREATE TABLE pk_include_test (a int, b int, c int, d int," |
| 38 | + " CONSTRAINT pk_include_test_pkey PRIMARY KEY (a, b) INCLUDE (c, d))", |
| 39 | + SQL_NTS); |
| 40 | + CHECK_STMT_RESULT(rc, "CREATE TABLE failed", hstmt); |
| 41 | + |
| 42 | + rc = SQLFreeStmt(hstmt, SQL_CLOSE); |
| 43 | + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); |
| 44 | + |
| 45 | + /* Call SQLPrimaryKeys - should return only a and b, not c or d */ |
| 46 | + printf("Check SQLPrimaryKeys with INCLUDE columns\n"); |
| 47 | + rc = SQLPrimaryKeys(hstmt, |
| 48 | + NULL, 0, |
| 49 | + (SQLCHAR *) "public", SQL_NTS, |
| 50 | + (SQLCHAR *) "pk_include_test", SQL_NTS); |
| 51 | + CHECK_STMT_RESULT(rc, "SQLPrimaryKeys failed", hstmt); |
| 52 | + print_result(hstmt); |
| 53 | + |
| 54 | + rc = SQLFreeStmt(hstmt, SQL_CLOSE); |
| 55 | + CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt); |
| 56 | + |
| 57 | + /* Clean up */ |
| 58 | + rc = SQLExecDirect(hstmt, |
| 59 | + (SQLCHAR *) "DROP TABLE pk_include_test", SQL_NTS); |
| 60 | + CHECK_STMT_RESULT(rc, "DROP TABLE failed", hstmt); |
| 61 | + |
| 62 | + test_disconnect(); |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
0 commit comments