Drizzled Public API Documentation

result_set.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  * Copyright (C) 2011, Brian Aker
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *   * Redistributions of source code must retain the above copyright notice,
00011  *     this list of conditions and the following disclaimer.
00012  *   * Redistributions in binary form must reproduce the above copyright notice,
00013  *     this list of conditions and the following disclaimer in the documentation
00014  *     and/or other materials provided with the distribution.
00015  *   * Neither the name of Patrick Galbraith nor the names of its contributors
00016  *     may be used to endorse or promote products derived from this software
00017  *     without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00029  * THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include "config.h"
00033 
00034 #include <drizzled/sql/exception.h>
00035 #include <drizzled/sql/result_set.h>
00036 
00037 #include <iostream>
00038 
00039 namespace drizzled {
00040 namespace sql {
00041 
00042 static Exception exception_unknown_column("Unknown Column", "S0022", ER_BAD_FIELD_ERROR);
00043 static Exception exception_no_more_results("No additional rows founds", "S0022", ER_BAD_FIELD_ERROR);
00044 
00045 ResultSet::~ResultSet()
00046 {
00047 }
00048 
00049 const std::string ResultSet::getString(size_t column_number) const
00050 {
00051   if (not isMore(column_number))
00052     return "";
00053 
00054   return (*_current_row)[column_number].value();
00055 }
00056 
00057 bool ResultSet::isNull(size_t column_number) const
00058 {
00059   return (*_current_row)[column_number].isNull();
00060 }
00061 
00062 void ResultSet::pushException(const Exception &arg) const
00063 {
00064   if (_exceptions.empty())
00065   {
00066     _exceptions.push(arg);
00067     return;
00068   }
00069 
00070   _exceptions.front().setNextException(arg);
00071 }
00072 
00073 bool ResultSet::isMore() const
00074 {
00075   if (_current_row == _results.end())
00076   {
00077     pushException(exception_no_more_results);
00078     return false;
00079   }
00080 
00081   return true;
00082 }
00083 
00084 bool ResultSet::isMore(size_t column_number) const
00085 {
00086   if (column_number >= _meta_data.getColumnCount())
00087   {
00088     pushException(exception_unknown_column);
00089 
00090     return false;
00091   }
00092 
00093   return isMore();
00094 }
00095 
00096 bool ResultSet::error() const
00097 {
00098   return not _exceptions.empty();
00099 }
00100 
00101 sql::Exception ResultSet::getException() const
00102 {
00103   return _exceptions.empty() ? sql::Exception() : _exceptions.front();
00104 }
00105 
00106 const ResultSetMetaData &ResultSet::getMetaData() const
00107 {
00108   return _meta_data;
00109 }
00110 
00111 void ResultSet::createRow()
00112 {
00113   assert(_meta_data.getColumnCount());
00114   _results.resize(_results.size() +1);
00115   _results.back().resize(_meta_data.getColumnCount());
00116 }
00117 
00118 void ResultSet::setColumn(size_t column_number, const std::string &arg)
00119 {
00120   assert(column_number < _meta_data.getColumnCount());
00121   assert(_results.back().at(column_number).isNull() == false); // ie the default value
00122   assert(_results.back().at(column_number).value().empty() == true); // ie no value has been set yet
00123   _results.back().at(column_number).set_value(arg);
00124 }
00125 
00126 void ResultSet::setColumnNull(size_t column_number)
00127 {
00128   assert(column_number < _meta_data.getColumnCount());
00129   assert(_results.back().at(column_number).isNull() == false); // ie the default value
00130   assert(_results.back().at(column_number).value().empty() == true); // ie no value has been set yet
00131   _results.back().at(column_number).set_null();
00132 }
00133 
00134 bool ResultSet::next() const
00135 {
00136   if (not _has_next_been_called)
00137   {
00138     _current_row= _results.begin();
00139     _has_next_been_called= true;
00140   }
00141   else
00142   {
00143     _current_row++;
00144   }
00145 
00146   if (_current_row == _results.end())
00147     return false;
00148 
00149   return true;
00150 }
00151 
00152 std::ostream& operator<<(std::ostream& output, const ResultSet &result_set)
00153 {
00154   while (result_set.next())
00155   {
00156     for (size_t x= 0; x < result_set.getMetaData().getColumnCount(); x++)
00157     {
00158       if (result_set.isNull(x))
00159       {
00160         output << "<null>" << '\t';
00161       }
00162       else 
00163       {
00164         output << result_set.getString(x) << '\t';
00165       }
00166     }
00167     output << std::endl;
00168   }
00169 
00170   return output;
00171 }
00172 
00173 } // namespace sql 
00174 } // namespace drizzled