Skip to content
Permalink
Browse files

Issue #12: Add support for foreign key dropdown in SQLite grid fields…

…. Note that SQLite does not support LEFT(), while MSSQL does not support SUBSTR(), so here's a template per network type: spFuncLeft.
  • Loading branch information
ansgarbecker committed Jan 13, 2020
1 parent 094f65e commit ca87fc0e6b42f311877332cf683e4a2033f1e54d
Showing with 7 additions and 3 deletions.
  1. +5 −1 source/dbconnection.pas
  2. +2 −2 source/main.pas
@@ -314,7 +314,7 @@ TConnectionParameters = class(TObject)
spSessionVariables, spGlobalVariables,
spISTableSchemaCol,
spUSEQuery, spKillQuery, spKillProcess,
spFuncLength, spFuncCeil,
spFuncLength, spFuncCeil, spFuncLeft,
spLockedTables);

TDBConnection = class(TComponent)
@@ -2470,6 +2470,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spKillProcess] := 'KILL %d';
FSQLSpecifities[spFuncLength] := 'LENGTH';
FSQLSpecifities[spFuncCeil] := 'CEIL';
FSQLSpecifities[spFuncLeft] := 'LEFT(%s, %d)';
FSQLSpecifities[spLockedTables] := '';
end;
ngMSSQL: begin
@@ -2487,6 +2488,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spKillProcess] := 'KILL %d';
FSQLSpecifities[spFuncLength] := 'LEN';
FSQLSpecifities[spFuncCeil] := 'CEILING';
FSQLSpecifities[spFuncLeft] := 'LEFT(%s, %d)';
FSQLSpecifities[spLockedTables] := '';
end;
ngPgSQL: begin
@@ -2504,6 +2506,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spKillProcess] := 'SELECT pg_cancel_backend(%d)';
FSQLSpecifities[spFuncLength] := 'LENGTH';
FSQLSpecifities[spFuncCeil] := 'CEIL';
FSQLSpecifities[spFuncLeft] := 'LEFT(%s, %d)';
FSQLSpecifities[spLockedTables] := '';
end;
ngSQLite: begin
@@ -2521,6 +2524,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spKillProcess] := 'KILL %d';
FSQLSpecifities[spFuncLength] := 'LENGTH';
FSQLSpecifities[spFuncCeil] := 'CEIL';
FSQLSpecifities[spFuncLeft] := 'SUBSTR(%s, 1, %d)';
FSQLSpecifities[spLockedTables] := '';
end;

@@ -9532,7 +9532,7 @@ procedure TMainForm.AnyGridCreateEditor(Sender: TBaseVirtualTree; Node:
idx := ForeignKey.Columns.IndexOf(DataGrid.Header.Columns[Column].Text);
if idx > -1 then try
// Find the first text column if available and use that for displaying in the pulldown instead of using meaningless id numbers
RefDb := ForeignKey.ReferenceTable.Substring(1, Pos('.', ForeignKey.ReferenceTable));
RefDb := ForeignKey.ReferenceTable.Substring(0, Pos('.', ForeignKey.ReferenceTable)-1);
if not RefDb.IsEmpty then begin
RefTable := ForeignKey.ReferenceTable.Substring(Length(RefDb)+1);
end else begin
@@ -9553,7 +9553,7 @@ procedure TMainForm.AnyGridCreateEditor(Sender: TBaseVirtualTree; Node:

KeyCol := Conn.QuoteIdent(ForeignKey.ForeignColumns[idx]);
SQL := 'SELECT '+KeyCol;
if TextCol <> '' then SQL := SQL + ', LEFT(' + Conn.QuoteIdent(TextCol) + ', 256)';
if TextCol <> '' then SQL := SQL + ', ' + Format(Conn.GetSQLSpecifity(spFuncLeft), [Conn.QuoteIdent(TextCol), 256]);
SQL := SQL + ' FROM '+Conn.QuoteIdent(ForeignKey.ReferenceTable, True, '.')+' GROUP BY '+KeyCol+' ORDER BY ';
if TextCol <> '' then SQL := SQL + Conn.QuoteIdent(TextCol) else SQL := SQL + KeyCol;
SQL := SQL + ' LIMIT ' + ForeignItemsLimit.ToString;

0 comments on commit ca87fc0

Please sign in to comment.
You can’t perform that action at this time.