4e63bed76dff477748a936c389a4a1a60e318111
[prosody.git] / util / sql.lua
1
2 local setmetatable, getmetatable = setmetatable, getmetatable;
3 local ipairs, unpack, select = ipairs, unpack, select;
4 local tonumber, tostring = tonumber, tostring;
5 local assert, xpcall, debug_traceback = assert, xpcall, debug.traceback;
6 local t_concat = table.concat;
7 local s_char = string.char;
8 local log = require "util.logger".init("sql");
9
10 local DBI = require "DBI";
11 -- This loads all available drivers while globals are unlocked
12 -- LuaDBI should be fixed to not set globals.
13 DBI.Drivers();
14 local build_url = require "socket.url".build;
15
16 module("sql")
17
18 local column_mt = {};
19 local table_mt = {};
20 local query_mt = {};
21 --local op_mt = {};
22 local index_mt = {};
23
24 function is_column(x) return getmetatable(x)==column_mt; end
25 function is_index(x) return getmetatable(x)==index_mt; end
26 function is_table(x) return getmetatable(x)==table_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
31 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
56 function Column(definition)
57         return setmetatable(definition, column_mt);
58 end
59 function Table(definition)
60         local c = {}
61         for i,col in ipairs(definition) do
62                 if is_column(col) then
63                         c[i], c[col.name] = col, col;
64                 elseif is_index(col) then
65                         col.table = definition.name;
66                 end
67         end
68         return setmetatable({ __table__ = definition, c = c, name = definition.name }, table_mt);
69 end
70 function Index(definition)
71         return setmetatable(definition, index_mt);
72 end
73
74 function table_mt:__tostring()
75         local s = { 'name="'..self.__table__.name..'"' }
76         for i,col in ipairs(self.__table__) do
77                 s[#s+1] = tostring(col);
78         end
79         return 'Table{ '..t_concat(s, ", ")..' }'
80 end
81 table_mt.__index = {};
82 function table_mt.__index:create(engine)
83         return engine:_create_table(self);
84 end
85 function table_mt:__call(...)
86         -- TODO
87 end
88 function column_mt:__tostring()
89         return 'Column{ name="'..self.name..'", type="'..self.type..'" }'
90 end
91 function index_mt:__tostring()
92         local s = 'Index{ name="'..self.name..'"';
93         for i=1,#self do s = s..', "'..self[i]:gsub("[\\\"]", "\\%1")..'"'; end
94         return s..' }';
95 --      return 'Index{ name="'..self.name..'", type="'..self.type..'" }'
96 end
97 --
98
99 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)
101         local scheme, secondpart, database = url:match("^([%w%+]+)://([^/]*)/?(.*)");
102         assert(scheme, "Invalid URL format");
103         local username, password, host, port;
104         local authpart, hostpart = secondpart:match("([^@]+)@([^@+])");
105         if not authpart then hostpart = secondpart; end
106         if authpart then
107                 username, password = authpart:match("([^:]*):(.*)");
108                 username = username or authpart;
109                 password = password and urldecode(password);
110         end
111         if hostpart then
112                 host, port = hostpart:match("([^:]*):(.*)");
113                 host = host or hostpart;
114                 port = port and assert(tonumber(port), "Invalid URL format");
115         end
116         return {
117                 scheme = scheme:lower();
118                 username = username; password = password;
119                 host = host; port = port;
120                 database = #database > 0 and database or nil;
121         };
122 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
144 local engine = {};
145 function engine:connect()
146         if self.conn then return true; end
147
148         local params = self.params;
149         assert(params.driver, "no driver")
150         local dbh, err = DBI.Connect(
151                 params.driver, params.database,
152                 params.username, params.password,
153                 params.host, params.port
154         );
155         if not dbh then return nil, err; end
156         dbh:autocommit(false); -- don't commit automatically
157         self.conn = dbh;
158         self.prepared = {};
159         return true;
160 end
161 function engine:execute(sql, ...)
162         local success, err = self:connect();
163         if not success then return success, err; end
164         local prepared = self.prepared;
165
166         local stmt = prepared[sql];
167         if not stmt then
168                 local err;
169                 stmt, err = self.conn:prepare(sql);
170                 if not stmt then return stmt, err; end
171                 prepared[sql] = stmt;
172         end
173
174         local success, err = stmt:execute(...);
175         if not success then return success, err; end
176         return stmt;
177 end
178
179 local result_mt = { __index = {
180         affected = function(self) return self.__stmt:affected(); end;
181         rowcount = function(self) return self.__stmt:rowcount(); end;
182 } };
183
184 function engine:execute_query(sql, ...)
185         if self.params.driver == "PostgreSQL" then
186                 sql = sql:gsub("`", "\"");
187         end
188         local stmt = assert(self.conn:prepare(sql));
189         assert(stmt:execute(...));
190         return stmt:rows();
191 end
192 function engine:execute_update(sql, ...)
193         if self.params.driver == "PostgreSQL" then
194                 sql = sql:gsub("`", "\"");
195         end
196         local prepared = self.prepared;
197         local stmt = prepared[sql];
198         if not stmt then
199                 stmt = assert(self.conn:prepare(sql));
200                 prepared[sql] = stmt;
201         end
202         assert(stmt:execute(...));
203         return setmetatable({ __stmt = stmt }, result_mt);
204 end
205 engine.insert = engine.execute_update;
206 engine.select = engine.execute_query;
207 engine.delete = engine.execute_update;
208 engine.update = engine.execute_update;
209 function engine:_transaction(func, ...)
210         if not self.conn then
211                 local a,b = self:connect();
212                 if not a then return a,b; end
213         end
214         --assert(not self.__transaction, "Recursive transactions not allowed");
215         local args, n_args = {...}, select("#", ...);
216         local function f() return func(unpack(args, 1, n_args)); end
217         self.__transaction = true;
218         local success, a, b, c = xpcall(f, debug_traceback);
219         self.__transaction = nil;
220         if success then
221                 log("debug", "SQL transaction success [%s]", tostring(func));
222                 local ok, err = self.conn:commit();
223                 if not ok then return ok, err; end -- commit failed
224                 return success, a, b, c;
225         else
226                 log("debug", "SQL transaction failure [%s]: %s", tostring(func), a);
227                 if self.conn then self.conn:rollback(); end
228                 return success, a;
229         end
230 end
231 function engine:transaction(...)
232         local a,b = self:_transaction(...);
233         if not a then
234                 local conn = self.conn;
235                 if not conn or not conn:ping() then
236                         self.conn = nil;
237                         a,b = self:_transaction(...);
238                 end
239         end
240         return a,b;
241 end
242 function engine:_create_index(index)
243         local sql = "CREATE INDEX `"..index.name.."` ON `"..index.table.."` (";
244         for i=1,#index do
245                 sql = sql.."`"..index[i].."`";
246                 if i ~= #index then sql = sql..", "; end
247         end
248         sql = sql..");"
249         if self.params.driver == "PostgreSQL" then
250                 sql = sql:gsub("`", "\"");
251         elseif self.params.driver == "MySQL" then
252                 sql = sql:gsub("`([,)])", "`(20)%1");
253         end
254         if index.unique then
255                 sql = sql:gsub("^CREATE", "CREATE UNIQUE");
256         end
257         --print(sql);
258         return self:execute(sql);
259 end
260 function engine:_create_table(table)
261         local sql = "CREATE TABLE `"..table.name.."` (";
262         for i,col in ipairs(table.c) do
263                 sql = sql.."`"..col.name.."` "..col.type;
264                 if col.nullable == false then sql = sql.." NOT NULL"; end
265                 if col.primary_key == true then sql = sql.." PRIMARY KEY"; end
266                 if col.auto_increment == true then
267                         if self.params.driver == "PostgreSQL" then
268                                 sql = sql.." SERIAL";
269                         elseif self.params.driver == "MySQL" then
270                                 sql = sql.." AUTO_INCREMENT";
271                         elseif self.params.driver == "SQLite3" then
272                                 sql = sql.." AUTOINCREMENT";
273                         end
274                 end
275                 if i ~= #table.c then sql = sql..", "; end
276         end
277         sql = sql.. ");"
278         if self.params.driver == "PostgreSQL" then
279                 sql = sql:gsub("`", "\"");
280         elseif self.params.driver == "MySQL" then
281                 sql = sql:gsub(";$", " CHARACTER SET 'utf8' COLLATE 'utf8_bin';");
282         end
283         local success,err = self:execute(sql);
284         if not success then return success,err; end
285         for i,v in ipairs(table.__table__) do
286                 if is_index(v) then
287                         self:_create_index(v);
288                 end
289         end
290         return success;
291 end
292 function engine:set_encoding() -- to UTF-8
293         if self.params.driver == "SQLite3" then return end
294         local driver = self.params.driver;
295         local set_names_query = "SET NAMES '%s';"
296         local charset = "utf8";
297         if driver == "MySQL" then
298                 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
299                 local ok, charsets = self:transaction(function()
300                         return self:select"SELECT `CHARACTER_SET_NAME` FROM `CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;";
301                 end);
302                 local row = ok and charsets();
303                 charset = row and row[1] or charset;
304         end
305         self.charset = charset;
306         return self:transaction(function() return engine:execute(set_names_query:format(charset)); end);
307 end
308 local engine_mt = { __index = engine };
309
310 local function db2uri(params)
311         return build_url{
312                 scheme = params.driver,
313                 user = params.username,
314                 password = params.password,
315                 host = params.host,
316                 port = params.port,
317                 path = params.database,
318         };
319 end
320 local engine_cache = {}; -- TODO make weak valued
321 function create_engine(self, params)
322         local url = db2uri(params);
323         if not engine_cache[url] then
324                 local engine = setmetatable({ url = url, params = params }, engine_mt);
325                 engine_cache[url] = engine;
326         end
327         return engine_cache[url];
328 end
329
330
331 --[[Users = Table {
332         name="users";
333         Column { name="user_id", type=String(), primary_key=true };
334 };
335 print(Users)
336 print(Users.c.user_id)]]
337
338 --local engine = create_engine('postgresql://scott:tiger@localhost:5432/mydatabase');
339 --[[local engine = create_engine{ driver = "SQLite3", database = "./alchemy.sqlite" };
340
341 local i = 0;
342 for row in assert(engine:execute("select * from sqlite_master")):rows(true) do
343         i = i+1;
344         print(i);
345         for k,v in pairs(row) do
346                 print("",k,v);
347         end
348 end
349 print("---")
350
351 Prosody = Table {
352         name="prosody";
353         Column { name="host", type="TEXT", nullable=false };
354         Column { name="user", type="TEXT", nullable=false };
355         Column { name="store", type="TEXT", nullable=false };
356         Column { name="key", type="TEXT", nullable=false };
357         Column { name="type", type="TEXT", nullable=false };
358         Column { name="value", type="TEXT", nullable=false };
359         Index { name="prosody_index", "host", "user", "store", "key" };
360 };
361 --print(Prosody);
362 assert(engine:transaction(function()
363         assert(Prosody:create(engine));
364 end));
365
366 for row in assert(engine:execute("select user from prosody")):rows(true) do
367         print("username:", row['username'])
368 end
369 --result.close();]]
370
371 return _M;