Thread: (Update) Implicit smallint to boolean conversion?
Here's a code snippet that shows the problem: +--+ boolean bNeuroPsych = objResults.getBoolean("NeuroPsych"); short sNeuroPsych = objResults.getShort("NeuroPsych"); System.out.println("Neurologic/Psychiatric: "+bNeuroPsych); System.out.println("Neurologic/Psychiatric: "+sNeuroPsych); +--+ The "NeuroPsych" field is defined in PostgreSQL as a SMALLINT. The following is printed out when the code above is run: Neurologic/Psychiatric: false Neurologic/Psychiatric: 1 Apparently, Oracle, Sybase, SQL Server & Access treat a smallint of '1' as 'true' when returned from getBoolean(). Is this a bug in the driver for PostgreSQL? Any help would be appreciated! See original message below for background & further details of the problem. TIA & Best Regards, Brice Ruth (Original Message) Greetings. I'm working with a third party product that is essentially a Java API to data housed in the database of your choice. My choice is PostgreSQL in this case. Access to the database is through JDBC, in this case, jdbc7.0-1.2.jar. One of the API calls isn't returning the correct results to my application - so I turned to the vendor for help. They've tested the call through JDBC to Access, Sybase, Oracle, and SQL Server and received the correct results. So, the current thought is that something is wrong in the JDBC library provided for PostgreSQL. Here's what's happening: The table being queried has 'smallint' fields in it. Apparently these fields will contain either 0 or 1, depending on if a condition is 'true' or 'false'. So, when the API queries the database, it apparently expects that these smallint fields will be implicitly converted to boolean 'true/false' values in Java, which is what is then returned to the application calling into the API. I don't know what Java code they're using - I've requested the relevant snippets. I can provide the query that is being run by the API, I can provide the structure of the table used in the SQL CREATE statement, and I can provide an example of the data in a record that would be returned. I won't paste all those things in this e-mail since I don't know if its necessary in this case - just let me know if it is. Any help or guidance on this issue would be greatly appreciated, as always. Sincerest regards & TIA, -- Brice Ruth WebProjkt, Inc. VP, Director of Internet Technology http://www.webprojkt.com/
Found the problem: In org/postgresql/jdbc2/ResultSet.java The following line determines true/false: return ((c == 't') || (c == 'T')); Changing this to: return ((c == 't') || (c == 'T') || (c == '1')); Fixes the problem. This could probably be improved to: return ((c == 't') || (c == 'T') || (c != '0')); and in that way any result is 'true' and only '0' returns false (for SMALLINT). Regards, Brice Ruth Brice Ruth wrote: > > Here's a code snippet that shows the problem: > > +--+ > boolean bNeuroPsych = objResults.getBoolean("NeuroPsych"); > short sNeuroPsych = objResults.getShort("NeuroPsych"); > > System.out.println("Neurologic/Psychiatric: "+bNeuroPsych); > System.out.println("Neurologic/Psychiatric: "+sNeuroPsych); > +--+ > > The "NeuroPsych" field is defined in PostgreSQL as a SMALLINT. The > following is printed out when the code above is run: > > Neurologic/Psychiatric: false > Neurologic/Psychiatric: 1 > > Apparently, Oracle, Sybase, SQL Server & Access treat a smallint of '1' > as 'true' when returned from getBoolean(). Is this a bug in the driver > for PostgreSQL? Any help would be appreciated! > > See original message below for background & further details of the problem. > > TIA & Best Regards, > Brice Ruth > > (Original Message) > Greetings. > > I'm working with a third party product that is essentially a Java API to > data housed in the database of your choice. My choice is PostgreSQL in > this case. Access to the database is through JDBC, in this case, > jdbc7.0-1.2.jar. One of the API calls isn't returning the correct > results to my application - so I turned to the vendor for help. They've > tested the call through JDBC to Access, Sybase, Oracle, and SQL Server > and received the correct results. So, the current thought is that > something is wrong in the JDBC library provided for PostgreSQL. > > Here's what's happening: > > The table being queried has 'smallint' fields in it. Apparently these > fields will contain either 0 or 1, depending on if a condition is 'true' > or 'false'. So, when the API queries the database, it apparently > expects that these smallint fields will be implicitly converted to > boolean 'true/false' values in Java, which is what is then returned to > the application calling into the API. I don't know what Java code > they're using - I've requested the relevant snippets. I can provide the > query that is being run by the API, I can provide the structure of the > table used in the SQL CREATE statement, and I can provide an example of > the data in a record that would be returned. I won't paste all those > things in this e-mail since I don't know if its necessary in this case - > just let me know if it is. > > Any help or guidance on this issue would be greatly appreciated, as always. > > Sincerest regards & TIA, > -- > Brice Ruth > WebProjkt, Inc. > VP, Director of Internet Technology > http://www.webprojkt.com/ -- Brice Ruth WebProjkt, Inc. VP, Director of Internet Technology http://www.webprojkt.com/
True ... good thing that's not what I set it to :) I used: (c == '1') which is more appropriate, I guess. :) That way 'f' will still evaluate to false. -Brice Martijn van Oosterhout wrote: > > Brice Ruth wrote: > > Fixes the problem. This could probably be improved to: > > > > return ((c == 't') || (c == 'T') || (c != '0')); > > > > and in that way any result is 'true' and only '0' returns false (for SMALLINT). > > That statement doesn't have the effect you want :) > > 'f' != '0' > > -- > Martijn van Oosterhout <kleptog@cupid.suninternet.com> > http://cupid.suninternet.com/~kleptog/ -- Brice Ruth WebProjkt, Inc. VP, Director of Internet Technology http://www.webprojkt.com/