view C++/Driver.cpp @ 172:a61cddaf7547

Rename Driver postgres
author David Demelier <markand@malikania.fr>
date Wed, 11 Sep 2013 15:27:00 +0200
parents e47c4e9e3f9d
children f0cca031bcce
line wrap: on
line source

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

#include <map>
#include <string>
#include <sstream>

#include "Driver.h"

/* ---------------------------------------------------------
 * Query class
 * ---------------------------------------------------------*/

Query::Query()
{
}

Query::~Query()
{
}

void Query::checkValidRequest(int row, const std::string &column, QueryCheck check)
{
	std::ostringstream oss;

	switch (check)
	{
	case QueryCheck::InvalidColumn:
		oss << "Invalid column '" << column << "'";
		throw QueryError(oss.str());
	case QueryCheck::InvalidRow:
		oss << "Invalid row number '" << row << "'";
		throw QueryError(oss.str());
	case QueryCheck::InvalidType:
		oss << "Column " << column;
		oss << " type does not match requested type";

		throw QueryError(oss.str());
	default:
		break;
	}
}

bool Query::getBool(int row, const std::string &column)
{
	// Throw an exception on bad arguments
	checkValidRequest(row, column,
	    checkRequest(row, column, ColumnType::Boolean));

	return checkBool(row, column);
}

Date Query::getDate(int row, const std::string &column)
{
	// Throw an exception on bad arguments
	checkValidRequest(row, column,
	    checkRequest(row, column, ColumnType::Date));

	return checkDate(row, column);
}

double Query::getDouble(int row, const std::string &column)
{
	// Throw an exception on bad arguments
	checkValidRequest(row, column,
	    checkRequest(row, column, ColumnType::Double));

	return checkDouble(row, column);
}

int Query::getInt(int row, const std::string &column)
{
	// Throw an exception on bad arguments
	checkValidRequest(row, column,
	    checkRequest(row, column, ColumnType::Integer));

	return checkInt(row, column);
}

std::string Query::getString(int row, const std::string &column)
{
	// Throw an exception on bad arguments
	checkValidRequest(row, column,
	    checkRequest(row, column, ColumnType::String));

	return checkString(row, column);
}

/* ---------------------------------------------------------
 * QueryError class
 * --------------------------------------------------------- */

QueryError::QueryError()
{
}

QueryError::QueryError(const std::string &error)
	: m_error(error)
{
}

QueryError::~QueryError()
{
}

const char *QueryError::what() const throw()
{
	return m_error.c_str();
}

/* --------------------------------------------------------
 * Driver class
 * -------------------------------------------------------- */

Driver::Driver()
{
}

Driver::~Driver()
{
}

const std::string &Driver::getError() const
{
	return m_error;
}