File: hk_postgresqlconnection.cpp

package info (click to toggle)
hk-classes 0.7.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,976 kB
  • ctags: 8,014
  • sloc: cpp: 84,248; sh: 8,328; python: 3,255; makefile: 257
file content (259 lines) | stat: -rw-r--r-- 6,025 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// ****************************************************************************
// copyright (c) 2000-2005 Horst Knorr <hk_classes@knoda.org>  
// Edited by Anirban Biswas <utpal@cal2.vsnl.net.in>
// This file is part of the hk_postgreclearsqlclasses library.
// This file may be distributed and/or modified under the terms of the
// GNU Library Public License version 2 as published by the Free Software
// Foundation and appearing in the file COPYING included in the
// packaging of this file.
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
// ****************************************************************************
#include "hk_postgresqlconnection.h"
#include <hk_postgresqldatabase.h>
#include <dlfcn.h>
#include "hk_drivermanager.h"
#include "hk_actionquery.h"

hk_postgresqlconnection::hk_postgresqlconnection(hk_drivermanager* c):hk_connection(c)
{
    p_SQL_Connection=NULL;
    set_tcp_port(default_tcp_port());
    set_host("");
    set_user("postgres");
    p_connectionsuccessfullyestablished=false;
}


hk_postgresqlconnection::~hk_postgresqlconnection()
{
//driver_specific_disconnect();
    if (p_SQL_Connection) PQfinish(p_SQL_Connection);
    p_SQL_Connection=NULL;
}


bool  hk_postgresqlconnection::driver_specific_connect()
{
//  char* db=0;
//  char* xunix_port=0;
    hk_string conn_info;
    if (!p_connected)
    {
        if (user().size()>0)
	{
	 conn_info="user=";
         conn_info+=user();
	}
	if (password().size()>0)
	{
	 conn_info+=" password='"+password()+"'";

	}
        conn_info+=" dbname=";
        conn_info+=p_postgresdatabase.size()==0?"template1":"'"+p_postgresdatabase+"'";
	if (host().size()>0)
	{
	 conn_info+=" host=";
         conn_info+=host();
         conn_info+=" port=";
         conn_info+=longint2string(tcp_port());
	}
        if (p_SQL_Connection!=NULL) PQfinish(p_SQL_Connection);
        p_SQL_Connection = PQconnectdb(conn_info.c_str());
        if(PQstatus(p_SQL_Connection) == CONNECTION_OK)
        {
            p_connected = true;
            p_connectionsuccessfullyestablished=true;
        }
        else
        {
            cerr << "NOT WORKING"<<endl;
            p_connected = false;
            if (p_connectionsuccessfullyestablished&&p_postgresdatabase.size()>0)
            {
//login was possible, but database name is wrong
//so try to connect again with the default databasename
                p_postgresdatabase="";
                driver_specific_connect();
                return false;
            }
        }

    }

    if (!p_connected)
        servermessage();

    return p_connected;

}


PGconn * hk_postgresqlconnection::dbhandler()
{
    return p_SQL_Connection;
}


void hk_postgresqlconnection::getdbhandler(PGconn *newconnection)
{
    p_SQL_Connection = newconnection;

}


bool hk_postgresqlconnection::driver_specific_disconnect()

{
    if (p_connected)
    {
        PQfinish(p_SQL_Connection);
        p_SQL_Connection=NULL;
        p_connected=false;

    }
    return p_connected;

}


vector<hk_string>* hk_postgresqlconnection::driver_specific_dblist(void)
{

    PGresult  * Res;
    unsigned int i;
    p_databaselist.erase(p_databaselist.begin(),p_databaselist.end());

    if (p_connected)
    {

        Res=PQexec(dbhandler() , "SELECT datname FROM pg_database");
        if (  (PQresultStatus(Res)) == PGRES_TUPLES_OK )
        {
            for (i=0 ; i < PQntuples(Res); i++)
            {
                p_databaselist.insert(p_databaselist.end(), PQgetvalue(Res , i , 0) );
            }
        }
        PQclear(Res);
    } else cerr <<"not connected"<<endl;
    ;
    return &p_databaselist;
}


hk_database* hk_postgresqlconnection::driver_specific_new_database(void)
{
    hk_postgresqldatabase* db;
    db = new hk_postgresqldatabase(this);
    return db;
}


bool hk_postgresqlconnection::server_supports(support_enum t) const
{
    switch (t)
    {
        case SUPPORTS_TRANSACTIONS:
        case SUPPORTS_DATETIMECOLUMN:
	case SUPPORTS_RENAME_DATABASE:
	case SUPPORTS_LOCAL_FILEFORMAT:
            return false;

        default : return true;
    }

}


bool hk_postgresqlconnection::server_needs(need_enum t) const
{
    switch (t)
    {
        case NEEDS_HOST:
        case NEEDS_USERNAME:
        case NEEDS_PASSWORD:
        case NEEDS_PORT:
	case NEEDS_NULL_TERMINATED_SQL:
	case NEEDS_DATABASENAME:
             return true;

        default: return false;
    }

}


bool hk_postgresqlconnection::driver_specific_new_password(const hk_string& newpasswd)
{
    hk_postgresqldatabase* db=new hk_postgresqldatabase(this);
    hk_actionquery* p_q= db->driver_specific_new_actionquery();
    if (p_q==NULL) return false;
    hk_string pwdsql="ALTER USER ";
    pwdsql+=user();
    pwdsql+=" WITH PASSWORD '";
    pwdsql+=newpasswd;
    pwdsql+="'";
    p_q->set_sql(pwdsql.c_str(),pwdsql.size());
    bool result= p_q->execute();
    delete p_q;
    delete db;
    return result;

}


void hk_postgresqlconnection::servermessage(void)
{
    if (!p_SQL_Connection)return;
    set_last_servermessage(PQerrorMessage(p_SQL_Connection));
    cerr<< "Postgresql error message "<<PQerrorMessage(p_SQL_Connection)<<endl;
}


void    hk_postgresqlconnection::set_postgresdatabase(const hk_string& db)
{
    p_postgresdatabase=db;

}


hk_string hk_postgresqlconnection::drivername(void) const
{

    return "postgres";
}


unsigned int    hk_postgresqlconnection::default_tcp_port(void) const
{
    return 5432;
}


bool    hk_postgresqlconnection::driver_specific_delete_database(const hk_string& dbase)
{

if (database() && database()->name()==dbase)
{
   new_database("template1");
}
return hk_connection::driver_specific_delete_database(dbase);

}



//***********************************************************

hk_connection* create_connection(hk_drivermanager* cl)
{
    return new hk_postgresqlconnection(cl);
}


hk_string hk_classesversion(void)
{
return (hk_string)HK_VERSION;
}