-
Notifications
You must be signed in to change notification settings - Fork 5
/
SqlField.cpp
172 lines (132 loc) · 3.1 KB
/
SqlField.cpp
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
#include "SqlField.h"
NS_SQL_BEGIN
////////////////////////////////////////////////////////////////////////////////
// Field, public:
// Creation and lifetime --------------------------------------------------------
Field::Field(field_use use)
{
this->_name.clear();
this->_use = use;
this->_type = type_undefined;
this->_index = -1;
this->_flags = flag_none;
if (use == FIELD_KEY)
{
this->_name = "_ID";
this->_type = type_int;
this->_flags = flag_primary_key;
}
}
Field::Field(string name, field_type type, int flags)
{
this->_name = name;
this->_use = FIELD_DEFAULT;
this->_type = type;
this->_index = -1;
this->_flags = flags;
}
Field::Field(const Field& value)
{
_name = value._name;
_use = value._use;
_type = value._type;
_index = value._index;
_flags = value._flags;
}
// Getters --------------------------------------------------------
field_type Field::getType() const {
return _type;
}
int Field::getIndex() const {
return _index;
}
string Field::getName() const {
return _name;
}
string Field::getTypeStr() const {
switch (_type) {
case type_int: return "INTEGER";
case type_text: return "TEXT";
case type_float: return "REAL";
case type_bool: return "INTEGER";
case type_time: return "INTEGER";
default: return "";
}
return "";
}
// Utils --------------------------------------------------------
bool Field::isKeyIdField() const {
return (_use == FIELD_KEY);
}
bool Field::isEndingField() const {
return (_use == DEFINITION_END);
}
bool Field::isPrimaryKey() const {
return ((_flags & flag_primary_key) == flag_primary_key);
}
bool Field::isNotNull() const {
return ((_flags & flag_not_null) == flag_not_null);
}
string Field::getDefinition() const {
string value = _name + " " + getTypeStr();
if (isPrimaryKey())
value += " PRIMARY KEY";
if (isNotNull())
value += " NOT NULL";
return trim(value);
}
Field* Field::createFromDefinition(string value)
{
std::vector<string> vec;
listToVector(value, vec, " ");
const int count = (int)vec.size();
string _name;
field_use _use = FIELD_DEFAULT;
field_type _type = type_undefined;
int _flags = flag_none;
//parse name
if (count > 0)
_name = vec[0];
//parse type
if (count > 1)
{
std::string& type = vec[1];
if (type.compare("INTEGER") == 0)
_type = type_int;
if (type.compare("TEXT") == 0)
_type = type_text;
if (type.compare("REAL") == 0)
_type = type_float;
}
//parse optional flags
if (count > 2)
{
std::string flags = vec[2];
if (count > 3)
flags += " " + vec[3];
if (flags.find("PRIMARY KEY") != std::string::npos)
{
_use = FIELD_KEY;
_flags = flag_primary_key;
}
if (flags.find("NOT NULL") != std::string::npos)
{
_flags |= flag_not_null;
}
}
Field* field = NULL;
if (!_name.empty())
{
if (_type != type_undefined)
{
if (_use == FIELD_DEFAULT)
{
field = new Field(_name, _type, _flags);
} else {
field = new Field(_use);
}
}
}
return field;
}
NS_SQL_END