1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import re
17 from buildbot.util import sautils
18 from sqlalchemy.engine import reflection
19 from sqlalchemy.dialects.sqlite.base import SQLiteDialect, _pragma_cursor
20 from sqlalchemy.dialects.sqlite.base import sqltypes, util
21
22 @reflection.cache
24 quote = self.identifier_preparer.quote_identifier
25 if schema is not None:
26 pragma = "PRAGMA %s." % quote(schema)
27 else:
28 pragma = "PRAGMA "
29 qtable = quote(table_name)
30 c = _pragma_cursor(connection.execute("%stable_info(%s)" % (pragma, qtable)))
31
32 columns = []
33 while True:
34 row = c.fetchone()
35 if row is None:
36 break
37 (name, type_, nullable, default, has_default, primary_key) = (row[1], row[2].upper(), not row[3], row[4], row[4] is not None, row[5])
38 name = re.sub(r'^\"|\"$', '', name)
39
40
41 match = re.match(r'(\w+)(\(.*?\))?', type_)
42 if match:
43 coltype = match.group(1)
44 args = match.group(2)
45 else:
46 coltype = "VARCHAR"
47 args = ''
48 try:
49 coltype = self.ischema_names[coltype]
50 except KeyError:
51 util.warn("Did not recognize type '%s' of column '%s'" %
52 (coltype, name))
53 coltype = sqltypes.NullType
54 if args is not None:
55 args = re.findall(r'(\d+)', args)
56 coltype = coltype(*[int(a) for a in args])
57
58 columns.append({
59 'name' : name,
60 'type' : coltype,
61 'nullable' : nullable,
62 'default' : default,
63 'primary_key': primary_key
64 })
65 return columns
66
67
68 @reflection.cache
70 quote = self.identifier_preparer.quote_identifier
71 if schema is not None:
72 pragma = "PRAGMA %s." % quote(schema)
73 else:
74 pragma = "PRAGMA "
75 qtable = quote(table_name)
76 c = _pragma_cursor(connection.execute("%stable_info(%s)" % (pragma, qtable)))
77
78 columns = []
79 while True:
80 row = c.fetchone()
81 if row is None:
82 break
83 (name, type_, nullable, default, has_default, primary_key) = (row[1], row[2].upper(), not row[3], row[4], row[4] is not None, row[5])
84 name = re.sub(r'^\"|\"$', '', name)
85
86
87 match = re.match(r'(\w+)(\(.*?\))?', type_)
88 if match:
89 coltype = match.group(1)
90 args = match.group(2)
91 else:
92 coltype = "VARCHAR"
93 args = ''
94 try:
95 coltype = self.ischema_names[coltype]
96 if args is not None:
97 args = re.findall(r'(\d+)', args)
98 coltype = coltype(*[int(a) for a in args])
99 except KeyError:
100 util.warn("Did not recognize type '%s' of column '%s'" %
101 (coltype, name))
102 coltype = sqltypes.NullType()
103
104 columns.append({
105 'name' : name,
106 'type' : coltype,
107 'nullable' : nullable,
108 'default' : default,
109 'autoincrement':default is None,
110 'primary_key': primary_key
111 })
112 return columns
113
121