gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / scripts / tools / sqlconsole / sqlconsole.py @ 545
History | View | Annotate | Download (8.39 KB)
1 |
|
---|---|
2 |
|
3 |
# Habilitamos el acceso a los plugins de geodb y del documento tabla
|
4 |
use_plugin("org.gvsig.app.document.table.app.mainplugin")
|
5 |
use_plugin("org.gvsig.geodb.app.mainplugin")
|
6 |
|
7 |
from gvsig import * |
8 |
from commonsdialog import * |
9 |
|
10 |
from libs.formpanel import FormPanel |
11 |
|
12 |
from java.awt import BorderLayout |
13 |
from org.gvsig.fmap.dal import DALLocator |
14 |
from org.gvsig.fmap.dal.store.jdbc import JDBCServerExplorerParameters |
15 |
from org.gvsig.fmap.mapcontrol.dal.feature.swing.table import FeatureTableModel |
16 |
from org.gvsig.fmap.mapcontrol.dal.feature.swing.table import ConfigurableFeatureTableModel |
17 |
from org.gvsig.fmap.mapcontrol.dal.feature.swing import FeatureTablePanel |
18 |
from org.gvsig.fmap.mapcontrol.dal.feature.swing import FeatureTable |
19 |
from org.gvsig.app.project.documents.table import TableManager |
20 |
from org.gvsig.fmap.mapcontext import MapContextLocator |
21 |
from org.gvsig.geodb.vectorialdb.wizard import VectorialDBConnectionParamsDialog |
22 |
from org.gvsig.fmap.geom import GeometryLocator |
23 |
from org.gvsig.fmap.geom import Geometry |
24 |
|
25 |
from org.fife.ui.rsyntaxtextarea import RSyntaxTextArea |
26 |
from org.fife.ui.rsyntaxtextarea import SyntaxConstants |
27 |
from org.fife.ui.rtextarea import RTextScrollPane |
28 |
|
29 |
|
30 |
geom_types = ( |
31 |
( "GEOMETRY", Geometry.TYPES.GEOMETRY ),
|
32 |
( "POINT", Geometry.TYPES.POINT ),
|
33 |
( "LINE", Geometry.TYPES.LINE ),
|
34 |
( "POLYGON", Geometry.TYPES.POLYGON ),
|
35 |
( "MULTIPOINT", Geometry.TYPES.MULTIPOINT ),
|
36 |
( "MULTILINE", Geometry.TYPES.MULTICURVE ),
|
37 |
( "MULTIPOLYGON", Geometry.TYPES.MULTISURFACE )
|
38 |
) |
39 |
|
40 |
geom_subtypes = ( |
41 |
( "2D", Geometry.SUBTYPES.GEOM2D ),
|
42 |
( "2DM", Geometry.SUBTYPES.GEOM2DM ),
|
43 |
( "3D", Geometry.SUBTYPES.GEOM3D ),
|
44 |
( "3DM", Geometry.SUBTYPES.GEOM3DM )
|
45 |
) |
46 |
|
47 |
|
48 |
class SQLConsole(FormPanel): |
49 |
|
50 |
def __init__(self): |
51 |
FormPanel.__init__(self, script.getResource("sqlconsole.xml")) |
52 |
self.featureTable = None |
53 |
self.currentStore = None |
54 |
|
55 |
dataManager = DALLocator.getDataManager() |
56 |
pool = dataManager.getDataServerExplorerPool() |
57 |
for entry in pool.iterator(): |
58 |
if isinstance(entry.getExplorerParameters(), JDBCServerExplorerParameters ) : |
59 |
self.cboBaseDeDatos.addItem(entry.getName())
|
60 |
|
61 |
self.sqlContainer.setLayout(BorderLayout())
|
62 |
self.sqlContainer.add(self.getSQLTextarea(), BorderLayout.CENTER) |
63 |
self.sqlContainer.updateUI()
|
64 |
|
65 |
self.cboGeomType.addItem("") |
66 |
for geom_type in geom_types: |
67 |
self.cboGeomType.addItem(geom_type[0]) |
68 |
|
69 |
self.cboGeomSubtype.addItem("") |
70 |
for geom_subtype in geom_subtypes: |
71 |
self.cboGeomSubtype.addItem(geom_subtype[0]) |
72 |
|
73 |
|
74 |
def show(self): |
75 |
self.showWindow("SQL Console") |
76 |
|
77 |
def getExplorer(self): |
78 |
dataManager = DALLocator.getDataManager() |
79 |
pool = dataManager.getDataServerExplorerPool() |
80 |
name = self.cboBaseDeDatos.getSelectedItem()
|
81 |
params = pool.get(name).getExplorerParameters() |
82 |
explorer = dataManager.openServerExplorer(params.getExplorerName(),params) |
83 |
return explorer
|
84 |
|
85 |
def getMessageStack(self,ex): |
86 |
msgs = ""
|
87 |
lastmsg = None
|
88 |
while ex != None: |
89 |
msg = ex.getMessage() |
90 |
if msg!=None and lastmsg != msg: |
91 |
lastmsg = msg |
92 |
msgs += "\n - " + msg
|
93 |
ex = ex.getCause() |
94 |
return msgs
|
95 |
|
96 |
def getSQLTextarea(self): |
97 |
self.txtSQL = RSyntaxTextArea(6,30) |
98 |
self.txtSQL.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SQL)
|
99 |
self.txtSQL.setCodeFoldingEnabled(True) |
100 |
self.txtSQL.setClearWhitespaceLinesEnabled(True) |
101 |
self.txtSQL.setAutoIndentEnabled(True) |
102 |
self.txtSQL.setCloseCurlyBraces(True) |
103 |
self.txtSQL.setWhitespaceVisible(True) |
104 |
self.txtSQL.setAnimateBracketMatching(True) |
105 |
self.txtSQL.setBracketMatchingEnabled(True) |
106 |
self.txtSQL.setAutoIndentEnabled(True) |
107 |
self.txtSQL.setTabsEmulated(True) |
108 |
self.txtSQL.setTabSize(2) |
109 |
self.txtSQL.setAntiAliasingEnabled(True) |
110 |
self.txtSQL.setText("select * from municipios_navarra") |
111 |
return RTextScrollPane(self.txtSQL) |
112 |
|
113 |
|
114 |
def btnEjecutar_click(self, *args): |
115 |
explorer = self.getExplorer()
|
116 |
openParams = explorer.getOpenParameters() |
117 |
openParams.setTable("--")
|
118 |
openParams.setSQL(self.txtSQL.getText())
|
119 |
try:
|
120 |
self.currentStore = explorer.open(openParams)
|
121 |
except Throwable, ex:
|
122 |
self.txtMensajes.setText(self.getMessageStack(ex)) |
123 |
if self.featureTable!=None: |
124 |
self.featureTable.setVisible(False) |
125 |
self.tabSalida.setSelectedIndex(1) |
126 |
self.currentStore = None |
127 |
return
|
128 |
if self.featureTable == None: |
129 |
tablePanel = FeatureTablePanel(self.currentStore)
|
130 |
self.tableContainer.setLayout(BorderLayout())
|
131 |
self.tableContainer.add(tablePanel, BorderLayout.CENTER)
|
132 |
self.featureTable = tablePanel.getTable()
|
133 |
self.tableContainer.updateUI()
|
134 |
else:
|
135 |
tableModel = ConfigurableFeatureTableModel(self.currentStore, None) |
136 |
self.featureTable.setModel(tableModel)
|
137 |
|
138 |
self.cboCampoGeometria.removeAllItems()
|
139 |
self.cboClavePrimaria.removeAllItems()
|
140 |
self.cboClavePrimaria.addItem("") |
141 |
|
142 |
columNames = list()
|
143 |
ft = self.currentStore.getDefaultFeatureType()
|
144 |
for at in ft.getAttributeDescriptors(): |
145 |
name = at.getName() |
146 |
columNames.append(name) |
147 |
self.cboCampoGeometria.addItem(name)
|
148 |
self.cboClavePrimaria.addItem(name)
|
149 |
|
150 |
|
151 |
self.featureTable.getModel().setVisibleColumns(columNames)
|
152 |
self.cboCampoGeometria.setSelectedItem(ft.getDefaultGeometryAttributeName())
|
153 |
if not ft.getDefaultGeometryAttribute() in ("",None): |
154 |
gt = ft.getDefaultGeometryAttribute().getGeomType() |
155 |
n = 0
|
156 |
for v in geom_types: |
157 |
if v[1] == gt.getType(): |
158 |
self.cboGeomType.setSelectedIndex(n)
|
159 |
break
|
160 |
n+=1
|
161 |
n = 0
|
162 |
for v in geom_subtypes: |
163 |
if v[1] == gt.getSubType(): |
164 |
self.cboGeomSubtype.setSelectedIndex(n+1) |
165 |
break
|
166 |
n+=1
|
167 |
|
168 |
self.txtMensajes.setText("") |
169 |
self.featureTable.setVisible(True) |
170 |
self.tabSalida.setSelectedIndex(0) |
171 |
|
172 |
|
173 |
def btnAdministrar_click(self, *args): |
174 |
dialog = VectorialDBConnectionParamsDialog() |
175 |
dialog.showDialog() |
176 |
|
177 |
def btnCargar_click(self, *args): |
178 |
cargarComo = str(self.cboCargarComo.getSelectedItem()) |
179 |
if "tabla" in cargarComo.lower(): |
180 |
application = ApplicationLocator.getManager() |
181 |
projectManager = application.getProjectManager() |
182 |
tableDoc = projectManager.createDocument(TableManager.TYPENAME) |
183 |
tableDoc.setStore(self.featureTable.getModel().getFeatureStore())
|
184 |
name = inputbox("Nombre del documento tabla", "Nombre", QUESTION, "Sql") |
185 |
tableDoc.setName(name) |
186 |
project = application.getCurrentProject() |
187 |
project.addDocument(tableDoc) |
188 |
else:
|
189 |
view = currentView() |
190 |
if view == None: |
191 |
msgbox("Debera haber una vista activa para realizar esta accion")
|
192 |
return
|
193 |
name = inputbox("Nombre de la capa", "Nombre", QUESTION, "Sql") |
194 |
layer = MapContextLocator.getMapContextManager().createLayer( |
195 |
name, |
196 |
self.featureTable.getModel().getFeatureStore()
|
197 |
) |
198 |
layers = view.getMapContext().getLayers().addLayer(layer) |
199 |
|
200 |
|
201 |
def cboClavePrimaria_click(self, *args): |
202 |
name = self.cboClavePrimaria.getSelectedItem()
|
203 |
if name in ("",None): |
204 |
return
|
205 |
if self.currentStore == None: |
206 |
return
|
207 |
ft = self.currentStore.getDefaultFeatureType().getEditable()
|
208 |
attr = ft.get(name) |
209 |
if attr == None: |
210 |
print "???? no encuentro la descripcion del atributo '%s'." % name |
211 |
return
|
212 |
attr.setIsPrimaryKey(True)
|
213 |
self.currentStore.edit()
|
214 |
self.currentStore.update(ft)
|
215 |
self.currentStore.finishEditing()
|
216 |
|
217 |
def getGeomTypeByName(self,name): |
218 |
for v in geom_types: |
219 |
if v[0].lower() == name.lower(): |
220 |
return v[1] |
221 |
return Geometry.TYPES.GEOMETRY
|
222 |
|
223 |
def getGeomSubtypeByName(self,name): |
224 |
for v in geom_subtypes: |
225 |
if v[0].lower() == name.lower(): |
226 |
return v[1] |
227 |
return Geometry.SUBTYPES.GEOM2D
|
228 |
|
229 |
def cboGeomType_click(self,*args): |
230 |
geom_type_name = self.cboGeomType.getSelectedItem()
|
231 |
geom_subtype_name = self.cboGeomSubtype.getSelectedItem()
|
232 |
if geom_type_name in ("",None): |
233 |
return
|
234 |
geom_type = GeometryLocator.getGeometryManager().getGeometryType( |
235 |
self.getGeomTypeByName(geom_type_name),
|
236 |
self.getGeomSubtypeByName(geom_subtype_name)
|
237 |
) |
238 |
ft = self.currentStore.getDefaultFeatureType().getEditable()
|
239 |
attr = ft.getDefaultGeometryAttribute() |
240 |
if attr == None: |
241 |
return
|
242 |
attr.setGeometryType(geom_type) |
243 |
self.currentStore.edit()
|
244 |
self.currentStore.update(ft)
|
245 |
self.currentStore.finishEditing()
|
246 |
|
247 |
def cboGeomSubtype_click(self,*args): |
248 |
self.cboGeomType_click(*args)
|
249 |
|
250 |
|
251 |
def main(*args): |
252 |
sqlconsole = SQLConsole() |
253 |
sqlconsole.show() |
254 |
|