LuaDataSource.cpp 4.0 KB

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