Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MySQL-монитор
- local mysql=require("mysql")
- local session=mysql.connect()
- if not session then print(mysql.error()) return end
- print("Welcome to the MySQL monitor.")
- print("Your MySQL connection id is "..session.id)
- do
- local res=mysql.query("select version()", session)
- local ver
- if res then
- ver=mysql.fetch_row(res)[1]
- else
- ver="unknown"
- end
- print("Server version: "..ver)
- end
- local len=require("unicode").len
- local uptime=require("computer").uptime
- local inv1="mysql> "
- local inv2=" -> "
- local inv=inv1
- local buff,query=""
- function draw(width,n)
- n=n or 2
- local b={"╔","╠","╚"}
- local m={"╤","╪","╧"}
- local e={"╗","╣","╝"}
- local c=b[n]
- for i=1,#width do io.write(c..string.rep("═",width[i]+2)) c=m[n] end
- print(e[n])
- end
- function printline(width,dat)
- if type(dat)=="table" then
- local c="║"
- for i=1,#width do
- io.write(c.." "..tostring(dat[i])..string.rep(" ",width[i]-len(tostring(dat[i]))+1))
- c="│"
- end
- else
- io.write("║ "..tostring(dat)..string.rep(" ",width[1]-len(tostring(dat))+1))
- end
- print("║")
- end
- while true do
- io.write(inv)
- buff=buff..io.read().." "
- if buff=="exit " then mysql.close(session) break end
- while buff:find(";") do
- query,buff=buff:match("(.-);(.-)%s*$")
- local del=uptime()
- local res=mysql.query(query, session)
- del=uptime()-del
- if res then
- if type(res)=="boolean" then print(string.format("%s (%1.2f sec)",mysql.error(session), del)) end
- if type(res)=="table" then
- local width={}
- if type(res.name)=="table" then
- for i=1,#res.name do width[i]=len(res.name[i]) or 0 end
- for row in mysql.fetch_rows(res) do
- for i=1,#res.name do
- local l=len(tostring(row[i])) or 0
- if width[i]<l then width[i]=l end
- end
- end
- else
- width[1]=len(res.name) or 0
- for row in mysql.fetch_rows(res) do
- local l=len(tostring(row)) or 0
- if width[1]<l then width[1]=l end
- end
- end
- draw(width,1)
- printline(width,res.name)
- draw(width,2)
- for row in mysql.fetch_rows(res) do printline(width,row) end
- draw(width,3)
- print(string.format("Получено строк %d, (%1.2f sec)",mysql.num_rows(res),del))
- end
- else
- print("ОШИБКА:"..mysql.error(session))
- end
- end
- if buff=="" then inv=inv1 else inv=inv2 end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement