Recent

Author Topic: SQLite and Boolean value of -1  (Read 20506 times)

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: SQLite and Boolean value of -1
« Reply #15 on: June 16, 2015, 08:47:05 am »
Finally, for all those having multiple values (1 and -1) for boolean already in their database, checking for ABS(boolean_value) <> 0 is the safest course.mention other programs querying the database and expecting 1 for TRUE)

Why "ABS(boolean_value) <> 0" ?
isn't enough "boolean_value <> 0"

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: SQLite and Boolean value of -1
« Reply #16 on: June 16, 2015, 09:11:27 am »
Why "ABS(boolean_value) <> 0" ?
isn't enough "boolean_value <> 0"
Yeah, that was suposed to be: ABS(boolean_value) = 1

When using a parameterized query you want something like this:
SELECT * FROM table WHERE visible_for_me = :param
and
Query.Parambyname('visible_for_me').asBoolean := true; // or false;

When you have an existing database with values of 1 and -1 this doesn't work in SQLite.
Using this it will work:
SELECT * FROM table WHERE ABS(visible_for_me) = :param

Because this bug existed a long time in SQLite-connection this construction will still be necessary until all values are correct (or you correct the wrong values in one time).

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: SQLite and Boolean value of -1
« Reply #17 on: June 16, 2015, 11:01:09 am »
Good point. So it will break existing applications (and databases).
It makes patch bit problematic as it will require users change existing SQL statements.

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: SQLite and Boolean value of -1
« Reply #18 on: June 16, 2015, 11:07:53 am »
Good point. So it will break existing applications (and databases).
It makes patch bit problematic as it will require users change existing SQL statements.
As it is now it's also breaks existing application if they use an existing database with records filled by other programs or even with the TSqlite3Dataset-component (because that one does write the 1 correctly).

So it's choosing the least bad option.

I, for one, now know of this incompatibility, so I will use ABS(boolean)=:param in the future, but the longer this incompatibility with SQLite-requirements exists, the harder it becomes to correct.

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: SQLite and Boolean value of -1
« Reply #19 on: June 16, 2015, 02:28:19 pm »
Wow....this is an interesting discussion.
And only because i asked you a question RVK :)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: SQLite and Boolean value of -1
« Reply #20 on: June 16, 2015, 02:53:12 pm »
Wow....this is an interesting discussion.
And only because i asked you a question RVK :)
Yep, what a small question can lead to  %)

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: SQLite and Boolean value of -1
« Reply #21 on: June 17, 2015, 02:36:41 pm »
I did commit patch in trunk: rev.31086
And comment here: http://wiki.freepascal.org/User_Changes_Trunk#DB

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: SQLite and Boolean value of -1
« Reply #22 on: June 17, 2015, 02:49:54 pm »
I did commit patch in trunk: rev.31086
And comment here: http://wiki.freepascal.org/User_Changes_Trunk#DB
Yes, It's working. Thanks.

The comment is also clear. Let's hope others don't have problems with it, otherwise we can direct them to that comments.

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: SQLite and Boolean value of -1
« Reply #23 on: June 17, 2015, 10:27:20 pm »
The comment is also clear. Let's hope others don't have problems with it, otherwise we can direct them to that comments.
I am still considering add conditional compiler define, which will allow relax old behavior.
Something like:
{$IFDEF SQLITE_OLD_BOOLEAN}
   // use P.AsInteger
{$ELSE}
  // use ord(P.AsBoolean)
{$ENDIF}
Not nice , but hard to say how many users will be affected with this change and if they will be happier with such solution.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: SQLite and Boolean value of -1
« Reply #24 on: June 17, 2015, 10:38:43 pm »
The comment is also clear. Let's hope others don't have problems with it, otherwise we can direct them to that comments.
I am still considering add conditional compiler define, which will allow relax old behavior.
Something like:
{$IFDEF SQLITE_OLD_BOOLEAN}
   // use P.AsInteger
{$ELSE}
  // use ord(P.AsBoolean)
{$ENDIF}
Not nice , but hard to say how many users will be affected with this change and if they will be happier with such solution.
I wouldn't spend any more thought on the subject. The change is simple enough and it shouldn't affect to many people. Although SQLite is one of the most used data containers I would guess not many would use 1 or 0 and not true or false or even where X or where not X which I would expect to work regardless of the value of boolean in the database.

Then again I'm not using sqlite not even for demos.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6989
Re: SQLite and Boolean value of -1
« Reply #25 on: June 17, 2015, 11:14:02 pm »
Although SQLite is one of the most used data containers I would guess not many would use 1 or 0 and not true or false or even where X or where not X which I would expect to work regardless of the value of boolean in the database.
Well... the problem with SQLite is that you can't do something like
SELECT * FROM testtable WHERE bol = true
because SQLite does not know the term FALSE and TRUE.

Luckily you can use
SELECT * FROM testtable WHERE bol
and
SELECT * FROM testtable WHERE not bol
but the problem with that is you can't put it in a parameterized statement.
And this doesn't work again:
SELECT * FROM testtable WHERE bol = :param_boolean

But you are correct. I think the impact to existing users will be minimum and I think when they post the question about booleans in SQLite we can point them in the right direction.

I am still considering add conditional compiler define, which will allow relax old behavior.
Not nice , but hard to say how many users will be affected with this change and if they will be happier with such solution.
You could. But I think someone capable of recompiling FPC with this conditional compiler define is also capable of correcting his/her own program to act as it should according to SQLite-requirements. The largest problem that I see is that they initially don't know about this change and can stumble upon it and waste some time looking for a solution. But hey... that's what we are here for :)

avra

  • Hero Member
  • *****
  • Posts: 2586
    • Additional info
Re: SQLite and Boolean value of -1
« Reply #26 on: June 18, 2015, 08:40:52 am »
Are there any other DB-engines that have no real notion of TRUE and FALSE and store boolean as an integer?
Firebird has native boolean type since v3, which is still in beta. Interbase had the same problem and I don't know if it's fixed yet.
http://www.firebirdfaq.org/faq12
http://www.firebirdnews.org/boolean-datatype-implemented-in-fb-3
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

LacaK

  • Hero Member
  • *****
  • Posts: 703
Re: SQLite and Boolean value of -1
« Reply #27 on: June 18, 2015, 10:47:38 am »
Ok. Thanks for reply. So I will leave it as is. Hopping, that users will read http://wiki.freepascal.org/User_Changes_Trunk#DB when they will upgrade.

Btw. Interbase has already introduced native boolean datatype and it is supported by IBConnection (together with Firebirds boolean)

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: SQLite and Boolean value of -1
« Reply #28 on: June 18, 2015, 02:37:26 pm »
Well, is hard to believe that none which has tested their app wouldn't have noted this problem.

Once I've been advised to do not use the boolean/logic field since it was not portable to other RDBS, and to use this instead:
Code: [Select]
  --   POSTGRES EXTRACT  --
  ENABLED character varying(1) NOT NULL DEFAULT '1'::character varying,
  CONSTRAINT MYTABLE_ENABLED_CHECK CHECK (upper(ENABLED ::text) = ANY (ARRAY['0'::text, '1'::text])),

 

TinyPortal © 2005-2018