/*
 *  Copyright (C) 2003, Ian Reinhart Geiser <geiseri@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include <kjsembed/global.h>
#include <tqsqldatabase.h>
#include <tqsqlquery.h>
#include <tqsqlerror.h>
#include "sql_imp.h"



namespace KJSEmbed {
namespace Bindings {

SqlDatabase::SqlDatabase( TQObject *parent, const char *name): BindingObject(parent, name)
{
	connectionName = "defaultConnection";
	setJSClassName( "SqlDatabase" );
}
SqlDatabase::~SqlDatabase()
{
	TQSqlDatabase::removeDatabase(connectionName);
}
bool SqlDatabase::addDatabase ( const TQString &type, const TQString &conn )
{
	connectionName = conn;
	TQSqlDatabase *db = TQSqlDatabase::addDatabase(type, connectionName);
	if ( !db )
		return false;
	return true;
}
TQStringList SqlDatabase::drivers ()
{
	return TQSqlDatabase::drivers();
}
bool SqlDatabase::open ( )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->open();
	return false;
}
bool SqlDatabase::open ( const TQString &user, const TQString &password )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->open(user, password);
	return false;
}
void SqlDatabase::close ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
	{
		db->close();
	}
}
bool SqlDatabase::isOpen ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->isOpen();
	return false;
}
bool SqlDatabase::isOpenError ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->isOpenError();
	return true;
}
TQStringList SqlDatabase::tables ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->tables();
	return TQStringList();
}
SqlQuery *SqlDatabase::exec (const TQString &query  )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
	{
		kdDebug() <<" exec query " << query << endl;
		SqlQuery *qw = new SqlQuery(this, "query",db->exec( query ));
		//JSFactory::instance()->addType( "SqlQuery" );
		kdDebug() <<" size " << qw->size() << endl;
		kdDebug() <<" valid " << qw->isValid() << endl;
		return qw;
	}
	return 0L;
}
TQString  SqlDatabase::lastError () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
	{
		return db->lastError().text();
	}
	return "No Database Driver Loaded";
}
bool SqlDatabase::transaction ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->transaction();
	return false;
}
bool SqlDatabase::commit ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->commit();
	return false;
}
bool SqlDatabase::rollback ()
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->rollback();
	return false;
}
void SqlDatabase::setDatabaseName (const TQString &name )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		db->setDatabaseName(name);
}
void SqlDatabase::setUserName (const TQString &name )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		db->setUserName(name);
}
void SqlDatabase::setPassword (const TQString &password )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		db->setPassword(password);
}
void SqlDatabase::setHostName (const TQString &host )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		db->setHostName(host);
}
void SqlDatabase::setPort ( int p )
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		db->setPort(p);
}
TQString SqlDatabase::databaseName () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->databaseName();
	return "";
}
TQString SqlDatabase::userName () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->userName();
	return "";
}
TQString SqlDatabase::password () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->password();
	return "";
}
TQString SqlDatabase::hostName () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->hostName();
	return "";
}
TQString SqlDatabase::driverName () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->driverName();
	return "";
}
int SqlDatabase::port () const
{
	TQSqlDatabase *db = TQSqlDatabase::database(connectionName,false);
	if ( db )
		return db->port();
	return 0;
}

SqlQuery::SqlQuery( TQObject *parent, const char *name ): BindingObject(parent, name)
{
	kdDebug() << "New null SQL Query" << endl;
	m_query = TQSqlQuery();
	setJSClassName( "SqlQuery" );
}
SqlQuery::SqlQuery(  TQObject *parent, const char *name, const TQSqlQuery &q ): BindingObject(parent, name)
{
	kdDebug() << "New SQL Query with argument" << endl;
	m_query = q;
}
SqlQuery::~SqlQuery()
{
	kdDebug() << "SQL Query going away..." << endl;
}

/*SqlQuery::SqlQuery(const SqlQuery &copy) : BindingObject(copy.parent(), copy.name())
{
	m_query = copy.m_query;
}
*/
bool SqlQuery::isValid () const
{
	if ( m_query.isValid())
		kdDebug() << "Query is valid" << endl;
	else
		kdDebug() << "Query is not valid" << endl;
	return m_query.isValid();
}
bool SqlQuery::isActive () const
{
	return m_query.isActive();
}
bool SqlQuery::isNull ( int field )
{
	return m_query.isNull(field);
}
int SqlQuery::at () const
{
	return m_query.at();
}
TQString SqlQuery::lastQuery () const
{
	kdDebug() << "Last query error: " << m_query.lastQuery() << endl;
	return m_query.lastQuery();
}
int SqlQuery::numRowsAffected () const
{
	return m_query.numRowsAffected();
}
TQString  SqlQuery::lastError () const
{
	return m_query.lastError().text();
}
bool SqlQuery::isSelect () const
{
	return m_query.isSelect();
}
int SqlQuery::size () const
{
	return m_query.size();
}
bool SqlQuery::exec ( const TQString & query )
{
	return m_query.exec( query );
}
TQVariant SqlQuery::value ( int i )
{
	return m_query.value(i);
}
bool SqlQuery::seek ( int i, bool relative )
{
	return m_query.seek(i,relative);
}
bool SqlQuery::next ()
{
	return m_query.next();
}
bool SqlQuery::prev ()
{
	return m_query.prev();
}
bool SqlQuery::first ()
{
	return m_query.first();
}
bool SqlQuery::last ()
{
	return m_query.last();
}

} // namespace KJSEmbed::Bindings
} // namespace KJSEmbed

#ifndef QT_ONLY
#include "sql_imp.moc"
#endif
