Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Statements = {}
- function Statements:_add(name, stmtarr)
- if self.values[name] then
- return "Statement '", name, "' already exists."
- end
- local stmt = table.concat(stmtarr, "\n")
- self.values[name] = stmt
- end
- function Statements:_assert(lineN, ...)
- if ... then
- error("Error parsing "..self.curFile..". Line #"..lineN.. ". "..
- table.concat({...}, ""))
- else return lineN, ... end
- end
- function Statements:_addFile(file)
- self.curFile = file
- local name, stmtarr = nil, {}
- local first = true
- local lineN = 0
- for line in io.lines(file) do
- lineN = lineN + 1
- if first and line:match("^%s*$") then goto continue end
- local commentName = line:match("^--!%s*(.+)$")
- if commentName then
- if first then first = false
- else self:_assert(lineN, self:_add(name, stmtarr)) end
- name, stmtarr = commentName, {}
- else
- if first then self:_assert(lineN, "Meets unnamed SQL statement") end
- table.insert(stmtarr, line)
- end
- ::continue::
- end
- self:_assert(lineN, self:_add(name, stmtarr))
- end
- function Statements:new(...)
- local o = { values = {} }
- setmetatable(o, self)
- self.__index = self
- if ... then o:addFiles(...) end
- return o
- end
- function Statements:addFiles(...)
- local files = {...}
- if #files == 0 then error('No files to load') end
- for _,f in ipairs(files) do self:_addFile(f) end
- end
- return Statements
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement