forked from ifgi-sil/orientationMapsCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbConnection.py
executable file
·183 lines (135 loc) · 4.53 KB
/
dbConnection.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import qgis.core
import orientationMapsCreator_utils as Utils
class ConnectionManager:
SUPPORTED_CONNECTORS = ['postgis']
MISSED_CONNECTORS = []
@classmethod
def initConnectionSupport(self):
conntypes = ConnectionManager.SUPPORTED_CONNECTORS
for c in conntypes:
try:
connector = self.getConnection( c )
except ImportError, e:
module = e.args[0][ len("No module named "): ]
ConnectionManager.SUPPORTED_CONNECTORS.remove( c )
ConnectionManager.MISSED_CONNECTORS.append( (c, module) )
return len(ConnectionManager.SUPPORTED_CONNECTORS) > 0
@classmethod
def getConnection(self, conntype, uri=None):
if not self.isSupported(conntype):
raise NotSupportedConnTypeException(conntype)
# import the connector
exec( "from connectors import %s as connector" % conntype)
return connector.Connection(uri) if uri else connector.Connection
@classmethod
def isSupported(self, conntype):
return conntype in ConnectionManager.SUPPORTED_CONNECTORS
@classmethod
def getAvailableConnections(self, conntypes=None):
if conntypes == None:
conntypes = ConnectionManager.SUPPORTED_CONNECTORS
if not hasattr(conntypes, '__iter__'):
conntypes = [conntypes]
connections = []
for c in conntypes:
connection = self.getConnection( c )
connections.extend( connection.getAvailableConnections() )
return connections
class NotSupportedConnTypeException(Exception):
def __init__(self, conntype):
self.msg = u"%s is not supported yet" % conntype
def __str__(self):
return self.msg.encode('utf-8')
class DbError(Exception):
def __init__(self, errormsg, query=None):
self.msg = unicode( errormsg )
self.query = unicode( query ) if query else None
def __str__(self):
msg = self.msg.encode('utf-8')
if self.query != None:
msg += "\nQuery:\n" + self.query.encode('utf-8')
return msg
class Connection:
def __init__(self, uri):
self.uri = uri
@classmethod
def getTypeName(self):
pass
@classmethod
def getTypeNameString(self):
pass
@classmethod
def getProviderName(self):
pass
@classmethod
def getSettingsKey(self):
pass
@classmethod
def icon(self):
pass
@classmethod
def getAvailableConnections(self):
connections = []
settings = QSettings()
settings.beginGroup( "/%s/connections" % self.getSettingsKey() )
keys = settings.childGroups()
for name in keys:
connections.append( Connection.ConnectionAction(name, self.getTypeName()) )
settings.endGroup()
return connections
def getURI(self):
# returns a new QgsDataSourceURI instance
return qgis.core.QgsDataSourceURI( self.uri.connectionInfo() )
def getAction(self, parent=None):
return Connection.ConnectionAction(self.uri.database(), self.getTypeName(), parent)
class ConnectionAction(QAction):
def __init__(self, text, conntype, parent=None):
self.type = conntype
icon = ConnectionManager.getConnection(self.type).icon()
QAction.__init__(self, icon, text, parent)
def connect(self):
selected = self.text()
conn = ConnectionManager.getConnection( self.type ).connect( selected, self.parent() )
# set as default in QSettings
settings = QSettings()
settings.setValue( "/%s/connections/selected" % conn.getSettingsKey(), selected )
return conn
class TableAttribute:
pass
class TableConstraint:
""" class that represents a constraint of a table (relation) """
TypeCheck, TypeForeignKey, TypePrimaryKey, TypeUnique = range(4)
types = { "c" : TypeCheck, "f" : TypeForeignKey, "p" : TypePrimaryKey, "u" : TypeUnique }
on_action = { "a" : "NO ACTION", "r" : "RESTRICT", "c" : "CASCADE", "n" : "SET NULL", "d" : "SET DEFAULT" }
match_types = { "u" : "UNSPECIFIED", "f" : "FULL", "p" : "PARTIAL" }
pass
class TableIndex:
pass
class TableTrigger:
# Bits within tgtype (pg_trigger.h)
TypeRow = (1 << 0) # row or statement
TypeBefore = (1 << 1) # before or after
# events: one or more
TypeInsert = (1 << 2)
TypeDelete = (1 << 3)
TypeUpdate = (1 << 4)
TypeTruncate = (1 << 5)
pass
class TableRule:
pass
class TableField:
def is_null_txt(self):
if self.is_null:
return "NULL"
else:
return "NOT NULL"
def field_def(self, db):
""" return field definition as used for CREATE TABLE or ALTER TABLE command """
data_type = self.data_type if (not self.modifier or self.modifier < 0) else "%s(%d)" % (self.data_type, self.modifier)
txt = "%s %s %s" % (db._quote(self.name), data_type, self.is_null_txt())
if self.default and len(self.default) > 0:
txt += " DEFAULT %s" % self.default
return txt