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
|
/***************************************************************************
* Copyright (C) 2005 by *
* Jason Kivlighn ([email protected] *
* *
* 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. *
***************************************************************************/
#include "convert_sqlite3.h"
#include <tqapplication.h>
#include <tqfile.h>
#include <kdebug.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <tdemessagebox.h>
#include <tdeprocio.h>
//FIXME: Some messages should be given to the user about success/failure, but that can't be done in the 0.8.x branch due to i18n.
ConvertSQLite3::ConvertSQLite3( const TQString &db_file ) : TQObject(), error(false)
{
TQString file = db_file;
if ( file.isEmpty() ) {
TDEConfig *config = TDEGlobal::config();
config->setGroup("Server");
file = config->readEntry("DBFile");
}
TDEProcIO *p = new TDEProcIO;
p->setUseShell(true);
//sqlite OLD.DB .dump | sqlite3 NEW.DB
*p << "sqlite" << file << ".dump" <<
"|" << "sqlite3" << file+".new";
TQApplication::connect( p, TQ_SIGNAL(readReady(TDEProcIO*)), this, TQ_SLOT(processOutput(TDEProcIO*)) );
bool success = p->start( TDEProcess::Block, true );
if ( !success ) {
kdDebug()<<"Conversion failed... unable to start TDEProcess"<<endl;
return;
}
if ( error )
return;
TQString backup_file = file+".sqlite2";
int i = 1;
while ( TQFile::exists(backup_file) ) {
backup_file = backup_file.left(file.length()+8)+"."+TQString::number(i);
++i;
}
if ( !copyFile( file, backup_file ) ) {
kdDebug()<<"Unable to backup SQLite 2 database... aborting"<<endl
<<"A successfully converted SQLite 3 file is available at \""<<file<<".new\"."<<endl;
}
else {
kdDebug()<<"SQLite 2 database backed up to "<<backup_file<<endl;
if ( !copyFile( file+".new", file ) ) {
kdDebug()<<"Unable to copy the new SQLite 3 database to: "<<file<<"."<<endl
<<"You may manually move \""<<file<<".new\" to \""<<file<<"\""<<endl;
}
else {
kdDebug()<<"Conversion successful!"<<endl;
TQFile::remove(file+".new");
}
}
}
ConvertSQLite3::~ConvertSQLite3()
{
}
void ConvertSQLite3::processOutput( TDEProcIO* p )
{
TQString error_str, buffer;
while ( p->readln(buffer) != -1 ) {
error_str += buffer;
}
KMessageBox::error( 0, error_str );
error = true;
p->ackRead();
}
bool ConvertSQLite3::copyFile( const TQString &oldFilePath, const TQString &newFilePath )
{
//load both files
TQFile oldFile(oldFilePath);
TQFile newFile(newFilePath);
bool openOld = oldFile.open( IO_ReadOnly );
bool openNew = newFile.open( IO_WriteOnly );
//if either file fails to open bail
if(!openOld || !openNew) { return false; }
//copy contents
uint BUFFER_SIZE = 16000;
char* buffer = new char[BUFFER_SIZE];
while(!oldFile.atEnd())
{
TQ_ULONG len = oldFile.readBlock( buffer, BUFFER_SIZE );
newFile.writeBlock( buffer, len );
}
//deallocate buffer
delete[] buffer;
buffer = NULL;
return true;
}
#include "convert_sqlite3.moc"
|