| 1 | /// @file |
| 2 | /// @brief Base class providing schema information to grammar |
| 3 | |
| 4 | #ifndef SCHEMA_HH |
| 5 | #define SCHEMA_HH |
| 6 | |
| 7 | #include <iostream> |
| 8 | #include <memory> |
| 9 | #include <numeric> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "random.hh" |
| 13 | #include "relmodel.hh" |
| 14 | |
| 15 | struct schema { |
| 16 | sqltype *booltype; |
| 17 | sqltype *inttype; |
| 18 | sqltype *internaltype; |
| 19 | sqltype *arraytype; |
| 20 | |
| 21 | std::vector<sqltype *> types; |
| 22 | |
| 23 | std::vector<table> tables; |
| 24 | std::vector<op> operators; |
| 25 | std::vector<routine> routines; |
| 26 | std::vector<routine> aggregates; |
| 27 | |
| 28 | typedef std::tuple<sqltype *, sqltype *, sqltype *> typekey; |
| 29 | std::multimap<typekey, op> index; |
| 30 | typedef std::multimap<typekey, op>::iterator op_iterator; |
| 31 | |
| 32 | std::multimap<sqltype *, routine *> routines_returning_type; |
| 33 | std::multimap<sqltype *, routine *> aggregates_returning_type; |
| 34 | std::multimap<sqltype *, routine *> parameterless_routines_returning_type; |
| 35 | std::multimap<sqltype *, table *> tables_with_columns_of_type; |
| 36 | std::multimap<sqltype *, op *> operators_returning_type; |
| 37 | std::multimap<sqltype *, sqltype *> concrete_type; |
| 38 | std::vector<table *> base_tables; |
| 39 | |
| 40 | string version; |
| 41 | int version_num; // comparable version number |
| 42 | |
| 43 | const char *true_literal = "true" ; |
| 44 | const char *false_literal = "false" ; |
| 45 | |
| 46 | virtual std::string quote_name(const std::string &id) = 0; |
| 47 | |
| 48 | void summary() { |
| 49 | std::cout << "Found " << tables.size() << " user table(s) in information schema." << std::endl; |
| 50 | } |
| 51 | void fill_scope(struct scope &s) { |
| 52 | for (auto &t : tables) |
| 53 | s.tables.push_back(&t); |
| 54 | s.schema = this; |
| 55 | } |
| 56 | virtual void register_operator(op &o) { |
| 57 | operators.push_back(o); |
| 58 | typekey t(o.left, o.right, o.result); |
| 59 | index.insert(std::pair<typekey, op>(t, o)); |
| 60 | } |
| 61 | virtual void register_routine(routine &r) { |
| 62 | routines.push_back(r); |
| 63 | } |
| 64 | virtual void register_aggregate(routine &r) { |
| 65 | aggregates.push_back(r); |
| 66 | } |
| 67 | virtual op_iterator find_operator(sqltype *left, sqltype *right, sqltype *res) { |
| 68 | typekey t(left, right, res); |
| 69 | auto cons = index.equal_range(t); |
| 70 | if (cons.first == cons.second) |
| 71 | return index.end(); |
| 72 | else |
| 73 | return random_pick<>(cons.first, cons.second); |
| 74 | } |
| 75 | schema() { |
| 76 | } |
| 77 | virtual ~schema() { |
| 78 | } |
| 79 | void generate_indexes(); |
| 80 | }; |
| 81 | |
| 82 | #endif |
| 83 | |