|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + * |
| 4 | + * This file is part of osm2pgsql (https://osm2pgsql.org/). |
| 5 | + * |
| 6 | + * Copyright (C) 2006-2022 by the osm2pgsql developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include "flex-lua-index.hpp" |
| 11 | +#include "lua-utils.hpp" |
| 12 | +#include "output-flex.hpp" |
| 13 | +#include "pgsql-capabilities.hpp" |
| 14 | +#include "util.hpp" |
| 15 | + |
| 16 | +#include <string> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +static void check_and_add_column(flex_table_t const &table, |
| 20 | + std::vector<std::string> *columns, |
| 21 | + char const *column_name) |
| 22 | +{ |
| 23 | + auto const *column = util::find_by_name(table, column_name); |
| 24 | + if (!column) { |
| 25 | + throw std::runtime_error{"Unknown column '{}' in table '{}'."_format( |
| 26 | + column_name, table.name())}; |
| 27 | + } |
| 28 | + columns->push_back(column_name); |
| 29 | +} |
| 30 | + |
| 31 | +static void check_and_add_columns(flex_table_t const &table, |
| 32 | + std::vector<std::string> *columns, |
| 33 | + lua_State *lua_state) |
| 34 | +{ |
| 35 | + lua_pushnil(lua_state); |
| 36 | + while (lua_next(lua_state, -2) != 0) { |
| 37 | + if (!lua_isnumber(lua_state, -2)) { |
| 38 | + throw std::runtime_error{ |
| 39 | + "The 'column' field must contain a string or an array."}; |
| 40 | + } |
| 41 | + if (!lua_isstring(lua_state, -1)) { |
| 42 | + throw std::runtime_error{ |
| 43 | + "The entries in the 'column' array must be strings."}; |
| 44 | + } |
| 45 | + check_and_add_column(table, columns, lua_tostring(lua_state, -1)); |
| 46 | + lua_pop(lua_state, 1); // table |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table) |
| 51 | +{ |
| 52 | + char const *const method = |
| 53 | + luaX_get_table_string(lua_state, "method", -1, "Index definition"); |
| 54 | + if (!has_index_method(method)) { |
| 55 | + throw std::runtime_error{"Unknown index method '{}'."_format(method)}; |
| 56 | + } |
| 57 | + |
| 58 | + auto &index = table->add_index(method); |
| 59 | + |
| 60 | + std::vector<std::string> columns; |
| 61 | + lua_getfield(lua_state, -2, "column"); |
| 62 | + if (lua_isstring(lua_state, -1)) { |
| 63 | + check_and_add_column(*table, &columns, lua_tostring(lua_state, -1)); |
| 64 | + index.set_columns(columns); |
| 65 | + } else if (lua_istable(lua_state, -1)) { |
| 66 | + check_and_add_columns(*table, &columns, lua_state); |
| 67 | + if (columns.empty()) { |
| 68 | + throw std::runtime_error{ |
| 69 | + "The 'column' field in an index definition can not be an " |
| 70 | + "empty array."}; |
| 71 | + } |
| 72 | + index.set_columns(columns); |
| 73 | + } else if (!lua_isnil(lua_state, -1)) { |
| 74 | + throw std::runtime_error{ |
| 75 | + "The 'column' field in an index definition must contain a " |
| 76 | + "string or an array."}; |
| 77 | + } |
| 78 | + |
| 79 | + std::string const expression = luaX_get_table_string( |
| 80 | + lua_state, "expression", -3, "Index definition", ""); |
| 81 | + |
| 82 | + index.set_expression(expression); |
| 83 | + |
| 84 | + if (expression.empty() == columns.empty()) { |
| 85 | + throw std::runtime_error{"You must set either the 'column' or the " |
| 86 | + "'expression' field in index definition."}; |
| 87 | + } |
| 88 | + |
| 89 | + std::vector<std::string> include_columns; |
| 90 | + lua_getfield(lua_state, -4, "include"); |
| 91 | + if (get_database_version() >= 110000) { |
| 92 | + if (lua_isstring(lua_state, -1)) { |
| 93 | + check_and_add_column(*table, &include_columns, |
| 94 | + lua_tostring(lua_state, -1)); |
| 95 | + } else if (lua_istable(lua_state, -1)) { |
| 96 | + check_and_add_columns(*table, &include_columns, lua_state); |
| 97 | + } else if (!lua_isnil(lua_state, -1)) { |
| 98 | + throw std::runtime_error{ |
| 99 | + "The 'include' field in an index definition must contain a " |
| 100 | + "string or an array."}; |
| 101 | + } |
| 102 | + index.set_include_columns(include_columns); |
| 103 | + } else if (!lua_isnil(lua_state, -1)) { |
| 104 | + throw std::runtime_error{ |
| 105 | + "Database version ({}) doesn't support" |
| 106 | + " include columns in indexes."_format(get_database_version())}; |
| 107 | + } |
| 108 | + |
| 109 | + std::string const tablespace = luaX_get_table_string( |
| 110 | + lua_state, "tablespace", -5, "Index definition", ""); |
| 111 | + check_identifier(tablespace, "tablespace"); |
| 112 | + if (!has_tablespace(tablespace)) { |
| 113 | + throw std::runtime_error{"Unknown tablespace '{}'."_format(tablespace)}; |
| 114 | + } |
| 115 | + index.set_tablespace(tablespace.empty() ? table->index_tablespace() |
| 116 | + : tablespace); |
| 117 | + |
| 118 | + index.set_is_unique(luaX_get_table_bool(lua_state, "unique", -6, |
| 119 | + "Index definition", false)); |
| 120 | + |
| 121 | + index.set_where_condition( |
| 122 | + luaX_get_table_string(lua_state, "where", -7, "Index definition", "")); |
| 123 | + |
| 124 | + // stack has: "where", "unique", "tablespace", "includes", "expression", |
| 125 | + // "column", "method" |
| 126 | + lua_pop(lua_state, 7); |
| 127 | +} |
0 commit comments