File: hk_sqlitetable.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 (470 lines) | stat: -rw-r--r-- 10,979 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// ****************************************************************************
// 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.
// ****************************************************************************
//$Revision: 1.9 $
#include "hk_sqlitetable.h"
#include "hk_sqlitedatabase.h"
#include <hk_actionquery.h>
hk_sqlitetable::hk_sqlitetable(hk_sqlitedatabase* db,hk_presentation* p):hk_sqlitedatasource(db,p)

{
    p_readonly=false;

}


hk_datasource::enum_datasourcetypes hk_sqlitetable::type() const
{
    return ds_table;
}


bool hk_sqlitetable::driver_specific_name(const hk_string& n)
{
    return true;
}


bool hk_sqlitetable::driver_specific_enable(void)
{
//  p_readonly=false;
    return hk_sqlitedatasource::driver_specific_enable();

}


bool hk_sqlitetable::driver_specific_create_table_now(void)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::driver_specific_create_table_now");
#endif
    hk_string csql="CREATE TABLE ";
    primarystring="";
//    csql+=replace_all(" ",name(),"_");
   csql+= p_identifierdelimiter+name()+p_identifierdelimiter;
    csql+=" ( ";
    csql+=internal_new_fields_arguments(false);
    csql+=getprimarystring(false)+" ) ";

    hk_actionquery* query= p_database->new_actionquery();
    if (!query) return false;
    query->set_sql(csql.c_str(),csql.size());
    bool result=query->execute();

    if (result) cerr<<"Table created"; 
      else cerr<<"Error: table could not be created";
    cerr <<endl;
    delete query;
    return result;
}


hk_string hk_sqlitetable::getprimarystring(bool alter)
{
    if (primarystring.size()==0)
    {
        return "";
    }
    hk_string key=", ";
    if (alter) key+="ADD ";
    key+="PRIMARY KEY ( ";
    key=key+primarystring+" )";
    return key;

}


hk_string hk_sqlitetable::internal_new_fields_arguments(bool alter)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::internal_new_fields_arguments");
#endif
    hk_string csql;
    hk_string line;
    hk_string fields;

    list<hk_column*>::iterator it=p_newcolumns.begin();
    while (it!=p_newcolumns.end())
    {
        it++;
    }

    it=p_newcolumns.begin();
    while (it!=p_newcolumns.end())
    {
        hk_string n=(*it)->name();
        if (n.size()>0)
        {
            if (line.size()>0)line+=" , ";
            if (alter) fields =" ADD COLUMN "; else fields="";
            fields+=((*it)->name().size()>0?p_identifierdelimiter+(*it)->name()+p_identifierdelimiter:"");
            if (fields.size()==0)
            {
                return "";
            }
            fields+=" ";
            fields+=field2string((*it)->columntype(),longint2string((*it)->size()<256?(*it)->size():255));
#ifdef HK_DEBUG
            hkdebug("nach field2string");
#endif

            if ((*it)->columntype()!=hk_column::auto_inccolumn&&((*it)->is_primary()))
            {
                if (primarystring.size()>0)primarystring+=" , ";
                primarystring+=p_identifierdelimiter+(*it)->name()+p_identifierdelimiter;
            }
//else
            if (((*it)->is_notnull()||(*it)->is_primary())&&(*it)->columntype()!=hk_column::auto_inccolumn)
                fields+=" NOT NULL ";
            line+=fields;
        }
        else
        {
            show_warningmessage(hk_translate("Warning: Column with no name!"));
        }
        it++;
#ifdef HK_DEBUG
        hkdebug("while ENDE");
#endif
    }

    csql=csql+line+p_sql_delimiter;
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::internal_new_fields_arguments   ENDE");
#endif
    return csql;
}


