Merge commit 'origin/master'
[versaplex.git] / vxodbc / t / column.cc
blob30605e6b43e71a5048700d4cd3c39bd876d63f92
1 #include "column.h"
3 #include "wvstring.h"
5 WvString ColumnInfo::ColTypeNames[ColumnTypeMax + 1] = {
6 "Int64",
7 "Int32",
8 "Int16",
9 "UInt8",
10 "Bool",
11 "Double",
12 "Uuid",
13 "Binary",
14 "String",
15 "DateTime",
16 "Decimal",
17 WvString::null
20 WvString ColumnInfo::ColTypeDBusType[ColumnTypeMax + 1] = {
21 "x",
22 "i",
23 "n",
24 "y",
25 "b",
26 "d",
27 "s",
28 "ay",
29 "s",
30 "(xi)",
31 "s"
34 // Note: These use the MSSQL names for data types.
35 // FIXME: It may make sense to specify the SQL datatypes in the enum, and
36 // convert them to more DBussy types later, IOW flip this around.
37 WvString ColumnInfo::getSqlColtype()
39 switch(coltype)
41 case Int64:
42 return "bigint";
43 case Int32:
44 return "int";
45 case Int16:
46 return "smallint";
47 case UInt8:
48 return "tinyint";
49 case Bool:
50 return "bit";
51 case Double:
52 return "float";
53 case Uuid:
54 return "uniqueidentifier";
55 case Binary:
56 return "binary";
57 case String:
58 if (size > 0)
59 return WvString("varchar(%s)", size);
60 else
61 return WvString("text");
62 case DateTime:
63 return "datetime";
64 case Decimal:
65 return WvString("numeric(%s,%s)", precision, scale);
66 case ColumnTypeMax:
67 default:
68 WVFAILEQ(WvString("Unknown SQL type %d", coltype), WvString::null);
69 return "Unknown";
73 void ColumnInfo::writeHeader(WvDBusMsg &msg)
75 msg.struct_start(getDBusSignature());
76 msg.append(size);
77 msg.append(colname);
78 msg.append(ColumnInfo::ColTypeNames[coltype]);
79 msg.append(precision);
80 msg.append(scale);
81 msg.append(nullable);
82 msg.struct_end();
85 void Column::addDataTo(WvDBusMsg &reply)
87 if (data.size() < 1)
88 return;
90 switch (info.coltype)
92 case ColumnInfo::Int64:
93 reply.append(*(long long *)data[0]);
94 break;
95 case ColumnInfo::Int32:
96 reply.append(*(int *)data[0]);
97 break;
98 case ColumnInfo::Int16:
99 reply.append(*(short *)data[0]);
100 break;
101 case ColumnInfo::UInt8:
102 reply.append(*(unsigned char *)data[0]);
103 break;
104 case ColumnInfo::Bool:
105 reply.append(*(bool *)data[0]);
106 break;
107 case ColumnInfo::Double:
108 reply.append(*(double *)data[0]);
109 break;
110 case ColumnInfo::Uuid:
111 reply.append((char *)data[0]);
112 break;
113 case ColumnInfo::Binary:
115 unsigned char *blob = (unsigned char *)data[0];
116 reply.array_start("y");
117 for (int i = 0; i < info.size; ++i)
118 reply.append(blob[i]);
119 reply.array_end();
120 break;
122 case ColumnInfo::String:
123 reply.append((char *)data[0]);
124 break;
125 case ColumnInfo::DateTime:
126 reply.struct_start("ii");
127 // FIXME: Each element in the vector should be a complete entry
128 WVPASS(data.size() >= 2);
129 reply.append(*(long long *)data[0]);
130 reply.append(*(int *)data[1]);
131 reply.struct_end();
132 break;
133 case ColumnInfo::Decimal:
134 reply.append((char *)data[0]);
135 break;
136 case ColumnInfo::ColumnTypeMax:
137 default:
138 WVFAILEQ(WvString("Unknown SQL type %d", info.coltype), WvString::null);
139 break;
141 return;
144 Column& Column::append(WvStringParm str)
146 char *newstr = (char *)malloc(str.len() + 1);
147 strcpy(newstr, str.cstr());
148 data.push_back(newstr);
149 return *this;
152 // FIXME: It might be nice to template this.
153 Column& Column::append(long long element)
155 long long *newelem = (long long *)malloc(sizeof(element));
156 *newelem = element;
157 data.push_back(newelem);
158 return *this;
161 Column& Column::append(int element)
163 // It's error-prone to have to specify LL after every time value literal,
164 // even the ones that fit into 32 bits.
165 if (info.coltype == ColumnInfo::DateTime && data.size() == 0)
166 return append((long long)element);
168 int *newelem = (int *)malloc(sizeof(element));
169 *newelem = element;
170 data.push_back(newelem);
171 return *this;
174 Column& Column::append(short element)
176 short *newelem = (short *)malloc(sizeof(element));
177 *newelem = element;
178 data.push_back(newelem);
179 return *this;
182 Column& Column::append(unsigned char element)
184 unsigned char *newelem = (unsigned char *)malloc(sizeof(element));
185 *newelem = element;
186 data.push_back(newelem);
187 return *this;
190 Column& Column::append(signed char element)
192 signed char *newelem = (signed char *)malloc(sizeof(element));
193 *newelem = element;
194 data.push_back(newelem);
195 return *this;
198 Column& Column::append(double element)
200 double *newelem = (double *)malloc(sizeof(element));
201 *newelem = element;
202 data.push_back(newelem);
203 return *this;