4 @brief Support classes for Database Manager
9 (C) 2007-2011 by the GRASS Development Team
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
14 @author Martin Landa <landa.martin gmail.com>
30 if type(value) == types.UnicodeType:
33 enc = UserSettings.Get(group =
'atm', key =
'encoding', subkey =
'value')
34 if not enc
and 'GRASS_DB_ENCODING' in os.environ:
35 enc = os.environ[
'GRASS_DB_ENCODING']
39 return unicode(value, enc, errors =
'replace')
42 """!Create database connection information content"""
43 infoFlexSizer = wx.FlexGridSizer (cols = 2, hgap = 1, vgap = 1)
44 infoFlexSizer.AddGrowableCol(1)
46 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
48 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
49 label = mapDBInfo.layers[layer][
'driver']))
50 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
52 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
53 label = mapDBInfo.layers[layer][
'database']))
54 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
56 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
57 label = mapDBInfo.layers[layer][
'table']))
58 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
60 infoFlexSizer.Add(item = wx.StaticText(parent = panel, id = wx.ID_ANY,
61 label = mapDBInfo.layers[layer][
'key']))
66 """!Class providing information about attribute tables
67 linked to the vector map"""
69 VectorDBInfoBase.__init__(self, map)
72 """!Return list of columns names (based on their index)"""
74 names = [
''] * len(self.tables[table].keys())
78 for name, desc
in self.tables[table].iteritems():
79 names[desc[
'index']] = name
84 """!Get attributes by coordinates (all available layers)
86 Return line id or None if no line is found"""
90 data = grass.vector_what(map = self.map,
91 coord = (float(queryCoords[0]), float(queryCoords[1])),
92 distance = float(qdist))
94 if len(data) < 1
or all((
'Table' not in record)
for record
in data):
99 for key
in [
'Category',
'Layer',
'Table',
'Id']:
103 if not 'Table' in record:
106 table = record[
'Table']
107 for key, value
in record[
'Attributes'].iteritems():
111 if self.tables[table][key][
'ctype'] != types.StringType:
112 value = self.tables[table][key][
'ctype'] (value)
115 self.tables[table][key][
'values'].append(value)
117 for key, value
in record.iteritems():
118 if key ==
'Attributes':
121 ret[key].append(value)
122 if 'Id' not in record.keys():
123 ret[
'Id'].append(
None)
128 """!Select records from the table
130 Return number of selected records, -1 on error
137 table = self.layers[layer][
"table"]
139 if where
is None or where
is '':
140 sql =
"SELECT %s FROM %s" % (cols, table)
142 sql =
"SELECT %s FROM %s WHERE %s" % (cols, table, where)
150 database = self.layers[layer][
"database"],
151 driver = self.layers[layer][
"driver"])
155 for line
in ret.splitlines():
156 name, value = line.split(
'|')
159 if self.tables[table][name][
'ctype'] !=
type(
''):
160 value = self.tables[table][name][
'ctype'] (value)
165 self.tables[table][name][
'values'].append(value)