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