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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*---------------------------------------------------------------------
FILE NAME:
dbtest.c
PURPOSE:
Main entry point for the database tester.
REVISION HISTORY:
Date Programmer Revision Function
01/10/02 M.S. Teel 0 Original
ASSUMPTIONS:
None.
------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <radsysdefs.h>
#include <radmsgLog.h>
#include <radsqlite.h>
/* ... global references
*/
/* ... local memory
*/
#define TEST_DB_USER "testdbuser"
#define TEST_DB_PASSWORD "testdbpw"
#define TEST_DATABASE "/home/mteel/dbtest.sdb"
#define TEST_TABLE "testtable"
/* ... methods
*/
/* ... THE entry point
*/
int main (int argc, char *argv[])
{
DATABASE_ID dbId;
char query[1024];
ROW_ID row;
FIELD_ID field;
int i, retVal;
char temp[1024];
RESULT_SET_ID resultId;
radMsgLogInit ("dbtest", TRUE, TRUE);
if ((dbId = radsqliteOpen (TEST_DATABASE)) == NULL)
{
printf ("dbtest: databaseOpen failed\n");
exit (1);
}
if (!radsqliteTableIfExists (dbId, TEST_TABLE))
{
printf ("dbtest: table %s does not exist, creating ...\n", TEST_TABLE);
row = radsqliteRowDescriptionCreate ();
if (row == NULL)
{
printf ("dbtest: databaseRowDescriptionCreate failed!\n");
radsqliteClose (dbId);
exit (1);
}
retVal = radsqliteRowDescriptionAddField (row,
"f1",
FIELD_BIGINT | FIELD_PRI_KEY,
0);
if (retVal == ERROR)
{
printf ("dbtest: databaseRowDescriptionAddField failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
retVal = radsqliteRowDescriptionAddField (row, "f2", FIELD_STRING, 128);
if (retVal == ERROR)
{
printf ("dbtest: databaseRowDescriptionAddField failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
// now try to create the table:
retVal = radsqliteTableCreate (dbId, TEST_TABLE, row);
if (retVal == ERROR)
{
printf ("dbtest: radsqliteTableCreate failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
radsqliteRowDescriptionDelete (row);
}
else
{
if (radsqliteTableTruncate (dbId, TEST_TABLE) != OK)
{
printf ("dbtest: radsqliteTableTruncate failed!\n");
radsqliteClose (dbId);
exit (1);
}
}
printf ("dbtest: obtaining table description ...\n");
row = radsqliteTableDescriptionGet (dbId, TEST_TABLE);
if (row == NULL)
{
printf ("dbtest: databaseTableDescriptionGet failed!\n");
radsqliteClose (dbId);
exit (1);
}
for (field = (FIELD_ID)radListGetFirst(&row->fields);
field != NULL;
field = (FIELD_ID)radListGetNext(&row->fields, (NODE_PTR)field))
{
printf ("FIELD: %s: type %8.8X\n", field->name, field->type);
}
printf ("\n");
printf ("dbtest: inserting 100 rows ...\n");
for (i = 0; i < 100; i ++)
{
field = radsqliteFieldGet (row, "f1");
if (field == NULL)
{
printf ("dbtest: databaseFieldGet 'f1' failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
radsqliteFieldSetBigIntValue (field, (ULONGLONG)i);
field = radsqliteFieldGet (row, "f2");
if (field == NULL)
{
printf ("dbtest: databaseFieldGet 'f2' failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
sprintf (temp, "Test string #%d", i);
radsqliteFieldSetCharValue (field, temp, strlen (temp));
/* ... insert the row
*/
if (radsqliteTableInsertRow (dbId, TEST_TABLE, row) == ERROR)
{
printf ("dbtest: radsqliteTableInsertRow failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
}
printf ("dbtest: deleting odd numbered rows ...\n");
// get a fresh description
radsqliteRowDescriptionDelete (row);
row = radsqliteTableDescriptionGet (dbId, TEST_TABLE);
if (row == NULL)
{
printf ("dbtest: databaseTableDescriptionGet failed!\n");
radsqliteClose (dbId);
exit (1);
}
for (i = 1; i < 100; i += 2)
{
field = radsqliteFieldGet (row, "f1");
if (field == NULL)
{
printf ("dbtest: databaseFieldGet 'f1' failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
radsqliteFieldSetBigIntValue (field, (ULONGLONG)i);
/* ... delete the row
*/
if (radsqliteTableDeleteRows (dbId, TEST_TABLE, row) == ERROR)
{
printf ("dbtest: radsqliteTableDeleteRows failed!\n");
radsqliteRowDescriptionDelete (row);
radsqliteClose (dbId);
exit (1);
}
}
radsqliteRowDescriptionDelete (row);
printf ("dbtest: querying for rows between 10 and 20 inclusive ...\n");
sprintf (query,
"SELECT * FROM %s WHERE f1 >= 10 AND f1 <= 20",
TEST_TABLE);
if (radsqliteQuery (dbId, query, TRUE) == ERROR)
{
printf ("dbtest: radsqliteQuery failed!\n");
radsqliteClose (dbId);
exit (1);
}
resultId = radsqliteGetResults (dbId);
if (resultId == NULL)
{
printf ("dbtest: radsqliteGetResults failed!\n");
radsqliteClose (dbId);
exit (1);
}
for (row = radsqliteResultsGetFirst (resultId);
row != NULL;
row = radsqliteResultsGetNext (resultId))
{
field = radsqliteFieldGet (row, "f1");
if (field == NULL)
{
printf ("dbtest: radsqliteFieldGet failed!\n");
radsqliteReleaseResults (dbId, resultId);
radsqliteClose (dbId);
exit (1);
}
printf ("dbtest: f1=%lld : ", radsqliteFieldGetBigIntValue (field));
field = radsqliteFieldGet (row, "f2");
if (field == NULL)
{
printf ("dbtest: radsqliteFieldGet failed!\n");
radsqliteReleaseResults (dbId, resultId);
radsqliteClose (dbId);
exit (1);
}
printf ("f2=%s\n", radsqliteFieldGetCharValue (field));
}
radsqliteReleaseResults (dbId, resultId);
printf ("dbtest: done!\n");
radsqliteClose (dbId);
radMsgLogExit ();
exit (0);
}
|