|
| 1 | +local modname = minetest.get_current_modname() |
| 2 | + |
| 3 | +local thismod = _G[modname] |
| 4 | + |
| 5 | +---- Table creation & deletion |
| 6 | + |
| 7 | +function thismod.create_table_sql(name, params) |
| 8 | + local lines = {} |
| 9 | + for _, coldata in ipairs(params.columns) do |
| 10 | + local line = (coldata.name or coldata[1]) .. ' ' .. (coldata.type or coldata[2]) |
| 11 | + if coldata.notnull then |
| 12 | + line = line .. ' NOT NULL' |
| 13 | + end |
| 14 | + if coldata.autoincrement then |
| 15 | + line = line .. ' AUTO_INCREMENT' |
| 16 | + end |
| 17 | + table.insert(lines, line) |
| 18 | + end |
| 19 | + table.insert(lines, 'PRIMARY KEY (' .. table.concat(params.pkey, ',') .. ')') |
| 20 | + for fkeyname, fkeydata in pairs(params.fkeys or {}) do |
| 21 | + table.insert(lines, 'FOREIGN KEY (' .. fkeyname .. ') REFERENCES ' .. fkeydata.table .. |
| 22 | + '(' .. fkeydata.column .. ')') |
| 23 | + end |
| 24 | + for _, ucol in pairs(params.unique or {}) do |
| 25 | + if type(ucol) == 'table' then |
| 26 | + table.insert(lines, 'UNIQUE (' .. table.concat(ucol, ',') .. ')') |
| 27 | + else |
| 28 | + table.insert(lines, 'UNIQUE (' .. ucol .. ')') |
| 29 | + end |
| 30 | + end |
| 31 | + return 'CREATE TABLE ' .. name .. ' (' .. table.concat(lines, ',') .. ')' |
| 32 | +end |
| 33 | +function thismod.create_table(name, params) |
| 34 | + thismod.conn:query(thismod.create_table_sql(name, params)) |
| 35 | +end |
| 36 | + |
| 37 | +function thismod.drop_table_sql(name) |
| 38 | + return 'DROP TABLE ' .. name |
| 39 | +end |
| 40 | +function thismod.drop_table(name) |
| 41 | + thismod.conn:query(thismod.drop_table_sql(name)) |
| 42 | +end |
| 43 | + |
| 44 | +---- INSERT prepare |
| 45 | + |
| 46 | +function thismod.prepare_insert_sql(tablename, colnames) |
| 47 | + local qmarks = {} |
| 48 | + for i = 1, #colnames do |
| 49 | + qmarks[i] = '?' |
| 50 | + end |
| 51 | + return 'INSERT INTO ' .. tablename .. '(' .. table.concat(colnames, ',') .. ') VALUES (' .. |
| 52 | + table.concat(qmarks, ',') .. ')' |
| 53 | +end |
| 54 | +function thismod.prepare_insert(tablename, cols) |
| 55 | + local colnames, coltypes = {}, {} |
| 56 | + for _, col in ipairs(cols) do |
| 57 | + table.insert(colnames, col.name or col[1]) |
| 58 | + table.insert(coltypes, col.type or col[2]) |
| 59 | + end |
| 60 | + local stmt = thismod.conn:prepare(thismod.prepare_insert_sql(tablename, colnames)) |
| 61 | + return stmt, stmt:bind_params(coltypes) |
| 62 | +end |
| 63 | + |
| 64 | +---- UPDATE prepare |
| 65 | + |
| 66 | +function thismod.prepare_update_sql(tablename, colnames, where) |
| 67 | + return 'UPDATE ' .. tablename .. ' SET ' .. table.concat(colnames, ',') .. ' WHERE ' .. where |
| 68 | +end |
| 69 | +function thismod.prepare_update(tablename, cols, where, wheretypes) |
| 70 | + local colnames, paramtypes = {}, {} |
| 71 | + for _, col in ipairs(cols) do |
| 72 | + table.insert(colnames, (col.name or col[1]) .. '=?') |
| 73 | + table.insert(paramtypes, col.type or col[2]) |
| 74 | + end |
| 75 | + for _, wheretype in ipairs(wheretypes) do |
| 76 | + table.insert(paramtypes, wheretype) |
| 77 | + end |
| 78 | + local stmt = thismod.conn:prepare(thismod.prepare_update_sql(tablename, colnames, where)) |
| 79 | + return stmt, stmt:bind_params(paramtypes) |
| 80 | +end |
| 81 | + |
| 82 | +---- DELETE prepare |
| 83 | + |
| 84 | +function thismod.prepare_delete(tablename, where, wheretypes) |
| 85 | + local stmt = thismod.conn:prepare('DELETE FROM ' .. tablename .. ' WHERE ' .. where) |
| 86 | + return stmt, stmt:bind_params(wheretypes) |
| 87 | +end |
| 88 | + |
| 89 | +---- SELECT prepare |
| 90 | + |
| 91 | +function thismod.prepare_select_sql(tablename, colnames, where) |
| 92 | + return 'SELECT ' .. table.concat(colnames, ',') .. ' FROM ' .. tablename .. ' WHERE ' .. where |
| 93 | +end |
| 94 | +function thismod.prepare_select(tablename, cols, where, wheretypes) |
| 95 | + local colnames, coltypes = {}, {} |
| 96 | + for _, col in ipairs(cols) do |
| 97 | + table.insert(colnames, col.name or col[1]) |
| 98 | + table.insert(coltypes, col.type or col[2]) |
| 99 | + end |
| 100 | + local stmt = thismod.conn:prepare(thismod.prepare_select_sql(tablename, colnames, where)) |
| 101 | + return stmt, stmt:bind_params(wheretypes), stmt:bind_result(coltypes) |
| 102 | +end |
0 commit comments