/************************* * * * * * * * * * * * * *************************** Copyright (c) 1999-2005 Ryan Bobko ryan@ostrich-emulators.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ************************** * * * * * * * * * * * * **************************/ #include "sqldbplugin.h" #include "qhacctable.h" #include "qhaccutils.h" #include "tableops.h" #include #include /***********************************************************************/ /* QHACCSQLDBPLUGIN */ /* an abstract class that provides the format of the functions that */ /* will need to be satisfied by plugin DB drivers */ /* */ /***********************************************************************/ const QHaccSQLDBInfo QHaccSQLDBPlugin::pinfo; QString QHaccSQLDBPlugin::table( Table t ){ //if( t==XTRANS ) return QC::TABLENAMES[QC::TRANT]+QString( " t, " )+ // QC::TABLENAMES[QC::SPLTT]+QString( " s" ); if( t==XTRANS ) return QC::TABLENAMES[QC::SPLTT]+QString( " s, " )+ QC::TABLENAMES[QC::TRANT]+QString( " t" ); else return QC::TABLENAMES[( int )t]; } int QHaccSQLDBPlugin::table( const QString& t ){ for( int i=0; i0 ) stmt.append( ", " ); PosVal pv=tu[i]; stmt.append( Utils::tcname( t, pv.getp() ) ); stmt.append( "="+sqlField( pv.getv(), Utils::tctype( t, pv.getp() ) ) ); } if( ts.sqlValid() ){ stmt.append( " where " ); stmt.append( Utils::tcname( t, pos )+ sqlField( ts, Utils::tctype( t, pos ) ) ); } run( stmt ); } void QHaccSQLDBPlugin::updateWhere( Table t, const TableSelect& ts, const TableRow& replace ){ if( t==XTRANS ){ std::ostream * str=0; if( Utils::error( Utils::ERROPER, str ) ) *str<<"cannot update XTRANS"< cols; for( int i=0; i<( int )tgsz; i++ ) cols.push_back( i ); tg=TableGet( cols ); } vector types; for( uint i=0; i0 ) stmt.append( "," ); int tgcol=tg[i]; QString name=Utils::tcname( t, tgcol ); ColType type=Utils::tctype( t, tgcol ); types.push_back( type ); if( tg.getMod( i )==TableGet::UQ ) stmt.append( "distinct " ); stmt.append( selField( name, type ) ); } stmt.append( " from " ); stmt.append( table( t ) ); bool gotone=false; uint arsz=arr.size(); for( uint i=0; i QHaccSQLDBPlugin::getWhere( Table t, vector ts, uint& rr ){ return getWhere( t, TableGet(), ts, rr ); } auto_ptr QHaccSQLDBPlugin::getWhere( Table t, const TableSelect& ts, uint& rr ){ return getWhere( t, TableGet(), vector( 1, ts ), rr ); } void QHaccSQLDBPlugin::deleteWhere( Table t, const TableSelect& ts ){ if( t==XTRANS ){ std::ostream * str=0; if( Utils::error( Utils::ERROPER, str ) ) *str<<"cannot delete from XTRANS"<rows(); startLoad( t, mrows ); for( uint i=0; iat( i ); QString ret("insert into "+table( t )+" values (" ); ret.append( sqlField( r.get( 0 ), Utils::tctype( t, 0 ) ) ); for( int i=1; i v( 1, ct ); auto_ptr j=sel( query, v, r ); return ( r>0 ? j->at( 0 )[0] : TableCol() ); } TableCol QHaccSQLDBPlugin::min( Table t, int col ){ return minmax( minmax( t, col, false ), Utils::tctype( t, col ) ); } TableCol QHaccSQLDBPlugin::max( Table t, int col ){ return minmax( minmax( t, col, true ), Utils::tctype( t, col ) ); } bool QHaccSQLDBPlugin::dirty() const { return false; } void QHaccSQLDBPlugin::startLoad( Table, uint ){ setAtom( BEGIN ); } void QHaccSQLDBPlugin::stopLoad( Table ){ setAtom( COMMIT ); } bool QHaccSQLDBPlugin::imprt( QHaccResultSet * tbls ){ bool ret=true; // order the inserts so that RI is maintained. this is easy // so long as we order the inserts by APID and not AID const uint ROWS=tbls[QC::ACCTT].rows(); QHaccResultSet tbl( QC::ACOLS, QC::ACOLTYPES, ROWS ); QHaccTableIndex idx( &tbls[QC::ACCTT], QC::APID, CTUINT, QC::AID, CTUINT ); for( uint j=0; j rslt=getWhere( ( Table )i, TableSelect(), rr ); tbls[i]=QHaccResultSet( *rslt ); } return true; } void QHaccSQLDBPlugin::setAtom( AtomicOp, QString ){} QString QHaccSQLDBPlugin::selField( const QString& fname, ColType ) const{ return fname; } QString QHaccSQLDBPlugin::sqlField( const TableCol& tc, ColType ct ) const { QString ret=tc.gets(); if( !( ct==CTUINT || ct==CTINT || ct==CTFLOAT ) ) { ret.replace( QRegExp( "'" ), "''" ); ret.prepend( "'" ); ret.append( "'" ); } return ret; } const PluginInfo& QHaccSQLDBPlugin::info() const { return pinfo; } QHaccSQLDBInfo::QHaccSQLDBInfo(){ atom=raw=false; guisel=true; targ=DATABASE; piprefs.reset( new QHaccResultSet( QC::IPICOLS, QC::IPICOLTYPES ) ); } QHaccSQLDBInfo::~QHaccSQLDBInfo(){}