-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgspostgresdataitems.cpp
230 lines (193 loc) · 6.49 KB
/
qgspostgresdataitems.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "qgspostgresdataitems.h"
#include "qgslogger.h"
#include "qgspostgresconnection.h"
#include "qgspgsourceselect.h"
#include "qgspgnewconnection.h"
// ---------------------------------------------------------------------------
QgsPGConnectionItem::QgsPGConnectionItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QIcon( getThemePixmap( "mIconConnect.png" ) );
}
QgsPGConnectionItem::~QgsPGConnectionItem()
{
}
QVector<QgsDataItem*> QgsPGConnectionItem::createChildren()
{
QgsDebugMsg( "Entered" );
QVector<QgsDataItem*> children;
QgsPostgresConnection connection( mName );
QgsPostgresProvider *pgProvider = connection.provider( );
if ( !pgProvider )
return children;
QString mConnInfo = connection.connectionInfo();
QgsDebugMsg( "mConnInfo = " + mConnInfo );
if ( !pgProvider->supportedLayers( mLayerProperties, true, false, false ) )
{
children.append( new QgsErrorItem( this, tr( "Failed to retrieve layers" ), mPath + "/error" ) );
return children;
}
QMap<QString, QVector<QgsPostgresLayerProperty> > schemasMap;
foreach( QgsPostgresLayerProperty layerProperty, mLayerProperties )
{
schemasMap[ layerProperty.schemaName ].push_back( layerProperty );
}
QMap<QString, QVector<QgsPostgresLayerProperty> >::const_iterator it = schemasMap.constBegin();
for ( ; it != schemasMap.constEnd(); it++ )
{
QgsDebugMsg( "schema: " + it.key() );
QgsPGSchemaItem * schema = new QgsPGSchemaItem( this, it.key(), mPath + "/" + it.key(), mConnInfo, it.value() );
children.append( schema );
}
return children;
}
bool QgsPGConnectionItem::equal( const QgsDataItem *other )
{
if ( type() != other->type() )
{
return false;
}
const QgsPGConnectionItem *o = dynamic_cast<const QgsPGConnectionItem *>( other );
return ( mPath == o->mPath && mName == o->mName && mConnInfo == o->mConnInfo );
}
QList<QAction*> QgsPGConnectionItem::actions()
{
QList<QAction*> lst;
QAction* actionEdit = new QAction( tr( "Edit..." ), this );
connect( actionEdit, SIGNAL( triggered() ), this, SLOT( editConnection() ) );
lst.append( actionEdit );
QAction* actionDelete = new QAction( tr( "Delete" ), this );
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteConnection() ) );
lst.append( actionDelete );
return lst;
}
void QgsPGConnectionItem::editConnection()
{
QgsPgNewConnection nc( NULL, mName );
if ( nc.exec() )
{
// the parent should be updated
mParent->refresh();
}
}
void QgsPGConnectionItem::deleteConnection()
{
QgsPgSourceSelect::deleteConnection( mName );
// the parent should be updated
mParent->refresh();
}
// ---------------------------------------------------------------------------
QgsPGLayerItem::QgsPGLayerItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QgsLayerItem::LayerType layerType, QgsPostgresLayerProperty layerProperty )
: QgsLayerItem( parent, name, path, QString(), layerType, "postgres" ),
mConnInfo( connInfo ),
mLayerProperty( layerProperty )
{
mUri = createUri();
mPopulated = true;
}
QgsPGLayerItem::~QgsPGLayerItem()
{
}
QString QgsPGLayerItem::createUri()
{
QString pkColName = mLayerProperty.pkCols.size() > 0 ? mLayerProperty.pkCols.at( 0 ) : QString::null;
QgsDataSourceURI uri( mConnInfo );
uri.setDataSource( mLayerProperty.schemaName, mLayerProperty.tableName, mLayerProperty.geometryColName, mLayerProperty.sql, pkColName );
return uri.uri();
}
// ---------------------------------------------------------------------------
QgsPGSchemaItem::QgsPGSchemaItem( QgsDataItem* parent, QString name, QString path, QString connInfo, QVector<QgsPostgresLayerProperty> layerProperties )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QIcon( getThemePixmap( "mIconDbSchema.png" ) );
mConnInfo = connInfo;
mLayerProperties = layerProperties;
populate();
}
QVector<QgsDataItem*> QgsPGSchemaItem::createChildren()
{
QVector<QgsDataItem*> children;
// Populate everything, it costs nothing, all info about layers is collected
foreach( QgsPostgresLayerProperty layerProperty, mLayerProperties )
{
QgsDebugMsg( "table: " + layerProperty.schemaName + "." + layerProperty.tableName );
QgsLayerItem::LayerType layerType = QgsLayerItem::NoType;
if ( layerProperty.type.contains( "POINT" ) )
{
layerType = QgsLayerItem::Point;
}
else if ( layerProperty.type.contains( "LINE" ) )
{
layerType = QgsLayerItem::Line;
}
else if ( layerProperty.type.contains( "POLYGON" ) )
{
layerType = QgsLayerItem::Polygon;
}
else if ( layerProperty.type == QString::null )
{
if ( layerProperty.geometryColName == QString::null )
{
layerType = QgsLayerItem::TableLayer;
}
}
QgsPGLayerItem * layer = new QgsPGLayerItem( this, layerProperty.tableName, mPath + "/" + layerProperty.tableName, mConnInfo, layerType, layerProperty );
children.append( layer );
}
return children;
}
QgsPGSchemaItem::~QgsPGSchemaItem()
{
}
// ---------------------------------------------------------------------------
QgsPGRootItem::QgsPGRootItem( QgsDataItem* parent, QString name, QString path )
: QgsDataCollectionItem( parent, name, path )
{
mIcon = QIcon( getThemePixmap( "mIconPostgis.png" ) );
populate();
}
QgsPGRootItem::~QgsPGRootItem()
{
}
QVector<QgsDataItem*>QgsPGRootItem::createChildren()
{
QVector<QgsDataItem*> connections;
foreach( QString connName, QgsPostgresConnection::connectionList() )
{
QgsDataItem * conn = new QgsPGConnectionItem( this, connName, mPath + "/" + connName );
connections.push_back( conn );
}
return connections;
}
QList<QAction*> QgsPGRootItem::actions()
{
QList<QAction*> lst;
QAction* actionNew = new QAction( tr( "New..." ), this );
connect( actionNew, SIGNAL( triggered() ), this, SLOT( newConnection() ) );
lst.append( actionNew );
return lst;
}
QWidget * QgsPGRootItem::paramWidget()
{
QgsPgSourceSelect *select = new QgsPgSourceSelect( 0, 0, true, true );
connect( select, SIGNAL( connectionsChanged() ), this, SLOT( connectionsChanged() ) );
return select;
}
void QgsPGRootItem::connectionsChanged()
{
refresh();
}
void QgsPGRootItem::newConnection()
{
QgsPgNewConnection nc( NULL );
if ( nc.exec() )
{
refresh();
}
}
// ---------------------------------------------------------------------------
QGISEXTERN QgsDataItem * dataItem( QString thePath, QgsDataItem* parentItem )
{
Q_UNUSED( thePath );
return new QgsPGRootItem( parentItem, "PostGIS", "pg:" );
}