/***************************************************************************
                            QSqlDatabase.m -  description
                             -------------------
    begin                : Wed Nov 21 12:08:37 2001
    copyright            : (C) 2000-2001 Lost Highway Ltd. All rights reserved.
    email                : Lost_Highway@tipitina.demon.co.uk
    generated by         : duke@tipitina on Wed Nov 21 12:08:37 2001, using kalyptus 0.9.
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                       *
 *                                                                         *
 ***************************************************************************/

#include <qtc/qt_QString.h>
#include <qtc/qt_QCString.h>
#include <qtc/qt_QSqlDatabase.h>
#include <qtobjc/QSqlDatabase.h>
#include <qtobjc/QSqlQuery.h>


@implementation  QSqlDatabase


- (void) dealloc
{
	if ([self qt] != 0L) {
		if ([self allocatedInObjcWorld] && [self parent] == nil) {
			qt_del_QSqlDatabase((qt_QSqlDatabase *)[self qt]);
		}
		[QtSupport removeQtKey: [self qt]];
		[self setQt: 0L];
	}
	[super dealloc];
}

- (BOOL) open
{
	return (BOOL) qt_QSqlDatabase_open((qt_QSqlDatabase *)[self qt]);
}

- (BOOL) open: (NSString *)username password: (NSString *)password
{
static qt_QString * _qstring_password = 0L;
static qt_QString * _qstring_user = 0L;
	return (BOOL) qt_QSqlDatabase_open1((qt_QSqlDatabase *)[self qt], (qt_QString *) [username toQString: &_qstring_user], (qt_QString *) [password toQString: &_qstring_password]);
}

- close
{
	qt_QSqlDatabase_close((qt_QSqlDatabase *)[self qt]);
	return self;
}

- (BOOL) isOpen
{
	return (BOOL) qt_QSqlDatabase_isOpen((qt_QSqlDatabase *)[self qt]);
}

- (BOOL) isOpenError
{
	return (BOOL) qt_QSqlDatabase_isOpenError((qt_QSqlDatabase *)[self qt]);
}

- (NSArray *) tables
{
static qt_QStringList * _qlist = 0L;
	_qlist = qt_QSqlDatabase_tables((qt_QSqlDatabase *)[self qt]);
	return (NSArray *) [NSMutableArray arrayWithQStringList: _qlist];
}

- (QSqlIndex*) primaryIndex: (NSString *)tablename
{
static qt_QString * _qstring_tablename = 0L;
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlDatabase_primaryIndex((qt_QSqlDatabase *)[self qt], (qt_QString *) [tablename toQString: &_qstring_tablename]) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) recordForTable: (NSString *)tablename
{
static qt_QString * _qstring_tablename = 0L;
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlDatabase_record((qt_QSqlDatabase *)[self qt], (qt_QString *) [tablename toQString: &_qstring_tablename]) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) recordForQuery: (QSqlQuery*)query
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlDatabase_record1((qt_QSqlDatabase *)[self qt], (qt_QSqlQuery*) [query qt]) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

//- (QSqlRecordInfo*) recordInfoForTable: (NSString *)tablename
//{
//static qt_QString * _qstring_tablename = 0L;
//	return (QSqlRecordInfo*) [QtSupport objectForQtKey: qt_QSqlDatabase_recordInfo((qt_QSqlDatabase *)[self qt], (qt_QString *) [tablename toQString: &_qstring_tablename]) withClass: [QSqlRecordInfo class] allocatedInObjcWorld: YES];
//}

//- (QSqlRecordInfo*) recordInfoForQuery: (QSqlQuery*)query
//{
//	return (QSqlRecordInfo*) [QtSupport objectForQtKey: qt_QSqlDatabase_recordInfo1((qt_QSqlDatabase *)[self qt], (qt_QSqlQuery*) [query qt]) withClass: [QSqlRecordInfo class] allocatedInObjcWorld: YES];
//}

- (QSqlQuery*) exec: (NSString *)query
{
static qt_QString * _qstring_query = 0L;
	return (QSqlQuery*) [QtSupport objectForQtKey: qt_QSqlDatabase_exec((qt_QSqlDatabase *)[self qt], (qt_QString *) [query toQString: &_qstring_query]) withClass: [QSqlQuery class] allocatedInObjcWorld: YES];
}

- (QSqlQuery*) exec
{
	return (QSqlQuery*) [QtSupport objectForQtKey: qt_QSqlDatabase_exec((qt_QSqlDatabase *)[self qt], qt_QString_null()) withClass: [QSqlQuery class] allocatedInObjcWorld: YES];
}

- (QSqlError*) lastError
{
	return (QSqlError*) [QtSupport objectForQtKey: qt_QSqlDatabase_lastError((qt_QSqlDatabase *)[self qt]) withClass: [QSqlError class] allocatedInObjcWorld: YES];
}

- (BOOL) transaction
{
	return (BOOL) qt_QSqlDatabase_transaction((qt_QSqlDatabase *)[self qt]);
}

- (BOOL) commit
{
	return (BOOL) qt_QSqlDatabase_commit((qt_QSqlDatabase *)[self qt]);
}

- (BOOL) rollback
{
	return (BOOL) qt_QSqlDatabase_rollback((qt_QSqlDatabase *)[self qt]);
}

- setDatabaseName: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlDatabase_setDatabaseName((qt_QSqlDatabase *)[self qt], (qt_QString *) [name toQString: &_qstring_name]);
	return self;
}

