summaryrefslogtreecommitdiff
path: root/test/src/colattribute-test.c
blob: 900e87dd4d23027c0d64349c0ef2c89fe03cb5a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Test SQLColAttribute
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "common.h"

static void
runtest(char *extra_conn_options)
{
	int rc;
	HSTMT hstmt = SQL_NULL_HSTMT;
	SQLUSMALLINT i;
	SQLSMALLINT numcols;

	printf("Running tests with %s...\n", extra_conn_options);

	/* The behavior of these tests depend on the UnknownSizes parameter */
	test_connect_ext(extra_conn_options);

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

	/*
	 * Get column attributes of a simple query.
	 */
	printf("Testing SQLColAttribute...\n");
	rc = SQLExecDirect(hstmt,
			(SQLCHAR *) "SELECT '1'::int AS intcol, 'foobar'::text AS textcol, 'varchar string'::varchar as varcharcol, ''::varchar as empty_varchar_col, 'varchar-5-col'::varchar(5) as varchar5col, '5 days'::interval day to second",
			SQL_NTS);
	CHECK_STMT_RESULT(rc, "SQLExecDirect failed", hstmt);

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

	for (i = 1 ; i <= numcols; i++)
	{
		char buffer[64];
		SQLLEN number;

		rc = SQLColAttribute(hstmt, i, SQL_DESC_LABEL, buffer, sizeof(buffer), NULL, NULL);
		CHECK_STMT_RESULT(rc, "SQLColAttribute failed", hstmt);
		printf("\n-- Column %d: %s --\n", i, buffer);

		rc = SQLColAttribute(hstmt, i, SQL_DESC_OCTET_LENGTH, NULL, SQL_IS_INTEGER, NULL, &number);
		CHECK_STMT_RESULT(rc, "SQLColAttribute failed", hstmt);
		printf("SQL_DESC_OCTET_LENGTH: %d\n", (int) number);

		rc = SQLColAttribute(hstmt, i, SQL_DESC_TYPE_NAME, buffer, sizeof(buffer), NULL, NULL);
		CHECK_STMT_RESULT(rc, "SQLColAttribute failed", hstmt);
		printf("SQL_DESC_TYPE_NAME: %s\n", buffer);
	}

	/* Clean up */
	test_disconnect();
}

int main(int argc, char **argv)
{

	/*
	 * The output of these tests depend on the UnknownSizes and
	 * MaxVarcharSize parameters
	 */
	// runtest("UnknownSizes=-1;MaxVarcharSize=100"); meaningless
	runtest("UnknownSizes=0;MaxVarcharSize=100");
	runtest("UnknownSizes=1;MaxVarcharSize=100");
	runtest("UnknownSizes=2;MaxVarcharSize=100");
	// runtest("UnknownSizes=100;MaxVarcharSize=100"); meaningless

	return 0;
}