Skip to content

Commit e862650

Browse files
committed
Refactor flex_lua_setup_index() code
To avoid having to count numbers of values on the Lua stack.
1 parent ff5369d commit e862650

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

src/flex-lua-index.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,18 @@ static void check_and_add_columns(flex_table_t const &table,
4949

5050
void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
5151
{
52+
// get method
5253
char const *const method =
5354
luaX_get_table_string(lua_state, "method", -1, "Index definition");
5455
if (!has_index_method(method)) {
5556
throw std::runtime_error{"Unknown index method '{}'."_format(method)};
5657
}
57-
58+
lua_pop(lua_state, 1);
5859
auto &index = table->add_index(method);
5960

61+
// get columns
6062
std::vector<std::string> columns;
61-
lua_getfield(lua_state, -2, "column");
63+
lua_getfield(lua_state, -1, "column");
6264
if (lua_isstring(lua_state, -1)) {
6365
check_and_add_column(*table, &columns, lua_tostring(lua_state, -1));
6466
index.set_columns(columns);
@@ -75,19 +77,21 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
7577
"The 'column' field in an index definition must contain a "
7678
"string or an array."};
7779
}
80+
lua_pop(lua_state, 1);
7881

82+
// get expression
7983
std::string const expression = luaX_get_table_string(
80-
lua_state, "expression", -3, "Index definition", "");
81-
82-
index.set_expression(expression);
83-
84+
lua_state, "expression", -1, "Index definition", "");
85+
lua_pop(lua_state, 1);
8486
if (expression.empty() == columns.empty()) {
8587
throw std::runtime_error{"You must set either the 'column' or the "
8688
"'expression' field in index definition."};
8789
}
90+
index.set_expression(expression);
8891

92+
// get include columns
8993
std::vector<std::string> include_columns;
90-
lua_getfield(lua_state, -4, "include");
94+
lua_getfield(lua_state, -1, "include");
9195
if (get_database_version() >= 110000) {
9296
if (lua_isstring(lua_state, -1)) {
9397
check_and_add_column(*table, &include_columns,
@@ -105,23 +109,26 @@ void flex_lua_setup_index(lua_State *lua_state, flex_table_t *table)
105109
"Database version ({}) doesn't support"
106110
" include columns in indexes."_format(get_database_version())};
107111
}
112+
lua_pop(lua_state, 1);
108113

114+
// get tablespace
109115
std::string const tablespace = luaX_get_table_string(
110-
lua_state, "tablespace", -5, "Index definition", "");
116+
lua_state, "tablespace", -1, "Index definition", "");
117+
lua_pop(lua_state, 1);
111118
check_identifier(tablespace, "tablespace");
112119
if (!has_tablespace(tablespace)) {
113120
throw std::runtime_error{"Unknown tablespace '{}'."_format(tablespace)};
114121
}
115122
index.set_tablespace(tablespace.empty() ? table->index_tablespace()
116123
: tablespace);
117124

118-
index.set_is_unique(luaX_get_table_bool(lua_state, "unique", -6,
125+
// get unique
126+
index.set_is_unique(luaX_get_table_bool(lua_state, "unique", -1,
119127
"Index definition", false));
128+
lua_pop(lua_state, 1);
120129

130+
// get where condition
121131
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);
132+
luaX_get_table_string(lua_state, "where", -1, "Index definition", ""));
133+
lua_pop(lua_state, 1);
127134
}

0 commit comments

Comments
 (0)