pg_namedpipes.diff

Patch demonstrating support for non-tcpip postgres connections. - Frank Warmerdam -, 2007-11-18 07:54 PM

Download (7.77 KB)

View differences:

src/app/qgsnewconnection.cpp (working copy)
22 22

  
23 23
#include "qgsnewconnection.h"
24 24
#include "qgscontexthelp.h"
25
#include "qgslogger.h"
26

  
25 27
extern "C"
26 28
{
27 29
#include <libpq-fe.h>
......
104 106
  password.replace('\\', "\\\\");
105 107
  password.replace('\'', "\\'");
106 108

  
107
  QString connInfo =
108
    "host=" + txtHost->text() + 
109
    " dbname=" + txtDatabase->text() + 
110
    " port=" + txtPort->text() +
111
    " user=" + txtUsername->text() + 
112
    " password='" + password + "'";
109
  QString connInfo = "dbname=" + txtDatabase->text();
110

  
111
  // Do we need TCP/IP connect info?
112
  if( txtHost->text() != "" )
113
      connInfo += "host=" + txtHost->text() + 
114
          " port=" + txtPort->text();
115

  
116
  // Do we need a username and password?
117
  if( txtUsername->text() != "" )
118
      connInfo += " user=" + txtUsername->text() + 
119
          " password='" + password + "'";
120

  
121
  QgsLogger::debug( "PQconnectdb("+connInfo+");" );
122

  
113 123
  PGconn *pd = PQconnectdb(connInfo.toLocal8Bit().data());
114 124
//  std::cout << pd->ErrorMessage();
115 125
  if (PQstatus(pd) == CONNECTION_OK)
src/app/qgsdbsourceselect.cpp (working copy)
299 299
  QSettings settings;
300 300

  
301 301
  QString key = "/PostgreSQL/connections/" + cmbConnections->currentText();
302
  QString connString = "host=";
302
  QString database = settings.readEntry(key + "/database");
303
  QString connString = "dbname=" + database;
304

  
303 305
  QString host = settings.readEntry(key + "/host");
304
  connString += host;
305
  connString += " dbname=";
306
  QString database = settings.readEntry(key + "/database");
307
  connString += database + " port=";
308
  QString port = settings.readEntry(key + "/port");
309
  if(port.length() == 0){
310
    port = "5432";
306
  if( host != "" )
307
  {
308
    connString += " host="+host;
309

  
310
    QString port = settings.readEntry(key + "/port");
311
    if(port.length() == 0){
312
      port = "5432";
313
    }
314
    connString += " port=" + port;
311 315
  }
312
  connString += port + " user=";
316

  
317
  bool makeConnection = true;
318
  bool searchGeometryColumnsOnly = settings.readBoolEntry(key + "/geometryColumnsOnly");
319
  bool searchPublicOnly = settings.readBoolEntry(key + "/publicOnly");
320

  
313 321
  QString username = settings.readEntry(key + "/username");
314
  connString += username;
315 322
  QString password = settings.readEntry(key + "/password");
316
  bool searchPublicOnly = settings.readBoolEntry(key + "/publicOnly");
317
  bool searchGeometryColumnsOnly = settings.readBoolEntry(key + "/geometryColumnsOnly");
318
  bool makeConnection = true;
319
  if (password == QString::null)
323
  if( username != "" )
320 324
  {
321
    // get password from user 
322
    makeConnection = false;
323
    QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host,
324
        tr("Please enter your password:"),
325
        QLineEdit::Password, QString::null, &makeConnection, this);
326
    // allow null password entry in case its valid for the database
325
    connString += " user="+username;
326
    if (password == QString::null)
327
    {
328
      // get password from user 
329
      makeConnection = false;
330
      QString password = QInputDialog::getText(tr("Password for ") + database + "@" + host,
331
                                               tr("Please enter your password:"),
332
                                               QLineEdit::Password, QString::null, &makeConnection, this);
333
      // allow null password entry in case its valid for the database
334
    }
335

  
336
    // Need to escape the password to allow for single quotes and backslashes
337
    password.replace('\\', "\\\\");
338
    password.replace('\'', "\\'");
339
    connString += " password='" + password + "'";
327 340
  }
328 341

  
329
  // Need to escape the password to allow for single quotes and backslashes
330
  password.replace('\\', "\\\\");
331
  password.replace('\'', "\\'");
332
  connString += " password='" + password + "'";
333 342
#ifdef QGISDEBUG
334 343
  std::cout << "Connection info: " << connString.toLocal8Bit().data() << std::endl;
335 344
#endif
345

  
336 346
  if (makeConnection)
337 347
  {
338 348
    m_connInfo = connString;  //host + " " + database + " " + username + " " + password;
src/core/qgsdatasourceuri.cpp (working copy)
92 92
  
93 93
  // parse the connection info
94 94
  QStringList conParts = QStringList::split(" ", connInfo);
95
  QStringList parm = QStringList::split("=", conParts[0]);
96
  if(parm.size() == 2)
95
  int         myPart;
96

  
97
  // Note, several components (everything but dbname) are optional, so 
98
  // leave the value empty if not found.
99
  for( myPart = 0; myPart < conParts.size(); myPart++ )
97 100
  {
98
    host = parm[1];
99
  }
100
  parm = QStringList::split("=", conParts[1]);
101
  if(parm.size() == 2)
102
  {
103
    database = parm[1];
104
  }
105
  parm = QStringList::split("=", conParts[2]);
106
  if(parm.size() == 2)
107
  {
108
    port = parm[1];
109
  }
101
    QStringList parm = QStringList::split("=", conParts[0]);
110 102

  
111
  parm = QStringList::split("=", conParts[3]);
112
  if(parm.size() == 2)
113
  {
114
    username = parm[1];
103
    if( parm.size() != 2 )
104
        continue;
105

  
106
    if( parm[0] == "host" )
107
        host = parm[1];
108
    
109
    if( parm[0] == "dbname" )
110
        database = parm[1];
111
    
112
    if( parm[0] == "port" )
113
        port = parm[1];
114

  
115
    if( parm[0] == "user" )
116
        username = parm[1];
117

  
118
    if( parm[0] == "password" )
119
    {
120
      // The password can have '=' and ' ' characters in it, so we can't 
121
      // use the split on '=' and ' ' technique - use indexOf() 
122
      // instead. 
123
      QString key="password='"; 
124
      int i = connInfo.indexOf(key); 
125
      if (i != -1) 
126
      { 
127
        QString pass = connInfo.mid(i+key.length()); 
128
        // Now walk through the string till we find a ' character, but 
129
        // need to allow for an escaped ' character (which will be the 
130
        // \' character pair). 
131
        int n = 0; 
132
        bool escaped = false; 
133
        while (n < pass.length() && (pass[n] != '\'' || escaped)) 
134
        { 
135
          if (pass[n] == '\\') 
136
              escaped = true; 
137
          else 
138
              escaped = false; 
139
          n++; 
140
        } 
141
        // The -1 is to remove the trailing ' character 
142
        password = pass.left(n); 
143
      } 
144
    }
115 145
  }
116
  
117
  // The password can have '=' and ' ' characters in it, so we can't 
118
  // use the split on '=' and ' ' technique - use indexOf() 
119
  // instead. 
120
  QString key="password='"; 
121
  int i = connInfo.indexOf(key); 
122
  if (i != -1) 
123
  { 
124
    QString pass = connInfo.mid(i+key.length()); 
125
    // Now walk through the string till we find a ' character, but 
126
    // need to allow for an escaped ' character (which will be the 
127
    // \' character pair). 
128
    int n = 0; 
129
    bool escaped = false; 
130
    while (n < pass.length() && (pass[n] != '\'' || escaped)) 
131
    { 
132
      if (pass[n] == '\\') 
133
        escaped = true; 
134
      else 
135
        escaped = false; 
136
      n++; 
137
    } 
138
    // The -1 is to remove the trailing ' character 
139
    password = pass.left(n); 
140
  } 
141 146
}
142 147

  
143 148

  
144 149
QString QgsDataSourceURI::text() const
145 150
{
146
  return QString("host=" + host + 
147
      " dbname=" + database + 
148
      " port=" + port + 
149
      " user=" + username + 
150
      " password='" + password + 
151
      "' table=" + schema + '.' + table + 
152
      " (" + geometryColumn + ")" +
153
      " sql=" + sql);
151
  QString connInfo = "dbname="+database;
152

  
153
  if( host != "" )
154
    connInfo += " host=" + host + " port=" + port;
155

  
156
  if( username != "" )
157
    connInfo += " user=" + username + " password='"+password+"'";
158

  
159
  return connInfo;
154 160
}
155 161

  
156 162
void QgsDataSourceURI::setConnection(const QString& aHost,