Browse Source

* Result all indexes on UpdateIndexDefs, not only the PK. Patch from Ladislav Karrach, bug #16493

git-svn-id: trunk@15325 -
joost 15 years ago
parent
commit
73df2c730d
1 changed files with 48 additions and 46 deletions
  1. 48 46
      packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp

+ 48 - 46
packages/fcl-db/src/sqldb/sqlite/sqlite3conn.pp

@@ -82,7 +82,6 @@ type
     // New methods
     // New methods
     procedure execsql(const asql: string);
     procedure execsql(const asql: string);
     procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override; // Differs from SQLDB.
     procedure UpdateIndexDefs(IndexDefs : TIndexDefs;TableName : string); override; // Differs from SQLDB.
-    function getprimarykeyfield(const atablename: string; const acursor: tsqlcursor): string;
     function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
     function RowsAffected(cursor: TSQLCursor): TRowsCount; override;
     function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
     function GetSchemaInfoSQL(SchemaType : TSchemaType; SchemaObjectName, SchemaPattern : string) : string; override;
     function StrToStatementType(s : string) : TStatementType; override;
     function StrToStatementType(s : string) : TStatementType; override;
@@ -663,29 +662,6 @@ begin
   checkerror(sqlite3_exec(fhandle,pchar(asql),@execscallback,@result,nil));
   checkerror(sqlite3_exec(fhandle,pchar(asql),@execscallback,@result,nil));
 end;
 end;
 
 
-function TSQLite3Connection.getprimarykeyfield(const atablename: string;
-                                const acursor: tsqlcursor): string;
-var
-  int1,int2: integer;
-  ar1: TArrayStringArray;
-  str1: string;
-  
-begin
-  result:= '';
-  if atablename <> '' then 
-    begin
-    ar1:= stringsquery('PRAGMA table_info('+atablename+');');
-    for int1:= 0 to high(ar1) do
-      begin
-      if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then 
-        begin
-        if result<>'' then result := result+';';
-        result:= result+ar1[int1][1];
-        end;
-      end;
-    end;
-end;
-
 function TSQLite3Connection.RowsAffected(cursor: TSQLCursor): TRowsCount;
 function TSQLite3Connection.RowsAffected(cursor: TSQLCursor): TRowsCount;
 begin
 begin
   if assigned(cursor) then
   if assigned(cursor) then
@@ -722,32 +698,58 @@ end;
 
 
 procedure TSQLite3Connection.UpdateIndexDefs(IndexDefs: TIndexDefs; TableName: string);
 procedure TSQLite3Connection.UpdateIndexDefs(IndexDefs: TIndexDefs; TableName: string);
 var
 var
-  str1: string;
-  
+  artableinfo, arindexlist, arindexinfo: TArrayStringArray;
+  il,ii: integer;
+  IndexName: string;
+  IndexOptions: TIndexOptions;
+  PKFields, IXFields: TStrings;
+  l: boolean;
+
+  function CheckPKFields:boolean;
+  var i: integer;
+  begin
+    Result:=false;
+    if IXFields.Count<>PKFields.Count then Exit;
+    for i:=0 to IXFields.Count-1 do
+      if PKFields.IndexOf(IXFields[i])<0 then Exit;
+    Result:=true;
+    PKFields.Clear;
+  end;
+
 begin
 begin
-  str1:= getprimarykeyfield(tablename,nil);
-  if str1 <> '' then 
+  PKFields:=TStringList.Create;
+  IXFields:=TStringList.Create;
+  IXFields.Delimiter:=';';
+
+  //primary key fields
+  artableinfo := stringsquery('PRAGMA table_info('+TableName+');');
+  for ii:=low(artableinfo) to high(artableinfo) do
+    if (high(artableinfo[ii]) >= 5) and (artableinfo[ii][5] = '1') then
+      PKFields.Add(artableinfo[ii][1]);
+
+  //list of all table indexes
+  arindexlist:=stringsquery('PRAGMA index_list('+TableName+');');
+  for il:=low(arindexlist) to high(arindexlist) do
     begin
     begin
-    indexdefs.add('$PRIMARY_KEY$',str1,[ixPrimary,ixUnique]);
+    IndexName:=arindexlist[il][1];
+    if arindexlist[il][2]='1' then
+      IndexOptions:=[ixUnique]
+    else
+      IndexOptions:=[];
+    //list of columns in given index
+    arindexinfo:=stringsquery('PRAGMA index_info('+IndexName+');');
+    IXFields.Clear;
+    for ii:=low(arindexinfo) to high(arindexinfo) do
+      IXFields.Add(arindexinfo[ii][2]);
+
+    if CheckPKFields then IndexOptions:=IndexOptions+[ixPrimary];
+
+    IndexDefs.Add(IndexName, IXFields.DelimitedText, IndexOptions);
     end;
     end;
+
+  PKFields.Free;
+  IXFields.Free;
 end;
 end;
-{
-procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
-                              const TableName: string);
-var
- int1,int2: integer;
- ar1: TArrayStringArray;
- str1: string;
-begin
- ar1:= stringsquery('PRAGMA table_info('+tablename+');');
- for int1:= 0 to high(ar1) do begin
-  if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then begin
-   indexdefs.add('$PRIMARY_KEY$',ar1[int1][1],[ixPrimary,ixUnique]);
-   break;
-  end;
- end;
-end;
-}
 
 
 function TSQLite3Connection.getinsertid: int64;
 function TSQLite3Connection.getinsertid: int64;
 begin
 begin