|
11 | 11 | #include "expire-tiles.hpp" |
12 | 12 | #include "flex-index.hpp" |
13 | 13 | #include "flex-lua-geom.hpp" |
| 14 | +#include "flex-lua-index.hpp" |
14 | 15 | #include "format.hpp" |
15 | 16 | #include "geom-from-osm.hpp" |
16 | 17 | #include "geom-functions.hpp" |
@@ -1180,37 +1181,6 @@ void output_flex_t::setup_flex_table_columns(flex_table_t *table) |
1180 | 1181 | lua_pop(lua_state(), 1); // "columns" |
1181 | 1182 | } |
1182 | 1183 |
|
1183 | | -static void check_and_add_column(flex_table_t const &table, |
1184 | | - std::vector<std::string> *columns, |
1185 | | - char const *column_name) |
1186 | | -{ |
1187 | | - auto const *column = util::find_by_name(table, column_name); |
1188 | | - if (!column) { |
1189 | | - throw std::runtime_error{"Unknown column '{}' in table '{}'."_format( |
1190 | | - column_name, table.name())}; |
1191 | | - } |
1192 | | - columns->push_back(column_name); |
1193 | | -} |
1194 | | - |
1195 | | -static void check_and_add_columns(flex_table_t const &table, |
1196 | | - std::vector<std::string> *columns, |
1197 | | - lua_State *lua_state) |
1198 | | -{ |
1199 | | - lua_pushnil(lua_state); |
1200 | | - while (lua_next(lua_state, -2) != 0) { |
1201 | | - if (!lua_isnumber(lua_state, -2)) { |
1202 | | - throw std::runtime_error{ |
1203 | | - "The 'column' field must contain a string or an array."}; |
1204 | | - } |
1205 | | - if (!lua_isstring(lua_state, -1)) { |
1206 | | - throw std::runtime_error{ |
1207 | | - "The entries in the 'column' array must be strings."}; |
1208 | | - } |
1209 | | - check_and_add_column(table, columns, lua_tostring(lua_state, -1)); |
1210 | | - lua_pop(lua_state, 1); // table |
1211 | | - } |
1212 | | -} |
1213 | | - |
1214 | 1184 | void output_flex_t::setup_indexes(flex_table_t *table) |
1215 | 1185 | { |
1216 | 1186 | assert(table); |
@@ -1245,87 +1215,12 @@ void output_flex_t::setup_indexes(flex_table_t *table) |
1245 | 1215 | } |
1246 | 1216 | if (!lua_istable(lua_state(), -1)) { |
1247 | 1217 | throw std::runtime_error{ |
1248 | | - "The entries in the 'indexes' array must be tables."}; |
1249 | | - } |
1250 | | - |
1251 | | - char const *const method = luaX_get_table_string( |
1252 | | - lua_state(), "method", -1, "Index definition"); |
1253 | | - if (!has_index_method(method)) { |
1254 | | - throw std::runtime_error{ |
1255 | | - "Unknown index method '{}'."_format(method)}; |
1256 | | - } |
1257 | | - |
1258 | | - auto &index = table->add_index(method); |
1259 | | - |
1260 | | - std::vector<std::string> columns; |
1261 | | - lua_getfield(lua_state(), -2, "column"); |
1262 | | - if (lua_isstring(lua_state(), -1)) { |
1263 | | - check_and_add_column(*table, &columns, |
1264 | | - lua_tostring(lua_state(), -1)); |
1265 | | - index.set_columns(columns); |
1266 | | - } else if (lua_istable(lua_state(), -1)) { |
1267 | | - check_and_add_columns(*table, &columns, lua_state()); |
1268 | | - if (columns.empty()) { |
1269 | | - throw std::runtime_error{ |
1270 | | - "The 'column' field in an index definition can not be an " |
1271 | | - "empty array."}; |
1272 | | - } |
1273 | | - index.set_columns(columns); |
1274 | | - } else if (!lua_isnil(lua_state(), -1)) { |
1275 | | - throw std::runtime_error{ |
1276 | | - "The 'column' field in an index definition must contain a " |
1277 | | - "string or an array."}; |
1278 | | - } |
1279 | | - |
1280 | | - std::string const expression = luaX_get_table_string( |
1281 | | - lua_state(), "expression", -3, "Index definition", ""); |
1282 | | - |
1283 | | - index.set_expression(expression); |
1284 | | - |
1285 | | - if (expression.empty() == columns.empty()) { |
1286 | | - throw std::runtime_error{"You must set either the 'column' or the " |
1287 | | - "'expression' field in index definition."}; |
1288 | | - } |
1289 | | - |
1290 | | - std::vector<std::string> include_columns; |
1291 | | - lua_getfield(lua_state(), -4, "include"); |
1292 | | - if (get_database_version() >= 110000) { |
1293 | | - if (lua_isstring(lua_state(), -1)) { |
1294 | | - check_and_add_column(*table, &include_columns, |
1295 | | - lua_tostring(lua_state(), -1)); |
1296 | | - } else if (lua_istable(lua_state(), -1)) { |
1297 | | - check_and_add_columns(*table, &include_columns, lua_state()); |
1298 | | - } else if (!lua_isnil(lua_state(), -1)) { |
1299 | | - throw std::runtime_error{ |
1300 | | - "The 'include' field in an index definition must contain a " |
1301 | | - "string or an array."}; |
1302 | | - } |
1303 | | - index.set_include_columns(include_columns); |
1304 | | - } else if (!lua_isnil(lua_state(), -1)) { |
1305 | | - throw std::runtime_error{ |
1306 | | - "Database version ({}) doesn't support" |
1307 | | - " include columns in indexes."_format(get_database_version())}; |
| 1218 | + "The entries in the 'indexes' array must be Lua tables."}; |
1308 | 1219 | } |
1309 | 1220 |
|
1310 | | - std::string const tablespace = luaX_get_table_string( |
1311 | | - lua_state(), "tablespace", -5, "Index definition", ""); |
1312 | | - check_identifier(tablespace, "tablespace"); |
1313 | | - if (!has_tablespace(tablespace)) { |
1314 | | - throw std::runtime_error{ |
1315 | | - "Unknown tablespace '{}'."_format(tablespace)}; |
1316 | | - } |
1317 | | - index.set_tablespace(tablespace.empty() ? table->index_tablespace() |
1318 | | - : tablespace); |
1319 | | - |
1320 | | - index.set_is_unique(luaX_get_table_bool(lua_state(), "unique", -6, |
1321 | | - "Index definition", false)); |
| 1221 | + flex_lua_setup_index(lua_state(), table); |
1322 | 1222 |
|
1323 | | - index.set_where_condition(luaX_get_table_string( |
1324 | | - lua_state(), "where", -7, "Index definition", "")); |
1325 | | - |
1326 | | - // stack has: "where", "unique", "tablespace", "includes", "expression", |
1327 | | - // "column", "method", table |
1328 | | - lua_pop(lua_state(), 8); |
| 1223 | + lua_pop(lua_state(), 1); |
1329 | 1224 | } |
1330 | 1225 |
|
1331 | 1226 | lua_pop(lua_state(), 1); // "indexes" |
|
0 commit comments