// **************************************************************************** // copyright (c) 2000-2005 Horst Knorr // This file is part of the hk_sqlite3classes 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.8 $ #include "hk_sqlite3table.h" #include "hk_sqlite3database.h" #include "hk_sqlite3connection.h" #include "hk_actionquery.h" hk_sqlite3table::hk_sqlite3table(hk_sqlite3database* db,hk_presentation* p):hk_sqlite3datasource(db,p) { p_readonly=false; } hk_datasource::enum_datasourcetypes hk_sqlite3table::type() const { return ds_table; } bool hk_sqlite3table::driver_specific_name(const hk_string& ) { return true; } bool hk_sqlite3table::driver_specific_enable(void) { // p_readonly=false; return hk_sqlite3datasource::driver_specific_enable(); } bool hk_sqlite3table::driver_specific_create_table_now(void) { #ifdef HK_DEBUG hkdebug("hk_sqlite3table::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<<"Error: table could not be created"<::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_sqlite3table::internal_new_fields_arguments ENDE"); #endif return csql; } hk_string hk_sqlite3table::field2string(hk_column::enum_columntype f,const hk_string& m) { #ifdef HK_DEBUG hkdebug("hk_sqlite3table::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_sqlite3table::driver_specific_alter_table_now(void) { #ifdef HK_DEBUG hkdebug("hk_sqlite3table::driver_specific_alter_table_now"); #endif hk_string asql="ALTER TABLE "; asql+=p_identifierdelimiter+name()+p_identifierdelimiter; hk_string r=internal_delete_fields_arguments(); hk_string error; if (r.size()>0) { error=hk_translate("hk_sqlite3table:: Driver does not support the deletion of columns!\n"); p_sqlitedatabase->sqliteconnection()->servermessage(error); } r=internal_new_fields_arguments(true); if (r.size()==0) { error+=hk_translate("hk_sqlite3table::No columns to add!"); p_sqlitedatabase->sqliteconnection()->servermessage(error); return false; } asql+=r; cerr <<"ALTER SQL="<new_actionquery(); if (!query) return false; query->set_sql(asql.c_str(),asql.size()); bool result=query->execute(); if (result) cerr<<"tabelle ge�dert"; else cerr<<"fehler";cerr <::iterator it=p_deletecolumns.begin(); while (it!=p_deletecolumns.end()) { if (result.size()>0) result+=" , "; result+=" DROP COLUMN "; result+=p_identifierdelimiter+(*it)+p_identifierdelimiter; it++; } cerr <<"internal_delete_fields_arguments return:#"<::iterator it=p_altercolumns.begin(); while (it!=p_altercolumns.end()) { if ((*it).name==f)return true; it++; } return false; } bool hk_sqlite3table::is_deletedfield(const hk_string f) { #ifdef HK_DEBUG hkdebug("hk_sqlite3table::is_deletedfield"); #endif list::iterator it=p_deletecolumns.begin(); while (it!=p_deletecolumns.end()) { if ((*it)==f)return true; it++; } return false; } list* hk_sqlite3table::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* namecol=d->column_by_name("name"); hk_column* sqlcol=d->column_by_name("sql"); if (!namecol || !sqlcol) { show_warningmessage(hk_translate("ERROR hk_sqlitetable::driver_specific_indices System columns not found!")); delete d; return &p_indices; } for(unsigned long i=0;iasstring().size()>0) { indexclass index; index.name=namecol->asstring(); list tokens; hk_string::size_type startpos=sqlcol->asstring().find_first_of("("); hk_string::size_type endpos=sqlcol->asstring().find_last_of(")"); if (startpos!=hk_string::npos && endpos!=hk_string::npos) { //cerr <<"sql.asstring "<asstring().substr(0,startpos-1)); list::iterator it=tokens.begin(); if (tokens.size()>2) { ++it; index.unique=(string2upper(*it)=="UNIQUE"); } //cerr <<"zweiter Teil"<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::iterator hk_sqlite3table::findindex(const hk_string& i) { list::iterator it=p_indices.begin(); while (it!=p_indices.end()) { if ((*it).name==i)return it; it++; } return p_indices.end(); } bool hk_sqlite3table::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_sqlite3table::driver_specific_create_index(const hk_string& i,bool unique, list& 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::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: "<set_sql(s.c_str(),s.size()); bool res =query->execute(); delete query; return res; } void hk_sqlite3table::parse_indices(list& tokens, const hk_string & indiceslist) { 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="<::iterator it=tokens.begin(); while (it!=tokens.end()) { cerr << "token1="<<*it<