Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsdatasourceuri.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdatasourceuri.h - Structure to contain the component parts
3  of a data source URI
4  -------------------
5  begin : Dec 5, 2004
6  copyright : (C) 2004 by Gary E.Sherman
7  email : sherman at mrcc.com
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 /* $Id: qgsdatasourceuri.h 5839 2006-09-19 18:04:21Z wonder $ */
19 
20 #include "qgsdatasourceuri.h"
21 #include "qgslogger.h"
22 
23 #include <QStringList>
24 #include <QRegExp>
25 
26 QgsDataSourceURI::QgsDataSourceURI() : mSSLmode( SSLprefer ), mKeyColumn( "" ), mUseEstimatedMetadata( false )
27 {
28  // do nothing
29 }
30 
31 QgsDataSourceURI::QgsDataSourceURI( QString uri ) : mSSLmode( SSLprefer ), mKeyColumn( "" ), mUseEstimatedMetadata( false )
32 {
33  int i = 0;
34  while ( i < uri.length() )
35  {
36  skipBlanks( uri, i );
37 
38  if ( uri[i] == '=' )
39  {
40  QgsDebugMsg( "parameter name expected before =" );
41  i++;
42  continue;
43  }
44 
45  int start = i;
46 
47  while ( i < uri.length() && uri[i] != '=' && !uri[i].isSpace() )
48  i++;
49 
50  QString pname = uri.mid( start, i - start );
51 
52  skipBlanks( uri, i );
53 
54  if ( uri[i] != '=' )
55  {
56  QgsDebugMsg( "= expected after parameter name" );
57  return;
58  }
59 
60  i++;
61 
62  if ( pname == "sql" )
63  {
64  // rest of line is a sql where clause
65  skipBlanks( uri, i );
66  mSql = uri.mid( i );
67  break;
68  }
69  else
70  {
71  QString pval = getValue( uri, i );
72 
73  if ( pname == "table" )
74  {
75  if ( uri[i] == '.' )
76  {
77  i++;
78 
79  mSchema = pval;
80  mTable = getValue( uri, i );
81  }
82  else
83  {
84  mSchema = "";
85  mTable = pval;
86  }
87 
88  if ( uri[i] == '(' )
89  {
90  i++;
91 
92  int start = i;
93  QString col;
94  while ( i < uri.length() && uri[i] != ')' )
95  i++;
96 
97  if ( i == uri.length() )
98  {
99  QgsDebugMsg( "closing parenthesis missing" );
100  }
101 
102  mGeometryColumn = uri.mid( start, i - start );
103 
104  i++;
105  }
106  else
107  {
108  mGeometryColumn = QString::null;
109  }
110  }
111  else if ( pname == "key" )
112  {
113  mKeyColumn = pval;
114  }
115  else if ( pname == "estimatedmetadata" )
116  {
117  mUseEstimatedMetadata = pval == "true";
118  }
119  else if ( pname == "service" )
120  {
121  mService = pval;
122  }
123  else if ( pname == "user" )
124  {
125  mUsername = pval;
126  }
127  else if ( pname == "password" )
128  {
129  mPassword = pval;
130  }
131  else if ( pname == "connect_timeout" )
132  {
133  QgsDebugMsg( "connection timeout ignored" );
134  }
135  else if ( pname == "dbname" )
136  {
137  mDatabase = pval;
138  }
139  else if ( pname == "host" )
140  {
141  mHost = pval;
142  }
143  else if ( pname == "hostaddr" )
144  {
145  QgsDebugMsg( "database host ip address ignored" );
146  }
147  else if ( pname == "port" )
148  {
149  mPort = pval;
150  }
151  else if ( pname == "tty" )
152  {
153  QgsDebugMsg( "backend debug tty ignored" );
154  }
155  else if ( pname == "options" )
156  {
157  QgsDebugMsg( "backend debug options ignored" );
158  }
159  else if ( pname == "sslmode" )
160  {
161  if ( pval == "disable" )
163  else if ( pval == "allow" )
164  mSSLmode = SSLallow;
165  else if ( pval == "prefer" )
167  else if ( pval == "require" )
169  }
170  else if ( pname == "requiressl" )
171  {
172  if ( pval == "0" )
174  else
176  }
177  else if ( pname == "krbsrvname" )
178  {
179  QgsDebugMsg( "kerberos server name ignored" );
180  }
181  else if ( pname == "gsslib" )
182  {
183  QgsDebugMsg( "gsslib ignored" );
184  }
185  else
186  {
187  QgsDebugMsg( "invalid connection option \"" + pname + "\" ignored" );
188  }
189  }
190  }
191 }
192 
193 QString QgsDataSourceURI::removePassword( const QString& aUri )
194 {
195  QRegExp regexp;
196  regexp.setMinimal( true );
197  QString safeName( aUri );
198  if ( aUri.contains( " password=" ) )
199  {
200  regexp.setPattern( " password=.* " );
201  safeName.replace( regexp, " " );
202  }
203  else if ( aUri.contains( ",password=" ) )
204  {
205  regexp.setPattern( ",password=.*," );
206  safeName.replace( regexp, "," );
207  }
208  else if ( aUri.contains( "IDB:" ) )
209  {
210  regexp.setPattern( " pass=.* " );
211  safeName.replace( regexp, " " );
212  }
213  else if (( aUri.contains( "OCI:" ) )
214  || ( aUri.contains( "ODBC:" ) ) )
215  {
216  regexp.setPattern( "/.*@" );
217  safeName.replace( regexp, "/@" );
218  }
219  else if ( aUri.contains( "SDE:" ) )
220  {
221  QStringList strlist = aUri.split( "," );
222  safeName = strlist[0] + "," + strlist[1] + "," + strlist[2] + "," + strlist[3];
223  }
224  return safeName;
225 }
226 
228 {
229  return mUsername;
230 }
231 
232 void QgsDataSourceURI::setUsername( QString username )
233 {
235 }
236 
238 {
239  return mService;
240 }
241 
242 QString QgsDataSourceURI::host() const
243 {
244  return mHost;
245 }
246 
248 {
249  return mDatabase;
250 }
251 
253 {
254  return mPassword;
255 }
256 
257 void QgsDataSourceURI::setPassword( QString password )
258 {
260 }
261 
262 QString QgsDataSourceURI::port() const
263 {
264  return mPort;
265 }
266 
268 {
269  return mSSLmode;
270 }
271 
273 {
274  return mSchema;
275 }
276 
277 QString QgsDataSourceURI::table() const
278 {
279  return mTable;
280 }
281 
282 QString QgsDataSourceURI::sql() const
283 {
284  return mSql;
285 }
286 
288 {
289  return mGeometryColumn;
290 }
291 
293 {
294  return mKeyColumn;
295 }
296 
297 void QgsDataSourceURI::setKeyColumn( QString column )
298 {
299  mKeyColumn = column;
300 }
301 
302 
304 {
305  mUseEstimatedMetadata = theFlag;
306 }
307 
309 {
310  return mUseEstimatedMetadata;
311 }
312 
313 void QgsDataSourceURI::setSql( QString sql )
314 {
315  mSql = sql;
316 }
317 
319 {
320  mSchema = "";
321 }
322 
323 QString QgsDataSourceURI::escape( const QString &theVal, QChar delim = '\'' ) const
324 {
325  QString val = theVal;
326 
327  val.replace( "\\", "\\\\" );
328  val.replace( delim, QString( "\\%1" ).arg( delim ) );
329 
330  return val;
331 }
332 
333 void QgsDataSourceURI::skipBlanks( const QString &uri, int &i )
334 {
335  // skip space before value
336  while ( i < uri.length() && uri[i].isSpace() )
337  i++;
338 }
339 
340 QString QgsDataSourceURI::getValue( const QString &uri, int &i )
341 {
342  skipBlanks( uri, i );
343 
344  // Get the parameter value
345  QString pval;
346  if ( uri[i] == '\'' || uri[i] == '"' )
347  {
348  QChar delim = uri[i];
349 
350  i++;
351 
352  // value is quoted
353  for ( ;; )
354  {
355  if ( i == uri.length() )
356  {
357  QgsDebugMsg( "unterminated quoted string in connection info string" );
358  return pval;
359  }
360 
361  if ( uri[i] == '\\' )
362  {
363  i++;
364  if ( i == uri.length() )
365  continue;
366  if ( uri[i] != delim && uri[i] != '\\' )
367  i--;
368  }
369  else if ( uri[i] == delim )
370  {
371  i++;
372  break;
373  }
374 
375  pval += uri[i++];
376  }
377  }
378  else
379  {
380  // value is not quoted
381  while ( i < uri.length() )
382  {
383  if ( uri[i].isSpace() )
384  {
385  // end of value
386  break;
387  }
388 
389  if ( uri[i] == '\\' )
390  {
391  i++;
392  if ( i == uri.length() )
393  break;
394  if ( uri[i] != '\\' && uri[i] != '\'' )
395  i--;
396  }
397 
398  pval += uri[i++];
399  }
400  }
401 
402  skipBlanks( uri, i );
403 
404  return pval;
405 }
406 
408 {
409  QStringList connectionItems;
410 
411  if ( mDatabase != "" )
412  {
413  connectionItems << "dbname='" + escape( mDatabase ) + "'";
414  }
415 
416  if ( mService != "" )
417  {
418  connectionItems << "service='" + escape( mService ) + "'";
419  }
420  else if ( mHost != "" )
421  {
422  connectionItems << "host=" + mHost;
423  if ( mPort != "" )
424  connectionItems << "port=" + mPort;
425  }
426 
427  if ( mUsername != "" )
428  {
429  connectionItems << "user='" + escape( mUsername ) + "'";
430 
431  if ( mPassword != "" )
432  {
433  connectionItems << "password='" + escape( mPassword ) + "'";
434  }
435  }
436 
437  if ( mSSLmode == SSLdisable )
438  connectionItems << "sslmode=disable";
439  else if ( mSSLmode == SSLallow )
440  connectionItems << "sslmode=allow";
441  else if ( mSSLmode == SSLrequire )
442  connectionItems << "sslmode=require";
443 #if 0
444  else if ( mSSLmode == SSLprefer )
445  connectionItems << "sslmode=prefer";
446 #endif
447 
448  return connectionItems.join( " " );
449 }
450 
451 QString QgsDataSourceURI::uri() const
452 {
453  QString theUri = connectionInfo();
454 
455  if ( !mKeyColumn.isEmpty() )
456  {
457  theUri += QString( " key='%1'" ).arg( escape( mKeyColumn ) );
458  }
459 
460  if ( mUseEstimatedMetadata )
461  {
462  theUri += QString( " estimatedmetadata=true" );
463  }
464 
465  theUri += QString( " table=%1%2 sql=%3" )
466  .arg( quotedTablename() )
467  .arg( mGeometryColumn.isNull() ? QString() : QString( " (%1)" ).arg( mGeometryColumn ) )
468  .arg( mSql );
469 
470  return theUri;
471 }
472 
474 {
475  if ( !mSchema.isEmpty() )
476  return QString( "\"%1\".\"%2\"" )
477  .arg( escape( mSchema, '"' ) )
478  .arg( escape( mTable, '"' ) );
479  else
480  return QString( "\"%1\"" )
481  .arg( escape( mTable, '"' ) );
482 }
483 
484 void QgsDataSourceURI::setConnection( const QString &host,
485  const QString &port,
486  const QString &database,
487  const QString &username,
488  const QString &password,
489  SSLmode sslmode )
490 {
491  mHost = host;
493  mPort = port;
496  mSSLmode = sslmode;
497 }
498 
499 void QgsDataSourceURI::setConnection( const QString &service,
500  const QString &database,
501  const QString &username,
502  const QString &password,
503  SSLmode sslmode )
504 {
505  mService = service;
509  mSSLmode = sslmode;
510 }
511 
512 void QgsDataSourceURI::setDataSource( const QString &schema,
513  const QString &table,
514  const QString &geometryColumn,
515  const QString &sql,
516  const QString &keyColumn )
517 {
518  mSchema = schema;
519  mTable = table;
521  mSql = sql;
523 }
524 
525 void QgsDataSourceURI::setDatabase( const QString &database )
526 {
528 }