View Issue Details

IDProjectCategoryView StatusLast Update
0030837FPCFCLpublic2016-11-09 03:19
ReportersilvioprogAssigned ToLacaK 
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product Version3.1.1Product Build 
Target VersionFixed in Version3.2.0 
Summary0030837: [PATCH] fcl-db/sqlite3: declare required fields
DescriptionHello,

The attached patch applies a SQLite3 API call to declared required field (ie NOT NULL) in the fielddef items.
Steps To ReproduceJust apply the patch, rebuild your fcl-db.
Additional Information* No SQL used in the patch, just API call.
* The attached picture show the patch in action.
TagsNo tags attached.
Fixed in Revision34846
FPCOldBugId
FPCTarget
Attached Files
  • 0001-fcl-db-sqlite3conn-declares-required-fields.-patch-b.patch (1,801 bytes)
    From e491b44af917d97fa2df053258faf709901eff79 Mon Sep 17 00:00:00 2001
    From: silvioprog <silvioprog@gmail.com>
    Date: Tue, 1 Nov 2016 15:26:39 -0300
    Subject: [PATCH] fcl-db/sqlite3conn: declares required fields. (patch by
     Silvio Clecio)
    
    ---
     packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp | 13 ++++++++++---
     1 file changed, 10 insertions(+), 3 deletions(-)
    
    diff --git a/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp b/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
    index dca6207..4c5b3dc 100644
    --- a/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
    +++ b/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
    @@ -399,7 +399,9 @@ Const
     
     procedure TSQLite3Connection.AddFieldDefs(cursor: TSQLCursor; FieldDefs: TFieldDefs);
     var
    - i, fi : integer;
    + req: boolean;
    + i, fi, nn : integer;
    + CN: PAnsiChar;
      FN, FD, PrimaryKeyFields : string;
      ft1   : TFieldType;
      size1, size2 : integer;
    @@ -451,8 +453,13 @@ begin
       st:=TSQLite3Cursor(cursor).fstatement;
       for i:= 0 to sqlite3_column_count(st) - 1 do 
         begin
    -    FN:=sqlite3_column_name(st,i);
    +    CN:=sqlite3_column_name(st,i);
    +    FN:=CN;
         FD:=uppercase(sqlite3_column_decltype(st,i));
    +    req := sqlite3_table_column_metadata(fhandle, sqlite3_column_database_name(st,i),
    +      sqlite3_column_table_name(st,i), CN, nil, nil, @nn, nil, nil) = SQLITE_OK;
    +    if req then
    +      req := nn = 1;
         ft1:= ftUnknown;
         for fi := 1 to FieldMapCount do if pos(FieldMap[fi].N,FD)=1 then
           begin
    @@ -500,7 +507,7 @@ begin
                    end;
           ftUnknown : DatabaseErrorFmt('Unknown or unsupported data type %s of column %s', [FD, FN]);
         end; // Case
    -    FieldDefs.Add(FN, ft1, size1, size2, false, false, i+1, CP_UTF8);
    +    FieldDefs.Add(FN, ft1, size1, size2, req, false, i+1, CP_UTF8);
         end;
     end;
     
    -- 
    2.7.4
    
    

Activities

silvioprog

2016-11-01 19:57

reporter  

0001-fcl-db-sqlite3conn-declares-required-fields.-patch-b.patch (1,801 bytes)
From e491b44af917d97fa2df053258faf709901eff79 Mon Sep 17 00:00:00 2001
From: silvioprog <silvioprog@gmail.com>
Date: Tue, 1 Nov 2016 15:26:39 -0300
Subject: [PATCH] fcl-db/sqlite3conn: declares required fields. (patch by
 Silvio Clecio)

---
 packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp b/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
index dca6207..4c5b3dc 100644
--- a/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
+++ b/packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp
@@ -399,7 +399,9 @@ Const
 
 procedure TSQLite3Connection.AddFieldDefs(cursor: TSQLCursor; FieldDefs: TFieldDefs);
 var
- i, fi : integer;
+ req: boolean;
+ i, fi, nn : integer;
+ CN: PAnsiChar;
  FN, FD, PrimaryKeyFields : string;
  ft1   : TFieldType;
  size1, size2 : integer;
@@ -451,8 +453,13 @@ begin
   st:=TSQLite3Cursor(cursor).fstatement;
   for i:= 0 to sqlite3_column_count(st) - 1 do 
     begin
-    FN:=sqlite3_column_name(st,i);
+    CN:=sqlite3_column_name(st,i);
+    FN:=CN;
     FD:=uppercase(sqlite3_column_decltype(st,i));
+    req := sqlite3_table_column_metadata(fhandle, sqlite3_column_database_name(st,i),
+      sqlite3_column_table_name(st,i), CN, nil, nil, @nn, nil, nil) = SQLITE_OK;
+    if req then
+      req := nn = 1;
     ft1:= ftUnknown;
     for fi := 1 to FieldMapCount do if pos(FieldMap[fi].N,FD)=1 then
       begin
@@ -500,7 +507,7 @@ begin
                end;
       ftUnknown : DatabaseErrorFmt('Unknown or unsupported data type %s of column %s', [FD, FN]);
     end; // Case
-    FieldDefs.Add(FN, ft1, size1, size2, false, false, i+1, CP_UTF8);
+    FieldDefs.Add(FN, ft1, size1, size2, req, false, i+1, CP_UTF8);
     end;
 end;
 
-- 
2.7.4

silvioprog

2016-11-01 19:58

reporter  

LacaK

2016-11-02 10:52

developer   ~0095473

I have used for CN := sqlite3_column_origin_name() as my understanding is, that in call of sqlite3_table_column_metadata() we must use column names as they are declared in table.

So when we use in SELECT c AS c2 then:
  sqlite3_column_origin_name() returns 'c'
while
  sqlite3_column_name() returns 'c2'

Another problem is function in select: SELECT lower(a) AS a
then I get SIGSEGV because in call to sqlite3_table_column_metadata() 2,3,4 parameters all are nil, so I added check before call.

LacaK

2016-11-08 12:31

developer   ~0095657

Applied patch with modifications.
Please check and close.

silvioprog

2016-11-09 03:19

reporter   ~0095696

Worked like a charm, thank you very much dude! :-)

Issue History

Date Modified Username Field Change
2016-11-01 19:57 silvioprog New Issue
2016-11-01 19:57 silvioprog File Added: 0001-fcl-db-sqlite3conn-declares-required-fields.-patch-b.patch
2016-11-01 19:58 silvioprog File Added: Screenshot_2016-11-01_15-52-50.jpg
2016-11-02 08:41 LacaK Assigned To => LacaK
2016-11-02 08:41 LacaK Status new => assigned
2016-11-02 10:52 LacaK Note Added: 0095473
2016-11-08 12:31 LacaK Fixed in Revision => 34846
2016-11-08 12:31 LacaK Note Added: 0095657
2016-11-08 12:31 LacaK Status assigned => resolved
2016-11-08 12:31 LacaK Fixed in Version => 3.2.0
2016-11-08 12:31 LacaK Resolution open => fixed
2016-11-09 03:19 silvioprog Note Added: 0095696
2016-11-09 03:19 silvioprog Status resolved => closed