00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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);
00122 assert(_results.back().at(column_number).value().empty() == true);
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);
00130 assert(_results.back().at(column_number).value().empty() == true);
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 }
00174 }