Commit f1243acd authored by Vadim Zeitlin's avatar Vadim Zeitlin

Add helper function for copying string to PostgreSQL backend

This helper is trivial but it's still better not to repeat these 2 lines
4 times.
parent 5e6f37e3
...@@ -204,6 +204,10 @@ struct postgresql_standard_use_type_backend : details::standard_use_type_backend ...@@ -204,6 +204,10 @@ struct postgresql_standard_use_type_backend : details::standard_use_type_backend
int position_; int position_;
std::string name_; std::string name_;
char * buf_; char * buf_;
private:
// Allocate buf_ of appropriate size and copy string data into it.
void copy_from_string(std::string const& s);
}; };
struct postgresql_vector_use_type_backend : details::vector_use_type_backend struct postgresql_vector_use_type_backend : details::vector_use_type_backend
......
...@@ -71,11 +71,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind) ...@@ -71,11 +71,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind)
} }
break; break;
case x_stdstring: case x_stdstring:
{ copy_from_string(exchange_type_cast<x_stdstring>(data_));
std::string const& s = exchange_type_cast<x_stdstring>(data_);
buf_ = new char[s.size() + 1];
std::strcpy(buf_, s.c_str());
}
break; break;
case x_short: case x_short:
{ {
...@@ -114,12 +110,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind) ...@@ -114,12 +110,7 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind)
} }
break; break;
case x_double: case x_double:
{ copy_from_string(double_to_cstring(exchange_type_cast<x_double>(data_)));
std::string const s = double_to_cstring(exchange_type_cast<x_double>(data_));
buf_ = new char[s.size() + 1];
std::strcpy(buf_, s.c_str());
}
break; break;
case x_stdtm: case x_stdtm:
{ {
...@@ -163,19 +154,13 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind) ...@@ -163,19 +154,13 @@ void postgresql_standard_use_type_backend::pre_use(indicator const * ind)
case x_xmltype: case x_xmltype:
{ {
xml_type * xml = static_cast<xml_type *>(data_); xml_type * xml = static_cast<xml_type *>(data_);
std::string * s = &(xml->value); copy_from_string(xml->value);
buf_ = new char[s->size() + 1];
std::strcpy(buf_, s->c_str());
} }
break; break;
case x_longstring: case x_longstring:
{ {
long_string * ls = static_cast<long_string *>(data_); long_string * ls = static_cast<long_string *>(data_);
std::string * s = &(ls->value); copy_from_string(xml->value);
buf_ = new char[s->size() + 1];
std::strcpy(buf_, s->c_str());
} }
break; break;
...@@ -217,3 +202,9 @@ void postgresql_standard_use_type_backend::clean_up() ...@@ -217,3 +202,9 @@ void postgresql_standard_use_type_backend::clean_up()
buf_ = NULL; buf_ = NULL;
} }
} }
void postgresql_standard_use_type_backend::copy_from_string(std::string const& s)
{
buf_ = new char[s.size() + 1];
std::strcpy(buf_, s.c_str());
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment