|
1 #!/usr/bin/lua |
|
2 |
|
3 module('DBI', package.seeall) |
|
4 |
|
5 local name_to_module = { |
|
6 MySQL = 'dbdmysql', |
|
7 PostgreSQL = 'dbdpostgresql', |
|
8 SQLite3 = 'dbdsqlite3', |
|
9 } |
|
10 |
|
11 local string = require('string') |
|
12 |
|
13 local function available_drivers() |
|
14 local available = {} |
|
15 |
|
16 for driver, modulefile in pairs(name_to_module) do |
|
17 local m, err = pcall(require, modulefile) |
|
18 |
|
19 if m then |
|
20 table.insert(available, driver) |
|
21 end |
|
22 end |
|
23 |
|
24 if table.maxn(available) < 1 then |
|
25 return '(None)' |
|
26 end |
|
27 |
|
28 return table.concat(available, ',') |
|
29 end |
|
30 |
|
31 function Connect(driver, name, username, password, host, port) |
|
32 local modulefile = name_to_module[driver] |
|
33 |
|
34 if not modulefile then |
|
35 error(string.format("Driver '%s' not found. Available drivers are: %s", driver, available_drivers())) |
|
36 end |
|
37 |
|
38 local m, err = pcall(require, modulefile) |
|
39 |
|
40 if not m then |
|
41 error(string.format('Cannot load driver %s. Available drivers are: %s', driver, available_drivers())) |
|
42 end |
|
43 |
|
44 local class_str = string.format('DBD.%s.Connection', driver) |
|
45 |
|
46 local connection_class = package.loaded[class_str] |
|
47 |
|
48 return connection_class.New(name, username, password, host, port) |
|
49 end |
|
50 |