Using LuaCOM to access ADO for Hit-O-Meter function

You can share your Lua Scripts with everybody here.

Using LuaCOM to access ADO for Hit-O-Meter function

Postby FTP » Fri Oct 30, 2009 5:38 pm

-- Description: insert ftp download logs into Microsoft Access database, you can use it for Hit-O-Meter function.
-- Author: Luke
-- Date: 2009-10-29

Code: Select all
do

require("luacom")
local dblua_data = {}

function DBOpen(connection_string)
  dblua_data.connection = luacom.CreateObject("ADODB.Connection")
  assert(dblua_data.connection)
  dblua_data.connection.ConnectionString = connection_string
  dblua_data.connection:Open()
end

function DBClose()
  dblua_data.connection:Close()
  dblua_data.connection = nil
  dblua_data.recordset = nil
end

function DBExec(statement)
  if dblua_data.recordset == nil then
    dblua_data.recordset = luacom.CreateObject("ADODB.RecordSet")
  elseif dblua_data.recordset.State ~= 0 then
    dblua_data.recordset:Close()
  end
  dblua_data.recordset:Open(statement, dblua_data.connection)
end

--create a mdb database c:/wing_filelog.mdb
local source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/wing_filelog.mdb"
if c_FileExist("c:/wing_filelog.mdb") == false then
  --create mdb database if no existing
  cat = luacom.CreateObject("ADOX.Catalog")
  cat:Create(source)
  DBOpen(source)
  DBExec("create table downloadlog ( username varchar(64), filepath varchar(255), filesize varchar(64), downloadtime varchar(64) )")
  DBExec("create table uploadlog ( username varchar(64), filepath varchar(255), filesize varchar(64), uploadtime varchar(64) )")
else
  DBOpen(source)
end
--insert record
DBExec("insert into downloadlog values ('%Name', '%PathName', '%FileSize', '"..os.date("%Y-%m-%d %H:%M:%S").."')")
DBClose()

end


You should add the above lua script to the "OnFileDownloaded" event for FTP(HTTP,SSH).

Image

if you want to view download hits statistics, you may execute a simple SQL like this:
"select filepath,count(*) from downloadlog group by filepath"



-- Description: insert ftp upload logs into Microsoft Access database.
-- Author: Luke
-- Date: 2009-10-29

Code: Select all
do

require("luacom")
local dblua_data = {}

function DBOpen(connection_string)
  dblua_data.connection = luacom.CreateObject("ADODB.Connection")
  assert(dblua_data.connection)
  dblua_data.connection.ConnectionString = connection_string
  dblua_data.connection:Open()
end

function DBClose()
  dblua_data.connection:Close()
  dblua_data.connection = nil
  dblua_data.recordset = nil
end

function DBExec(statement)
  if dblua_data.recordset == nil then
    dblua_data.recordset = luacom.CreateObject("ADODB.RecordSet")
  elseif dblua_data.recordset.State ~= 0 then
    dblua_data.recordset:Close()
  end
  dblua_data.recordset:Open(statement, dblua_data.connection)
end

--create or use a mdb database c:/wing_filelog.mdb
local source = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/wing_filelog.mdb"
if c_FileExist("c:/wing_filelog.mdb") == false then
  --create mdb database
  cat = luacom.CreateObject("ADOX.Catalog")
  cat:Create(source)
  DBOpen(source)
  DBExec("create table downloadlog ( username varchar(64), filepath varchar(255), filesize varchar(64), downloadtime varchar(64) )")
  DBExec("create table uploadlog ( username varchar(64), filepath varchar(255), filesize varchar(64), uploadtime varchar(64) )")
else
  DBOpen(source)
end
--insert record
DBExec("insert into uploadlog values ('%Name', '%PathName', '%FileSize', '"..os.date("%Y-%m-%d %H:%M:%S").."')")
DBClose()

end


You should add the above lua script to the "OnFileUploaded" event for FTP(HTTP,SSH).

Image


Here we used a LuaCOM lib, you must download it then unzip it into the working directory of Wing FTP Server, the download URL: http://www.wftpserver.com/bbsres/libcom.zip
FTP
Site Admin
 
posts 1242
 
joined Tue Sep 29, 2009 6:09 am

Return to Lua scripts

Who is online

Users browsing this forum: No registered users and 1 guest