*** empty log message ***
[csql.git] / test / dbapi / Index / common.h
blobd8b678822e9258a375d325702ae055947107e847
1 #include<CSql.h>
2 int createIndex(DatabaseManager *dbMgr, bool unique)
4 //Creating hash index on field f1 of table t1
5 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
6 strcpy(idxInfo->tableName, "t1");
7 idxInfo->list.append("f1");
8 #ifdef TREEINDEX
9 idxInfo->indType = treeIndex;
10 #elif TRIEINDEX
11 idxInfo->indType = trieIndex;
12 #else
13 idxInfo->indType = hashIndex;
14 #endif
15 #ifndef DEFAULT
16 idxInfo->isUnique = unique;
17 #endif
18 DbRetVal rv = dbMgr->createIndex("indx1", idxInfo);
19 if (rv != OK) { printf("Index creation failed\n"); return 1; }
20 printf("Index created\n");
21 delete idxInfo;
22 return 0;
24 int createTable(DatabaseManager *dbMgr)
26 TableDef tabDef;
27 tabDef.addField("f1", typeInt, 0, NULL, true);
28 tabDef.addField("f2", typeInt);
29 tabDef.addField("f3", typeString, 20);
30 DbRetVal rv = dbMgr->createTable("t1", tabDef);
31 if (rv != OK) { printf("Table creation failed\n"); return 1; }
32 printf("Table created\n");
33 return 0;
35 int insertTupleWithSameValue(DatabaseManager *dbMgr, Connection &conn)
37 Table *table = dbMgr->openTable("t1");
38 if (table == NULL)
40 printf("Unable to open table\n");
41 return 0;
43 int id1 = 0, id2 = 5;
44 char name[20] = "PRAVEEN";
45 table->bindFld("f1", &id1);
46 table->bindFld("f2", &id2);
47 table->bindFld("f3", name);
48 int icount =0;
49 DbRetVal rv = OK;
50 for (int i = 0 ; i < 10 ; i++)
52 conn.startTransaction();
53 id1= 10;
54 rv = table->insertTuple();
55 if (rv != OK) break;
56 icount++;
57 conn.commit();
60 printf("Total Tuples inserted is %d\n", icount);
61 dbMgr->closeTable(table);
62 return icount;
66 int insertTuplef2NUL(DatabaseManager *dbMgr, Connection &conn)
68 Table *table = dbMgr->openTable("t1");
69 if (table == NULL)
71 printf("Unable to open table\n");
72 return 0;
74 int id1 = 0, id2;
75 int count=0;
76 table->bindFld("f1", &id1);
77 //table->bindFld("f2", &id2);
78 DbRetVal rv = OK;
79 for( int i=10;i<=20;i=i+10)
81 conn.startTransaction();
82 id1= i;
83 rv = table->insertTuple();
84 if (rv != OK) { printf("Insertion failed "); break; }
85 count++;
86 conn.commit();
88 printf("Tuple inserted is %d \n",count);
89 dbMgr->closeTable(table);
90 return 0;
93 int creatTable(DatabaseManager *dbMgr)
95 TableDef tabDef;
96 tabDef.addField("f1", typeInt, 0, NULL, true);
97 tabDef.addField("f2", typeInt);
98 DbRetVal rv = dbMgr->createTable("t1", tabDef);
99 if (rv != OK) { printf("Table creation failed\n"); return 1; }
100 printf("Table created\n");
101 return 0;
103 int createIndex(DatabaseManager *dbMgr)
105 //Creating hash index on field f1 of table t1
106 HashIndexInitInfo *idxInfo = new HashIndexInitInfo();
107 strcpy(idxInfo->tableName, "t1");
108 idxInfo->list.append("f1");
109 #ifdef TREEINDEX
110 idxInfo->indType = treeIndex;
111 #elif TRIEINDEX
112 idxInfo->indType = trieIndex;
113 #else
114 idxInfo->indType = hashIndex;
115 #endif
117 DbRetVal rv = dbMgr->createIndex("indx1", idxInfo);
118 if (rv != OK) { printf("Index creation failed\n"); return 1; }
119 printf("Index created for t1(f1)\n");
121 //Creating hash index on field f2 of table t1
122 strcpy(idxInfo->tableName, "t1");
123 idxInfo->list.remove("f1");
124 idxInfo->list.append("f2");
125 #ifdef TREEINDEX
126 idxInfo->indType = treeIndex;
127 #elif TRIEINDEX
128 idxInfo->indType = trieIndex;
129 #else
130 idxInfo->indType = hashIndex;
131 #endif
133 rv = dbMgr->createIndex("indx2", idxInfo);
134 if (rv != OK) { printf("Index creation failed\n"); return 1; }
135 printf("Index created for t1(f2)\n");
137 delete idxInfo;
138 return 0;