summaryrefslogtreecommitdiff
path: root/sources/db/update_characters.rb
blob: dbfc5cd835c547cead826cdc372822bfbf7c5b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
#------------------------------------------------------------------------------
#
#  Taginfo source: DB
#
#  update_characters.rb
#
#------------------------------------------------------------------------------
#
#  Copyright (C) 2013  Jochen Topf <jochen@remote.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#------------------------------------------------------------------------------

require 'sqlite3'

dir = ARGV[0] || '.'
db = SQLite3::Database.new(dir + '/taginfo-db.db')
db.results_as_hash = true

db.execute("PRAGMA journal_mode  = OFF")
db.execute("PRAGMA synchronous   = OFF")
db.execute("PRAGMA count_changes = OFF")
db.execute("PRAGMA temp_store    = MEMORY")
db.execute("PRAGMA cache_size    = 5000000")

#------------------------------------------------------------------------------

regexes = [
    [ 'plain',   %r{^[a-z]([a-z_]*[a-z])?$} ],
    [ 'colon',   %r{^[a-z][a-z_:]*[a-z]$} ],
    [ 'letters', %r{^[\p{L}\p{M}]([\p{L}\p{M}\p{N}_:]*[\p{L}\p{M}\p{N}])?$}u ],
    [ 'space',   %r{[\s\p{Z}]}u ],
    [ 'problem', %r{[=+/&<>;\@'"?%#\\,\p{C}]}u ]
]

keys = {}
db.execute("SELECT key FROM keys").map{ |row| row['key'] }.each do |key|
    keys[key] = 'rest'
    regexes.each do |type, regex|
        if key.match(regex)
            keys[key] = type
            break
        end
    end
end

db.transaction do |db|
    keys.each do |key, type|
        db.execute("UPDATE keys SET characters=? WHERE key=?", type, key)
    end
end

#-- THE END -------------------------------------------------------------------