File: hk_sqliteconnection.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 (257 lines) | stat: -rw-r--r-- 6,079 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
// ****************************************************************************
// copyright (c) 2000-2005 Horst Knorr <hk_classes@knoda.org>  
// This file is part of the hk_sqliteclasses 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_sqliteconnection.h>
#include <hk_sqlitedatabase.h>
#include <dlfcn.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream.h>
#endif

#include "hk_drivermanager.h"
#include "hk_actionquery.h"
#include "hk_url.h"

hk_sqliteconnection::hk_sqliteconnection(hk_drivermanager* c):hk_connection(c)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::hk_sqliteconnection");
#endif
    /*p_SQL_Connection=NULL;
    set_tcp_port(default_tcp_port());*/

}


hk_sqliteconnection::~hk_sqliteconnection()
{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::~hk_sqliteconnection");
#endif
}


bool  hk_sqliteconnection::driver_specific_connect()
{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::driver_specific_connect");
#endif
p_connected=true;
return true;
}




bool hk_sqliteconnection::driver_specific_disconnect()

{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::driver_specific_disconnect");
#endif
return true;

}


vector<hk_string>* hk_sqliteconnection::driver_specific_dblist(void)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::driver_specific_dblist");
#endif
    DIR *dp;
    struct dirent *entry;
    hk_string datei;
    hk_string fileend=".hk_sqlite";

    p_databaselist.erase(p_databaselist.begin(),p_databaselist.end());
    if ((dp=opendir(databasepath().c_str()))!=NULL)
    {

        while ((entry = readdir(dp))!=NULL)
        {
            datei=entry->d_name;
    	   hk_string fname=databasepath()+"/"+datei;
           struct stat statbuf;
	   stat(fname.c_str(),&statbuf);
            if (S_ISREG(statbuf.st_mode))
	    {
            size_t p =datei.find(fileend);
            if (p<datei.size())
            {
                datei.replace(p,datei.size()-p,"");
                p_databaselist.insert(p_databaselist.end(),datei);
            }
	    }
        }
        closedir(dp);

    }
    sort(p_databaselist.begin(),p_databaselist.end());
    return &p_databaselist;
}


hk_database* hk_sqliteconnection::driver_specific_new_database(void)
{
    hk_sqlitedatabase* db;
    db = new hk_sqlitedatabase(this);
    return db;
}



bool hk_sqliteconnection::server_supports(support_enum t) const
{
    switch (t)
    {
          case SUPPORTS_NEW_DATABASE:;
          case SUPPORTS_DELETE_DATABASE:;
          case SUPPORTS_NEW_TABLE:;
          case SUPPORTS_DELETE_TABLE:;
	  case SUPPORTS_REFERENTIALINTEGRITY:;
	  case SUPPORTS_TRANSACTIONS:;
	  case SUPPORTS_SQL:; 
          case  SUPPORTS_AUTOINCCOLUMN:; 
          case SUPPORTS_BOOLCOLUMN:; 
          case SUPPORTS_DATECOLUMN:; 
          case SUPPORTS_TIMECOLUMN:; 
          case SUPPORTS_DATETIMECOLUMN:; 
          //case SUPPORTS_BINARYCOLUMN:; 
          case SUPPORTS_MEMOCOLUMN:; 
          case SUPPORTS_TIMESTAMPCOLUMN:; 
	  case SUPPORTS_LOCAL_FILEFORMAT:
	  case SUPPORTS_VIEWS:
	  case SUPPORTS_NEW_VIEW:
	  case SUPPORTS_ALTER_VIEW:
	  case SUPPORTS_DELETE_VIEW:
            return true;
        default : return false;
    }

}


hk_string hk_sqliteconnection::drivername(void) const
{

    return "sqlite2";
}



bool hk_sqliteconnection::server_needs(need_enum t) const
{
    switch (t)
    {
        case NEEDS_NOTHING:
	case NEEDS_NULL_TERMINATED_SQL:
            return true;

        default: return false;
    }

}


bool hk_sqliteconnection::driver_specific_new_password(const hk_string& newpasswd)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqliteconnection::driver_specific_new_password");
#endif
    return true;

}


void hk_sqliteconnection::servermessage(const hk_string& error)
{
       set_last_servermessage(error);

}


unsigned int    hk_sqliteconnection::default_tcp_port(void) const
{
    return 3306;
}


bool    hk_sqliteconnection::create_database(const hk_string& dbase)
{
    if (database()==NULL) new_database();
    if (database()==NULL||!is_connected()) return false;
    driver_specific_dblist();
    if (find(p_databaselist.begin(),p_databaselist.end(),dbase)!=p_databaselist.end())
      return false;
      char* errormsg=0;
      hk_url url=dbase;
      hk_string n=(url.directory().size()>0?dbase:databasepath()+"/"+dbase+".hk_sqlite");
      
      ifstream ifs(n.c_str(),ios::in);
      if (ifs)
        {
         //servermessage(hk_translate("Database already exists"));
	  return false;
	}
      

      
      sqlite* p_sqlitehandler=sqlite_open(n.c_str(),0,&errormsg);

      if (!p_sqlitehandler && errormsg)
       {
         servermessage(errormsg);
	 sqlite_freemem(errormsg);
	 errormsg=0;
	 return false;
       }
    sqlite_close(p_sqlitehandler);
    hk_database* db=driver_specific_new_database();
    if (db)
    {db->set_name(dbase);
    db->create_centralstoragetable();
    delete db;
    }
    return true;
}

bool    hk_sqliteconnection::delete_database(const hk_string& dbase)
{
    hk_string warning=hk_translate("Delete the database \"%DBNAME%\"?");
    warning=replace_all("%DBNAME%",warning,dbase);
    if(!show_yesnodialog(warning,true)) return false;
    
    hk_url url=dbase;    
    hk_string filename=url.directory().size()>0?dbase:databasepath()+"/"+dbase+".hk_sqlite";
    int r=unlink(filename.c_str());
    return r==0;

}



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

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


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