comparison util/sql.lua @ 5897:296e32a1ff33

Merge 0.10 -> trunk
author Kim Alvefur <zash@zash.se>
date Thu, 31 Oct 2013 20:49:03 +0100
parents 544ca3d94596
children a19b3646d5f0
comparison
equal deleted inserted replaced
5880:11f14d44438e 5897:296e32a1ff33
249 if self.params.driver == "PostgreSQL" then 249 if self.params.driver == "PostgreSQL" then
250 sql = sql:gsub("`", "\""); 250 sql = sql:gsub("`", "\"");
251 elseif self.params.driver == "MySQL" then 251 elseif self.params.driver == "MySQL" then
252 sql = sql:gsub("`([,)])", "`(20)%1"); 252 sql = sql:gsub("`([,)])", "`(20)%1");
253 end 253 end
254 if index.unique then
255 sql = sql:gsub("^CREATE", "CREATE UNIQUE");
256 end
254 --print(sql); 257 --print(sql);
255 return self:execute(sql); 258 return self:execute(sql);
256 end 259 end
257 function engine:_create_table(table) 260 function engine:_create_table(table)
258 local sql = "CREATE TABLE `"..table.name.."` ("; 261 local sql = "CREATE TABLE `"..table.name.."` (";
259 for i,col in ipairs(table.c) do 262 for i,col in ipairs(table.c) do
260 sql = sql.."`"..col.name.."` "..col.type; 263 local col_type = col.type;
264 if col_type == "MEDIUMTEXT" and self.params.driver ~= "MySQL" then
265 col_type = "TEXT"; -- MEDIUMTEXT is MySQL-specific
266 end
267 sql = sql.."`"..col.name.."` "..col_type;
261 if col.nullable == false then sql = sql.." NOT NULL"; end 268 if col.nullable == false then sql = sql.." NOT NULL"; end
269 if col.primary_key == true then sql = sql.." PRIMARY KEY"; end
270 if col.auto_increment == true then
271 if self.params.driver == "PostgreSQL" then
272 sql = sql.." SERIAL";
273 elseif self.params.driver == "MySQL" then
274 sql = sql.." AUTO_INCREMENT";
275 elseif self.params.driver == "SQLite3" then
276 sql = sql.." AUTOINCREMENT";
277 end
278 end
262 if i ~= #table.c then sql = sql..", "; end 279 if i ~= #table.c then sql = sql..", "; end
263 end 280 end
264 sql = sql.. ");" 281 sql = sql.. ");"
265 if self.params.driver == "PostgreSQL" then 282 if self.params.driver == "PostgreSQL" then
266 sql = sql:gsub("`", "\""); 283 sql = sql:gsub("`", "\"");
273 if is_index(v) then 290 if is_index(v) then
274 self:_create_index(v); 291 self:_create_index(v);
275 end 292 end
276 end 293 end
277 return success; 294 return success;
295 end
296 function engine:set_encoding() -- to UTF-8
297 local driver = self.params.driver;
298 if driver == "SQLite3" then
299 return self:transaction(function()
300 if self:select"PRAGMA encoding;"()[1] == "UTF-8" then
301 self.charset = "utf8";
302 end
303 end);
304 end
305 local set_names_query = "SET NAMES '%s';"
306 local charset = "utf8";
307 if driver == "MySQL" then
308 set_names_query = set_names_query:gsub(";$", " COLLATE 'utf8_bin';");
309 local ok, charsets = self:transaction(function()
310 return self:select"SELECT `CHARACTER_SET_NAME` FROM `CHARACTER_SETS` WHERE `CHARACTER_SET_NAME` LIKE 'utf8%' ORDER BY MAXLEN DESC LIMIT 1;";
311 end);
312 local row = ok and charsets();
313 charset = row and row[1] or charset;
314 end
315 self.charset = charset;
316 return self:transaction(function() return engine:execute(set_names_query:format(charset)); end);
278 end 317 end
279 local engine_mt = { __index = engine }; 318 local engine_mt = { __index = engine };
280 319
281 local function db2uri(params) 320 local function db2uri(params)
282 return build_url{ 321 return build_url{