hk_string hk_sqlitetable::field2string(hk_column::enum_columntype f,const hk_string& m)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::field2string");
#endif
    hk_string fields;
    switch (f)
    {
        case hk_column::auto_inccolumn :    return "INTEGER PRIMARY KEY";
        case hk_column::integercolumn : return "INTEGER";
        case hk_column::smallintegercolumn :    return "SMALLINT";
        case hk_column::floatingcolumn :    return "DOUBLE";
        case hk_column::smallfloatingcolumn :   return "FLOAT";
        case hk_column::datecolumn :    return "DATE";
        case hk_column::datetimecolumn :    return "DATETIME";
        case hk_column::timestampcolumn :   return "TIMESTAMP";
        case hk_column::timecolumn :    return "TIME";
        case hk_column::binarycolumn :  return "BLOB";
        case hk_column::memocolumn :    return "MEMO";
        case hk_column::boolcolumn :    return "BOOL";
        case hk_column::textcolumn :    fields +="CHAR(";
        fields+=m;
        fields+=") ";
        return fields;
        default :       return "CHAR(255)";

    }

}


bool hk_sqlitetable::driver_specific_alter_table_now(void)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::driver_specific_alter_table_now");
#endif
return false;
}


hk_string  hk_sqlitetable::internal_delete_fields_arguments(void)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::internal_delete_fields_arguments");
#endif
   return "";
}


hk_string  hk_sqlitetable::internal_alter_fields_arguments(void)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::internal_alter_fields_arguments");
#endif
return "";
}


bool hk_sqlitetable::is_alteredfield(const hk_string f)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::is_alteredfield");
#endif
    list<class_altercolumns>::iterator it=p_altercolumns.begin();
    while (it!=p_altercolumns.end())
    {
        if ((*it).name==f)return true;
        it++;
    }
    return false;
}


bool hk_sqlitetable::is_deletedfield(const hk_string f)
{
#ifdef HK_DEBUG
    hkdebug("hk_sqlitetable::is_deletedfield");
#endif
    list<hk_string>::iterator it=p_deletecolumns.begin();
    while (it!=p_deletecolumns.end())
    {
        if ((*it)==f)return true;
        it++;
    }
    return false;
}


list<hk_datasource::indexclass>* hk_sqlitetable::driver_specific_indices(void)
{
    hk_datasource* d= database()->new_resultquery();
    
    if (d==NULL)
    {
        return NULL;
    }
    hk_string s="SELECT * FROM sqlite_master WHERE type='index' AND tbl_name='"+name()+"'";
    d->set_sql(s);
    d->enable();
    p_indices.erase(p_indices.begin(),p_indices.end());
    unsigned long max=d->max_rows();
    hk_column* name=d->column_by_name("name");
    hk_column* sql=d->column_by_name("sql");
    if (!name || !sql)
    {
       show_warningmessage(hk_translate("ERROR hk_sqlitetable::driver_specific_indices System columns not found!"));
       delete d;
       return &p_indices;
    }
    
    for(unsigned long i=0;i<max;++i)
    {
    
     if (sql->asstring().size()>0)
     {

    indexclass index;
     index.name=name->asstring();
     list<hk_string> tokens;
     
     hk_string::size_type startpos=sql->asstring().find_first_of("(");
     hk_string::size_type endpos=sql->asstring().find_last_of(")");
     
     if (startpos!=hk_string::npos && endpos!=hk_string::npos)
     {
     parse_indices(tokens,sql->asstring().substr(0,startpos-1));
     list<hk_string>::iterator it=tokens.begin();
     if (tokens.size()>2)
     {
      ++it;
      index.unique=(string2upper(*it)=="UNIQUE");
     }
     parse_indices(tokens,sql->asstring().substr(startpos+1,endpos-startpos-1));
     it=tokens.begin();
     while (it!=tokens.end())
     {
      index.fields.insert(index.fields.end(),*it);
       ++it;
     }
     
       
     }

     p_indices.insert(p_indices.end(),index);
     }
     d->goto_next();
    }
    
    delete d;

    return &p_indices;
}


list<hk_datasource::indexclass>::iterator hk_sqlitetable::findindex(const hk_string& i)
{

    list<hk_datasource::indexclass>::iterator it=p_indices.begin();
    while (it!=p_indices.end())
    {
        if ((*it).name==i)return it;
        it++;
    }
    return p_indices.end();
}


bool    hk_sqlitetable::driver_specific_drop_index(const hk_string& i)
{
    hk_string s;
        s+=" DROP INDEX ";
        s+=p_identifierdelimiter+i+p_identifierdelimiter;
    
    hk_actionquery* query= p_database->new_actionquery();
    if (query==NULL) return false;
    query->set_sql(s.c_str(),s.size());
    bool res =query->execute();
    delete query;

    return res;
}


bool    hk_sqlitetable::driver_specific_create_index(const hk_string& i,bool unique, list<hk_string>& fields)
{
    hk_string s="CREATE ";
    if (unique)s+="UNIQUE ";
    s+="INDEX ";
    s+=p_identifierdelimiter+i+p_identifierdelimiter;
    s+=" ON ";
    s+=p_identifierdelimiter+name()+p_identifierdelimiter;
    s+=" ( ";
    hk_string f;
    list<hk_string>::iterator it=fields.begin();
    while (it!=fields.end())
    {
        if (f.size()>0)f+=" , ";
        f+=p_identifierdelimiter+(*it)+p_identifierdelimiter;
        it++;
    }

    s+=f+" )";
    hk_actionquery* query= p_database->new_actionquery();
    if (query==NULL) return false;
// cout <<"Create Index: "<<s<<endl;
    query->set_sql(s.c_str(),s.size());
    bool res =query->execute();
    delete query;

    return res;
}


void hk_sqlitetable::parse_indices(list<hk_string>& tokens, const hk_string & indices)
{

    hk_string::size_type offset=0;

    enum
    {
	S_START,S_IN_DOUBLEQUOTE,S_IN_QUOTE,S_IN_TAG
    } state=S_START;

tokens.clear();
//cerr <<"   indices="<<indices<<endl;
//parser begin
int row=1;
int col=1;
hk_string tag;

while (offset<indices.size())
{
	hk_string x(1,indices[offset]);
	switch (state)
	{
	case S_START:   
			tag="";
			if (isspace(x[0]))
				{
					if (x=="\n")
					{
						++row;col=0;
					}
				  break;
				}
			else
			if (x=="\"")
			   {
			     state=S_IN_DOUBLEQUOTE;
			   }
			else
			if (x=="'")
			 state=S_IN_QUOTE;   
			else 	
			if (x==",")
			 {
			   //tag=",";
			   //tokens.insert(tokens.end(),tag);
			   
			 } 
			else
			 {
			  state=S_IN_TAG;
			  tag=x;
			 }
			break;	
	case S_IN_QUOTE:
			  if (x=="'")
			   {
 			   tokens.insert(tokens.end(),tag);
			    state=S_START;
			   } 
			  else tag+=x;
 
			break;		
	
	
	case S_IN_DOUBLEQUOTE:
			  if (x=="\"")
			   {
 			   tokens.insert(tokens.end(),tag);
			    state=S_START;
			   } 
			  else tag+=x;
 
			break;		
	
	
	
	case S_IN_TAG:
			if (isspace(x[0]))
			 {
			   tokens.insert(tokens.end(),tag);
			   tag="";
			   state=S_START;
			     
			 }
			 else
			if (x==",")
			 {
			   tokens.insert(tokens.end(),tag);
			   state=S_START;
			 }
			else
			 tag+=x;
			break;
	}
	++offset;++col;

} // endloop
  
if (state!=S_START)
   tokens.insert(tokens.end(),tag);

/*list<hk_string>::iterator it=tokens.begin();
while (it!=tokens.end())
{
  cerr << "token1="<<*it<<endl;
  ++it;
}
cerr <<endl;
*/
}