/***************************************************************************
                            QSqlCursor.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_QSqlCursor.h>
#include <qtc/qt_QSqlQuery.h>
#include <qtobjc/QSqlCursor.h>
#include <qtobjc/QSqlDatabase.h>
#include <qtobjc/QSqlField.h>
#include <qtobjc/QSqlIndex.h>


@implementation  QSqlCursor


- initWithText: (NSString *)name autopopulate: (BOOL)autopopulate db: (QSqlDatabase*)db
{
static qt_QString * _qstring_name = 0L;
	if ([self qt] == 0L) {
		[self setQt: qt_new_QSqlCursor((qt_QString *) [name toQString: &_qstring_name], (int)autopopulate, (qt_QSqlDatabase*) [db qt])];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- init
{
	if ([self qt] == 0L) {
		[self setQt: qt_new_QSqlCursor(qt_QString_null(), 1, 0)];
		[self setAllocatedInObjcWorld: YES];
		[QtSupport setObject: self forQtKey: [self qt]];
	}
	[super init];
	return self;
}

- copyWithZone: (NSZone *) zone
{
	QSqlCursor * aCopy;
	aCopy = [[(QSqlCursor *)[[self class] allocWithZone: zone] setQt: qt_new_QSqlCursor1([self qt])] init];
	[aCopy setAllocatedInObjcWorld: YES];
	[QtSupport setObject: aCopy forQtKey: [aCopy qt]];
	return aCopy;
}

- (void) dealloc
{
	if ([self qt] != 0L) {
		if ([self allocatedInObjcWorld]) {
			qt_del_QSqlCursor((qt_QSqlCursor *)[self qt]);
		}
		[QtSupport removeQtKey: [self qt]];
		[self setQt: 0L];
	}
	[super dealloc];
}

- (QVariant*) value: (int)i
{
	return (QVariant*) [QtSupport objectForQtKey: qt_QSqlCursor_value((qt_QSqlCursor *)[self qt], (int)i) withClass: [QVariant class] allocatedInObjcWorld: YES];
}

- (QVariant*) valueForName: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	return (QVariant*) [QtSupport objectForQtKey: qt_QSqlCursor_value1((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name]) withClass: [QVariant class] allocatedInObjcWorld: YES];
}

- (QSqlIndex*) primaryIndex: (BOOL)prime
{
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_primaryIndex((qt_QSqlCursor *)[self qt], (int)prime) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

- (QSqlIndex*) primaryIndex
{
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_primaryIndex((qt_QSqlCursor *)[self qt], 1) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

- (QSqlIndex*) indexForFields: (NSArray *)fieldNames
{
static qt_QStringList * _qlist_fieldNames = 0L;
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_index((qt_QSqlCursor *)[self qt], (qt_QStringList*) [fieldNames toQStringList: (void *)&_qlist_fieldNames]) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

- (QSqlIndex*) indexForField: (NSString *)fieldName
{
static qt_QString * _qstring_fieldName = 0L;
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_index1((qt_QSqlCursor *)[self qt], (qt_QString *) [fieldName toQString: &_qstring_fieldName]) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

//- (QSqlIndex*) indexForField: (NSString *)fieldName
//{
//	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_index2((qt_QSqlCursor *)[self qt], (char*) [fieldName cString]) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
//}

- setPrimaryIndex: (QSqlIndex*)idx
{
	qt_QSqlCursor_setPrimaryIndex((qt_QSqlCursor *)[self qt], (qt_QSqlIndex*) [idx qt]);
	return self;
}

//- append: (QSqlFieldInfo*)fieldInfo
//{
//	qt_QSqlCursor_append((qt_QSqlCursor *)[self qt], (qt_QSqlFieldInfo*) [fieldInfo qt]);
//	return self;
//}

//- insert: (int)pos fieldInfo: (QSqlFieldInfo*)fieldInfo
//{
//	qt_QSqlCursor_insert((qt_QSqlCursor *)[self qt], (int)pos, (qt_QSqlFieldInfo*) [fieldInfo qt]);
//	return self;
//}

- remove: (int)pos
{
	qt_QSqlCursor_remove((qt_QSqlCursor *)[self qt], (int)pos);
	return self;
}

- clear
{
	qt_QSqlCursor_clear((qt_QSqlCursor *)[self qt]);
	return self;
}

- setGeneratedForName: (NSString *)name generated: (BOOL)generated
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlCursor_setGenerated((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name], (int)generated);
	return self;
}

- setGenerated: (int)i generated: (BOOL)generated
{
	qt_QSqlCursor_setGenerated1((qt_QSqlCursor *)[self qt], (int)i, (int)generated);
	return self;
}

- (QSqlRecord*) editBuffer: (BOOL)copy
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlCursor_editBuffer((qt_QSqlCursor *)[self qt], (int)copy) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) editBuffer
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlCursor_editBuffer((qt_QSqlCursor *)[self qt], 0) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) primeInsert
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlCursor_primeInsert((qt_QSqlCursor *)[self qt]) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) primeUpdate
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlCursor_primeUpdate((qt_QSqlCursor *)[self qt]) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (QSqlRecord*) primeDelete
{
	return (QSqlRecord*) [QtSupport objectForQtKey: qt_QSqlCursor_primeDelete((qt_QSqlCursor *)[self qt]) withClass: [QSqlRecord class] allocatedInObjcWorld: YES];
}

- (int) insert: (BOOL)invalidate
{
	return (int) qt_QSqlCursor_insert1((qt_QSqlCursor *)[self qt], (int)invalidate);
}

- (int) insert
{
	return (int) qt_QSqlCursor_insert1((qt_QSqlCursor *)[self qt], 1);
}

- (int) update: (BOOL)invalidate
{
	return (int) qt_QSqlCursor_update((qt_QSqlCursor *)[self qt], (int)invalidate);
}

- (int) update
{
	return (int) qt_QSqlCursor_update((qt_QSqlCursor *)[self qt], 1);
}

- (int) del: (BOOL)invalidate
{
	return (int) qt_QSqlCursor_del((qt_QSqlCursor *)[self qt], (int)invalidate);
}

- (int) del
{
	return (int) qt_QSqlCursor_del((qt_QSqlCursor *)[self qt], 1);
}

- setMode: (int)flags
{
	qt_QSqlCursor_setMode((qt_QSqlCursor *)[self qt], (int)flags);
	return self;
}

- (int) mode
{
	return (int) qt_QSqlCursor_mode((qt_QSqlCursor *)[self qt]);
}

- setCalculated: (NSString *)name calculated: (BOOL)calculated
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlCursor_setCalculated((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name], (int)calculated);
	return self;
}

- (BOOL) isCalculated: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	return (BOOL) qt_QSqlCursor_isCalculated((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name]);
}

- setTrimmed: (NSString *)name trim: (BOOL)trim
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlCursor_setTrimmed((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name], (int)trim);
	return self;
}

- (BOOL) isTrimmed: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	return (BOOL) qt_QSqlCursor_isTrimmed((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name]);
}

- (BOOL) isReadOnly
{
	return (BOOL) qt_QSqlCursor_isReadOnly((qt_QSqlCursor *)[self qt]);
}

- (BOOL) canInsert
{
	return (BOOL) qt_QSqlCursor_canInsert((qt_QSqlCursor *)[self qt]);
}

- (BOOL) canUpdate
{
	return (BOOL) qt_QSqlCursor_canUpdate((qt_QSqlCursor *)[self qt]);
}

- (BOOL) canDelete
{
	return (BOOL) qt_QSqlCursor_canDelete((qt_QSqlCursor *)[self qt]);
}

- (BOOL) select
{
	return (BOOL) qt_QSqlCursor_select((qt_QSqlCursor *)[self qt]);
}

- (BOOL) select: (QSqlIndex*)sort
{
	return (BOOL) qt_QSqlCursor_select1((qt_QSqlCursor *)[self qt], (qt_QSqlIndex*) [sort qt]);
}

- (BOOL) select: (QSqlIndex*)filter sort: (QSqlIndex*)sort
{
	return (BOOL) qt_QSqlCursor_select2((qt_QSqlCursor *)[self qt], (qt_QSqlIndex*) [filter qt], (qt_QSqlIndex*) [sort qt]);
}

- (BOOL) selectWithFilter: (NSString *)filter sort: (QSqlIndex*)sort
{
static qt_QString * _qstring_filter = 0L;
	return (BOOL) qt_QSqlCursor_select3((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], (qt_QSqlIndex*) [sort qt]);
}

- (BOOL) selectWithFilter: (NSString *)filter
{
static qt_QString * _qstring_filter = 0L;
	return (BOOL) qt_QSqlCursor_select3((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], 0);
}

- setSort: (QSqlIndex*)sort
{
	qt_QSqlCursor_setSort((qt_QSqlCursor *)[self qt], (qt_QSqlIndex*) [sort qt]);
	return self;
}

- (QSqlIndex*) sort
{
	return (QSqlIndex*) [QtSupport objectForQtKey: qt_QSqlCursor_sort((qt_QSqlCursor *)[self qt]) withClass: [QSqlIndex class] allocatedInObjcWorld: YES];
}

- setFilter: (NSString *)filter
{
static qt_QString * _qstring_filter = 0L;
	qt_QSqlCursor_setFilter((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter]);
	return self;
}

- (NSString *) filter
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlCursor_filter((qt_QSqlCursor *)[self qt]);
	return [NSString fromQString: _qstring];
}

- setName: (NSString *)name autopopulate: (BOOL)autopopulate
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlCursor_setName((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name], (int)autopopulate);
	return self;
}

- setName: (NSString *)name 
{
static qt_QString * _qstring_name = 0L;
	qt_QSqlCursor_setName((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name], 1);
	return self;
}

- (NSString *) name
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlCursor_name((qt_QSqlCursor *)[self qt]);
	return [NSString fromQString: _qstring];
}

//- (NSString *) toString: (NSString *)prefix separator: (NSString*) sep
//{
//static qt_QString * _qstring = 0L;
//static qt_QString * _qstring_sep = 0L;
//static qt_QString * _qstring_prefix = 0L;
//	_qstring = qt_QSqlCursor_toString((qt_QSqlCursor *)[self qt], (qt_QString *) [prefix toQString: &_qstring_prefix], (qt_QString *) [sep toQString: &_qstring_sep]);
//	return [NSString fromQString: _qstring];
//}

- afterSeek
{
	qt_QSqlCursor_afterSeek((qt_QSqlCursor *)[self qt]);
	return self;
}

- (BOOL) exec: (NSString *)sql
{
static qt_QString * _qstring_sql = 0L;
	return (BOOL) qt_QSqlCursor_exec((qt_QSqlCursor *)[self qt], (qt_QString *) [sql toQString: &_qstring_sql]);
}

- (QVariant*) calculateField: (NSString *)name
{
static qt_QString * _qstring_name = 0L;
	return (QVariant*) [QtSupport objectForQtKey: qt_QSqlCursor_calculateField((qt_QSqlCursor *)[self qt], (qt_QString *) [name toQString: &_qstring_name]) withClass: [QVariant class] allocatedInObjcWorld: YES];
}

- (int) updateWithFilter: (NSString *)filter invalidate: (BOOL)invalidate
{
static qt_QString * _qstring_filter = 0L;
	return (int) qt_QSqlCursor_update1((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], (int)invalidate);
}

- (int) updateWithFilter: (NSString *)filter
{
static qt_QString * _qstring_filter = 0L;
	return (int) qt_QSqlCursor_update1((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], 1);
}

- (int) deleteWithFilter: (NSString *)filter invalidate: (BOOL)invalidate
{
static qt_QString * _qstring_filter = 0L;
	return (int) qt_QSqlCursor_del1((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], (int)invalidate);
}

- (int) deleteWithFilter: (NSString *)filter
{
static qt_QString * _qstring_filter = 0L;
	return (int) qt_QSqlCursor_del1((qt_QSqlCursor *)[self qt], (qt_QString *) [filter toQString: &_qstring_filter], 1);
}

- (NSString *) toString: (NSString *)prefix field: (QSqlField*)field fieldSep: (NSString *)fieldSep
{
static qt_QString * _qstring = 0L;
static qt_QString * _qstring_fieldSep = 0L;
static qt_QString * _qstring_prefix = 0L;
	_qstring = qt_QSqlCursor_toString1((qt_QSqlCursor *)[self qt], (qt_QString *) [prefix toQString: &_qstring_prefix], (qt_QSqlField*) [field qt], (qt_QString *) [fieldSep toQString: &_qstring_fieldSep]);
	return [NSString fromQString: _qstring];
}

- (NSString *) toString: (QSqlRecord*)rec prefix: (NSString *)prefix fieldSep: (NSString *)fieldSep separator: (NSString *)sep
{
static qt_QString * _qstring = 0L;
static qt_QString * _qstring_sep = 0L;
static qt_QString * _qstring_fieldSep = 0L;
static qt_QString * _qstring_prefix = 0L;
	_qstring = qt_QSqlCursor_toString2((qt_QSqlCursor *)[self qt], (qt_QSqlRecord*) [rec qt], (qt_QString *) [prefix toQString: &_qstring_prefix], (qt_QString *) [fieldSep toQString: &_qstring_fieldSep], (qt_QString *) [sep toQString: &_qstring_sep]);
	return [NSString fromQString: _qstring];
}

- (NSString *) toString: (QSqlIndex*)i rec: (QSqlRecord*)rec prefix: (NSString *)prefix fieldSep: (NSString *)fieldSep separator: (NSString *)sep
{
static qt_QString * _qstring = 0L;
static qt_QString * _qstring_sep = 0L;
static qt_QString * _qstring_fieldSep = 0L;
static qt_QString * _qstring_prefix = 0L;
	_qstring = qt_QSqlCursor_toString3((qt_QSqlCursor *)[self qt], (qt_QSqlIndex*) [i qt], (qt_QSqlRecord*) [rec qt], (qt_QString *) [prefix toQString: &_qstring_prefix], (qt_QString *) [fieldSep toQString: &_qstring_fieldSep], (qt_QString *) [sep toQString: &_qstring_sep]);
	return [NSString fromQString: _qstring];
}

+ (int) ReadOnly
{
	return (int) QSQLCURSOR_ReadOnly;
}

+ (int) Insert
{
	return (int) QSQLCURSOR_Insert;
}

+ (int) Update
{
	return (int) QSQLCURSOR_Update;
}

+ (int) Delete
{
	return (int) QSQLCURSOR_Delete;
}

+ (int) Writable
{
	return (int) QSQLCURSOR_Writable;
}

@end

@implementation QSqlCursor (QSqlQueryImplementation)

- (BOOL) isValid
{
	return (BOOL) qt_QSqlQuery_isValid(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (BOOL) isActive
{
	return (BOOL) qt_QSqlQuery_isActive(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (BOOL) isNull: (int)field
{
	return (BOOL) qt_QSqlQuery_isNull(qt_QSqlCursor_QSqlQuery([self qt]), (int)field);
}

- (int) at
{
	return (int) qt_QSqlQuery_at(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (NSString *) lastQuery
{
static qt_QString * _qstring = 0L;
	_qstring = qt_QSqlQuery_lastQuery(qt_QSqlCursor_QSqlQuery([self qt]));
	return [NSString fromQString: _qstring];
}

- (int) numRowsAffected
{
	return (int) qt_QSqlQuery_numRowsAffected(qt_QSqlCursor_QSqlQuery([self qt]));
}

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

- (BOOL) isSelect
{
	return (BOOL) qt_QSqlQuery_isSelect(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (int) size
{
	return (int) qt_QSqlQuery_size(qt_QSqlCursor_QSqlQuery([self qt]));
}

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

- (QSqlResult*) result
{
	return (QSqlResult*) [QtSupport objectForQtKey: qt_QSqlQuery_result(qt_QSqlCursor_QSqlQuery([self qt])) withClass: [QSqlResult class] allocatedInObjcWorld: YES];
}

- (BOOL) exec: (NSString *)query
{
static qt_QString * _qstring_query = 0L;
	return (BOOL) qt_QSqlQuery_exec(qt_QSqlCursor_QSqlQuery([self qt]), (qt_QString *) [query toQString: &_qstring_query]);
}

- (QVariant*) value: (int)i
{
	return (QVariant*) [QtSupport objectForQtKey: qt_QSqlQuery_value(qt_QSqlCursor_QSqlQuery([self qt]), (int)i) withClass: [QVariant class] allocatedInObjcWorld: YES];
}

- (BOOL) seek: (int)i relative: (BOOL)relative
{
	return (BOOL) qt_QSqlQuery_seek(qt_QSqlCursor_QSqlQuery([self qt]), (int)i, (int)relative);
}

- (BOOL) seek: (int)i
{
	return (BOOL) qt_QSqlQuery_seek(qt_QSqlCursor_QSqlQuery([self qt]), (int)i, 0);
}

- (BOOL) next
{
	return (BOOL) qt_QSqlQuery_next(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (BOOL) prev
{
	return (BOOL) qt_QSqlQuery_prev(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (BOOL) first
{
	return (BOOL) qt_QSqlQuery_first(qt_QSqlCursor_QSqlQuery([self qt]));
}

- (BOOL) last
{
	return (BOOL) qt_QSqlQuery_last(qt_QSqlCursor_QSqlQuery([self qt]));
}

- beforeSeek
{
	qt_QSqlQuery_beforeSeek(qt_QSqlCursor_QSqlQuery([self qt]));
	return self;
}

- afterSeek
{
	qt_QSqlQuery_afterSeek(qt_QSqlCursor_QSqlQuery([self qt]));
	return self;
}

@end
