|
| 1 | +-- Documenation SQL fixer-upper |
| 2 | + |
| 3 | +-- Author: Nick Gammon |
| 4 | +-- Date: 12th June 2011 |
| 5 | + |
| 6 | +-- Converts documentation from MySQL to SQLite3 format |
| 7 | + |
| 8 | +-- See: http://www.gammon.com.au/forum/bbshowpost.php?id=9241 |
| 9 | + |
| 10 | +local fixups = { |
| 11 | + ["\\a"] = "\a", -- Bell (alert) |
| 12 | + ["\\b"] = "\b", -- Backspace |
| 13 | + ["\\f"] = "\f", -- Formfeed |
| 14 | + ["\\n"] = "\n", -- Newline |
| 15 | + ["\\r"] = "", -- Carriage return |
| 16 | + ["\\t"] = "\t", -- Horizontal tab |
| 17 | + ["\\v"] = "\v", -- Vertical tab |
| 18 | + ["\\'"] = "''", -- Single quotation mark |
| 19 | + ["\\\""] = "\"", -- Double quotation mark |
| 20 | + ["\\\\"] = "\\", -- Backslash |
| 21 | + ["\\?"] = "?", -- Literal question mark |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | +function FixupEscapeSequences (s) |
| 26 | + |
| 27 | + -- look for backslash-something |
| 28 | + s = string.gsub (s, "\\.", fixups) |
| 29 | + |
| 30 | + -- handle newlines |
| 31 | +-- s = string.gsub (s, "\\n", "' || x'0A' || '") |
| 32 | + |
| 33 | + -- \xhh ASCII character in hexadecimal notation |
| 34 | + s = string.gsub (s, "\\[xX](%x%x)", function (s) return string.char (tonumber (s, 16)) end) |
| 35 | + |
| 36 | + -- \ character fixup |
| 37 | + s = string.gsub (s, "&#[xX](%x%x);", function (s) return string.char (tonumber (s, 16)) end) |
| 38 | + |
| 39 | + |
| 40 | + -- fix up newlines |
| 41 | +-- return string.gsub (s, "\n", "'\n") |
| 42 | + |
| 43 | +return s |
| 44 | +end -- function FixupEscapeSequences |
| 45 | + |
| 46 | +fi = assert (io.open ("\\\\10.0.0.222\\general\\documentation.sql", "r")) |
| 47 | + |
| 48 | +fo = assert (io.open ("documentation_fixed.sql", "w")) |
| 49 | + |
| 50 | +fo:write ("-- MUSHclient documentation\n\n") |
| 51 | +fo:write (os.date ("-- Written: %A, %d %B %Y at %H:%M:%S\n\n")) |
| 52 | + |
| 53 | +fo:write ("BEGIN TRANSACTION;\n\n") |
| 54 | + |
| 55 | +local line = fi:read () -- priming read |
| 56 | +local count |
| 57 | +local add_index |
| 58 | +while line do |
| 59 | + |
| 60 | + line = FixupEscapeSequences (line) |
| 61 | + |
| 62 | + -- fix UNIQUE KEY problem |
| 63 | + line = string.gsub (line, " UNIQUE KEY `[%w%d_]-`", " UNIQUE ") |
| 64 | + -- fix quotes inside strings |
| 65 | + --line = string.gsub (line, "\\'", "''") |
| 66 | + |
| 67 | + -- get rid of enum |
| 68 | + line = string.gsub (line, " `type_of_object` enum%('Method','Property'%) NOT NULL default 'Method',", |
| 69 | + " `type_of_object` varchar(10) NOT NULL default 'Method',") |
| 70 | + |
| 71 | + -- get rid of comma here |
| 72 | + line = string.gsub (line, " UNIQUE %(`doc_name`,`xref_name`%),", |
| 73 | + " UNIQUE (`doc_name`,`xref_name`)") |
| 74 | + |
| 75 | + -- get rid of keys |
| 76 | + line = string.gsub (line, " KEY `doc_name_2` %(`doc_name`%),", "") |
| 77 | + line, count = string.gsub (line, " KEY `xref_name` %(`xref_name`%)", "") |
| 78 | + |
| 79 | + if count > 0 then |
| 80 | + add_index = true |
| 81 | + end -- if |
| 82 | + |
| 83 | + fo:write (line, "\n") |
| 84 | + |
| 85 | + -- add keys back |
| 86 | + if add_index and string.match (line, "^%);") then |
| 87 | + fo:write ([[ |
| 88 | + |
| 89 | + CREATE INDEX doc_name_2 ON general_doc_xref (doc_name); |
| 90 | + CREATE INDEX xref_name ON general_doc_xref (xref_name); |
| 91 | +]]) |
| 92 | + |
| 93 | + add_index = false |
| 94 | + end -- if |
| 95 | + |
| 96 | + line = fi:read () -- read next |
| 97 | +end -- while |
| 98 | + |
| 99 | +-- create full-text search tables |
| 100 | + |
| 101 | +fo:write [[ |
| 102 | +DROP TABLE IF EXISTS commands_lookup; |
| 103 | +CREATE VIRTUAL TABLE commands_lookup USING FTS4(command_name, short_description, description); |
| 104 | +INSERT INTO commands_lookup (command_name, short_description, description) SELECT command_name, short_description, description FROM commands; |
| 105 | +
|
| 106 | +DROP TABLE IF EXISTS dialogs_lookup; |
| 107 | +CREATE VIRTUAL TABLE dialogs_lookup USING FTS4(dialog_name, title, description); |
| 108 | +INSERT INTO dialogs_lookup (dialog_name, title, description) SELECT dialog_name, title, description FROM dialogs; |
| 109 | +
|
| 110 | +DROP TABLE IF EXISTS functions_lookup; |
| 111 | +CREATE VIRTUAL TABLE functions_lookup USING FTS4(name, summary, description, lua_example, lua_notes); |
| 112 | +INSERT INTO functions_lookup (name, summary, description, lua_example, lua_notes) SELECT name, summary, description, lua_example, lua_notes FROM functions; |
| 113 | +
|
| 114 | +DROP TABLE IF EXISTS general_doc_lookup; |
| 115 | +CREATE VIRTUAL TABLE general_doc_lookup USING FTS4(doc_name, title, description); |
| 116 | +INSERT INTO general_doc_lookup (doc_name, title, description) SELECT doc_name, title, description FROM general_doc; |
| 117 | +
|
| 118 | +DROP TABLE IF EXISTS errors_lookup; |
| 119 | +CREATE VIRTUAL TABLE errors_lookup USING FTS4(error_name, error_code, meaning); |
| 120 | +INSERT INTO errors_lookup (error_name, error_code, meaning) SELECT error_name, error_code, meaning FROM errors; |
| 121 | +
|
| 122 | +DROP TABLE IF EXISTS lua_functions_lookup; |
| 123 | +CREATE VIRTUAL TABLE lua_functions_lookup USING FTS4(name, summary, description); |
| 124 | +INSERT INTO lua_functions_lookup (name, summary, description) SELECT name, summary, description FROM lua_functions; |
| 125 | +
|
| 126 | +DROP TABLE IF EXISTS `tblUnixControl`; |
| 127 | +]] |
| 128 | + |
| 129 | +fo:write ("COMMIT;\n\n") |
| 130 | + |
| 131 | +fo:close () |
| 132 | + |
| 133 | +fi:close () |
| 134 | + |
| 135 | +print "done" |
0 commit comments