Skip to content

Commit

Permalink
Added regression test for DescRec functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hunaid2000 committed Aug 9, 2024
1 parent 78460ee commit dcd388e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
34 changes: 34 additions & 0 deletions test/expected/descrec.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
connected

-- Column 1 --
SQL_DESC_NAME: col1
SQL_DESC_TYPE: 4
SQL_DESC_OCTET_LENGTH: 0
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0

-- Column 2 --
SQL_DESC_NAME: col2
SQL_DESC_TYPE: 2
SQL_DESC_OCTET_LENGTH: 6
SQL_DESC_PRECISION: 4
SQL_DESC_SCALE: 2
SQL_DESC_NULLABLE: 1

-- Column 3 --
SQL_DESC_NAME: col3
SQL_DESC_TYPE: 12
SQL_DESC_OCTET_LENGTH: 40
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0

-- Column 4 --
SQL_DESC_NAME: col4
SQL_DESC_TYPE: -5
SQL_DESC_OCTET_LENGTH: 8
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0
disconnecting
34 changes: 34 additions & 0 deletions test/expected/descrec_1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
connected

-- Column 1 --
SQL_DESC_NAME: col1
SQL_DESC_TYPE: 4
SQL_DESC_OCTET_LENGTH: 0
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0

-- Column 2 --
SQL_DESC_NAME: col2
SQL_DESC_TYPE: 2
SQL_DESC_OCTET_LENGTH: 6
SQL_DESC_PRECISION: 4
SQL_DESC_SCALE: 2
SQL_DESC_NULLABLE: 1

-- Column 3 --
SQL_DESC_NAME: col3
SQL_DESC_TYPE: 12
SQL_DESC_OCTET_LENGTH: 20
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0

-- Column 4 --
SQL_DESC_NAME: col4
SQL_DESC_TYPE: -5
SQL_DESC_OCTET_LENGTH: 8
SQL_DESC_PRECISION: 0
SQL_DESC_SCALE: 0
SQL_DESC_NULLABLE: 0
disconnecting
71 changes: 71 additions & 0 deletions test/src/descrec-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdio.h>
#include <stdlib.h>

#include "common.h"

int main(int argc, char **argv)
{
SQLRETURN rc;
HSTMT hstmt = SQL_NULL_HSTMT;
SQLSMALLINT colcount;
SQLHDESC hDesc;
SQLCHAR name[64];
SQLSMALLINT nameLen = 64;
SQLSMALLINT type, subType;
SQLSMALLINT precision, scale, nullable;
SQLLEN length;

test_connect();

rc = SQLAllocHandle(SQL_HANDLE_STMT, conn, &hstmt);
if (!SQL_SUCCEEDED(rc))
{
print_diag("failed to allocate stmt handle", SQL_HANDLE_DBC, conn);
exit(1);
}

/* Prepare a test table */
rc = SQLExecDirect(hstmt, (SQLCHAR *) "CREATE TEMPORARY TABLE desctable (col1 int4 not null, col2 numeric(4,2), col3 varchar(10) not null, col4 bigint not null)", SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLExecDirect failed while creating temp table", hstmt);

/* Prepare a statement */
rc = SQLPrepare(hstmt, (SQLCHAR *) "SELECT * FROM desctable", SQL_NTS);
CHECK_STMT_RESULT(rc, "SQLPrepare failed", hstmt);

/* Execute */
rc = SQLExecute(hstmt);
CHECK_STMT_RESULT(rc, "SQLExecute failed", hstmt);

rc = SQLNumResultCols(hstmt, &colcount);
CHECK_STMT_RESULT(rc, "SQLNumResultCols failed", hstmt);

/* Get the descriptor handle */
rc = SQLGetStmtAttr(hstmt, SQL_ATTR_IMP_ROW_DESC, &hDesc, 0, NULL);
CHECK_STMT_RESULT(rc, "SQLGetStmtAttr failed", hstmt);

for (int i = 1; i <= colcount; i++)
{
rc = SQLGetDescRec(hDesc, i, name, nameLen, NULL, &type, &subType, &length, &precision, &scale, &nullable);
if (!SQL_SUCCEEDED(rc))
{
print_diag("SQLGetDescRec failed", SQL_HANDLE_DESC, hDesc);
exit(1);
}

printf("\n-- Column %d --\n", i);
printf("SQL_DESC_NAME: %s\n", name);
printf("SQL_DESC_TYPE: %d\n", type);
printf("SQL_DESC_OCTET_LENGTH: %d\n", (int) length);
printf("SQL_DESC_PRECISION: %d\n", precision);
printf("SQL_DESC_SCALE: %d\n", scale);
printf("SQL_DESC_NULLABLE: %d\n", nullable);
}

rc = SQLFreeStmt(hstmt, SQL_CLOSE);
CHECK_STMT_RESULT(rc, "SQLFreeStmt failed", hstmt);

/* Clean up */
test_disconnect();

return 0;
}
3 changes: 2 additions & 1 deletion test/tests
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ TESTBINS = exe/connect-test \
exe/odbc-escapes-test \
exe/wchar-char-test \
exe/params-batch-exec-test \
exe/fetch-refcursors-test
exe/fetch-refcursors-test \
exe/descrec-test

0 comments on commit dcd388e

Please sign in to comment.