Comparison

util/sql.lua @ 6730:7889515bac86

util.sql: Remove unused functions and unused commented code
author Matthew Wild <mwild1@gmail.com>
date Sat, 30 May 2015 22:23:19 +0100
parent 6532:a966efeb6cb3
child 6733:36e2b35397b1
comparison
equal deleted inserted replaced
6720:936cf2f7531f 6730:7889515bac86
23 23
24 function is_column(x) return getmetatable(x)==column_mt; end 24 function is_column(x) return getmetatable(x)==column_mt; end
25 function is_index(x) return getmetatable(x)==index_mt; end 25 function is_index(x) return getmetatable(x)==index_mt; end
26 function is_table(x) return getmetatable(x)==table_mt; end 26 function is_table(x) return getmetatable(x)==table_mt; end
27 function is_query(x) return getmetatable(x)==query_mt; end 27 function is_query(x) return getmetatable(x)==query_mt; end
28 --function is_op(x) return getmetatable(x)==op_mt; end
29 --function expr(...) return setmetatable({...}, op_mt); end
30 function Integer(n) return "Integer()" end 28 function Integer(n) return "Integer()" end
31 function String(n) return "String()" end 29 function String(n) return "String()" end
32
33 --[[local ops = {
34 __add = function(a, b) return "("..a.."+"..b..")" end;
35 __sub = function(a, b) return "("..a.."-"..b..")" end;
36 __mul = function(a, b) return "("..a.."*"..b..")" end;
37 __div = function(a, b) return "("..a.."/"..b..")" end;
38 __mod = function(a, b) return "("..a.."%"..b..")" end;
39 __pow = function(a, b) return "POW("..a..","..b..")" end;
40 __unm = function(a) return "NOT("..a..")" end;
41 __len = function(a) return "COUNT("..a..")" end;
42 __eq = function(a, b) return "("..a.."=="..b..")" end;
43 __lt = function(a, b) return "("..a.."<"..b..")" end;
44 __le = function(a, b) return "("..a.."<="..b..")" end;
45 };
46
47 local functions = {
48
49 };
50
51 local cmap = {
52 [Integer] = Integer();
53 [String] = String();
54 };]]
55 30
56 function Column(definition) 31 function Column(definition)
57 return setmetatable(definition, column_mt); 32 return setmetatable(definition, column_mt);
58 end 33 end
59 function Table(definition) 34 function Table(definition)
92 local s = 'Index{ name="'..self.name..'"'; 67 local s = 'Index{ name="'..self.name..'"';
93 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end 68 for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end
94 return s..' }'; 69 return s..' }';
95 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }' 70 -- return 'Index{ name="'..self.name..'", type="'..self.type..'" }'
96 end 71 end
97 --
98 72
99 local function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return s_char(tonumber(c,16)); end)); end 73 local function urldecode(s) return s and (s:gsub("%%(%x%x)", function (c) return s_char(tonumber(c,16)); end)); end
100 local function parse_url(url) 74 local function parse_url(url)
101 local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)"); 75 local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)");
102 assert(scheme, "Invalid URL format"); 76 assert(scheme, "Invalid URL format");
118 username = username; password = password; 92 username = username; password = password;
119 host = host; port = port; 93 host = host; port = port;
120 database = #database > 0 and database or nil; 94 database = #database > 0 and database or nil;
121 }; 95 };
122 end 96 end
123
124 --[[local session = {};
125
126 function session.query(...)
127 local rets = {...};
128 local query = setmetatable({ __rets = rets, __filters }, query_mt);
129 return query;
130 end
131 --
132
133 local function db2uri(params)
134 return build_url{
135 scheme = params.driver,
136 user = params.username,
137 password = params.password,
138 host = params.host,
139 port = params.port,
140 path = params.database,
141 };
142 end]]
143 97
144 local engine = {}; 98 local engine = {};
145 function engine:connect() 99 function engine:connect()
146 if self.conn then return true; end 100 if self.conn then return true; end
147 101
337 engine_cache[url] = engine; 291 engine_cache[url] = engine;
338 end 292 end
339 return engine_cache[url]; 293 return engine_cache[url];
340 end 294 end
341 295
342
343 --[[Users = Table {
344 name="users";
345 Column { name="user_id", type=String(), primary_key=true };
346 };
347 print(Users)
348 print(Users.c.user_id)]]
349
350 --local engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase');
351 --[[local engine = create_engine{ driver = "SQLite3", database = "./alchemy.sqlite" };
352
353 local i = 0;
354 for row in assert(engine:execute("select * from sqlite_master")):rows(true) do
355 i = i+1;
356 print(i);
357 for k,v in pairs(row) do
358 print("",k,v);
359 end
360 end
361 print("---")
362
363 Prosody = Table {
364 name="prosody";
365 Column { name="host", type="TEXT", nullable=false };
366 Column { name="user", type="TEXT", nullable=false };
367 Column { name="store", type="TEXT", nullable=false };
368 Column { name="key", type="TEXT", nullable=false };
369 Column { name="type", type="TEXT", nullable=false };
370 Column { name="value", type="TEXT", nullable=false };
371 Index { name="prosody_index", "host", "user", "store", "key" };
372 };
373 --print(Prosody);
374 assert(engine:transaction(function()
375 assert(Prosody:create(engine));
376 end));
377
378 for row in assert(engine:execute("select user from prosody")):rows(true) do
379 print("username:", row['username'])
380 end
381 --result.close();]]
382
383 return _M; 296 return _M;