List:MySQL++« Previous MessageNext Message »
From:Tomaž Šolc Date:December 12 2008 9:33am
Subject:Locale and adding integers to query
View as plain text  
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi

Conversion from integer types to strings in C++ streams depends on the
global locale, if the locale isn't specifically set for a stream. It
looks like this also affects MySQL++ Query objects (in 3.0.8 release)

See the following example: if you add an integer to the query string
after changing the global locale thousands separators will get inserted,
resulting in a wrong query.

#include <mysql++.h>
#include <iostream>
#include <iomanip>
#include <locale>

using namespace std;

int main()
{
	std::locale lc("en_US.utf8");
	std::locale::global(lc);

	int n = 1000;

	mysqlpp::Connection conn(false);
	conn.connect("xxx", "localhost", "xxx", "xxx");

	mysqlpp::Query query = conn.query();
	query << "SELECT " << n;

	cout << query << endl;

	mysqlpp::StoreQueryResult res = query.store();

	cout << res[0][0] << endl;
}

This outputs:

SELECT 1,000
1

Is this the desired behaviour? It was a source of bugs in an application
I'm maintaining. It can be corrected by simply calling

query.imbue(std::locale::classic());

to set the classic C locale for the query stream.

I think a call like this should be added to the Query object
constructors. When you're adding an integer to the query, in majority of
cases you want it passed in the form MySQL understands (i.e. without
locale-dependant separators).

Best regards
Tomaž

- --
Tomaž Šolc, Research & Development
Zemanta Ltd, London, Ljubljana
www.zemanta.com
mail: tomaz@stripped
blog: http://www.tablix.org/~avian/blog
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklCL+oACgkQyJ/LzBrnoEjk/wCeJCp6/EOb4CO/5Pvu8VhBn5l2
fTgAn0KXHiYCwnN5EGl2PO1E/GIZgq0Q
=nmx+
-----END PGP SIGNATURE-----
Thread
Locale and adding integers to queryTomaž Šolc12 Dec
  • Re: Locale and adding integers to queryWarren Young12 Dec
    • Re: Locale and adding integers to queryRick Gutleber12 Dec
      • Re: Locale and adding integers to queryWarren Young12 Dec
        • Re: Locale and adding integers to queryRick Gutleber12 Dec