/*************************************************************************** * Copyright (C) 2003 by * * Unai Garro (ugarro@users.sourceforge.net) * * Cyril Bosselut (bosselut@b1project.com) * * Jason Kivlighn (jkivlighn@gmail.com) * * * * Copyright (C) 2006 Jason Kivlighn (jkivlighn@gmail.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. * ***************************************************************************/ #include "psqlrecipedb.h" #include #include #include #include #include #include //Note: PostgreSQL's database names are always lowercase PSqlRecipeDB::PSqlRecipeDB( const TQString& host, const TQString& user, const TQString& pass, const TQString& DBname, int port ) : TQSqlRecipeDB( host, user, pass, DBname.lower(), port ) {} PSqlRecipeDB::~PSqlRecipeDB() {} void PSqlRecipeDB::createDB() { TQString real_db_name = database->databaseName(); //we have to be connected to some database in order to create the Krecipes database //so long as the permissions given are allowed access to "template1', this works database->setDatabaseName( "template1" ); if ( database->open() ) { TQSqlQuery query( TQString( "CREATE DATABASE %1" ).arg( real_db_name ), database ); if ( !query.isActive() ) kdDebug() << "create query failed: " << database->lastError().databaseText() << endl; database->close(); } else kdDebug() << "create open failed: " << database->lastError().databaseText() << endl; database->setDatabaseName( real_db_name ); } TQStringList PSqlRecipeDB::backupCommand() const { TDEConfig *config = TDEGlobal::config(); config->setGroup("Server"); TQStringList command; command<readEntry( "PgDumpPath", "pg_dump" )<<"-d"<databaseName() <<"-U"<readEntry( "Username" ); int port = config->readNumEntry( "Port", 0 ); if ( port > 0 ) command<<"-p"<setGroup("Server"); TQStringList command; command<readEntry( "PsqlPath", "psql" )<databaseName() <<"-U"<readEntry( "Username" ); int port = config->readNumEntry( "Port", 0 ); if ( port > 0 ) command<<"-p"<transaction(); addColumn("CREATE TABLE %1 (recipe_id INTEGER, ingredient_id INTEGER, amount FLOAT, %2 unit_id INTEGER, prep_method_id INTEGER, order_index INTEGER, group_id INTEGER);","amount_offset FLOAT","'0'","ingredient_list",3); TQSqlQuery query(TQString::null,database); query.exec( "CREATE INDEX ridil_index ON ingredient_list USING BTREE (recipe_id);" ); query.exec( "CREATE INDEX iidil_index ON ingredient_list USING BTREE (ingredient_id);"); query.exec( "UPDATE db_info SET ver='0.81',generated_by='Krecipes SVN (20050816)';" ); if ( !database->commit() ) kdDebug()<<"Update to 0.81 failed. Maybe you should try again."<transaction(); //==================add a columns to 'recipes' to allow yield range + yield type database->exec( "CREATE TABLE recipes_copy (id SERIAL NOT NULL PRIMARY KEY,title CHARACTER VARYING, persons INTEGER, instructions TEXT, photo TEXT, prep_time TIME);" ); TQSqlQuery copyQuery = database->exec( "SELECT id,title,persons,instructions,photo,prep_time FROM recipes;" ); if ( copyQuery.isActive() ) { while ( copyQuery.next() ) { TQSqlQuery query(TQString::null,database); query.prepare( "INSERT INTO recipes_copy VALUES (?, ?, ?, ?, ?, ?)" ); query.addBindValue( copyQuery.value( 0 ) ); query.addBindValue( copyQuery.value( 1 ) ); query.addBindValue( copyQuery.value( 2 ) ); query.addBindValue( copyQuery.value( 3 ) ); query.addBindValue( copyQuery.value( 4 ) ); query.addBindValue( copyQuery.value( 5 ) ); query.exec(); emit progress(); } } database->exec( "DROP TABLE recipes" ); database->exec( "CREATE TABLE recipes (id SERIAL NOT NULL PRIMARY KEY,title CHARACTER VARYING, yield_amount FLOAT, yield_amount_offset FLOAT, yield_type_id INTEGER DEFAULT '-1', instructions TEXT, photo TEXT, prep_time TIME);" ); copyQuery = database->exec( "SELECT id,title,persons,instructions,photo,prep_time FROM recipes_copy" ); if ( copyQuery.isActive() ) { while ( copyQuery.next() ) { TQSqlQuery query(TQString::null,database); query.prepare( "INSERT INTO recipes VALUES (?, ?, ?, ?, ?, ?, ?, ?)" ); query.addBindValue( copyQuery.value( 0 ) ); //id query.addBindValue( copyQuery.value( 1 ) ); //title query.addBindValue( copyQuery.value( 2 ) ); //persons, now yield_amount query.addBindValue( 0 ); //yield_amount_offset query.addBindValue( -1 ); //yield_type_id query.addBindValue( copyQuery.value( 3 ) ); //instructions query.addBindValue( copyQuery.value( 4 ) ); //photo query.addBindValue( copyQuery.value( 5 ) ); //prep_time query.exec(); emit progress(); } } database->exec( "DROP TABLE recipes_copy" ); database->exec( "UPDATE db_info SET ver='0.82',generated_by='Krecipes SVN (20050902)';" ); if ( !database->commit() ) kdDebug()<<"Update to 0.82 failed. Maybe you should try again."<transaction(); //====add a id columns to 'ingredient_list' to identify it for the prep method list database->exec( "ALTER TABLE ingredient_list RENAME TO ingredient_list_copy;" ); database->exec( "CREATE TABLE ingredient_list (id SERIAL NOT NULL PRIMARY KEY, recipe_id INTEGER, ingredient_id INTEGER, amount FLOAT, amount_offset FLOAT, unit_id INTEGER, order_index INTEGER, group_id INTEGER);" ); TQSqlQuery copyQuery = database->exec( "SELECT recipe_id,ingredient_id,amount,amount_offset,unit_id,prep_method_id,order_index,group_id FROM ingredient_list_copy" ); if ( copyQuery.isActive() ) { while ( copyQuery.next() ) { int ing_list_id = getNextInsertID("ingredient_list","id"); TQSqlQuery query(TQString::null,database); query.prepare( "INSERT INTO ingredient_list VALUES (?, ?, ?, ?, ?, ?, ?, ?)" ); query.addBindValue( ing_list_id ); query.addBindValue( copyQuery.value( 0 ) ); query.addBindValue( copyQuery.value( 1 ) ); query.addBindValue( copyQuery.value( 2 ) ); query.addBindValue( copyQuery.value( 3 ) ); query.addBindValue( copyQuery.value( 4 ) ); query.addBindValue( copyQuery.value( 6 ) ); query.addBindValue( copyQuery.value( 7 ) ); query.exec(); int prep_method_id = copyQuery.value( 5 ).toInt(); if ( prep_method_id != -1 ) { query.prepare( "INSERT INTO prep_method_list VALUES (?, ?, ?);" ); query.addBindValue( ing_list_id ); query.addBindValue( prep_method_id ); query.addBindValue( 1 ); query.exec(); } emit progress(); } } database->exec( "DROP TABLE ingredient_list_copy" ); database->exec( "CREATE INDEX ridil_index ON ingredient_list USING BTREE (recipe_id);" ); database->exec( "CREATE INDEX iidil_index ON ingredient_list USING BTREE (ingredient_id);" ); database->exec( "UPDATE db_info SET ver='0.83',generated_by='Krecipes SVN (20050909)';" ); if ( !database->commit() ) { kdDebug()<<"Update to 0.83 failed. Maybe you should try again."<transaction(); database->exec( "ALTER TABLE recipes ADD COLUMN ctime TIMESTAMP" ); database->exec( "ALTER TABLE recipes ADD COLUMN mtime TIMESTAMP" ); database->exec( "ALTER TABLE recipes ADD COLUMN atime TIMESTAMP" ); database->exec( "UPDATE recipes SET ctime=CURRENT_TIMESTAMP, mtime=CURRENT_TIMESTAMP, atime=CURRENT_TIMESTAMP;" ); database->exec( "UPDATE db_info SET ver='0.84',generated_by='Krecipes SVN (20050913)';" ); if ( !database->commit() ) { kdDebug()<<"Update to 0.84 failed. Maybe you should try again."<transaction(); database->exec( "UPDATE db_info SET ver='0.85',generated_by='Krecipes SVN (20050926)';" ); if ( !database->commit() ) { kdDebug()<<"Update to 0.85 failed. Maybe you should try again."<transaction(); database->exec( "CREATE INDEX gidil_index ON ingredient_list USING BTREE (group_id);" ); TQSqlQuery query( "SELECT id,name FROM ingredient_groups ORDER BY name", database ); TQString last; int lastID; if ( query.isActive() ) { while ( query.next() ) { TQString name = query.value(1).toString(); int id = query.value(0).toInt(); if ( last == name ) { TQString command = TQString("UPDATE ingredient_list SET group_id=%1 WHERE group_id=%2").arg(lastID).arg(id); database->exec(command); command = TQString("DELETE FROM ingredient_groups WHERE id=%1").arg(id); database->exec(command); } last = name; lastID = id; emit progress(); } } database->exec( "UPDATE db_info SET ver='0.86',generated_by='Krecipes SVN (20050928)';" ); if ( !database->commit() ) kdDebug()<<"Update to 0.86 failed. Maybe you should try again."<exec( TQString("INSERT INTO rating_criteria VALUES (1,'%1')").arg(i18n("Overall")) ); database->exec( TQString("INSERT INTO rating_criteria VALUES (2,'%1')").arg(i18n("Taste") ) ); database->exec( TQString("INSERT INTO rating_criteria VALUES (3,'%1')").arg(i18n("Appearance") ) ); database->exec( TQString("INSERT INTO rating_criteria VALUES (4,'%1')").arg(i18n("Originality") ) ); database->exec( TQString("INSERT INTO rating_criteria VALUES (5,'%1')").arg(i18n("Ease of Preparation") ) ); database->exec( "UPDATE db_info SET ver='0.87',generated_by='Krecipes SVN (20051014)'" ); } if ( tqRound(version*100) < 90 ) { database->exec("UPDATE db_info SET ver='0.9',generated_by='Krecipes 0.9'"); } if ( tqRound(version*100) < 91 ) { database->exec("CREATE index parent_id_index ON categories USING BTREE(parent_id)"); database->exec("UPDATE db_info SET ver='0.91',generated_by='Krecipes SVN (20060526)'"); } if ( tqRound(version*100) < 92 ) { database->transaction(); //==================add a columns to 'units' to allow unit abbreviations database->exec( "ALTER TABLE units RENAME TO units_copy" ); int nextval = -1; TQSqlQuery getID( "SELECT nextval('units_id_seq')", database ); if ( getID.isActive() && getID.first() ) nextval = getID.value( 0 ).toInt(); if ( nextval == -1 ) kdDebug() << "Database update failed! Unable to update units sequence." << endl; database->exec( "CREATE TABLE units (id SERIAL NOT NULL PRIMARY KEY, name CHARACTER VARYING, name_abbrev CHARACTER VARYING, plural CHARACTER VARYING, plural_abbrev CHARACTER VARYING )" ); TQSqlQuery copyQuery = database->exec( "SELECT id,name,plural FROM units_copy" ); if ( copyQuery.isActive() ) { while ( copyQuery.next() ) { TQSqlQuery query(TQString::null,database); query.prepare( "INSERT INTO units VALUES(?, ?, ?, ?, ?)" ); query.addBindValue( copyQuery.value( 0 ) ); query.addBindValue( copyQuery.value( 1 ) ); query.addBindValue( TQVariant() ); query.addBindValue( copyQuery.value( 2 ) ); query.addBindValue( TQVariant() ); query.exec(); emit progress(); } } database->exec( "DROP TABLE units_copy" ); database->exec( "ALTER TABLE units_id_seq1 RENAME TO units_id_seq" ); database->exec( "ALTER SEQUENCE units_id_seq RESTART WITH "+TQString::number(nextval) ); database->exec("UPDATE db_info SET ver='0.92',generated_by='Krecipes SVN (20060609)'"); if ( !database->commit() ) kdDebug()<<"Update to 0.92 failed. Maybe you should try again."<transaction(); database->exec( "ALTER TABLE ingredient_list ADD COLUMN substitute_for INTEGER" ); database->exec("UPDATE db_info SET ver='0.93',generated_by='Krecipes SVN (20060616)'"); if ( !database->commit() ) kdDebug()<<"Update to 0.93 failed. Maybe you should try again."<transaction(); database->exec( "ALTER TABLE units ADD COLUMN type INTEGER NOT NULL DEFAULT '0'" ); database->exec("UPDATE db_info SET ver='0.94',generated_by='Krecipes SVN (20060712)'"); if ( !database->commit() ) kdDebug()<<"Update to 0.94 failed. Maybe you should try again."<exec( "DROP TABLE ingredient_weights" ); createTable( "ingredient_weights" ); database->exec( "UPDATE db_info SET ver='0.95',generated_by='Krecipes SVN (20060726)'" ); } } void PSqlRecipeDB::addColumn( const TQString &new_table_sql, const TQString &new_col_info, const TQString &default_value, const TQString &table_name, int col_index ) { TQString command; command = TQString(new_table_sql).arg(table_name+"_copy").arg(TQString::null); kdDebug()<<"calling: "<