|
| 1 | +import os, strutils, db_sqlite, parseopt |
| 2 | + |
| 3 | +const VERSION = "0.12" |
| 4 | + |
| 5 | +var |
| 6 | + db: DbConn |
| 7 | + |
| 8 | +proc ntcreate(group:string) = |
| 9 | + db.exec(sql"create table ? (name text, snippet text, language text)", group) |
| 10 | + |
| 11 | +proc ntadd(group, name, language, snippet: string) = |
| 12 | + if not db.tryExec(sql"insert into ? (name, snippet, language) values (?, ?, ?)", group, name, snippet, language): |
| 13 | + db.exec(sql"create table ? (name text, language text, snippet text)", group) |
| 14 | + if not db.tryExec(sql"insert into ? (name, snippet, language) values (?, ?, ?)", group, name, snippet, language): |
| 15 | + echo "Failed to insert... " |
| 16 | + dbError(db) |
| 17 | + |
| 18 | +proc ntdel(group, line: string) = |
| 19 | + db.exec(sql"delete from ? where name = ?", group, line) |
| 20 | + |
| 21 | +proc ntget(group, line: string) = |
| 22 | + var q = "SELECT * FROM "&group&" WHERE name like '"&line&"'" |
| 23 | + for x in db.fastRows(sql(q)): |
| 24 | + echo x[0]&" ("&x[1]&")" |
| 25 | + echo x[2] |
| 26 | + echo "----" |
| 27 | + |
| 28 | +proc ntshow(group, line: string) = |
| 29 | + var q = "SELECT * FROM "&group&" WHERE name = '"&line&"'" |
| 30 | + for x in db.fastRows(sql(q)): |
| 31 | + echo x[2] |
| 32 | + break |
| 33 | + |
| 34 | +proc ntgetl(group, line: string) = |
| 35 | + var q = "SELECT * FROM "&group&" where language = '"&line&"'" |
| 36 | + for x in db.fastRows(sql(q)): |
| 37 | + echo x |
| 38 | + |
| 39 | +proc ntgetc(group, line: string) = |
| 40 | + var q = "SELECT * FROM "&group&" where snippet like '%"&line&"%'" |
| 41 | + for x in db.fastRows(sql(q)): |
| 42 | + echo x |
| 43 | + |
| 44 | +proc ntedit(group, name, key, line: string) = |
| 45 | + db.exec(sql"update ? set ? = ? where name = ?", group, key, line, name) |
| 46 | + |
| 47 | +proc ntls(group: string) = |
| 48 | + var q = "SELECT * FROM "&group |
| 49 | + for x in db.fastRows(sql(q)): |
| 50 | + echo x[0]&" ("&x[1]&")" |
| 51 | + echo x[2] |
| 52 | + echo "----" |
| 53 | + |
| 54 | +proc ntls() = |
| 55 | + var fq = "SELECT * from sqlite_master" |
| 56 | + for x in db.fastRows(sql(fq)): |
| 57 | + var q = "SELECT * FROM "&x[1] |
| 58 | + for t in db.fastRows(sql(q)): |
| 59 | + echo t[0]&" ("&t[1]&")" |
| 60 | + echo t[2] |
| 61 | + echo "----" |
| 62 | + |
| 63 | +proc ntlst() = |
| 64 | + var fq = "SELECT * from sqlite_master" |
| 65 | + for x in db.fastRows(sql(fq)): |
| 66 | + echo x[1] |
| 67 | + |
| 68 | +proc ntdrop(group: string) = |
| 69 | + db.exec(sql"drop table ?", group) |
| 70 | + |
| 71 | +proc ntexec(line: string, catch: bool = false) = |
| 72 | + if catch: |
| 73 | + if not db.tryExec(sql(line)): |
| 74 | + dbError(db) |
| 75 | + else: |
| 76 | + for x in db.fastRows(sql(line)): |
| 77 | + echo x |
| 78 | + |
| 79 | +proc spinLine() = |
| 80 | + var |
| 81 | + p = initOptParser() |
| 82 | + command, table, lang, code, name: string |
| 83 | + args = newSeq[string]() |
| 84 | + |
| 85 | + while true: |
| 86 | + p.next() |
| 87 | + case p.kind |
| 88 | + of cmdEnd: break |
| 89 | + of cmdShortOption, cmdLongOption: |
| 90 | + if p.key == "version" or p.key == "v": |
| 91 | + echo "NT "&VERSION |
| 92 | + return |
| 93 | + |
| 94 | + if p.val != "": |
| 95 | + if p.key == "table" or p.key == "t": |
| 96 | + table = p.val |
| 97 | + if p.key == "lang" or p.key=="l": |
| 98 | + lang = p.val |
| 99 | + if p.key == "code" or p.key == "c": |
| 100 | + code = p.val |
| 101 | + if p.key == "name" or p.key == "n": |
| 102 | + name = p.val |
| 103 | + of cmdArgument: |
| 104 | + if command == "": |
| 105 | + command = p.key |
| 106 | + else: |
| 107 | + args.add(p.key) |
| 108 | + |
| 109 | + if table == "": |
| 110 | + table = "nt" |
| 111 | + |
| 112 | + case command: |
| 113 | + of "help": |
| 114 | + echo "-------" |
| 115 | + echo "NT: Nim Text storage "&VERSION |
| 116 | + echo "" |
| 117 | + echo "Variables: table/t, lang/l, code/c, name/n" |
| 118 | + echo "Table is given a default if not supplied." |
| 119 | + echo "" |
| 120 | + echo "Commands:" |
| 121 | + echo "\tadd/insert/a/i [-t:table_name] [-n:entry_name] [-l:code_language] [-c:code_block]" |
| 122 | + echo "\t\t Inserts line.\n\t\t nt add -t:pystuff -n:def -c:def bork(): pass -l:python3" |
| 123 | + echo "" |
| 124 | + echo "\tedit/e [key_to_edit] new_input [-n:name_of_entry] [-n:name_of_table]" |
| 125 | + echo "\t\t Edits a line\n\t\t Renaming: nt edit name test -n:tsst\n\t\t\t Changing the snippet: nt edit code 'def bork(): pass' -n:test" |
| 126 | + echo "" |
| 127 | + echo "\tdel/d [-t:table_name] [-n:entry_name]" |
| 128 | + echo "\t\t Deletes an entry from a table.\n\t\t nt del -t:pystuff -n:def" |
| 129 | + echo "" |
| 130 | + echo "\texec/eval [...sql_code]" |
| 131 | + echo "\t\tRuns raw SQL code on the database." |
| 132 | + echo "" |
| 133 | + echo "\tdrop/dr [-t:table]" |
| 134 | + echo "\t\t Deletes an entire table." |
| 135 | + echo "" |
| 136 | + echo "\ttables" |
| 137 | + echo "\t\tLists tables in the database." |
| 138 | + echo "" |
| 139 | + echo "\tlist/ls [-t:table]" |
| 140 | + echo "\t\tLists entries in a table. If table=* then lists all entries in all tables." |
| 141 | + echo "" |
| 142 | + echo "\tget/g [-n:name] OR getc/gc [-c:code_block] OR getl/gl [-l:language] WITH [-t:table_name]" |
| 143 | + echo "\t\t Finds entries based on search" |
| 144 | + echo "" |
| 145 | + echo "\tshow/s [-n:name] [-t:table_name]" |
| 146 | + echo "\t\tOutput entry with name matching." |
| 147 | + |
| 148 | + of "exec", "eval", "sql": |
| 149 | + ntexec(args.join(" "), false) |
| 150 | + of "add", "a", "insert": |
| 151 | + if lang == "": |
| 152 | + lang = "none" |
| 153 | + if name == "" or code == "" or table == "": |
| 154 | + echo "Value(s) missing for `add`: name, code, table" |
| 155 | + else: |
| 156 | + ntadd(table, name, lang, code) |
| 157 | + of "edit", "e", "mod", "modify", "m": |
| 158 | + ntedit(table, name, args[0], args[1..args.len-1].join(" ")) |
| 159 | + of "del", "d": |
| 160 | + if table == "" or name == "": |
| 161 | + echo "Value(s) missing for `del`: table, name" |
| 162 | + else: |
| 163 | + ntdel(table, name) |
| 164 | + of "drop", "dr": |
| 165 | + ntdrop(table) |
| 166 | + of "tables": |
| 167 | + ntlst() |
| 168 | + of "list", "ls": |
| 169 | + if table == "*": |
| 170 | + ntls() |
| 171 | + else: |
| 172 | + ntls(table) |
| 173 | + of "show", "s": |
| 174 | + if name == "" and args.len == 0: |
| 175 | + echo "Value missing: name" |
| 176 | + return |
| 177 | + |
| 178 | + if name == "": |
| 179 | + name = args.join(" ") |
| 180 | + |
| 181 | + ntshow(table, name) |
| 182 | + of "get", "g": |
| 183 | + if name == "" and args.len == 0: |
| 184 | + echo "Value missing: name" |
| 185 | + return |
| 186 | + |
| 187 | + if name == "": |
| 188 | + name = args.join(" ") |
| 189 | + |
| 190 | + ntget(table, name) |
| 191 | + of "getc", "gc", "getcode": |
| 192 | + if code == "" and args.len == 0: |
| 193 | + echo "Value missing: code" |
| 194 | + return |
| 195 | + |
| 196 | + if code == "": |
| 197 | + code = args.join(" ") |
| 198 | + |
| 199 | + ntgetc(table, code) |
| 200 | + |
| 201 | + of "getl", "gl", "getlang", "getlanguage": |
| 202 | + if lang == "" and args.len == 0: |
| 203 | + echo "Value missing: lang" |
| 204 | + return |
| 205 | + |
| 206 | + if lang == "": |
| 207 | + lang = args.join(" ") |
| 208 | + |
| 209 | + ntgetl(table, lang) |
| 210 | + |
| 211 | + of "create", "cr": |
| 212 | + if table == "" or table == "nt": |
| 213 | + echo "Can't create empty table." |
| 214 | + else: |
| 215 | + ntcreate(table) |
| 216 | + |
| 217 | + |
| 218 | +db = open(getHomeDir()&"/nt.db", "", "", "") |
| 219 | +spinLine() |
| 220 | +db.close() |
0 commit comments