Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
/***************************************************************************
*
* File: SQLModule.h
* Created: Sun Dec 12 1999
* (C) 1999 by David M. <captjay@superlink.net>
*
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#ifndef SQLMODULE_H
#define SQLMODULE_H
#include <qstring.h>
#include <mysql.h>
#include <qobject.h>
#include "CustomException.h"
/**
* @author David M.
* @short Transparent handling of SQL queries.
*
* the SQLModule class encapsulates access to a mySQL database.
* It provides methods to access, update, and delete data from
* the database.
*
*/
class SQLModule : public QObject {
public:
SQLModule(); //default constructor
~SQLModule();
//Public Methods
/**
* Connect to a mySQL database with a given username and password.
*
* @param host The hostname of the machine running the database server.
* @param dbaseName The name of the database to connect to.
* @param userName the user name to use to establish connection.
* @param password the password to use for the connection.
*
* @return true if the connection succeeded, false otherwise.
*/
bool connectTo(QString host,QString dbaseName,QString userName,QString password);
/*
* Send a SELECT type query to the server. After the query, the module will
* contain the retrieved data and have the first row active.
* You can access each field with the [] operator, and advance in rows
* with the @ref nextRow() method.
*
* PRECONDITIONS: The module must have an active connection to a server.
* (succesful call to connectTo())
*
* @param query A string containing the SQL statement to execute.
*
*/
void query(const char* query);
/*
* Send an update type query to the server. Use this method for SQL statements
* that do not return rows (UPDATE, DELETE, INSERT, CREATE, DROP, etc)
*
* PRECONDITIONS: The module must have an active connection to a server.
* (succesful call to connectTo())
*
* @param query A string containing the SQL statement to execute.
*
*/
void update(const char* query);
/**
* Fetch the next row of the result set, if there is one. This function can only
* be used after a SELECT query that returned a non-empty result set.
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return true if a new row was fetched, false otherwise.
*
* Here is an example of a typical multiple-row query.
* <pre>
* sqlmod.query("SELECT * from emp where salary > 500");
* for (; !sqlmod.atEnd(); sqlmod.nextRow())
* cout << sqlmod[0] << " " << sqlmod[1] << endl;
* </pre>
*/
bool nextRow();
/**
* Determine what mode the module is in currently.
*
* @return Either of ModeUndefied, ModeQuery or ModeUpdate.
*/
int mode() const;
/**
* Determine if the result set is at end position.
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return true if there are no more rows in the result set, false otherwise.
*/
bool atEnd() const;
/**
* Determine if the query returned an empty result set.
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return true if the result set is empty, false otherwise.
*/
bool isEmpty() const;
/**
* Get the number of rows that were returned by the query.
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return The number of rows returned by the SELECT query.
*/
int rowCount() const;
/**
* Get the number of fields (columns) in the result set.
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return The number of fields in the result set.
*/
int fieldCount() const;
/**
* Get the error message recieved from the server in case
* of an SQL error.
*
* @return A string containing the error message.
*/
QString error() const;
/**
* Get a specific field of the result set, by number.
*
* PRECONDITIONS: mode() == ModeQuery ; index < fieldCount()
*
* @return A string containing the value of the field.
* a NULL field generates an empty string.
*/
QString fields(int index) const;
//Public Operators
/**
* Get a specific field of the result set, by number. Same as @ref fields().
*
* PRECONDITIONS: mode() == ModeQuery ; index < fieldCount()
*
* @return A string containing the value of the field.
* a NULL field generates an empty string.
*/
QString operator[](int index);
/**
* Move to the next row in the result set. Same as @ref nextRow().
*
* PRECONDITIONS: mode() == ModeQuery
*
* @return true if the module could move to the next row, or
* false if it is at the end of the set.
*/
bool operator++();
bool operator++(int);
//Public Constants
static const int ModeUndefined = 0;
static const int ModeQuery = 1;
static const int ModeUpdate = 2;
protected:
//Protected Fields
MYSQL* _mysql; //main MySQL handle
MYSQL_RES* _res; //result set for a SELECT query
MYSQL_ROW _row; //Holds the rows fetched by a SELECT query
QString _error; //Error message
int _rowCount; //Number of rows matched
int _rowsFetched; //Number of rows currently fetched
int _numFields; //Number of fields in one row
int _mode; //Current control's mode
};
/**
* @short Exception for SQL errors.
* This class is used to represent errors related to SQL query handling.
*/
class ESQLError : public CustomException {
Q_OBJECT
public:
ESQLError(QString message);
};
#endif
Generated by: nightsky@centauri on Sat Jan 15 23:06:10 2000, using kdoc 2.0a30. |