Skip to content

Commit 64b532a

Browse files
author
g_j_m
committed
Do one less query per table column when loading a postgres layer.
Tidy up of sql lines too. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6684 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 2e3571a commit 64b532a

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/providers/postgres/qgspostgresprovider.cpp

+11-17
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ QgsPostgresProvider::QgsPostgresProvider(QString const & uri)
170170
getFeatureCount();
171171

172172
// Get the relation oid for use in later queries
173-
sql = "select oid from pg_class where relname = '" + mTableName + "' and relnamespace = ("
174-
"select oid from pg_namespace where nspname = '" + mSchemaName + "')";
173+
sql = "SELECT oid FROM pg_class WHERE relname = '" + mTableName + "' AND relnamespace = ("
174+
"SELECT oid FROM pg_namespace WHERE nspname = '" + mSchemaName + "')";
175175
PGresult *tresult= PQexec(pd, (const char *)(sql.utf8()));
176176
QString tableoid = PQgetvalue(tresult, 0, 0);
177177
PQclear(tresult);
@@ -190,7 +190,7 @@ to the rest of qgis...
190190

191191
// Populate the field vector for this layer. The field vector contains
192192
// field name, type, length, and precision (if numeric)
193-
sql = "select * from " + mSchemaTableName + " limit 1";
193+
sql = "select * from " + mSchemaTableName + " limit 0";
194194

195195
PGresult *result = PQexec(pd, (const char *) (sql.utf8()));
196196
//--std::cout << "Field: Name, Type, Size, Modifier:" << std::endl;
@@ -206,30 +206,24 @@ to the rest of qgis...
206206
int fieldModifier = PQfmod(result, i);
207207
QString fieldComment("");
208208

209-
sql = "select typelem from pg_type where typelem = " + typOid + " and typlen = -1";
210-
// //--std::cout << sql << std::endl;
211-
PGresult *oidResult = PQexec(pd, (const char *) sql);
212-
// get the oid of the "real" type
213-
QString poid = PQgetvalue(oidResult, 0, PQfnumber(oidResult, "typelem"));
214-
PQclear(oidResult);
209+
sql = "SELECT typname, typlen FROM pg_type WHERE "
210+
"oid = (SELECT typelem FROM pg_type WHERE "
211+
"typelem = " + typOid + " AND typlen = -1)";
215212

216-
sql = "select typname, typlen from pg_type where oid = " + poid;
217-
// //--std::cout << sql << std::endl;
218-
oidResult = PQexec(pd, (const char *) sql);
213+
PGresult* oidResult = PQexec(pd, (const char *) sql);
219214
QString fieldType = PQgetvalue(oidResult, 0, 0);
220215
QString fieldSize = PQgetvalue(oidResult, 0, 1);
221216
PQclear(oidResult);
222217

223-
sql = "select attnum from pg_attribute where attrelid = " + tableoid + " and attname = '" + fieldName + "'";
218+
sql = "SELECT attnum FROM pg_attribute WHERE "
219+
"attrelid = " + tableoid + " AND attname = '" + fieldName + "'";
224220
PGresult *tresult = PQexec(pd, (const char *)(sql.utf8()));
225221
QString attnum = PQgetvalue(tresult, 0, 0);
226222
PQclear(tresult);
227223

228-
sql = "SELECT description FROM pg_description WHERE objoid = " + tableoid +
229-
" AND objsubid = " + attnum;
230-
224+
sql = "SELECT description FROM pg_description WHERE "
225+
"objoid = " + tableoid + " AND objsubid = " + attnum;
231226
tresult = PQexec(pd, (const char*)(sql.utf8()));
232-
233227
if (PQntuples(tresult) > 0)
234228
fieldComment = PQgetvalue(tresult, 0, 0);
235229
PQclear(tresult);

0 commit comments

Comments
 (0)