11 -- This loads all available drivers while globals are unlocked |
11 -- This loads all available drivers while globals are unlocked |
12 -- LuaDBI should be fixed to not set globals. |
12 -- LuaDBI should be fixed to not set globals. |
13 DBI.Drivers(); |
13 DBI.Drivers(); |
14 local build_url = require "socket.url".build; |
14 local build_url = require "socket.url".build; |
15 |
15 |
16 module("sql") |
16 local _ENV = nil; |
17 |
17 |
18 local column_mt = {}; |
18 local column_mt = {}; |
19 local table_mt = {}; |
19 local table_mt = {}; |
20 local query_mt = {}; |
20 local query_mt = {}; |
21 --local op_mt = {}; |
21 --local op_mt = {}; |
22 local index_mt = {}; |
22 local index_mt = {}; |
23 |
23 |
24 function is_column(x) return getmetatable(x)==column_mt; end |
24 local function is_column(x) return getmetatable(x)==column_mt; end |
25 function is_index(x) return getmetatable(x)==index_mt; end |
25 local function is_index(x) return getmetatable(x)==index_mt; end |
26 function is_table(x) return getmetatable(x)==table_mt; end |
26 local function is_table(x) return getmetatable(x)==table_mt; end |
27 function is_query(x) return getmetatable(x)==query_mt; end |
27 local function is_query(x) return getmetatable(x)==query_mt; end |
28 function Integer(n) return "Integer()" end |
28 local function Integer(n) return "Integer()" end |
29 function String(n) return "String()" end |
29 local function String(n) return "String()" end |
30 |
30 |
31 function Column(definition) |
31 local function Column(definition) |
32 return setmetatable(definition, column_mt); |
32 return setmetatable(definition, column_mt); |
33 end |
33 end |
34 function Table(definition) |
34 local function Table(definition) |
35 local c = {} |
35 local c = {} |
36 for i,col in ipairs(definition) do |
36 for i,col in ipairs(definition) do |
37 if is_column(col) then |
37 if is_column(col) then |
38 c[i], c[col.name] = col, col; |
38 c[i], c[col.name] = col, col; |
39 elseif is_index(col) then |
39 elseif is_index(col) then |
40 col.table = definition.name; |
40 col.table = definition.name; |
41 end |
41 end |
42 end |
42 end |
43 return setmetatable({ __table__ = definition, c = c, name = definition.name }, table_mt); |
43 return setmetatable({ __table__ = definition, c = c, name = definition.name }, table_mt); |
44 end |
44 end |
45 function Index(definition) |
45 local function Index(definition) |
46 return setmetatable(definition, index_mt); |
46 return setmetatable(definition, index_mt); |
47 end |
47 end |
48 |
48 |
49 function table_mt:__tostring() |
49 function table_mt:__tostring() |
50 local s = { 'name="'..self.__table__.name..'"' } |
50 local s = { 'name="'..self.__table__.name..'"' } |
300 |
300 |
301 return true; |
301 return true; |
302 end |
302 end |
303 local engine_mt = { __index = engine }; |
303 local engine_mt = { __index = engine }; |
304 |
304 |
305 function db2uri(params) |
305 local function db2uri(params) |
306 return build_url{ |
306 return build_url{ |
307 scheme = params.driver, |
307 scheme = params.driver, |
308 user = params.username, |
308 user = params.username, |
309 password = params.password, |
309 password = params.password, |
310 host = params.host, |
310 host = params.host, |
311 port = params.port, |
311 port = params.port, |
312 path = params.database, |
312 path = params.database, |
313 }; |
313 }; |
314 end |
314 end |
315 |
315 |
316 function create_engine(self, params, onconnect) |
316 local function create_engine(self, params, onconnect) |
317 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); |
317 return setmetatable({ url = db2uri(params), params = params, onconnect = onconnect }, engine_mt); |
318 end |
318 end |
319 |
319 |
320 return _M; |
320 return { |
|
321 is_column = is_column; |
|
322 is_index = is_index; |
|
323 is_table = is_table; |
|
324 is_query = is_query; |
|
325 Integer = Integer; |
|
326 String = String; |
|
327 Column = Column; |
|
328 Table = Table; |
|
329 Index = Index; |
|
330 create_engine = create_engine; |
|
331 }; |