25
25
#include " qgsnewnamedialog.h"
26
26
27
27
#include < QMessageBox>
28
+ #include < QInputDialog>
28
29
#include < QProgressDialog>
29
30
#include < climits>
30
31
@@ -98,17 +99,29 @@ QList<QAction*> QgsPGConnectionItem::actions()
98
99
{
99
100
QList<QAction*> lst;
100
101
101
- QAction* actionEdit = new QAction ( tr ( " Edit..." ), this );
102
+ QAction* actionRefresh = new QAction ( tr ( " Refresh" ), this );
103
+ connect ( actionRefresh, SIGNAL ( triggered () ), this , SLOT ( refreshConnection () ) );
104
+ lst.append ( actionRefresh );
105
+
106
+ QAction* separator = new QAction ( this );
107
+ separator->setSeparator ( true );
108
+ lst.append ( separator );
109
+
110
+ QAction* actionEdit = new QAction ( tr ( " Edit Connection..." ), this );
102
111
connect ( actionEdit, SIGNAL ( triggered () ), this , SLOT ( editConnection () ) );
103
112
lst.append ( actionEdit );
104
113
105
- QAction* actionDelete = new QAction ( tr ( " Delete" ), this );
114
+ QAction* actionDelete = new QAction ( tr ( " Delete Connection " ), this );
106
115
connect ( actionDelete, SIGNAL ( triggered () ), this , SLOT ( deleteConnection () ) );
107
116
lst.append ( actionDelete );
108
117
109
- QAction* actionRefresh = new QAction ( tr ( " Refresh" ), this );
110
- connect ( actionRefresh, SIGNAL ( triggered () ), this , SLOT ( refreshConnection () ) );
111
- lst.append ( actionRefresh );
118
+ QAction* separator2 = new QAction ( this );
119
+ separator2->setSeparator ( true );
120
+ lst.append ( separator2 );
121
+
122
+ QAction* actionCreateSchema = new QAction ( tr ( " Create Schema..." ), this );
123
+ connect ( actionCreateSchema, SIGNAL ( triggered () ), this , SLOT ( createSchema () ) );
124
+ lst.append ( actionCreateSchema );
112
125
113
126
return lst;
114
127
}
@@ -143,6 +156,36 @@ void QgsPGConnectionItem::refreshConnection()
143
156
refresh ();
144
157
}
145
158
159
+ void QgsPGConnectionItem::createSchema ()
160
+ {
161
+ QString schemaName = QInputDialog::getText ( 0 , tr ( " Create Schema" ), tr ( " Shema name:" ) );
162
+ if ( schemaName.isEmpty () )
163
+ return ;
164
+
165
+ QgsDataSourceURI uri = QgsPostgresConn::connUri ( mName );
166
+ QgsPostgresConn *conn = QgsPostgresConn::connectDb ( uri.connectionInfo (), false );
167
+ if ( !conn )
168
+ {
169
+ QMessageBox::warning ( 0 , tr ( " Create Schema" ), tr ( " Unable to create schema." ) );
170
+ return ;
171
+ }
172
+
173
+ // create the schema
174
+ QString sql = QString ( " CREATE SCHEMA %1" ).arg ( QgsPostgresConn::quotedIdentifier ( schemaName ) );
175
+
176
+ QgsPostgresResult result = conn->PQexec ( sql );
177
+ if ( result.PQresultStatus () != PGRES_COMMAND_OK )
178
+ {
179
+ QMessageBox::warning ( 0 , tr ( " Create Schema" ), tr ( " Unable to create schema %1\n %2" ).arg ( schemaName )
180
+ .arg ( result.PQresultErrorMessage () ) );
181
+ conn->unref ();
182
+ return ;
183
+ }
184
+
185
+ conn->unref ();
186
+ refresh ();
187
+ }
188
+
146
189
bool QgsPGConnectionItem::handleDrop ( const QMimeData * data, Qt::DropAction )
147
190
{
148
191
if ( !QgsMimeDataUtils::isUriList ( data ) )
@@ -235,20 +278,29 @@ QList<QAction*> QgsPGLayerItem::actions()
235
278
{
236
279
QList<QAction*> lst;
237
280
238
- QAction* actionRenameLayer = new QAction ( tr ( " Rename Layer..." ), this );
281
+ QString typeName = mLayerProperty .isView ? tr ( " View" ) : tr ( " Table" );
282
+
283
+ QAction* actionRenameLayer = new QAction ( tr ( " Rename %1..." ).arg ( typeName ), this );
239
284
connect ( actionRenameLayer, SIGNAL ( triggered () ), this , SLOT ( renameLayer () ) );
240
285
lst.append ( actionRenameLayer );
241
286
242
- QAction* actionDeleteLayer = new QAction ( tr ( " Delete Layer " ), this );
287
+ QAction* actionDeleteLayer = new QAction ( tr ( " Delete %1 " ). arg ( typeName ), this );
243
288
connect ( actionDeleteLayer, SIGNAL ( triggered () ), this , SLOT ( deleteLayer () ) );
244
289
lst.append ( actionDeleteLayer );
245
290
291
+ if ( !mLayerProperty .isView )
292
+ {
293
+ QAction* actionTruncateLayer = new QAction ( tr ( " Truncate %1" ).arg ( typeName ), this );
294
+ connect ( actionTruncateLayer, SIGNAL ( triggered () ), this , SLOT ( truncateTable () ) );
295
+ lst.append ( actionTruncateLayer );
296
+ }
297
+
246
298
return lst;
247
299
}
248
300
249
301
void QgsPGLayerItem::deleteLayer ()
250
302
{
251
- if ( QMessageBox::question ( 0 , QObject::tr ( " Delete Object " ),
303
+ if ( QMessageBox::question ( 0 , QObject::tr ( " Delete Table " ),
252
304
QObject::tr ( " Are you sure you want to delete %1.%2?" ).arg ( mLayerProperty .schemaName ).arg ( mLayerProperty .tableName ),
253
305
QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
254
306
return ;
@@ -257,24 +309,26 @@ void QgsPGLayerItem::deleteLayer()
257
309
bool res = ::deleteLayer ( mUri , errCause );
258
310
if ( !res )
259
311
{
260
- QMessageBox::warning ( 0 , tr ( " Delete Layer " ), errCause );
312
+ QMessageBox::warning ( 0 , tr ( " Delete Table " ), errCause );
261
313
}
262
314
else
263
315
{
264
- QMessageBox::information ( 0 , tr ( " Delete Layer " ), tr ( " Layer deleted successfully." ) );
316
+ QMessageBox::information ( 0 , tr ( " Delete Table " ), tr ( " Table deleted successfully." ) );
265
317
if ( mParent )
266
318
mParent ->refresh ();
267
319
}
268
320
}
269
321
270
322
void QgsPGLayerItem::renameLayer ()
271
323
{
272
- QgsNewNameDialog dlg ( tr ( " layer %1.%2" ).arg ( mLayerProperty .schemaName ).arg ( mLayerProperty .tableName ), mLayerProperty .tableName );
273
- dlg.setWindowTitle ( tr ( " Rename Layer" ) );
324
+ QString typeName = mLayerProperty .isView ? tr ( " View" ) : tr ( " Table" );
325
+ QString lowerTypeName = mLayerProperty .isView ? tr ( " view" ) : tr ( " table" );
326
+
327
+ QgsNewNameDialog dlg ( tr ( " %1 %2.%3" ).arg ( lowerTypeName ).arg ( mLayerProperty .schemaName ).arg ( mLayerProperty .tableName ), mLayerProperty .tableName );
328
+ dlg.setWindowTitle ( tr ( " Rename %1" ).arg ( typeName ) );
274
329
if ( dlg.exec () != QDialog::Accepted || dlg.name () == mLayerProperty .tableName )
275
330
return ;
276
331
277
-
278
332
QString schemaName = mLayerProperty .schemaName ;
279
333
QString tableName = mLayerProperty .tableName ;
280
334
QString schemaTableName;
@@ -289,28 +343,75 @@ void QgsPGLayerItem::renameLayer()
289
343
QgsPostgresConn *conn = QgsPostgresConn::connectDb ( dsUri.connectionInfo (), false );
290
344
if ( !conn )
291
345
{
292
- QMessageBox::warning ( 0 , tr ( " Rename Layer " ), tr ( " Unable to rename layer. " ) );
346
+ QMessageBox::warning ( 0 , tr ( " Rename %1 " ). arg ( typeName ) , tr ( " Unable to rename %1. " ). arg ( lowerTypeName ) );
293
347
return ;
294
348
}
295
349
296
350
// rename the layer
297
- QString sql = QString ( " ALTER TABLE %1 RENAME TO %2" ).arg ( oldName ).arg ( newName );
351
+ QString sql;
352
+ if ( mLayerProperty .isView )
353
+ {
354
+ sql = QString ( " ALTER %1 VIEW %2 RENAME TO %3" ).arg ( mLayerProperty .relKind == " m" ? QString ( " MATERIALIZED" ) : QString () )
355
+ .arg ( oldName ).arg ( newName );
356
+ }
357
+ else
358
+ {
359
+ sql = QString ( " ALTER TABLE %1 RENAME TO %2" ).arg ( oldName ).arg ( newName );
360
+ }
298
361
299
362
QgsPostgresResult result = conn->PQexec ( sql );
300
363
if ( result.PQresultStatus () != PGRES_COMMAND_OK )
301
364
{
302
- QMessageBox::warning ( 0 , tr ( " Rename Layer " ), tr ( " Unable to rename layer %1 \n %2 " ).arg ( mName )
365
+ QMessageBox::warning ( 0 , tr ( " Rename %1 " ). arg ( typeName ) , tr ( " Unable to rename %1 %2 \n %3 " ). arg ( lowerTypeName ).arg ( mName )
303
366
.arg ( result.PQresultErrorMessage () ) );
304
367
conn->unref ();
305
368
return ;
306
369
}
307
370
308
371
conn->unref ();
309
- QMessageBox::information ( 0 , tr ( " Rename Layer" ), tr ( " Layer renamed successfully." ) );
310
372
if ( mParent )
311
373
mParent ->refresh ();
312
374
}
313
375
376
+ void QgsPGLayerItem::truncateTable ()
377
+ {
378
+ if ( QMessageBox::question ( 0 , QObject::tr ( " Truncate Table" ),
379
+ QObject::tr ( " Are you sure you want to truncate %1.%2?\n\n This will delete all data within the table." ).arg ( mLayerProperty .schemaName ).arg ( mLayerProperty .tableName ),
380
+ QMessageBox::Yes | QMessageBox::No, QMessageBox::No ) != QMessageBox::Yes )
381
+ return ;
382
+
383
+ QgsDataSourceURI dsUri ( mUri );
384
+ QgsPostgresConn *conn = QgsPostgresConn::connectDb ( dsUri.connectionInfo (), false );
385
+ if ( !conn )
386
+ {
387
+ QMessageBox::warning ( 0 , tr ( " Truncate Table" ), tr ( " Unable to truncate table." ) );
388
+ return ;
389
+ }
390
+
391
+ QString schemaName = mLayerProperty .schemaName ;
392
+ QString tableName = mLayerProperty .tableName ;
393
+ QString schemaTableName;
394
+ if ( !schemaName.isEmpty () )
395
+ {
396
+ schemaTableName = QgsPostgresConn::quotedIdentifier ( schemaName ) + " ." ;
397
+ }
398
+ QString tableRef = schemaTableName + QgsPostgresConn::quotedIdentifier ( tableName );
399
+
400
+ QString sql = QString ( " TRUNCATE TABLE %1" ).arg ( tableRef );
401
+
402
+ QgsPostgresResult result = conn->PQexec ( sql );
403
+ if ( result.PQresultStatus () != PGRES_COMMAND_OK )
404
+ {
405
+ QMessageBox::warning ( 0 , tr ( " Truncate Table" ), tr ( " Unable to truncate %1\n %2" ).arg ( mName )
406
+ .arg ( result.PQresultErrorMessage () ) );
407
+ conn->unref ();
408
+ return ;
409
+ }
410
+
411
+ conn->unref ();
412
+ QMessageBox::information ( 0 , tr ( " Truncate Table" ), tr ( " Table truncated successfully." ) );
413
+ }
414
+
314
415
QString QgsPGLayerItem::createUri ()
315
416
{
316
417
QString pkColName = mLayerProperty .pkCols .size () > 0 ? mLayerProperty .pkCols .at ( 0 ) : QString::null;
0 commit comments