Comparison

util/sql.lua @ 7306:98c4c3a2b536

util.sql: Catch errors from LuaDBI connect (Fixes #568)
author Kim Alvefur <zash@zash.se>
date Mon, 21 Mar 2016 09:50:52 +0100
parent 7276:30dfaf36ea6d
child 7312:b4e99602ae75
comparison
equal deleted inserted replaced
7305:c02e3d8f23fc 7306:98c4c3a2b536
100 if self.conn then return true; end 100 if self.conn then return true; end
101 101
102 local params = self.params; 102 local params = self.params;
103 assert(params.driver, "no driver") 103 assert(params.driver, "no driver")
104 log("debug", "Connecting to [%s] %s...", params.driver, params.database); 104 log("debug", "Connecting to [%s] %s...", params.driver, params.database);
105 local dbh, err = DBI.Connect( 105 local ok, dbh, err = pcall(DBI.Connect,
106 params.driver, params.database, 106 params.driver, params.database,
107 params.username, params.password, 107 params.username, params.password,
108 params.host, params.port 108 params.host, params.port
109 ); 109 );
110 if not ok then return ok, dbh; end
110 if not dbh then return nil, err; end 111 if not dbh then return nil, err; end
111 dbh:autocommit(false); -- don't commit automatically 112 dbh:autocommit(false); -- don't commit automatically
112 self.conn = dbh; 113 self.conn = dbh;
113 self.prepared = {}; 114 self.prepared = {};
114 local ok, err = self:set_encoding(); 115 local ok, err = self:set_encoding();