Line data Source code
1 : // Network Address -- classes used to handle table schemas.
2 : // Copyright (c) 2016-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : #include "snapwebsites/snap_exception.h"
20 : #include "snapwebsites/dbutils.h"
21 :
22 : #include <map>
23 : #include <memory>
24 :
25 :
26 : namespace snap
27 : {
28 :
29 :
30 0 : class snap_tables_exception : public snap::snap_logic_exception
31 : {
32 : public:
33 0 : snap_tables_exception(char const * what_msg) : snap_logic_exception(what_msg) {}
34 : snap_tables_exception(std::string const & what_msg) : snap_logic_exception(what_msg) {}
35 0 : snap_tables_exception(QString const & what_msg) : snap_logic_exception(what_msg) {}
36 : };
37 :
38 0 : class snap_table_invalid_xml_exception : public snap_tables_exception
39 : {
40 : public:
41 0 : snap_table_invalid_xml_exception(char const * what_msg) : snap_tables_exception(what_msg) {}
42 : snap_table_invalid_xml_exception(std::string const & what_msg) : snap_tables_exception(what_msg) {}
43 0 : snap_table_invalid_xml_exception(QString const & what_msg) : snap_tables_exception(what_msg) {}
44 : };
45 :
46 0 : class snap_table_unknown_table_exception : public snap_tables_exception
47 : {
48 : public:
49 : snap_table_unknown_table_exception(char const * what_msg) : snap_tables_exception(what_msg) {}
50 : snap_table_unknown_table_exception(std::string const & what_msg) : snap_tables_exception(what_msg) {}
51 0 : snap_table_unknown_table_exception(QString const & what_msg) : snap_tables_exception(what_msg) {}
52 : };
53 :
54 :
55 :
56 :
57 :
58 :
59 : class snap_tables
60 : {
61 : public:
62 : enum class model_t
63 : {
64 : MODEL_CONTENT,
65 : MODEL_DATA,
66 : MODEL_QUEUE,
67 : MODEL_LOG,
68 : MODEL_SESSION
69 : };
70 :
71 : enum class kind_t
72 : {
73 : KIND_THRIFT,
74 : KIND_BLOB
75 : };
76 :
77 : typedef std::shared_ptr<snap_tables> pointer_t;
78 :
79 0 : class column_t
80 : {
81 : public:
82 : typedef std::map<QString, column_t> map_t;
83 :
84 : void set_name(QString const & name);
85 : QString const & get_name() const;
86 :
87 : void set_type(dbutils::column_type_t type);
88 : dbutils::column_type_t get_type() const;
89 :
90 : void set_required(bool required = true);
91 : bool get_required() const;
92 :
93 : void set_description(QString const & description);
94 : QString get_description() const;
95 :
96 : bool has_default_value() const;
97 : void set_default(QString const & default_value);
98 : QString get_default() const;
99 :
100 : void set_min_value(double const min);
101 : double get_min_value() const;
102 :
103 : void set_max_value(double const max);
104 : double get_max_value() const;
105 :
106 : void set_min_length(int64_t const min);
107 : int64_t get_min_length() const;
108 :
109 : void set_max_length(int64_t const max);
110 : int64_t get_max_length() const;
111 :
112 : void set_validation(QString const & validation);
113 : QString get_validation() const;
114 :
115 : void set_limited(bool limited = true);
116 : bool get_limited() const;
117 :
118 : private:
119 : QString f_name = QString();
120 : dbutils::column_type_t f_type = dbutils::column_type_t::CT_string_value;
121 : QString f_description = QString("");
122 : QString f_default = QString();
123 : QString f_validation = QString();
124 0 : double f_min_value = std::numeric_limits<double>::min();
125 0 : double f_max_value = std::numeric_limits<double>::max();
126 0 : int64_t f_min_length = std::numeric_limits<int64_t>::min();
127 0 : int64_t f_max_length = std::numeric_limits<int64_t>::max();
128 : bool f_required = false;
129 : bool f_has_default = false;
130 : bool f_limited = false;
131 : };
132 :
133 0 : class secondary_index_t
134 : {
135 : public:
136 : typedef std::map<QString, secondary_index_t> map_t;
137 :
138 : void set_name(QString const & name);
139 : QString const & get_name() const;
140 :
141 : void set_column(QString const & column);
142 : QString const & get_column() const;
143 :
144 : private:
145 : QString f_name = QString();
146 : QString f_column = QString();
147 : };
148 :
149 0 : class table_schema_t
150 : {
151 : public:
152 : typedef std::map<QString, table_schema_t> map_t;
153 :
154 : void set_name(QString const & name);
155 : QString const & get_name() const;
156 :
157 : void set_description(QString const & description);
158 : QString const & get_description() const;
159 :
160 : void set_model(model_t model);
161 : model_t get_model() const;
162 :
163 : void set_kind(kind_t kind);
164 : kind_t get_kind() const;
165 :
166 : void set_drop(bool drop = true);
167 : bool get_drop() const;
168 :
169 : void set_column(column_t const & column);
170 : column_t::map_t const & get_columns() const;
171 :
172 : void set_secondary_index(secondary_index_t const & index);
173 : secondary_index_t::map_t const &
174 : get_secondary_indexes() const;
175 :
176 : private:
177 : QString f_name = QString();
178 : QString f_description = QString();
179 : model_t f_model = model_t::MODEL_CONTENT;
180 : kind_t f_kind = kind_t::KIND_THRIFT;
181 : column_t::map_t f_columns = column_t::map_t();
182 : secondary_index_t::map_t
183 : f_secondary_indexes = secondary_index_t::map_t();
184 : bool f_drop = false;
185 : };
186 :
187 : bool load(QString const & path);
188 : bool load_xml(QString const & filename);
189 : bool has_table(QString const & name) const;
190 : table_schema_t * get_table(QString const & name) const;
191 : table_schema_t::map_t const & get_schemas() const;
192 :
193 : static model_t string_to_model(QString const & model);
194 : static QString model_to_string(model_t model);
195 :
196 : static kind_t string_to_kind(QString const & kind);
197 : static QString kind_to_string(kind_t kind);
198 :
199 : private:
200 : table_schema_t::map_t f_schemas = table_schema_t::map_t();
201 : };
202 :
203 :
204 : }
205 : // snap namespace
206 : // vim: ts=4 sw=4 et
|