daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Jun 5th, 2017 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require("pgsql")
  2.  
  3. conn = pgsql.connect("hostaddr='10.6.6.1' port='5432' dbname='gmtest' user='gmtest' password='z0rteh' sslmode='disable'")
  4.  
  5. function conn:onInitialConnect()
  6.     print("Initial connection made")
  7. end
  8.  
  9. function conn:onConnect()
  10.     print("Connection made")
  11. end
  12.  
  13. function conn:onError(query, err)
  14.     ErrorNoHalt("pqtest: error in query '" .. query .. "';\n   " .. err:gsub("\n", "\n   "):sub(1, -4))
  15. end
  16.  
  17. concommand.Add("pgsql_reset", function()
  18.     if IsValid(ply) then return end
  19.     conn:reset()
  20. end )
  21.  
  22. concommand.Add("pgsql_query", function(ply, cmd, arg)
  23.     if IsValid(ply) then return end
  24.    
  25.     conn:query(arg[1], function(...)
  26.         for k, result in ipairs({...}) do
  27.             if result:success() then           
  28.                 print(unpack(result.columnNames))
  29.                
  30.                 for row in result do
  31.                     print(unpack(row))
  32.                 end
  33.             end
  34.         end
  35.     end)
  36. end )
  37.  
  38. concommand.Add("pgsql_test", function()
  39.     conn:query("SELECT generate_series(1, 2000) as \"Test a!\"", function(res)
  40.         if not res:success() then return print("UH OH") end    
  41.        
  42.         local sum = 0
  43.        
  44.         for row in res do
  45.             sum = sum + row["Test a!"]
  46.         end
  47.        
  48.         print(sum)
  49.     end)
  50. end )
  51.  
  52. concommand.Add("pgsql_multitest", function()
  53.     for i = 1, 100 do
  54.         conn:query(Format("SELECT %i as A", i), function(res)
  55.             if not res:success() then
  56.                 print(res.error)
  57.                 return
  58.             end
  59.            
  60.             print(i, tonumber(res:row(1)[1]) == i )
  61.         end)
  62.     end
  63. end )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top