Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QtSql>
- #include <QString>
- #include <QTextStream>
- #include <QFile>
- #include <stdio.h>
- #include <QDateTime>
- #include <stdlib.h>
- #include <sqlite3.h>
- int main(int argc, char *argv[])
- {
- QDateTime qd;
- QFile f1( "result.txt" );
- f1.open( QFile::WriteOnly | QFile::Text );
- freopen ("error.txt","a",stderr);
- qDebug()<<"*********************Error Log*************************";
- qDebug()<<qd.currentDateTime()<< "\n";
- QString par4=argv[4];
- if(argc!=5 && !par4.compare("n"))
- {
- printf("Error: Insufficient parameters - Execute the program as: \n");
- printf("./<executable file> <SQLite3 database file> <psql username> <psql pass> <y / n> [<module path>...]\n");
- printf("<y / n> is for specifying whether there are modules loaded in the sqlite3 database.\n");
- printf("If 'y' then mention the path to the module's library file to be loaded. Note that upto 5 modules can be loaded\n");
- exit(1);
- }
- if(!par4.compare("y") && argc < 5)
- {
- printf("Error: Please mention the module to be loaded\n");
- exit(1);
- }
- if(argc > 10)
- {
- printf("Error: Too many parameters added\n");
- exit(1);
- }
- QSqlDatabase db_source=QSqlDatabase::addDatabase("QSQLITE");
- QSqlDatabase db_int=QSqlDatabase::addDatabase("QPSQL","second");
- db_int.setHostName("localhost");
- db_int.setUserName(argv[2]);
- db_int.setPassword(argv[3]);
- db_int.setDatabaseName("postgres");
- db_source.setDatabaseName(argv[1]);
- if(db_source.open()==false)
- {
- qDebug() << "Can not open source database"<<"\n" << db_source.lastError()<<"\n";
- return -1;
- }
- if(db_int.open()==false)
- {
- qDebug() << "Can not open intermediate database"<<"\n"<< db_int.lastError()<<"\n";
- return -1;
- }
- else
- {
- qDebug() << "Opening Source database: " << argv[1] << db_source.lastError();
- qDebug() << "Opening interim database with the provided user and password" << db_int.lastError()<<"\n";
- QTextStream stream(&f1); // This enables to program to write data to a file
- QSqlQuery db_query,schema1_query,schema2_query,col_query,schema_query;
- QSqlQuery createdb_query(db_int);
- createdb_query.exec("create database twincdb2int");
- qDebug() << createdb_query.lastError() << "line 71" << endl;
- db_int.close();
- db_int.setDatabaseName("twincdb2int");
- if(db_int.open()==false)
- {
- qDebug() << "Can not open intermediate database twincdbint"<<"\n"<< db_int.lastError()<<"\n";
- return -1;
- }
- QSqlQuery psqldb_query("create table dbl(dname varchar(30),dpath varchar(60))",db_int);
- qDebug() << psqldb_query.lastError() << "line 81" << endl;
- QSqlQuery table_det_query,foreign_key_query,index_list_query,vtcount_query;
- db_query.exec("pragma database_list");
- qDebug()<< db_query.lastError() << "line 85" << endl;
- stream << "The list of databases:\n";
- QSqlQuery psql_dbs_query(db_int);
- psql_dbs_query.prepare("insert into dbl values(?,?)");
- while(db_query.next())
- {
- QString db_str0=db_query.value(0).toString();
- QString db_str1=db_query.value(1).toString();
- QString db_str2=db_query.value(2).toString();
- psql_dbs_query.bindValue(0,db_str1);
- psql_dbs_query.bindValue(1,db_str2);
- psql_dbs_query.exec();
- qDebug()<< psql_dbs_query.lastError() << "line 98" <<endl;
- stream << db_str0 << "|" << db_str1 << "|" << db_str2 << "\n";
- }
- stream <<"\nSchema:\n";
- QString sch_str[10];
- int i=0,vt=0;
- schema1_query.exec("select distinct(type) from sqlite_master");
- qDebug()<< schema1_query.lastError() << "line 108" << endl;
- schema2_query.prepare("select count(type) from sqlite_master where type=?");
- QSqlQuery dbschema_cnt_query("create table schcnt(type varchar(15), cnt int)",db_int);
- qDebug() << dbschema_cnt_query.lastError() << "line 112" << endl;
- dbschema_cnt_query.prepare("insert into schcnt values(?,?)");
- while(schema1_query.next())
- {
- sch_str[i]=schema1_query.value(0).toString();
- schema2_query.bindValue(0,sch_str[i]);
- schema2_query.exec();
- qDebug()<< schema2_query.lastError() << " line 121" << endl;
- if(schema2_query.next())
- {
- int sch_count=schema2_query.value(0).toInt();
- vt=0;
- if(!sch_str[i].compare("table"))
- {
- vtcount_query.exec("select count(type) from sqlite_master where sql like 'CREATE VIRTUAL TABLE%'");
- qDebug()<< vtcount_query.lastError() << "line 129" << endl;
- if(vtcount_query.next())
- {
- int vtcount=vtcount_query.value(0).toInt();
- stream<< vtcount <<" Virtual Tables \n";
- vt=vtcount;
- dbschema_cnt_query.bindValue(0,"Virtual Table");
- dbschema_cnt_query.bindValue(1,vt);
- dbschema_cnt_query.exec();
- qDebug() << dbschema_cnt_query.lastError() << "line 139" << endl;
- }
- }
- stream << (sch_count - vt) << " " << sch_str[i] << "\n";
- dbschema_cnt_query.bindValue(0,sch_str[i]);
- dbschema_cnt_query.bindValue(1,(sch_count-vt));
- dbschema_cnt_query.exec();
- qDebug() << dbschema_cnt_query.lastError() << "line 146" << endl;
- }
- i++;
- }
- int count=i,n=1;
- QSqlQuery index_info_query,vtable_query,table_query,view_query,index_query,trigger_query,loadmodule_query,vtable_det_q;
- vtable_query.exec("select name,sql from sqlite_master where sql like 'CREATE VIRTUAL TABLE%'");
- qDebug()<< vtable_query.lastError() << "line 155" <<endl;
- table_query.exec("select name,sql from sqlite_master where sql like 'CREATE TABLE%'");
- qDebug()<< table_query.lastError() << "line 158" <<endl;
- QSqlQuery itbl_query(db_int),itblcol_query(db_int),iforeign_q(db_int);
- itbl_query.exec("create table tblsch(tname varchar PRIMARY KEY,tsql varchar)");
- qDebug()<<itbl_query.lastError() << " line 164"<<"\n";
- QSqlQuery ivtbl_query(db_int);
- ivtbl_query.exec("create table vtblsch(vtname varchar PRIMARY KEY,vtsql varchar)");
- qDebug()<<ivtbl_query.lastError() << " line 168"<<"\n";
- iforeign_q.exec("create table tblforkey (tblname varchar,tcol varchar,ftbl varchar,fcol varchar)");
- qDebug()<<iforeign_q.lastError() << " line 171"<<"\n";
- QSqlQuery iidx_q(db_int),itrg_q(db_int),iview_q(db_int);
- iidx_q.exec("create table indexsch (idxname varchar,tbl varchar,col varchar,sql varchar)");
- qDebug()<<iidx_q.lastError() << " line 177"<<"\n";
- itrg_q.exec("create table trigsch (trigname varchar,tbl varchar, sql varchar)");
- qDebug()<<itrg_q.lastError() << " line 180"<<"\n";
- iview_q.exec("create table viewsch (viewname varchar,sql varchar)");
- qDebug()<<iview_q.lastError() << " line 183"<<"\n";
- for(int k=0;count>0;count--)
- {
- n=1;
- if(!sch_str[k].compare("table"))
- {
- QSqlQuery itb_sch_ins_q(db_int),itb_col_ins_q(db_int),iforkey_ins_q(db_int),itb_dat_q(db_int),itb_dat_col_ins_q(db_int);
- QSqlQuery itb_dat_ins_q(db_int);
- itb_sch_ins_q.prepare("insert into tblsch values(?,?)");
- iforkey_ins_q.prepare("insert into tblforkey values(?,?,?,?)");
- QSqlQuery ivtb_sch_ins_q(db_int),ivtbcol_q(db_int),ivtbcol_ins_q(db_int),vtb_data_q,ivtb_creat_q(db_int),ivtb_ins_q(db_int);
- QSqlQuery ivtb_dat_ins_q(db_int);
- ivtb_sch_ins_q.prepare("insert into vtblsch values(?,?)");
- QSqlQuery tb_data_q;
- if(!par4.compare("y"))
- {
- QVariant v = db_source.driver()->handle();
- sqlite3 *handle=0;
- if (v.isValid() && strcmp(v.typeName(), "sqlite3*") == 0)
- {
- // v.data() returns a pointer to the handle
- handle = *static_cast<sqlite3 **>(v.data());
- if (handle == 0)
- {
- qDebug() << "Could not get sqlite handle";
- }
- }
- else
- {
- qDebug() << "handle variant returned typename " << v.typeName();
- }
- sqlite3_enable_load_extension(handle, 1);
- int tot_par=argc;
- QSqlQuery load_module_q;
- while((tot_par-5)>0)
- {
- QString lmod=argv[tot_par-1];
- load_module_q.exec("select load_extension('" + lmod + "')");
- qDebug() << load_module_q.lastError() << "line 233" << endl;
- tot_par--;
- }
- stream << "\nThe list and details of Virtual Tables:\n";
- while(vtable_query.next())
- {
- stream << n <<". ";
- QString vtable_name=vtable_query.value(0).toString();
- QString vtable_sql=vtable_query.value(1).toString();
- stream << vtable_name <<"\n SQL code:\n "<< vtable_sql << "\n\n ";
- ivtb_sch_ins_q.bindValue(0,vtable_name);
- ivtb_sch_ins_q.bindValue(1,vtable_sql);
- ivtb_sch_ins_q.exec();
- qDebug() << ivtb_sch_ins_q.lastError() << "line 247" << endl;
- vtable_det_q.exec("pragma table_info("+vtable_name+")");
- qDebug()<< vtable_det_q.lastError() << "line 250" << endl;
- ivtbcol_q.exec("create table "+vtable_name+"_d (coln varchar,dtype varchar)");
- qDebug() << ivtbcol_q.lastError() << "line 253" << endl;
- ivtbcol_ins_q.prepare("insert into "+vtable_name+"_d values(?,'text')");
- ivtb_creat_q.exec("create table "+vtable_name+" ()");
- qDebug() << ivtb_creat_q.lastError() << "line 258" << endl;
- stream<<"Columns and their datatypes:\n ";
- int j=0;
- while(vtable_det_q.next())
- {
- QString table_det1=vtable_det_q.value(1).toString();
- ivtbcol_ins_q.bindValue(0,table_det1);
- ivtbcol_ins_q.exec();
- qDebug() << ivtbcol_ins_q.lastError() << endl;
- ivtb_ins_q.exec("alter table "+vtable_name+" ADD "+table_det1+" TEXT");
- qDebug() << ivtb_ins_q.lastError() << "line 271" << endl;
- j++;
- stream << table_det1 << " TEXT\n ";
- }
- stream <<"\n The data:\n";
- vtb_data_q.exec("select * from "+vtable_name);
- qDebug() << vtb_data_q.lastError() << "line 279" << endl;
- QString dat_ins="insert into "+vtable_name+" values (";
- while(vtb_data_q.next())
- {
- for(int k=0;k<j;k++)
- {
- QString data_str=vtb_data_q.value(k).toString();
- if(k!=(j-1))
- stream << data_str << "|";
- else
- stream << data_str << "\n";
- if(!data_str.compare(""))
- data_str="null";
- else
- data_str="'"+data_str+"'";
- dat_ins=dat_ins+data_str+",";
- }
- dat_ins.append(")");
- int cpos=dat_ins.indexOf(",)");
- dat_ins.replace(cpos,1,"");
- ivtb_dat_ins_q.exec(dat_ins);
- qDebug() << itb_dat_ins_q.lastError() << "line 305 " <<vtable_name << endl;
- dat_ins="insert into "+vtable_name+" values (";
- }
- stream<<"\n";
- n++;
- }
- }
- n=1;
- stream << "\nThe list and details of Tables:\n";
- while(table_query.next())
- {
- stream << n <<". ";
- QString table_name=table_query.value(0).toString();
- QString table_sql=table_query.value(1).toString();
- stream << table_name <<"\n SQL code:\n "<< table_sql << "\n\n ";
- table_det_query.exec("pragma table_info("+table_name+")");
- qDebug()<< table_det_query.lastError() << "line 323"<<endl;
- itb_sch_ins_q.bindValue(0,table_name);
- itb_sch_ins_q.bindValue(1,table_sql);
- itb_sch_ins_q.exec();
- qDebug()<< itb_sch_ins_q.lastError() << "line 328" << endl;
- itblcol_query.exec("create table "+table_name+"_d (coln varchar,dtype varchar,prim int)");
- qDebug() << itblcol_query.lastError() << "line 331" << endl;
- stream<<"Columns and their datatypes:\n ";
- itb_col_ins_q.prepare("insert into "+table_name+"_d values(?,?,?)");
- itb_dat_q.exec("create table "+table_name+" ()");
- qDebug() << itb_dat_q.lastError() << "line 338" << endl;
- int j=0;
- while(table_det_query.next())
- {
- QString table_det1=table_det_query.value(1).toString();
- QString table_det2=table_det_query.value(2).toString();
- int table_det3=table_det_query.value(5).toInt();
- stream << table_det1 << " " <<table_det2<<" ";
- if(table_det3 != 1)
- stream << "\n ";
- else
- stream <<"PRIMARY KEY\n ";
- itb_col_ins_q.bindValue(0,table_det1);
- itb_col_ins_q.bindValue(1,table_det2);
- itb_col_ins_q.bindValue(2,table_det3);
- itb_col_ins_q.exec();
- qDebug() << itb_col_ins_q.lastError() << "line 355" << endl;
- if(table_det2.contains("int", Qt::CaseInsensitive))
- table_det2="int";
- else if(table_det2.contains("blob", Qt::CaseInsensitive))
- table_det2="bytea";
- else if(!table_det2.compare(""))
- table_det2="varchar";
- itb_dat_col_ins_q.exec("alter table "+table_name+" ADD "+table_det1+" "+table_det2);
- qDebug() << itb_dat_col_ins_q.lastError() << "line 365" << endl;
- j++;
- }
- stream<<"\n List of foreign keys:\n ";
- foreign_key_query.exec("pragma foreign_key_list("+table_name+")");
- qDebug()<< foreign_key_query.lastError() << "line 370"<<endl;
- while(foreign_key_query.next())
- {
- QString table_foreign0=foreign_key_query.value(3).toString();
- QString table_foreign1=foreign_key_query.value(2).toString();
- QString table_foreign2=foreign_key_query.value(4).toString();
- stream<<table_foreign0<< " REFERENCES " << table_foreign1 <<"("<<table_foreign2<<")"<<"\n ";
- iforkey_ins_q.bindValue(0,table_name);
- iforkey_ins_q.bindValue(1,table_foreign0);
- iforkey_ins_q.bindValue(2,table_foreign1);
- iforkey_ins_q.bindValue(3,table_foreign2);
- iforkey_ins_q.exec();
- qDebug() << iforkey_ins_q.lastError() << "line 384" << endl;
- }
- stream<<"\n The list of index associated with this table:\n ";
- index_list_query.exec("pragma index_list("+table_name+")");
- qDebug()<< index_list_query.lastError() << "line 390"<<endl;
- while(index_list_query.next())
- {
- QString table_index1=index_list_query.value(1).toString();
- int table_index2=index_list_query.value(2).toInt();
- stream << table_index1 << " ";
- if(table_index2 == 1)
- stream << "UNIQUE";
- stream<<"\n ";
- }
- stream <<"\n The data:\n";
- tb_data_q.exec("select * from "+table_name);
- qDebug() << tb_data_q.lastError() << "line 405" << endl;
- QString dat_ins="insert into "+table_name+" values (";
- while(tb_data_q.next())
- {
- for(int k=0;k<j;k++)
- {
- QString data_str=tb_data_q.value(k).toString();
- if(k!=(j-1))
- stream << data_str << "|";
- else
- stream << data_str << "\n";
- if(!data_str.compare(""))
- data_str="null";
- else
- data_str="'"+data_str+"'";
- dat_ins=dat_ins+data_str+",";
- }
- dat_ins.append(")");
- int cpos=dat_ins.indexOf(",)");
- dat_ins.replace(cpos,1,"");
- itb_dat_ins_q.exec(dat_ins);
- qDebug() << itb_dat_ins_q.lastError() << "line 430 " <<table_name << endl;
- dat_ins="insert into "+table_name+" values (";
- }
- stream << "\n";
- n++;
- }
- }
- else if(!sch_str[k].compare("view"))
- {
- n=1;
- view_query.exec("select name,tbl_name,sql from sqlite_master where type='"+sch_str[k]+"' order by name asc");
- qDebug()<< view_query.lastError() << "line 441" <<endl;
- QSqlQuery iview_ins_q(db_int),iview_col_q(db_int),iview_col_ins_q(db_int),iview_create_q(db_int);
- iview_ins_q.prepare("insert into viewsch values(?,?)");
- stream << "\nThe list and details of View:\n";
- while(view_query.next())
- {
- stream << n <<". ";
- QString view_name=view_query.value(0).toString();
- QString view_sql=view_query.value(2).toString();
- stream << view_name <<"\n SQL code:\n "<< view_sql << "\n\n ";
- table_det_query.exec("pragma table_info("+view_name+")");
- qDebug()<< table_det_query.lastError() << "line 455"<<endl;
- iview_ins_q.bindValue(0,view_name);
- iview_ins_q.bindValue(1,view_sql);
- iview_ins_q.exec();
- qDebug() << iview_ins_q.lastError() << "line 460" << endl;
- iview_col_q.exec("create table "+view_name+"_d (colname varchar,coltype varchar)");
- qDebug() << iview_col_q.lastError() << "line 463" << endl;
- stream<<"Columns and their datatypes:\n ";
- iview_col_ins_q.prepare("insert into "+view_name+"_d values (?,?)");
- while(table_det_query.next())
- {
- QString view_det1=table_det_query.value(1).toString();
- QString view_det2=table_det_query.value(2).toString();
- stream << view_det1 << " " <<view_det2<<"\n ";
- iview_col_ins_q.bindValue(0,view_det1);
- iview_col_ins_q.bindValue(1,view_det2);
- iview_col_ins_q.exec();
- qDebug() << iview_col_ins_q.lastError() << "line 477" << endl;
- }
- iview_create_q.exec(view_sql);
- qDebug() << iview_create_q.lastError() << "line 480" << endl;
- n++;
- }
- }
- else if( !sch_str[k].compare("index"))
- {
- stream << "\nThe list and details of " << sch_str[k] <<":\n";
- index_query.exec("select name,tbl_name,sql from sqlite_master where type='"+sch_str[k]+"' order by name asc");
- qDebug()<< index_query.lastError() << "line 488" <<endl;
- QSqlQuery iidx_ins_q(db_int);
- iidx_ins_q.prepare("insert into indexsch values(?,?,?,?)");
- while(index_query.next())
- {
- stream << n <<". ";
- QString index_name=index_query.value(0).toString();
- QString index_table=index_query.value(1).toString();
- QString index_sql=index_query.value(2).toString();
- stream<<index_name <<"\n associated with table: "<< index_table;
- index_info_query.exec("pragma index_info("+index_name+")");
- qDebug()<< index_info_query.lastError() << "line 501"<<endl;
- if(index_info_query.next())
- {
- QString index_col=index_info_query.value(2).toString();
- stream<<" column: "<<index_col;
- stream<<"\n SQL code:\n "<<index_sql<<"\n\n";
- iidx_ins_q.bindValue(0,index_name);
- iidx_ins_q.bindValue(1,index_table);
- iidx_ins_q.bindValue(2,index_col);
- iidx_ins_q.bindValue(3,index_sql);
- iidx_ins_q.exec();
- qDebug() << iidx_ins_q.lastError() << "line 514" << endl;
- }
- n++;
- }
- }
- else
- {
- stream << "\nThe list and details of " << sch_str[k] <<":\n";
- trigger_query.exec("select name,tbl_name,sql from sqlite_master where type='"+sch_str[k]+"' order by name asc");
- qDebug()<< trigger_query.lastError() << "line 523" <<endl;
- QSqlQuery itrig_ins_q(db_int);
- itrig_ins_q.prepare("insert into trigsch values(?,?,?)");
- while(trigger_query.next())
- {
- stream << n <<". ";
- QString trig_name=trigger_query.value(0).toString();
- QString trig_tbl=trigger_query.value(1).toString();
- QString trig_sql=trigger_query.value(2).toString();
- stream << trig_name <<"\n On table: "<<trig_tbl<<"\n SQL code:\n "<< trig_sql << "\n\n";
- itrig_ins_q.bindValue(0,trig_name);
- itrig_ins_q.bindValue(1,trig_tbl);
- itrig_ins_q.bindValue(2,trig_sql);
- itrig_ins_q.exec();
- qDebug() << itrig_ins_q.lastError() << "line 540" << endl;
- n++;
- }
- }
- k++;
- }
- qDebug() << "***********************************************************" << "\n\n";
- f1.close();
- fclose (stderr);
- db_source.close();
- db_int.close();
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement