view C++/Driver.cpp @ 269:44dcc198bf0c

Dynlib (test): Add dl library for Linux
author David Demelier <markand@malikania.fr>
date Fri, 17 Oct 2014 14:20:00 +0200
parents bb0a7d1a3f86
children
line wrap: on
line source

/*
 * Driver.cpp -- generic SQL driver access
 *
 * Copyright (c) 2013, 2014 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 <stdexcept>
#include <sstream>

#include "Driver.h"

/* ---------------------------------------------------------
 * DriverQuery class
 * ---------------------------------------------------------*/

DriverQuery::DriverQuery(Ptr impl)
	: m_impl(impl)
{
}

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

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

		throw std::runtime_error(oss.str());
	}

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

		throw std::runtime_error(oss.str());
	}
}

DriverQuery::~DriverQuery()
{
}

DriverColumn DriverQuery::type(const std::string &column) const
{
	return m_impl->type(column);
}

int DriverQuery::countRows()
{
	return m_impl->countRows();
}

int DriverQuery::countColumns()
{
	return m_impl->countColumns();
}

bool DriverQuery::isNull(int row, const std::string &column)
{
	return m_impl->isNull(row, column);
}

void DriverQuery::dump()
{
	m_impl->dump();
}

/* --------------------------------------------------------
 * DriverRequest class
 * -------------------------------------------------------- */

DriverRequest::DriverRequest(Ptr impl, const std::string &command)
	: m_pos(0)
	, m_params(0)
	, m_command(command)
	, m_impl(impl)
{
	int i = -1;

	while ((i = command.find('#', i + 1)) != std::string::npos)
		++ m_params;
}

DriverRequest::~DriverRequest()
{
}

void DriverRequest::assertCorrect()
{
	if (m_params <= 0)
		throw std::runtime_error("no more arguments to set");
}

void DriverRequest::setValue(const std::string &value)
{
	assertCorrect();

	m_pos = m_command.find('#', m_pos);
	m_command.replace(m_pos, 1, value);
	m_pos += value.length();
	m_params --;
}

DriverRequest::operator std::string()
{
	return m_command;
}

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

void Driver::connect(const Params &params)
{
	m_impl->connect(params);
}

DriverRequest Driver::prepare(const std::string &command)
{
	return m_impl->prepare(command);
}

DriverQuery Driver::query(const std::string &sql)
{
	return m_impl->query(sql);
}

DriverQuery Driver::query(DriverRequest request)
{
	return query(static_cast<std::string>(request));
}

std::string Driver::description() const
{
	return m_impl->description();
}

std::string Driver::version() const
{
	return m_impl->version();
}