ZNZNCOOP

mysql_mon.lua

Mar 17th, 2016
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- MySQL-монитор
  2. local mysql=require("mysql")
  3. local session=mysql.connect()
  4. if not session then print(mysql.error()) return end
  5. print("Welcome to the MySQL monitor.")
  6. print("Your MySQL connection id is "..session.id)
  7. do
  8.   local res=mysql.query("select version()", session)
  9.   local ver
  10.   if res then
  11.     ver=mysql.fetch_row(res)[1]
  12.   else
  13.     ver="unknown"
  14.   end
  15.   print("Server version: "..ver)
  16. end
  17. local len=require("unicode").len
  18. local uptime=require("computer").uptime
  19. local inv1="mysql> "
  20. local inv2="    -> "
  21. local inv=inv1
  22. local buff,query=""
  23.  
  24. function draw(width,n)
  25.   n=n or 2
  26.   local b={"╔","╠","╚"}
  27.   local m={"╤","╪","╧"}
  28.   local e={"╗","╣","╝"}
  29.   local c=b[n]
  30.   for i=1,#width do io.write(c..string.rep("═",width[i]+2)) c=m[n] end
  31.   print(e[n])
  32. end
  33.  
  34. function printline(width,dat)
  35.   if type(dat)=="table" then
  36.     local c="║"
  37.     for i=1,#width do
  38.       io.write(c.." "..tostring(dat[i])..string.rep(" ",width[i]-len(tostring(dat[i]))+1))
  39.       c="│"
  40.     end
  41.   else
  42.     io.write("║ "..tostring(dat)..string.rep(" ",width[1]-len(tostring(dat))+1))
  43.   end
  44.   print("║")
  45. end
  46.  
  47. while true do
  48.   io.write(inv)
  49.   buff=buff..io.read().." "
  50.   if buff=="exit " then mysql.close(session) break end
  51.   while buff:find(";") do
  52.     query,buff=buff:match("(.-);(.-)%s*$")
  53.     local del=uptime()
  54.     local res=mysql.query(query, session)
  55.     del=uptime()-del
  56.     if res then
  57.       if type(res)=="boolean" then print(string.format("%s (%1.2f sec)",mysql.error(session), del)) end
  58.       if type(res)=="table" then
  59.         local width={}
  60.         if type(res.name)=="table" then
  61.          for i=1,#res.name do width[i]=len(res.name[i]) or 0 end
  62.          for row in mysql.fetch_rows(res) do
  63.           for i=1,#res.name do
  64.             local l=len(tostring(row[i])) or 0
  65.             if width[i]<l then width[i]=l end
  66.           end
  67.          end
  68.         else
  69.           width[1]=len(res.name) or 0
  70.           for row in mysql.fetch_rows(res) do
  71.             local l=len(tostring(row)) or 0
  72.             if width[1]<l then width[1]=l end
  73.           end
  74.         end
  75.         draw(width,1)
  76.         printline(width,res.name)
  77.         draw(width,2)
  78.         for row in mysql.fetch_rows(res) do printline(width,row) end
  79.         draw(width,3)
  80.         print(string.format("Получено строк %d, (%1.2f sec)",mysql.num_rows(res),del))
  81.       end
  82.     else
  83.       print("ОШИБКА:"..mysql.error(session))
  84.     end
  85.   end
  86.   if buff=="" then inv=inv1 else inv=inv2 end
  87. end
RAW Paste Data