File indexing completed on 2024-05-05 13:07:13
0001 /* This file is part of the KDE project 0002 Copyright (C) 2008 Sharan Rao <sharanrao@gmail.com> 0003 0004 This program is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This program is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this program; see the file COPYING. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 0021 #include <QTemporaryFile> 0022 0023 #include "XbaseConnection_p.h" 0024 #include "XbaseExport.h" 0025 0026 #include "KDbDriverManager.h" 0027 #include "KDb.h" 0028 #include "KDbConnectionData.h" 0029 #include <migration/keximigrate.h> 0030 #include <migration/migratemanager.h> 0031 0032 #include <core/kexiprojectdata.h> 0033 0034 xBaseConnectionInternal::xBaseConnectionInternal(KDbConnection* connection, KDbDriver* internalDriver ) 0035 : ConnectionInternal(connection), 0036 internalDriver(internalDriver) 0037 { 0038 } 0039 0040 xBaseConnectionInternal::~xBaseConnectionInternal() 0041 { 0042 // deletion of internalDriver and internalConn will be handled by KDbDriver* class ( creator ) 0043 } 0044 0045 void xBaseConnectionInternal::storeResult() 0046 { 0047 if (internalConn) { 0048 res = internalConn->serverResult(); 0049 errmsg = internalConn->serverErrorMsg(); 0050 } 0051 } 0052 0053 bool xBaseConnectionInternal::db_connect(const KDbConnectionData& data) 0054 { 0055 // we have to migrate the xbase source database into a .kexi file 0056 // xbase source database directory will be in connectiondata 0057 // we can choose a QTemporaryFile for the destination .kexi file 0058 0059 KexiMigration::MigrateManager xBase2KexiMigrateManager; 0060 0061 // create a temporary .kexi file 0062 QTemporaryFile temporaryKexiFile(QDir::tempPath() + QLatin1String("/kdb_xbase_XXXXXX.kexi")); 0063 temporaryKexiFile.setAutoRemove( false ); 0064 0065 if ( !temporaryKexiFile.open() ) { 0066 xbaseWarning() << "Couldn't create .kexi file for exporting from xBase to .kexi"; 0067 return false; 0068 } 0069 0070 tempDatabase = temporaryKexiFile.fileName(); 0071 0072 KDbConnectionData* kexiConnectionData = 0; 0073 kexiConnectionData = new KDbConnectionData(); 0074 0075 // set destination file name here. 0076 kexiConnectionData->driverId = KDb::defaultFileBasedDriverId(); 0077 kexiConnectionData->setFileName( tempDatabase ); 0078 //xbaseDebug() << "Current file name: " << tempDatabase; 0079 0080 QString sourceDriverId = "xbase"; 0081 // get the source migration driver 0082 KexiMigration::KexiMigrate* sourceDriver = 0; 0083 sourceDriver = xBase2KexiMigrateManager.driver( sourceDriverId ); 0084 if(!sourceDriver || xBase2KexiMigrateManager.error()) { 0085 xbaseWarning() << "Import migrate driver error..."; 0086 return false; 0087 } 0088 0089 KexiMigration::Data* md = new KexiMigration::Data(); 0090 md->keepData = true; 0091 md->destination = new KexiProjectData(*kexiConnectionData, tempDatabase); 0092 0093 // Setup XBase connection data from input connection data passed 0094 //! @todo Check sanity of this 0095 md->source = new KDbConnectionData(data); 0096 md->sourceName = ""; 0097 0098 sourceDriver->setData(md); 0099 if ( !sourceDriver->performImport() ) { 0100 xbaseWarning() << "Import failed"; 0101 return false; 0102 } 0103 0104 // finished transferring xBase database into .kexi file 0105 0106 // Get a driver to the destination database 0107 0108 if ( internalDriver ) 0109 internalConn = internalDriver->createConnection(*kexiConnectionData); 0110 else 0111 return false; 0112 0113 if (!internalConn || internalDriver->error()) { 0114 internalDriver->debugError(); 0115 return false; 0116 } 0117 if (!internalConn->connect()) { 0118 internalConn->debugError(); 0119 storeResult(); 0120 return false; 0121 } 0122 0123 if (!internalConn->useDatabase(tempDatabase)) { 0124 internalConn->debugError(); 0125 storeResult(); 0126 return false; 0127 } 0128 0129 // store mapping from xbase directory to .kexi file name for future use 0130 // Note: When a directory is specified ( as has to be done for xBase ), fileName() 0131 // will give directory name with an additional forward slash. dbPath() won't do so. 0132 // Need some more maintainable solution. 0133 0134 dbMap[data.fileName()] = tempDatabase; 0135 0136 return true; 0137 } 0138 0139 bool xBaseConnectionInternal::db_disconnect(const KDbConnectionData& data) 0140 { 0141 //! Export back to xBase 0142 xBaseExport export2xBase; 0143 KexiMigration::Data* migrateData = new KexiMigration::Data(); 0144 migrateData->source = internalConn->data(); 0145 migrateData->sourceName = tempDatabase; 0146 migrateData->destination = new KexiProjectData( data ); 0147 migrateData->keepData = true; 0148 0149 export2xBase.setData( migrateData ); 0150 0151 if (!export2xBase.performExport()) { 0152 return false; 0153 } 0154 0155 return internalConn->disconnect(); 0156 } 0157 0158 bool xBaseConnectionInternal::useDatabase(const QString &dbName) 0159 { 0160 if ( !internalConn ) { 0161 return false; 0162 } 0163 return internalConn->useDatabase(dbMap[dbName]); 0164 } 0165 0166 bool xBaseConnectionInternal::executeSql(const KDbEscapedString& sql) 0167 { 0168 //xbaseDebug() << statement; 0169 if ( !internalConn ) { 0170 return false; 0171 } 0172 return internalConn->executeSql(sql); 0173 }