1 /*-< TESTALTER.C >---------------------------------------------------*--------*
2  * FastDB                    Version 1.0         (1999  GARRET    *     ?  *
3  * (Main Memory Database Management System                          *   /\|  *
4  *                                                                   *  /  \  *
5  *                          Created:     29-Nov-2003 K.A. Knizhnik   * / [] \ *
6  *                          Last update: 29-Nov-2003 K.A. Knizhnik   * GARRET *
7  *-------------------------------------------------------------------*--------*
8  * Test for cli_alter_table
9  *-------------------------------------------------------------------*--------*/
10 
11 #include "cli.h"
12 #include <stdio.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 
18 #define N_RECORDS 100000
19 
20 typedef struct foo {
21     cli_int4_t  x;
22     char*       y;
23     cli_real8_t z;
24 } foo;
25 
26 typedef struct alter_foo {
27     cli_int8_t  x;
28     char*       y;
29     char*       a;
30     cli_real4_t z;
31     cli_int1_t  b;
32 } alter_foo;
33 
34 
35 static cli_field_descriptor foo_descriptor[] = {
36     {cli_int4, cli_indexed, "x"},
37     {cli_pasciiz, 0, "y"},
38     {cli_real8, 0, "z"}
39 };
40 
41 static cli_field_descriptor alter_foo_descriptor[] = {
42     {cli_int8, cli_indexed, "x"},
43     {cli_pasciiz, 0, "y"},
44     {cli_pasciiz, 0, "a"},
45     {cli_real4, 0, "z"},
46     {cli_int1, 0, "b"}
47 };
48 
49 
main()50 int main()
51 {
52     char_t* databaseName = _T("testalter");
53     char_t* filePath = _T("testalter.fdb");
54     int i, rc;
55     int session;
56     int statement;
57     cli_oid_t oid;
58     foo f;
59     alter_foo af;
60     time_t start = time(NULL);
61 
62     session = cli_create(databaseName,  filePath, 0, 0, 4*1024*1024, 4*1024*1024, 512*1024, 0);
63     if (session < 0) {
64 	fprintf(stderr, "cli_open failed with code %d\n", session);
65 	return EXIT_FAILURE;
66     }
67 
68     rc = cli_create_table(session,
69                           "foo",
70                           sizeof(foo_descriptor)/sizeof(cli_field_descriptor),
71 			  foo_descriptor);
72     if (rc != cli_ok) {
73 	fprintf(stderr, "cli_create_table failed with code %d\n", rc);
74 	return EXIT_FAILURE;
75     }
76     for (i = 0; i < N_RECORDS; i++) {
77         char buf[32];
78         sprintf(buf, "%d", i);
79         f.x = i;
80         f.y = buf;
81         f.z = i + (double)i / 10;
82         rc = cli_insert_struct(session, "foo", &f, &oid);
83         if (rc != cli_ok) {
84             fprintf(stderr, "cli_insert failed with code %d\n", rc);
85             return EXIT_FAILURE;
86         }
87     }
88     printf("Elapsed time for inserting %d records: %d seconds\n", N_RECORDS, (int)(time(NULL) - start));
89     start = time(NULL);
90     rc = cli_alter_table(session,
91                          "foo",
92                          sizeof(alter_foo_descriptor)/sizeof(cli_field_descriptor),
93                          alter_foo_descriptor);
94     if (rc != cli_ok) {
95 	fprintf(stderr, "cli_alter_table failed with code %d\n", rc);
96 	return EXIT_FAILURE;
97     }
98     printf("Elapsed time for reformating %d records: %d seconds\n", N_RECORDS, (int)(time(NULL) - start));
99     start = time(NULL);
100     statement = cli_prepare_query(session, "select * from foo where x=%i");
101     if (statement < 0) {
102         fprintf(stderr, "cli_statement failed with code %d\n", rc);
103         return EXIT_FAILURE;
104     }
105     for (i = 0; i < N_RECORDS; i++) {
106         char buf[32];
107         sprintf(buf, "%d", i);
108         rc = cli_execute_query(statement, cli_view_only, &af, i);
109         if (rc != 1) {
110             fprintf(stderr, "cli_execute_query failed with code %d\n", rc);
111             return EXIT_FAILURE;
112         }
113         rc = cli_get_next(statement);
114         if (rc != cli_ok) {
115             fprintf(stderr, "cli_get_next failed with code %d\n", rc);
116             return EXIT_FAILURE;
117         }
118         if (af.x != i || strcmp(buf, af.y) != 0 || *af.a != '\0' || (int)((af.z+0.01)*10) != i*10+i || af.b != 0) {
119             fprintf(stderr, "Conversion error: i=%d, f.x=%ld, f.y=%s, f.a=%s, f.z=%f, f.b=%d\n", i, (long)af.x, af.y, af.a, af.z, af.b);
120             return EXIT_FAILURE;
121         }
122     }
123     printf("Elapsed time for %d index searches: %d seconds\n", N_RECORDS, (int)(time(NULL) - start));
124     rc = cli_drop_table(session, "foo");
125     if (rc != 0) {
126         fprintf(stderr, "cli_drop_table failed with code %d\n", rc);
127         return EXIT_FAILURE;
128     }
129     rc = cli_close(session);
130     if (rc != cli_ok) {
131         fprintf(stderr, "cli_close failed with code %d\n", rc);
132         return EXIT_FAILURE;
133     }
134     printf("*** CLI alter test sucessfully passed!\n");
135     return EXIT_SUCCESS;
136 }
137 
138