1
2
3from odb import *
4import profiler
5import socket
6
7USER = 'root'
8PASSWORD = ''
9DATABASE = 'trans_data'
10
11class TransStringTable(Table):
12    # access patterns:
13    #   -> lookup individual entries by string_id
14    #   -> lookup entry by string
15    def _defineRows(self):
16        self.d_addColumn("string_id", kInteger, primarykey=1, autoincrement=1)
17        # we can't actually index this... but we can index part with myisam
18        self.d_addColumn("string", kBigString, indexed=1)
19
20## hmm, on second thought, storing this is in the database is kind
21## of silly..., since it essentially could change with each run.  It may
22## not even be necessary to store this anywhere except in memory while
23## trans is running
24class TransLocTable(Table):
25    # access patterns:
26    #   -> find "same" entry by filename/offset
27    #   -> dump all locations for a version
28    #   -> maybe: find all locations for a filename
29    def _defineRows(self):
30        self.d_addColumn("loc_id", kInteger, primarykey=1, autoincrement=1)
31        self.d_addColumn("string_id", kInteger, indexed=1)
32        self.d_addColumn("version", kInteger, default=0)
33        self.d_addColumn("filename", kVarString, 255, indexed=1)
34        self.d_addColumn("location", kVarString, 255)
35        # this can either be:
36        # ofs:x:y
37        # hdf:foo.bar.baz
38
39class TransMapTable(Table):
40    # access patterns:
41    #   -> dump all for a language
42    #   -> lookup entry by string_id/lang
43    def _defineRows(self):
44        self.d_addColumn("string_id", kInteger, primarykey=1)
45        self.d_addColumn("lang", kFixedString, 2, primarykey=1)
46        self.d_addColumn("string", kBigString)
47
48class DB(Database):
49    def __init__(self, db, debug=0):
50	self.db = db
51        self._cursor = None
52        self.debug = debug
53
54        self.addTable("strings", "nt_trans_strings", TransStringTable)
55        self.addTable("locs", "nt_trans_locs", TransLocTable)
56        self.addTable("maps", "nt_trans_maps", TransMapTable)
57
58    def defaultCursor(self):
59        # share one cursor for this db object!
60        if self._cursor is None:
61            if self.debug:
62                self._cursor = profiler.ProfilerCursor(self.db.cursor())
63            else:
64                self._cursor = self.db.cursor()
65
66        return self._cursor
67
68def trans_connect(host = 'localhost', debug=0):
69    # try to optimize connection if on this machine
70    if host != 'localhost':
71        local_name = socket.gethostname()
72        if string.find(local_name, '.') == -1:
73            local_name = local_name + ".neotonic.com"
74        if local_name == host:
75            host = 'localhost'
76
77    if debug: p = profiler.Profiler("SQL", "Connect -- %s:trans" % (host))
78    db = MySQLdb.connect(host = host, user=USER, passwd = PASSWORD, db=DATABASE)
79    if debug: p.end()
80
81    retval = DB(db, debug=debug)
82    return retval
83