2
0
Эх сурвалжийг харах

Domains: add support for check constraints in scripting. To do: display in GUI and verify use in comparison

Reinier Olislagers 11 жил өмнө
parent
commit
09255a60bd
4 өөрчлөгдсөн 29 нэмэгдсэн , 16 устгасан
  1. 15 9
      comparison.pas
  2. 4 2
      main.pas
  3. 5 1
      scriptdb.pas
  4. 5 4
      systables.pas

+ 15 - 9
comparison.pas

@@ -308,6 +308,7 @@ var
   SPOwner: string;
   ModuleName, EntryPoint, Params: string;
   Line: string;
+  CheckConstraint: string; //for domain check constraints
   Collation: string;
   DomainType, DefaultValue: string;
   DomainSize: Integer;
@@ -437,8 +438,8 @@ begin
       if (x = 8) and cxDomains.Checked then // Domains
       for i:= 0 to dbObjectsList[x].Count - 1 do
       begin
-        dmSysTables.GetDomainInfo(fdbIndex, dbObjectsList[x].Strings[i], DomainType, DomainSize, DefaultValue, Collation);
-        //todo: add support for collations and character sets.
+        dmSysTables.GetDomainInfo(fdbIndex, dbObjectsList[x].Strings[i], DomainType, DomainSize, DefaultValue, CheckConstraint, Collation);
+        //todo: add support for collations and character sets and check constraints
 
         Line:= 'Create Domain ' + dbObjectsList[x].Strings[i] + ' as ' + DomainType;
           if Pos('CHAR', DomainType) > 0 then
@@ -978,10 +979,10 @@ end;
 procedure TfmComparison.CheckModifiedDomains;
 var
   i: Integer;
-  Collation: string;
+  CheckConstraint, Collation: string;
   DomainName: string;
   DomainType, DefaultValue: string;
-  CCollation: string;
+  CCheckConstraint, CCollation: string;
   CDomainType, CDefaultValue: string;
   DomainSize, CDomainSize: Integer;
 begin
@@ -991,7 +992,7 @@ begin
 
   for i:= 0 to dbExistingObjectsList[8].Count - 1 do
   begin
-    // Check for cancel button press
+    // Check for pressed cancel button
     Application.ProcessMessages;
     if fCanceled then
       Exit;
@@ -999,13 +1000,14 @@ begin
     DomainName:= dbExistingObjectsList[8][i];
 
     // Read all domain properties
-    dmSysTables.GetDomainInfo(fdbIndex, DomainName, DomainType, DomainSize, DefaultValue, Collation);
-    dmSysTables.GetDomainInfo(cbComparedDatabase.ItemIndex, DomainName, CDomainType, CDomainSize, CDefaultValue,CCollation);
+    dmSysTables.GetDomainInfo(fdbIndex, DomainName, DomainType, DomainSize, DefaultValue, CheckConstraint, Collation);
+    dmSysTables.GetDomainInfo(cbComparedDatabase.ItemIndex, DomainName, CDomainType, CDomainSize, CDefaultValue, CCheckConstraint, CCollation);
 
     // Compare
     if (DomainType <> CDomainType) or
        (DomainSize <> CDomainSize) or
        (DefaultValue <> CDefaultValue) or
+       (CheckConstraint <> CCheckConstraint) or
        (Collation <> CCollation) then
     begin
       meLog.Lines.Add(' ' + DomainName);
@@ -1512,7 +1514,7 @@ end;
 procedure TfmComparison.ScriptModifiedDomains;
 var
   i: Integer;
-  Collation: string;
+  CheckConstraint, Collation: string;
   DomainName: string;
   DomainType, DefaultValue: string;
   DomainSize: Integer;
@@ -1528,13 +1530,17 @@ begin
   for i:= 0 to ModifiedDomainsList.Count - 1 do
   begin
     DomainName:= ModifiedDomainsList[i];
-    dmSysTables.GetDomainInfo(fdbIndex, DomainName, DomainType, domainSize, DefaultValue, Collation);
+    dmSysTables.GetDomainInfo(fdbIndex, DomainName, DomainType, domainSize, DefaultValue, CheckConstraint, Collation);
     fQueryWindow.meQuery.Lines.Add('');
     Line:= 'Alter DOMAIN ' + DomainName + ' type ' + DomainType;
     if Pos('char', LowerCase(DomainType)) > 0 then
       Line:= Line + '(' + IntToStr(DomainSize) + ')';
     fQueryWindow.meQuery.Lines.Add(Line);
 
+    // todo: verify if this check constraint clause works correctly
+    if Trim(CheckConstraint) <> '' then
+      FQueryWindow.meQuery.Lines.Add(Trim(CheckConstraint));
+
     // todo: verify if this collation clause works correctly
     if Trim(Collation) <> '' then
       FQueryWindow.meQuery.Lines.Add('collation '+Trim(Collation));

+ 4 - 2
main.pas

@@ -2392,6 +2392,7 @@ procedure TfmMain.lmViewDomainClick(Sender: TObject);
 var
   SelNode: TTreeNode;
   ADomainName: string;
