1 //-< TESTDB.CPP >----------------------------------------------------*--------*
2 // FastDB                    Version 1.0         (c) 1999  GARRET    *     ?  *
3 // (Main Memory Database Management System)                          *   /\|  *
4 //                                                                   *  /  \  *
5 //                          Created:      1-Jan-99    K.A. Knizhnik  * / [] \ *
6 //                          Last update: 18-Jan-99    K.A. Knizhnik  * GARRET *
7 //-------------------------------------------------------------------*--------*
8 // Test of FastDB data definition and manipulation facilities.
9 // First create database by running this application, then
10 // fill database by "subsql testddl.sql" and then run this applciation once
11 // again to check correctness of inserted data.
12 //-------------------------------------------------------------------*--------*
13 
14 #include "fastdb.h"
15 #include <stdio.h>
16 #include <math.h>
17 
18 USE_FASTDB_NAMESPACE
19 
20 class Subrecord {
21   public:
22     char* str;
23     db_int8  i;
24 
dump() const25     void dump() const {
26         printf("('%s' " INT8_FORMAT ") ", str, i);
27     }
28 
29     TYPE_DESCRIPTOR((FIELD(str),
30                      FIELD(i)));
31 };
32 
33 class Base {
34   public:
35     dbArray<Subrecord> x;
36     dbArray<char*>     y;
37     dbArray<int2>      z;
38     dbArray< dbArray<int4> > w;
39 
dump() const40     void dump() const {
41         size_t i, j;
42         printf("(");
43         for (i = 0; i < x.length(); i++) {
44             x[i].dump();
45         }
46         printf(") (");
47         for (i = 0; i < y.length(); i++) {
48             printf("'%s' ", y[i]);
49         }
50         printf(") (");
51         for (i = 0; i < z.length(); i++) {
52             printf("%d ", z[i]);
53         }
54         printf(") (");
55         for (i = 0; i < w.length(); i++) {
56             printf("(");
57             for (j = 0; j < w[i].length(); j++) {
58                 printf("%d ", w[i][j]);
59             }
60             printf(") ");
61         }
62         printf(")");
63     }
64     TYPE_DESCRIPTOR((FIELD(x),
65                      FIELD(y),
66                      FIELD(z),
67                      FIELD(w)));
68 };
69 
70 class Record : public Base {
71   public:
72     int4  a;
73     int1  b;
74     real8 c;
75     bool  d;
76     int2  e;
77     real4 f;
78     dbReference<Record> g;
79     Subrecord h;
80     struct obscure {
81         int1 data[16];
82     } o;
83 
dump() const84     void dump() const {
85         Base::dump();
86         printf(" %d %d %f %s %d %f (%02x%02x%02x%02x)",
87                a, b, c, d ? "true" : "false", e, f,
88                o.data[0], o.data[1], o.data[2], o.data[3]);
89         h.dump();
90         printf("\n");
91     }
92     TYPE_DESCRIPTOR((SUPERCLASS(Base),
93                      FIELD(a),
94                      FIELD(b),
95                      FIELD(c),
96                      FIELD(d),
97                      FIELD(e),
98                      FIELD(f),
99                      FIELD(g),
100                      FIELD(h),
101                      RAWFIELD(o)));
102 };
103 
104 REGISTER(Record);
105 
mysin(double x)106 static double mysin(double x) { return sin(x); }
mycos(double x)107 static double mycos(double x) { return cos(x); }
108 
109 USER_FUNC(mysin);
110 USER_FUNC(mycos);
111 
main()112 int main()
113 {
114     dbDatabase db;
115 
116     if (db.open(_T("testddl"))) {
117         printf("Database intialized\n");
118         dbCursor<Record> cursor;
119         if (cursor.select("abs(sin(c)^2 + cos(c)^2 - 1) < 1e-5") > 0) {
120             do {
121                 cursor->dump();
122             } while (cursor.next());
123         } else {
124             printf("No records in table\n");
125         }
126         db.close();
127     } else {
128         printf("Failed to open database\n");
129     }
130     return 0;
131 }
132