| 1 | /* |
| 2 | Copyright (c) 2013 Monty Program Ab |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License |
| 6 | as published by the Free Software Foundation; version 2 of |
| 7 | the License. |
| 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 | */ |
| 18 | |
| 19 | /* |
| 20 | a really minimal engine to test table discovery via sql statements. |
| 21 | See the archive engine if you're interested in real-life usable engine that |
| 22 | uses discovery via frm shipping. |
| 23 | */ |
| 24 | |
| 25 | #include <my_global.h> |
| 26 | #include <mysql_version.h> |
| 27 | #include <handler.h> |
| 28 | #include <table.h> |
| 29 | |
| 30 | static MYSQL_THDVAR_STR(statement, PLUGIN_VAR_MEMALLOC, |
| 31 | "The table name and the SQL statement to discover the next table" , |
| 32 | NULL, NULL, 0); |
| 33 | |
| 34 | static MYSQL_THDVAR_BOOL(write_frm, 0, |
| 35 | "Whether to cache discovered table metadata in frm files" , |
| 36 | NULL, NULL, TRUE); |
| 37 | |
| 38 | static struct st_mysql_sys_var *sysvars[] = { |
| 39 | MYSQL_SYSVAR(statement), |
| 40 | MYSQL_SYSVAR(write_frm), |
| 41 | NULL |
| 42 | }; |
| 43 | |
| 44 | class TSD_share : public Handler_share { |
| 45 | public: |
| 46 | THR_LOCK lock; |
| 47 | TSD_share() |
| 48 | { |
| 49 | thr_lock_init(&lock); |
| 50 | } |
| 51 | ~TSD_share() |
| 52 | { |
| 53 | thr_lock_delete(&lock); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | class ha_tsd: public handler |
| 58 | { |
| 59 | private: |
| 60 | THR_LOCK_DATA lock; |
| 61 | TSD_share *share; |
| 62 | TSD_share *get_share(); |
| 63 | |
| 64 | public: |
| 65 | ha_tsd(handlerton *hton, TABLE_SHARE *table_arg) |
| 66 | : handler(hton, table_arg) { } |
| 67 | ulonglong table_flags() const |
| 68 | { // NO_TRANSACTIONS and everything that affects CREATE TABLE |
| 69 | return HA_NO_TRANSACTIONS | HA_CAN_GEOMETRY | HA_NULL_IN_KEY | |
| 70 | HA_CAN_INDEX_BLOBS | HA_AUTO_PART_KEY | HA_CAN_RTREEKEYS | |
| 71 | HA_CAN_FULLTEXT; |
| 72 | } |
| 73 | |
| 74 | ulong index_flags(uint inx, uint part, bool all_parts) const { return 0; } |
| 75 | |
| 76 | THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, |
| 77 | enum thr_lock_type lock_type) |
| 78 | { |
| 79 | if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK) |
| 80 | lock.type = lock_type; |
| 81 | *to ++= &lock; |
| 82 | return to; |
| 83 | } |
| 84 | |
| 85 | int rnd_init(bool scan) { return 0; } |
| 86 | int rnd_next(unsigned char *buf) { return HA_ERR_END_OF_FILE; } |
| 87 | void position(const uchar *record) { } |
| 88 | int rnd_pos(uchar *buf, uchar *pos) { return HA_ERR_END_OF_FILE; } |
| 89 | int info(uint flag) { return 0; } |
| 90 | uint max_supported_keys() const { return 16; } |
| 91 | int create(const char *name, TABLE *table_arg, |
| 92 | HA_CREATE_INFO *create_info) { return HA_ERR_WRONG_COMMAND; } |
| 93 | |
| 94 | int open(const char *name, int mode, uint test_if_locked); |
| 95 | int close(void); |
| 96 | }; |
| 97 | |
| 98 | TSD_share *ha_tsd::get_share() |
| 99 | { |
| 100 | TSD_share *tmp_share; |
| 101 | lock_shared_ha_data(); |
| 102 | if (!(tmp_share= static_cast<TSD_share*>(get_ha_share_ptr()))) |
| 103 | { |
| 104 | tmp_share= new TSD_share; |
| 105 | if (!tmp_share) |
| 106 | goto err; |
| 107 | |
| 108 | set_ha_share_ptr(static_cast<Handler_share*>(tmp_share)); |
| 109 | } |
| 110 | err: |
| 111 | unlock_shared_ha_data(); |
| 112 | return tmp_share; |
| 113 | } |
| 114 | |
| 115 | int ha_tsd::open(const char *name, int mode, uint test_if_locked) |
| 116 | { |
| 117 | if (!(share= get_share())) |
| 118 | return HA_ERR_OUT_OF_MEM; |
| 119 | |
| 120 | thr_lock_data_init(&share->lock,&lock,NULL); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | int ha_tsd::close(void) |
| 125 | { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | static handler *create_handler(handlerton *hton, TABLE_SHARE *table, |
| 130 | MEM_ROOT *mem_root) |
| 131 | { |
| 132 | return new (mem_root) ha_tsd(hton, table); |
| 133 | } |
| 134 | |
| 135 | static int discover_table(handlerton *hton, THD* thd, TABLE_SHARE *share) |
| 136 | { |
| 137 | const char *sql= THDVAR(thd, statement); |
| 138 | |
| 139 | // the table is discovered if sql starts from "table_name:" |
| 140 | if (!sql || |
| 141 | strncmp(sql, share->table_name.str, share->table_name.length) || |
| 142 | sql[share->table_name.length] != ':') |
| 143 | return HA_ERR_NO_SUCH_TABLE; |
| 144 | |
| 145 | sql+= share->table_name.length + 1; |
| 146 | return share->init_from_sql_statement_string(thd, THDVAR(thd, write_frm), |
| 147 | sql, strlen(sql)); |
| 148 | } |
| 149 | |
| 150 | static int init(void *p) |
| 151 | { |
| 152 | handlerton *hton = (handlerton *)p; |
| 153 | hton->create = create_handler; |
| 154 | hton->discover_table = discover_table; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | struct st_mysql_storage_engine descriptor = |
| 159 | { MYSQL_HANDLERTON_INTERFACE_VERSION }; |
| 160 | |
| 161 | maria_declare_plugin(test_sql_discovery) |
| 162 | { |
| 163 | MYSQL_STORAGE_ENGINE_PLUGIN, |
| 164 | &descriptor, |
| 165 | "TEST_SQL_DISCOVERY" , |
| 166 | "Sergei Golubchik" , |
| 167 | "Minimal engine to test table discovery via sql statements" , |
| 168 | PLUGIN_LICENSE_GPL, |
| 169 | init, |
| 170 | NULL, |
| 171 | 0x0001, |
| 172 | NULL, |
| 173 | sysvars, |
| 174 | "0.1" , |
| 175 | MariaDB_PLUGIN_MATURITY_EXPERIMENTAL |
| 176 | } |
| 177 | maria_declare_plugin_end; |
| 178 | |
| 179 | |