adding test scripts
[csql.git] / test / odbc / Metadata / metadata6.c
blobebfa5350b9827fd54d50b1fc6e4638d17904cedf
1 /* CREATE TABLE "T1" WITH 10 FIELDS.
2 SQLStatistics() Should return all Index information
4 */
6 #include<stdio.h>
7 #include<stdlib.h>
8 #include<sql.h>
9 #include<sqlext.h>
10 #include<string.h>
11 #include<CSql.h>
12 //*************************************************************************
13 // ERROR CHECK FUNCTION
14 inline void checkrc(int rc,int line)
16 if(rc)
18 printf("ERROR %d at line %d\n",rc,line);
19 exit(1);
23 //*************************************************************************
24 // FUNCTION FOR INSERTING ROWS IN IT.
26 int FetchTest(SQLHANDLE env, SQLHANDLE dbc, SQLHANDLE stmt,char *tablename)
28 int ret;
29 char f2[50]= "praba";
30 SWORD f1;
31 char f4[50]= "praba";
32 SWORD f3;
33 int f5;
34 char f6[50]= "praba";
36 ret = SQLStatistics(stmt, NULL, 0, NULL, SQL_NTS,
37 (SQLCHAR*) tablename, SQL_NTS, SQL_INDEX_ALL, SQL_QUICK);
39 checkrc(ret,__LINE__);
40 short totalFields=0;
41 ret = SQLNumResultCols (stmt, &totalFields);
42 checkrc(ret,__LINE__);
43 printf( "No of columns in resultset = %d\n",totalFields);
45 ret = SQLBindCol(stmt,4,SQL_C_SHORT,(void*)&f1,0,NULL);
46 checkrc(ret,__LINE__);
47 ret = SQLBindCol(stmt,6,SQL_C_CHAR,f2,sizeof(f2),NULL);
48 checkrc(ret,__LINE__);
49 ret = SQLBindCol(stmt,7,SQL_C_SHORT,(void *)&f3,0,NULL);
50 checkrc(ret,__LINE__);
51 ret = SQLBindCol(stmt,9,SQL_C_CHAR,f4,sizeof(f4),NULL);
52 checkrc(ret,__LINE__);
53 while(SQL_SUCCEEDED(ret = SQLFetch(stmt))){
54 printf(" Unique =%hd \tIndexName = %s \t Type=%hd ColumnName = %s \n ",f1,f2,f3,f4);
56 ret = SQLCloseCursor(stmt);
57 checkrc(ret,__LINE__);
59 ret = SQLTransact(env,dbc,SQL_COMMIT);
60 checkrc(ret,__LINE__);
61 return 0;
66 int main()
68 SQLHENV env;
69 SQLHDBC dbc;
70 SQLHSTMT stmt;
71 SQLRETURN ret;
72 SQLCHAR outstr[1024];
73 SQLSMALLINT outstrlen;
75 // Aloocate an environment handle
76 ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&env);
77 checkrc(ret,__LINE__);
79 //we need odbc3 support
80 SQLSetEnvAttr(env,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
82 //ALLOCATE A Connection handle
83 ret = SQLAllocHandle(SQL_HANDLE_DBC,env,&dbc);
84 checkrc(ret,__LINE__);
86 // connect to the DSN mydsn
87 ret = SQLConnect (dbc,
88 (SQLCHAR *) "test", (SQLSMALLINT) strlen ("test"),
89 (SQLCHAR *) "root",
90 (SQLSMALLINT) strlen ("root"),
91 (SQLCHAR *) "manager",
92 (SQLSMALLINT) strlen (""));
95 if(SQL_SUCCEEDED(ret))
97 printf("\nConnected to the Data Source..\n");
102 else
104 printf("error in conenction\n");
106 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
107 checkrc(ret,__LINE__);
109 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
110 checkrc(ret,__LINE__);
111 return 1;
115 //*****************************************************
116 // TABLE CREATED
117 ret = SQLAllocHandle(SQL_HANDLE_STMT,dbc,&stmt);
118 checkrc(ret,__LINE__);
120 SQLCHAR table[200]=
121 "CREATE TABLE T1(F1 INT,F2 SMALLINT,F3 CHAR(30),F4 FLOAT,F5 FLOAT,F6 DATE,F7 TIME,F8 TIMESTAMP,F9 TINYINT,F10 BIGINT, PRIMARY KEY(F2));";
123 ret = SQLPrepare(stmt,table,SQL_NTS);
124 checkrc(ret,__LINE__);
125 ret = SQLExecute(stmt);
126 checkrc(ret,__LINE__);
127 ret = SQLPrepare(stmt,(SQLCHAR *)"CREATE INDEX INDS ON T1(F3) TREE;",SQL_NTS);
128 checkrc(ret,__LINE__);
129 ret = SQLExecute(stmt);
130 checkrc(ret,__LINE__);
132 printf("\nTABLE CREATED\n");
133 //***************************
134 FetchTest(env,dbc,stmt,"T1");
135 //**************************************************
136 SQLCHAR drop[100]="DROP TABLE T1";
137 ret = SQLPrepare(stmt,drop,SQL_NTS);
138 checkrc(ret,__LINE__);
139 ret = SQLExecute(stmt);
140 if(ret!=SQL_SUCCESS && ret !=SQL_SUCCESS_WITH_INFO)
142 printf("Statement failed\n");
143 return 1;
147 printf("Tables dropped successfully\n");
149 ret = SQLFreeHandle(SQL_HANDLE_STMT,stmt);
150 checkrc(ret,__LINE__);
152 ret = SQLDisconnect(dbc);
153 checkrc(ret,__LINE__);
155 ret = SQLFreeHandle(SQL_HANDLE_DBC,dbc);
156 checkrc(ret,__LINE__);
158 ret = SQLFreeHandle(SQL_HANDLE_ENV,env);
159 checkrc(ret,__LINE__);
160 return 0;