view C++/Driver.cpp @ 182:15b264d9e833

Add Luae class
author David Demelier <markand@malikania.fr>
date Tue, 29 Oct 2013 21:32:20 +0100
parents f0cca031bcce
children 4c746050969a
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::Error class
 * --------------------------------------------------------- */

Query::Error::Error(const std::string &error)
	: m_error(error)
{
}

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

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

void Query::assertRequest(int row, const std::string &column, ColumnType wanted)
{
	std::ostringstream oss;

	// Out of bounds?
	if (row < 0 || row >= countRows())
	{
		oss << "Invalid row index " << row;
		oss << ", expected [0.." << countRows() << "]";

		throw Error(oss.str());
	}

	// Not found or bad column?
	if (type(column) != wanted)
	{
		oss << "Invalid or not found column `" << column << "'";

		throw Error(oss.str());
	}
}

Query::~Query()
{
}

template <>
bool Query::get(int row, const std::string &column)
{
	assertRequest(row, column, ColumnType::Boolean);

	return getBoolean(row, column);
}

template <>
Date Query::get(int row, const std::string &column)
{
	assertRequest(row, column, ColumnType::Date);

	return getDate(row, column);
}

template <>
double Query::get(int row, const std::string &column)
{
	assertRequest(row, column, ColumnType::Double);

	return getDouble(row, column);
}

template <>
int Query::get(int row, const std::string &column)
{
	assertRequest(row, column, ColumnType::Integer);

	return getInt(row, column);
}

template <>
std::string Query::get(int row, const std::string &column)
{
	assertRequest(row, column, ColumnType::String);

	return getString(row, column);
}

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

Driver::Driver()
{
}

Driver::~Driver()
{
}

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