Skip to content

Commit

Permalink
refactor: :prepare notice in kong.lua
Browse files Browse the repository at this point in the history
:prepare might disappear in the future.
  • Loading branch information
thibaultcha committed Apr 19, 2015
1 parent adc5874 commit 2e9c28f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
55 changes: 29 additions & 26 deletions kong/dao/cassandra/factory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,39 @@ function CassandraFactory:drop()
]]
end

-- Prepare all statements in collection._queries and put them in statements cache
-- Should be called with only a collection and will recursively call itself for nested statements.
-- @param collection A collection with a ._queries property
local function prepare_collection(collection, collection_queries)
if not collection_queries then collection_queries = collection._queries end

for stmt_name, collection_query in pairs(collection_queries) do
if type(collection_query) == "table" and collection_query.query == nil then
-- Nested queries, let's recurse to prepare them too
prepare_collection(collection, collection_query)
else
-- _queries can contain strings or tables with string + keys of parameters to bind
local query_to_prepare
if type(collection_query) == "string" then
query_to_prepare = collection_query
elseif collection_query.query then
query_to_prepare = collection_query.query
end

local _, err = collection:prepare_kong_statement(query_to_prepare, collection_query.params)
if err then
error(err)
-- Prepare all statements of collections `._queries` property and put them
-- in a statements cache
--
-- Note:
-- Even if the BaseDAO's :_execute() method support preparation of statements on-the-go,
-- this method should be called when Kong starts in order to detect any failure in advance
-- as well as test the connection to Cassandra.
--
-- @return error if any
function CassandraFactory:prepare()
local function prepare_collection(collection, collection_queries)
if not collection_queries then collection_queries = collection._queries end
for stmt_name, collection_query in pairs(collection_queries) do
if type(collection_query) == "table" and collection_query.query == nil then
-- Nested queries, let's recurse to prepare them too
prepare_collection(collection, collection_query)
else
-- _queries can contain strings or tables with string + keys of parameters to bind
local query_to_prepare
if type(collection_query) == "string" then
query_to_prepare = collection_query
elseif collection_query.query then
query_to_prepare = collection_query.query
end

local _, err = collection:prepare_kong_statement(query_to_prepare, collection_query.params)
if err then
error(err)
end
end
end
end
end

-- Prepare all statements of collections
-- @return error if any
function CassandraFactory:prepare()
for _, collection in ipairs({ self.apis,
self.consumers,
self.plugins_configurations,
Expand Down
3 changes: 2 additions & 1 deletion kong/kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ function _M.init()
-- Loading configuration
configuration, dao = IO.load_configuration_and_dao(os.getenv("KONG_CONF"))

-- Initializing DAO
-- Prepare all collections' statements. Even if optional, this call is useful to check
-- all statements are valid in advance.
local err = dao:prepare()
if err then
error(err)
Expand Down

0 comments on commit 2e9c28f

Please sign in to comment.