jupyter-xeus / xeus-sql

Jupyter kernel for SQL databases
https://xeus-sql.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
163 stars 22 forks source link

postgresql: TRUE/FALSE display as 1/0 #35

Open jankatins opened 3 years ago

jankatins commented 3 years ago

Using

%LOAD postgresql dbname=postgres host=localhost
Select FALSE as bar;

I get a display of

bar
0

Which looks wrong to my eyes which are used to normal psql output. If this has to be fixed deeper, I can also open a ticket in soci...

marimeireles commented 3 years ago

Hey @jankatins thank you for the issue. Yeah, if the usual output is TRUE/FALSE then we should aim for that. The way we're displaying data in our tables is using the following piece of code, that can be found here:

                try {
                    switch(props.get_data_type())
                    {
                        case soci::dt_string:
                            cell = r.get<std::string>(i, "NULL");
                            break;
                        case soci::dt_double:
                            cell = std::to_string(r.get<double>(i));
                            cell.erase(cell.find_last_not_of('0') + 1, std::string::npos);
                            if (cell.back() == '.') {
                                cell.pop_back();
                            }
                            break;
                        case soci::dt_integer:
                            cell = std::to_string(r.get<int>(i));
                            break;
                        case soci::dt_long_long:
                            cell = std::to_string(r.get<long long>(i));
                            break;
                        case soci::dt_unsigned_long_long:
                            cell = std::to_string(r.get<unsigned long long>(i));
                            break;
                        case soci::dt_xml:
                        case soci::dt_blob:
                            break;
                        case soci::dt_date:
                            std::tm when = r.get<std::tm>(i);
                            cell = std::asctime(&when);
                            break;
                    }
                } catch (...) {
                    cell = "NULL";
                }

This was copied from SOCI's docs on how to display data. As you can see... It doesn't have a special case treating bools, I think the problem might be related to that? I don't have the time in the moment, but here's how I'd tackle the problem in case someone else is interested in doing it:

  1. Learn if SOCI has a specific type to treat bools, something like soci::dt_bool 1.1. If it does, just include this case on the switch statement, get the data with r.get and either attribute TRUE/FALSE to the cell var. 1.2 If it doesn't maybe it's possible to add an if inside the switch statement that is catching the bool types (maybe it's soci::dt_integer?) read the value inputed by the user with r.get and either attribute TRUE/FALSE to the cell var.

Thanks again!

marimeireles commented 1 year ago

We could also just overwrite this locally. Easier than proposing upstream.