diff C++/Driver.h @ 171:e47c4e9e3f9d

Add SQL drivers
author David Demelier <markand@malikania.fr>
date Tue, 10 Sep 2013 17:28:32 +0200
parents
children a61cddaf7547
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C++/Driver.h	Tue Sep 10 17:28:32 2013 +0200
@@ -0,0 +1,296 @@
+/*
+ * Driver.h -- generic SQL driver access
+ *
+ * Copyright (c) 2013, David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _DRIVER_H_
+#define _DRIVER_H_
+
+#include <iostream>
+#include <map>
+#include <memory>
+#include <string>
+
+class Query;
+
+class Date
+{
+private:
+	time_t m_timestamp;
+
+public:
+	Date()
+	{
+	}
+
+	Date(time_t)
+	{
+	}
+
+	~Date()
+	{
+	}
+};
+
+/**
+ * @enum ColumnType
+ * @brief The column type request
+ *
+ * Used for the drivers.
+ */
+enum class ColumnType {
+	Boolean,			//! bool or 0 / 1
+	Date,				//! date see Common/Date.h
+	Double,				//! double
+	Integer,			//! 32 or 64 bit int
+	String,				//! varchar to std::string
+};
+
+/**
+ * @enum QueryCheck
+ * @brief Result enumeration for check* functions
+ *
+ * This is used for the Query drivers check functions so we can now which
+ * kind of error happened.
+ */
+enum class QueryCheck {
+	NoError = 0,			//! success
+	InvalidColumn,			//! non existent column
+	InvalidRow,			//! out of range row
+	InvalidType,			//! bad column type requested
+};
+
+/**
+ * @class QueryError
+ * @brief Query exception on driver error
+ *
+ * Exception thrown usually when an error appeared in driver.
+ */
+class QueryError : public std::exception
+{
+private:
+	std::string m_error;
+public:
+	QueryError();
+
+	QueryError(const std::string &error);
+
+	~QueryError();
+
+	virtual const char *what() const throw();
+};
+
+/**
+ * @class Query
+ * @brief Class for querying the database
+ */
+class Query {
+private:
+	/**
+	 * Check if the request is valid and throws an exception
+	 * on error.
+	 *
+	 * @param row the row number
+	 * @param column the column name
+	 * @param check the result
+	 * @throw Query::ErrorException on erro
+	 */
+	void checkValidRequest(int row, const std::string &column, QueryCheck check);
+
+protected:
+	/**
+	 * Check if the request is correct, if the row is not out of range
+	 * if the column exist and if the type is correct.
+	 *
+	 * @param row the row number
+	 * @param column the column name
+	 * @param type the type requested
+	 * @return true if request is correct
+	 */
+	virtual QueryCheck checkRequest(int row, const std::string &column, ColumnType type) = 0;
+
+	/**
+	 * Get a bool.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return the value
+	 */
+	virtual bool checkBool(int row, const std::string &column) = 0;
+
+	/**
+	 * Get a Date.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return the value
+	 */
+	virtual Date checkDate(int row, const std::string &column) = 0;
+
+	/**
+	 * Get a double.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return the value
+	 */
+	virtual double checkDouble(int row, const std::string &column) = 0;
+
+	/**
+	 * Get a integer.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return the value
+	 */
+	virtual int checkInt(int row, const std::string &column) = 0;
+
+	/**
+	 * Get a string.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return the value
+	 */
+	virtual std::string checkString(int row, const std::string &column) = 0;
+
+public:
+	Query(void);
+	virtual ~Query(void);
+
+	/**
+	 * Get bool at a specified row / column.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return a boolean
+	 * @throw Query::ErrorException on error
+	 */
+	bool getBool(int row, const std::string &column);
+
+	/**
+	 * Get a Date at a specified row / column.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return a Date
+	 * @throw Query::ErrorException on error
+	 */
+	Date getDate(int row, const std::string &column);
+
+	/**
+	 * Get a double at a specified row / column.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return a double
+	 * @throw Query::ErrorException on error
+	 */
+	double getDouble(int row, const std::string &column);
+
+	/**
+	 * Get an integer at a specified row / column.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return an integer
+	 * @throw Query::ErrorException on error
+	 */
+	int getInt(int row, const std::string &column);
+
+	/**
+	 * Get a string at a specified row / column.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return a string
+	 * @throw Query::ErrorException on error
+	 */
+	std::string getString(int row, const std::string &column);
+
+	/**
+	 * Tells how many rows has been fetched.
+	 *
+	 * @return the number of rows
+	 */
+	virtual int countRows(void) = 0;
+
+	/**
+	 * Tells how many number of columns are present for each
+	 * row.
+	 *
+	 * @return the number of columns
+	 */
+	virtual int countColumns(void) = 0;
+
+	/**
+	 * Tells if the column is null or not.
+	 *
+	 * @param row the row number
+	 * @param column the column
+	 * @return an true if null
+	 */
+	virtual bool isNull(int row, const std::string &column) = 0;
+
+	/**
+	 * Dump all rows and columns.
+	 */
+	virtual void dump(void) = 0;
+};
+
+class Driver {
+protected:
+	std::string m_error;
+
+public:
+	typedef std::map<std::string, std::string> Params;
+
+	Driver();
+	virtual ~Driver();
+
+	/**
+	 * Get the error.
+	 *
+	 * @return the error
+	 */
+	const std::string & getError() const;
+
+	/**
+	 * Create a synchronous connection, it waits and block until
+	 * the connection is made up to a specified timeout max.
+	 *
+	 * @param params a list of parameters.
+	 */
+	virtual bool connect(const Params &params) = 0;
+
+	/**
+	 * Execute a query.
+	 *
+	 * @param query the SQL command
+	 * @return a result
+	 * @throw Query::Exception on failure
+	 */
+	virtual std::unique_ptr<Query> query(const std::string &command) = 0;
+
+	/**
+	 * Get the driver connection description.
+	 *
+	 * @return the description
+	 */
+	virtual std::string description() const = 0;
+};
+
+#endif // !_DRIVER_H_