+  CheckConstraint: string;
   Collation: string;
   DomainType: string;
   DomainSize: Integer;
@@ -2424,7 +2425,7 @@ begin
     PageControl1.ActivePage:= ATab;
 
     dbIndex:= SelNode.Parent.Parent.OverlayIndex;
-    dmSysTables.GetDomainInfo(dbIndex, ADomainName, DomainType, DomainSize, DefaultValue, Collation);
+    dmSysTables.GetDomainInfo(dbIndex, ADomainName, DomainType, DomainSize, DefaultValue, CheckConstraint, Collation);
     ATab.Tag:= dbIndex;
     if Pos('default', LowerCase(DefaultValue)) = 1 then
       DefaultValue:= Trim(Copy(DefaultValue, 8, Length(DefaultValue)));
@@ -2432,7 +2433,6 @@ begin
       (Pos('CSTRING', DomainType) >0) then
       DomainType:= DomainType + '(' + IntToStr(DomainSize) + ')';
 
-    //todo: how to present character set/collation?
     // Fill ViewDomain form
     with ADomainForm do
     begin
@@ -2442,6 +2442,8 @@ begin
       laType.Caption:= DomainType;
       laSize.Caption:= IntToStr(DomainSize);
       laDefault.Caption:= DefaultValue;
+      //todo: add caption with check constraint
+      //todo: add caption with character set/collation
     end;
     ADomainForm.Show;
   end;

+ 5 - 1
scriptdb.pas

@@ -169,6 +169,7 @@ var
   Collation: string;
   DomainType: string;
   DomainSize: Integer;
+  CheckConstraint: string;
   DefaultValue: string;
 begin
   //todo: add support for character set
@@ -177,12 +178,15 @@ begin
   dmSysTables.SortDependencies(List);
   for i:= 0 to List.Count - 1 do
   begin
-    dmSysTables.GetDomainInfo(dbIndex, List[i], DomainType, DomainSize, DefaultValue, Collation);
+    dmSysTables.GetDomainInfo(dbIndex, List[i], DomainType, DomainSize, DefaultValue, CheckConstraint, Collation);
 
     List[i]:= 'Create Domain ' + List[i] + ' as ' + DomainType;
     if (Pos('CHAR', DomainType) > 0) or (Pos('CSTRING', DomainType) > 0) then
       List[i]:= List[i] + '(' + IntToStr(DomainSize) + ')';
     List[i]:= List[i] + ' ' + DefaultValue;
+    // Check constraint
+    if CheckConstraint <> '' then
+      List[i]:= List[i] + ' ' + CheckConstraint;
     // Collation for text types:
     if Collation <> '' then
       List[i]:= List[i] + ' COLLATE ' +  Collation;

+ 5 - 4
systables.pas

@@ -66,7 +66,7 @@ type
     function GetExceptionInfo(ExceptionName: string; var Msg, Description, SqlQuery: string): Boolean;
     // Gets information about domain
     procedure GetDomainInfo(dbIndex: Integer; DomainName: string; var DomainType: string;
-      var DomainSize: Integer; var DefaultValue: string; var Collation: string);
+      var DomainSize: Integer; var DefaultValue: string; var CheckConstraint: string; var Collation: string);
     function GetConstraintForeignKeyFields(AIndexName: string; SqlQuery: TSQLQuery): string;
 
     function GetDBUsers(dbIndex: Integer; ObjectName: string = ''): string;
@@ -634,7 +634,7 @@ end;
 (************  View Domain info  ***************)
 
 procedure TdmSysTables.GetDomainInfo(dbIndex: Integer; DomainName: string; var DomainType: string;
-  var DomainSize: Integer; var DefaultValue: string; var Collation: string);
+  var DomainSize: Integer; var DefaultValue: string; var CheckConstraint: string; var Collation: string);
 const
   // Select domain and associated collation (if text type domain)
   // note weird double join fields required...
@@ -659,6 +659,7 @@ begin
       sqQuery.FieldByName('RDB$FIELD_SCALE').AsInteger);
     DomainSize:= sqQuery.FieldByName('RDB$FIELD_LENGTH').AsInteger;
     DefaultValue:= trim(sqQuery.FieldByName('RDB$DEFAULT_SOURCE').AsString);
+    CheckConstraint:= trim(sqQuery.FieldByName('RDB$VALIDATION_SOURCE').AsString); //e.g. CHECK (VALUE > 10000 AND VALUE <= 2000000)
     Collation:= trim(sqQuery.FieldByName('rdb$collation_name').AsString);
   end
   else
@@ -871,9 +872,9 @@ end;
 
 function TdmSysTables.GetDomainTypeSize(dbIndex: Integer; DomainTypeName: string): Integer;
 var
-  DomainType, DefaultValue, COllation: string;
+  DomainType, DefaultValue, CheckConstraint, Collation: string;
 begin
-  GetDomainInfo(dbIndex, DomainTypeName, DomainType, Result, DefaultValue, Collation);
+  GetDomainInfo(dbIndex, DomainTypeName, DomainType, Result, DefaultValue, CheckConstraint, Collation);
 end;