LuaDataSource.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "LuaDataSource.h"
  29. #include <RmlUi/Lua/Interpreter.h>
  30. #include <RmlUi/Core/Log.h>
  31. namespace Rml {
  32. namespace Lua {
  33. LuaDataSource::LuaDataSource(const String& name) : DataSource(name), getRowRef(LUA_NOREF), getNumRowsRef(LUA_NOREF)
  34. {}
  35. /// Fetches the contents of one row of a table within the data source.
  36. /// @param[out] row The list of values in the table.
  37. /// @param[in] table The name of the table to query.
  38. /// @param[in] row_index The index of the desired row.
  39. /// @param[in] columns The list of desired columns within the row.
  40. void LuaDataSource::GetRow(StringList& row, const String& table, int row_index, const StringList& columns)
  41. {
  42. if(getRowRef == LUA_NOREF || getRowRef == LUA_REFNIL) return;
  43. //setup the call
  44. Interpreter::BeginCall(getRowRef);
  45. lua_State* L = Interpreter::GetLuaState();
  46. lua_pushstring(L,table.c_str());
  47. lua_pushinteger(L,row_index);
  48. lua_newtable(L);
  49. int index = 0;
  50. for(StringList::const_iterator itr = columns.begin(); itr != columns.end(); ++itr)
  51. {
  52. lua_pushstring(L,itr->c_str());
  53. lua_rawseti(L,-2,index++);
  54. }
  55. Interpreter::ExecuteCall(3,1); //3 parameters, 1 return. After here, the top of the stack contains the return value
  56. int res = lua_gettop(L);
  57. if(lua_type(L,res) == LUA_TTABLE)
  58. {
  59. lua_pushnil(L);
  60. while(lua_next(L,res) != 0)
  61. {
  62. //key at -2, value at -1
  63. row.push_back(luaL_checkstring(L,-1));
  64. lua_pop(L,1); //pops value, leaves key for next iteration
  65. }
  66. lua_pop(L,1); //pop key
  67. }
  68. else
  69. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetRow must return a table, the function it called returned a %s", lua_typename(L,res));
  70. Interpreter::EndCall(1);
  71. }
  72. /// Fetches the number of rows within one of this data source's tables.
  73. /// @param[in] table The name of the table to query.
  74. /// @return The number of rows within the specified table. Returns -1 in case of an incorrect Lua function.
  75. int LuaDataSource::GetNumRows(const String& table)
  76. {
  77. if(getNumRowsRef == LUA_NOREF || getNumRowsRef == LUA_REFNIL) return -1;
  78. lua_State* L = Interpreter::GetLuaState();
  79. Interpreter::BeginCall(getNumRowsRef);
  80. lua_pushstring(L,table.c_str());
  81. Interpreter::ExecuteCall(1,1); //1 parameter, 1 return. After this, the top of the stack contains the return value
  82. int res = lua_gettop(L);
  83. if(lua_type(L,res) == LUA_TNUMBER)
  84. {
  85. return (int)luaL_checkinteger(L,res);
  86. }
  87. else
  88. {
  89. Log::Message(Log::LT_WARNING, "Lua: DataSource.GetNumRows must return an integer, the function it called returned a %s", lua_typename(L,res));
  90. return -1;
  91. }
  92. }
  93. } // namespace Lua
  94. } // namespace Rml