diff options
author | Christian Pointner <equinox@helsinki.at> | 2011-04-04 15:45:55 (GMT) |
---|---|---|
committer | Christian Pointner <equinox@helsinki.at> | 2011-04-04 15:45:55 (GMT) |
commit | 45519cb170245b0e9c1d358d23aad7cff1c7be01 (patch) | |
tree | a10e4235648acbe3b9f8a4f9bdc30ff7b498256d /rhnop-client/db.lua | |
parent | 7567ead1174c33d95b4d164b1b148bec89ce005b (diff) |
nopcollectd works now
Diffstat (limited to 'rhnop-client/db.lua')
-rw-r--r-- | rhnop-client/db.lua | 152 |
1 files changed, 78 insertions, 74 deletions
diff --git a/rhnop-client/db.lua b/rhnop-client/db.lua index ce3b15f..5a6a92c 100644 --- a/rhnop-client/db.lua +++ b/rhnop-client/db.lua @@ -21,103 +21,107 @@ require "luasql.mysql" --- CREATE DATABASE rhnop --- GRANT select,insert,update ON rhnop.* TO 'nopsyncd' IDENTIFIED BY '<password>'; --- GRANT select ON rhnop.* TO 'nopcollectd' IDENTIFIED BY '<password>'; --- USE rhnop --- CREATE TABLE IF NOT EXISTS now (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); +-- for destination database +-- CREATE DATABASE nop +-- GRANT select,insert,update,delete ON nop.* TO 'nopcollectd' IDENTIFIED BY '<password>'; +-- GRANT select ON nop.* TO 'nop' IDENTIFIED BY '<password>'; +-- USE nop +-- CREATE TABLE IF NOT EXISTS master (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); +-- CREATE TABLE IF NOT EXISTS standby (timestamp BIGINT UNSIGNED PRIMARY KEY NOT NULL, cart INT NOT NULL, len INT, showtitle VARCHAR(255), title VARCHAR(255), artist VARCHAR(255), album VARCHAR(255), ismusic BOOLEAN); local db = {} -function db:init(db, user, pwd, host, port, table) - self.table = table +function db.init(db, user, pwd, host, port, table) + local mydb = {} + + mydb.table = table local err - self.env, err = luasql.mysql() - if self.env == nil then + mydb.env, err = luasql.mysql() + if mydb.env == nil then return nil, err end - self.con, err = self.env:connect(db, user, pwd, host, port) - if self.con == nil then + mydb.con, err = mydb.env:connect(db, user, pwd, host, port) + if mydb.con == nil then return nil, err end - local ret, err = self.con:setautocommit(true) + local ret, err = mydb.con:setautocommit(true) if ret == nil then return nil, err end - return true -end - -function db:getLastEntry() - local cur, err = self.con:execute("SELECT MAX(timestamp) FROM " .. self.table) - if cur == nil then - return nil, err + function mydb:getLastEntry() + local cur, err = self.con:execute("SELECT MAX(timestamp) FROM " .. self.table) + if cur == nil then + return nil, err + end + + local timestamp = cur:fetch() + if timestamp == nil then timestamp = 0 end + return timestamp end - - local timestamp = cur:fetch() - if timestamp == nil then timestamp = 0 end - return timestamp -end - -function db:findMissingEntries(lasttimestamp) - local lastts = self.con:escape(lasttimestamp) - return = self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp > '" .. lastts .. "')") -end - -function db:getNextMissingEntry(cur) - local data = {} - data = cur:fetch(data, "a") - if data == nil then - return nil, "that's all folks" + + function mydb:findMissingEntries(lasttimestamp) + local lastts = self.con:escape(lasttimestamp) + return self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp > " .. lastts) end - return data -end - -function db:getEntry(timestamp) - local ts = self.con:escape(timestamp) - local cur, err = self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp = '" .. ts .. "')") - if cur == nil then - return nil, err + + function mydb:getNextMissingEntry(cur) + local data = {} + data = cur:fetch(data, "a") + if data == nil then + return nil, "that's all folks" + end + return data end - - local data = {} - data = cur:fetch(data, "a") - if data == nil then - return nil, "nothing found" + + function mydb:getEntry(timestamp) + local ts = self.con:escape(timestamp) + local cur, err = self.con:execute("SELECT * FROM " .. self.table .. " WHERE timestamp = " .. ts) + if cur == nil then + return nil, err + end + + local data = {} + data = cur:fetch(data, "a") + if data == nil then + return nil, "nothing found" + end + return data end - return data -end - -function db:addEntry(data) - local timestamp = self.con:escape(data.timestamp) - local cart = self.con:escape(data.cart) - local len = self.con:escape(data.len) - local showtitle = self.con:escape(data.showtitle) - local title = self.con:escape(data.title) - local artist = self.con:escape(data.artist) - local album = self.con:escape(data.album) - local ismusic = self.con:escape(data.ismusic) - - local cur, err = self.con:execute("REPLACE INTO " .. self.table .. " VALUES('" .. timestamp .. "', '" .. cart .. "', '" .. len .. "', '" .. showtitle .. "', '" .. title .. "', '" .. artist .."', '" .. album .. "', '" .. ismusic "')") - if cur == nil then - return nil, err + + function mydb:addEntry(data) + local timestamp = self.con:escape(data.timestamp) + local cart = self.con:escape(data.cart) + local len = self.con:escape(data.len) + local showtitle = self.con:escape(data.showtitle) + local title = self.con:escape(data.title) + local artist = self.con:escape(data.artist) + local album = self.con:escape(data.album) + local ismusic = self.con:escape(data.ismusic) + + local cur, err = self.con:execute("REPLACE INTO " .. self.table .. " VALUES('" .. timestamp .. "', '" .. cart .. "', '" .. len .. "', '" .. showtitle .. "', '" .. title .. "', '" .. artist .."', '" .. album .. "', '" .. ismusic .. "')") + if cur == nil then + return nil, err + end + + return true end - - return true -end - -function db:close() - if self.con then - self.con:close() + + function mydb:close() + if self.con then + self.con:close() + end + + if self.env then + self.env:close() + end end - if self.env then - self.env:close() - end + return mydb end return db |