db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Finn <m...@bitcount.com>
Subject Re: The dreaded BIT and PostgreSQL
Date Tue, 11 Nov 2003 20:30:01 GMT
According to the Postgres Docs, a single quoted 0 or 1 is acceptable for 
booleans.  This would also work for chars and integers, because it is 
acceptable to single quote them as well.

mfinn=# create table test (one smallint,two boolean,three character(1));
CREATE TABLE
mfinn=# insert into test(one, two, three) values(0,FALSE,'0');
INSERT 140016 1
mfinn=# insert into test(one, two, three) values(1,TRUE,'1');
INSERT 140017 1
mfinn=# select * from test where (one='0' and two='0' and three='0') or
mfinn-#                          (one='1' and two='1' and three='1');
 one | two | three
-----+-----+-------
   0 | f   | 0
   1 | t   | 1
(2 rows)

There would also have to be changes in 
src/generator/src/templates/om/Peer.vm to change from Y/N to 0/1 for 
booleanChars, which would fix booleanChars in select criteria. I don't 
think this would break anything..

 Comments?

- matt

Index: src/java/org/apache/torque/adapter/DBPostgres.java
===================================================================
RCS file: 
/home/cvspublic/db-torque/src/java/org/apache/torque/adapter/DBPostgres.java,v
retrieving revision 1.16
diff -u -b -r1.16 DBPostgres.java
--- src/java/org/apache/torque/adapter/DBPostgres.java  25 Aug 2003 
21:56:10 -0000      1.16
+++ src/java/org/apache/torque/adapter/DBPostgres.java  11 Nov 2003 
18:46:46 -0000
@@ -180,6 +180,6 @@
      */
     public String getBooleanString(Boolean b)
     {
-        return (b == null) ? "0" : (Boolean.TRUE.equals(b) ? "1" : "0");
+        return (b == null) ? "'0'" : (Boolean.TRUE.equals(b) ? "'1'" : 
"'0'");
     }
 }
Index: src/test/org/apache/torque/util/CriteriaTest.java
===================================================================
RCS file: 
/home/cvspublic/db-torque/src/test/org/apache/torque/util/CriteriaTest.java,v
retrieving revision 1.20
diff -u -b -r1.20 CriteriaTest.java
--- src/test/org/apache/torque/util/CriteriaTest.java   18 Jul 2003 
20:11:48 -0000      1.20
+++ src/test/org/apache/torque/util/CriteriaTest.java   11 Nov 2003 
18:46:52 -0000
@@ -313,7 +313,7 @@
             fail("Exception thrown in DBFactory");
         }

-        assertEquals("TABLE.COLUMN=1", cc.toString());
+        assertEquals("TABLE.COLUMN='1'", cc.toString());
     }

     /**




Henning P. Schmiedehausen wrote:

>But Torque generates 1 and 0 (true == 1, false == 0). Which leads
>to "cannot convert between boolean and integer, you need an explict
>cast" from the database.
>
>The choice of "0" and "1" is useful if the underlying column is not a
>real boolean column but an integer column (which is the choice with
>databases that don't have a real boolean type).
>
>
>  
>


Hi,

>the following situation. I have a table definition for a postgres
>database that has the following column definition:
>
>[...]
>    <column name="PROJEKTDATEN_LOCKED" required="true" type="BIT" javaName="Locked"/>
>[...]
>
>When building a Criteria for the "PROJEKTDATEN" table (e.g. deleting)
>then a criteria is built that contains a PROJEKTDATEN.PROJEKTDATEN_LOCKED = <something>
>
>For PostgreSQL, the "something" must be true or false.  
>
>But Torque generates 1 and 0 (true == 1, false == 0). Which leads
>to "cannot convert between boolean and integer, you need an explict
>cast" from the database.
>
>The choice of "0" and "1" is useful if the underlying column is not a
>real boolean column but an integer column (which is the choice with
>databases that don't have a real boolean type).
>
>The real solution would be if Torque looks at the column map and
>generated true and false for boolean columns and 0/1 for integer or
>other types. However, Torque isn't that clever.
>
>If you apply the following patch to the 3.1 branch, then some
>operations with criterias (most prominent: doDelete(<objecttype> obj)
>in the generated peers) will work with boolean column. But this breaks
>the behaviour where adding a "false" to a criteria put a "0" into an
>integer column and a "true" puts a "1". So this is a breaking change.
>
>Personally, I'd very much prefer applying this patch to Torque (3.2
>and 3.1) because IMHO it fixes a bug.
>
>Discussion wanted.
>
>	Regards
>		Henning
>
>Index: src/java/org/apache/torque/adapter/DBPostgres.java
>===================================================================
>RCS file: /home/cvs/db-torque/src/java/org/apache/torque/adapter/DBPostgres.java,v
>retrieving revision 1.16
>diff -u -b -r1.16 DBPostgres.java
>--- src/java/org/apache/torque/adapter/DBPostgres.java	25 Aug 2003 21:56:10 -0000	1.16
>+++ src/java/org/apache/torque/adapter/DBPostgres.java	3 Nov 2003 16:35:41 -0000
>@@ -180,6 +180,6 @@
>      */
>     public String getBooleanString(Boolean b)
>     {
>-        return (b == null) ? "0" : (Boolean.TRUE.equals(b) ? "1" : "0");
>+        return (b == null) ? "false" : (Boolean.TRUE.equals(b) ? "true" : "false");
>     }
> }
>Index: src/test/org/apache/torque/util/CriteriaTest.java
>===================================================================
>RCS file: /home/cvs/db-torque/src/test/org/apache/torque/util/CriteriaTest.java,v
>retrieving revision 1.20
>diff -u -b -r1.20 CriteriaTest.java
>--- src/test/org/apache/torque/util/CriteriaTest.java	18 Jul 2003 20:11:48 -0000	1.20
>+++ src/test/org/apache/torque/util/CriteriaTest.java	3 Nov 2003 16:35:41 -0000
>@@ -313,7 +313,7 @@
>             fail("Exception thrown in DBFactory");
>         }
> 
>-        assertEquals("TABLE.COLUMN=1", cc.toString());
>+        assertEquals("TABLE.COLUMN=true", cc.toString());
>     }
> 
>     /**
>
>
>
>
>
>
>
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message