- setUserName: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlDatabase_setUserName((qt_QSqlDatabase *)[self qt], (qt_QString *) [name toQString: &_qstring_name]);
	return self;
}

- setPassword: (NSString *)password
{
static qt_QString * _qstring_password = 0L;
	qt_QSqlDatabase_setPassword((qt_QSqlDatabase *)[self qt], (qt_QString *) [password toQString: &_qstring_password]);
	return self;
}

- setHostName: (NSString *)host
{
static qt_QString * _qstring_host = 0L;
	qt_QSqlDatabase_setHostName((qt_QSqlDatabase *)[self qt], (qt_QString *) [host toQString: &_qstring_host]);
	return self;
}

- setPort: (int)p
{
	qt_QSqlDatabase_setPort((qt_QSqlDatabase *)[self qt], (int)p);
	return self;
}

- (NSString *) databaseName
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlDatabase_databaseName((qt_QSqlDatabase *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) userName
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlDatabase_userName((qt_QSqlDatabase *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) password
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlDatabase_password((qt_QSqlDatabase *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) hostName
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlDatabase_hostName((qt_QSqlDatabase *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (NSString *) driverName
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlDatabase_driverName((qt_QSqlDatabase *)[self qt]);
	return [NSString fromQString: _qstring];
}

- (int) port
{
	return (int) qt_QSqlDatabase_port((qt_QSqlDatabase *)[self qt]);
}

- (QSqlDriver*) driver
{
	return (QSqlDriver*) [QtSupport objectForQtKey: qt_QSqlDatabase_driver((qt_QSqlDatabase *)[self qt]) withClass: [QSqlDriver class] allocatedInObjcWorld: YES];
}

+ (QSqlDatabase*) addDatabase: (NSString *)type connectionName: (NSString *)connectionName
{
static qt_QString * _qstring_connectionName = 0L;
static qt_QString * _qstring_type = 0L;
	return (QSqlDatabase*) [QtSupport objectForQtKey: qt_QSqlDatabase_addDatabase((qt_QString *) [type toQString: &_qstring_type], (qt_QString *) [connectionName toQString: &_qstring_connectionName]) withClass: [QSqlDatabase class] allocatedInObjcWorld: YES];
}

+ (QSqlDatabase*) addDatabase: (NSString *)type 
{
static qt_QString * _qstring_type = 0L;
	return (QSqlDatabase*) [QtSupport objectForQtKey: qt_QSqlDatabase_addDatabase((qt_QString *) [type toQString: &_qstring_type], "defaultConnection") withClass: [QSqlDatabase class] allocatedInObjcWorld: YES];
}

+ (QSqlDatabase*) database: (NSString *)connectionName open: (BOOL)open
{
static qt_QString * _qstring_connectionName = 0L;
	return (QSqlDatabase*) [QtSupport objectForQtKey: qt_QSqlDatabase_database((qt_QString *) [connectionName toQString: &_qstring_connectionName], (int)open) withClass: [QSqlDatabase class] allocatedInObjcWorld: YES];
}

+ (QSqlDatabase*) database
{
	return (QSqlDatabase*) [QtSupport objectForQtKey: qt_QSqlDatabase_database("defaultConnection", 1) withClass: [QSqlDatabase class] allocatedInObjcWorld: YES];
}

+ removeDatabase: (NSString *)connectionName
{
static qt_QString * _qstring_connectionName = 0L;
	qt_QSqlDatabase_removeDatabase((qt_QString *) [connectionName toQString: &_qstring_connectionName]);
	return self;
}

+ (BOOL) contains: (NSString *)connectionName
{
static qt_QString * _qstring_connectionName = 0L;
	return (BOOL) qt_QSqlDatabase_contains((qt_QString *) [connectionName toQString: &_qstring_connectionName]);
}

+ (BOOL) contains
{
	return (BOOL) qt_QSqlDatabase_contains("defaultConnection");
}

+ (NSArray *) drivers
{
static qt_QStringList * _qlist = 0L;
	_qlist = qt_QSqlDatabase_drivers();
	return (NSArray *) [NSMutableArray arrayWithQStringList: _qlist];
}

//+ registerSqlDriver: (NSString *)name dcb: (QSqlDriverCreatorBase*)dcb
//{
//static qt_QString * _qstring_name = 0L;
//	qt_QSqlDatabase_registerSqlDriver((qt_QString *) [name toQString: &_qstring_name], (qt_QSqlDriverCreatorBase*) [dcb qt]);
//	return self;
//}

- initWithText: (NSString *)type name: (NSString *)name parent: (QObject *)parent objname: (NSString *)objname
{
static qt_QString * _qstring_name = 0L;
static qt_QString * _qstring_type = 0L;
	if ([self qt] == 0L) {
		[self setQt: qt_new_QSqlDatabase((qt_QString *) [type toQString: &_qstring_type], (qt_QString *) [name toQString: &_qstring_name], (qt_QObject *) [parent qt], (char*) [objname cString])];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- initWithText: (NSString *)type name: (NSString *)name 
{
static qt_QString * _qstring_name = 0L;
static qt_QString * _qstring_type = 0L;
	if ([self qt] == 0L) {
		[self setQt: qt_new_QSqlDatabase((qt_QString *) [type toQString: &_qstring_type], (qt_QString *) [name toQString: &_qstring_name], 0, 0)];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

@end
