Browse Source

* Partially support for autoincrement columns for ODBC, patch from Ladislav Karrach, bug #16076

git-svn-id: trunk@15237 -
joost 15 years ago
parent
commit
354122a8c5

+ 2 - 1
packages/fcl-db/src/base/bufdataset.pas

@@ -1419,7 +1419,7 @@ begin
   case AField.DataType of
   case AField.DataType of
     ftString, ftFixedChar : ACompareRec.Comparefunc := @DBCompareText;
     ftString, ftFixedChar : ACompareRec.Comparefunc := @DBCompareText;
     ftSmallint : ACompareRec.Comparefunc := @DBCompareSmallInt;
     ftSmallint : ACompareRec.Comparefunc := @DBCompareSmallInt;
-    ftInteger, ftBCD : ACompareRec.Comparefunc :=
+    ftInteger, ftBCD, ftAutoInc : ACompareRec.Comparefunc :=
       @DBCompareInt;
       @DBCompareInt;
     ftWord : ACompareRec.Comparefunc := @DBCompareWord;
     ftWord : ACompareRec.Comparefunc := @DBCompareWord;
     ftBoolean : ACompareRec.Comparefunc := @DBCompareByte;
     ftBoolean : ACompareRec.Comparefunc := @DBCompareByte;
@@ -1556,6 +1556,7 @@ begin
       ftWideString:result := (FieldDef.Size + 1)*2;
       ftWideString:result := (FieldDef.Size + 1)*2;
     ftSmallint,
     ftSmallint,
       ftInteger,
       ftInteger,
+      ftAutoInc,
       ftword     : result := sizeof(longint);
       ftword     : result := sizeof(longint);
     ftBoolean    : result := sizeof(wordbool);
     ftBoolean    : result := sizeof(wordbool);
     ftBCD        : result := sizeof(currency);
     ftBCD        : result := sizeof(currency);

+ 1 - 0
packages/fcl-db/src/base/fields.inc

@@ -1707,6 +1707,7 @@ begin
   Inherited Create(AOWner);
   Inherited Create(AOWner);
   SetDataType(ftAutoInc);
   SetDataType(ftAutoInc);
   FReadOnly:=True;
   FReadOnly:=True;
+  FProviderFlags:=FProviderFlags-[pfInUpdate];
 end;
 end;
 
 
 Procedure TAutoIncField.SetAsLongint(AValue : Longint);
 Procedure TAutoIncField.SetAsLongint(AValue : Longint);

+ 22 - 2
packages/fcl-db/src/sqldb/odbc/odbcconn.pas

@@ -333,7 +333,7 @@ begin
     StrLenOrInd:=0;
     StrLenOrInd:=0;
 
 
     case AParams[ParamIndex].DataType of
     case AParams[ParamIndex].DataType of
-      ftInteger, ftSmallInt, ftWord:
+      ftInteger, ftSmallInt, ftWord, ftAutoInc:
         begin
         begin
           IntVal:=AParams[ParamIndex].AsInteger;
           IntVal:=AParams[ParamIndex].AsInteger;
           PVal:=@IntVal;
           PVal:=@IntVal;
@@ -729,7 +729,7 @@ begin
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_CHAR, buffer, FieldDef.Size, @StrLenOrInd);
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_CHAR, buffer, FieldDef.Size, @StrLenOrInd);
     ftSmallint:           // mapped to TSmallintField
     ftSmallint:           // mapped to TSmallintField
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SSHORT, buffer, SizeOf(Smallint), @StrLenOrInd);
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SSHORT, buffer, SizeOf(Smallint), @StrLenOrInd);
-    ftInteger,ftWord:     // mapped to TLongintField
+    ftInteger,ftWord,ftAutoInc:     // mapped to TLongintField
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SLONG, buffer, SizeOf(Longint), @StrLenOrInd);
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SLONG, buffer, SizeOf(Longint), @StrLenOrInd);
     ftLargeint:           // mapped to TLargeintField
     ftLargeint:           // mapped to TLargeintField
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SBIGINT, buffer, SizeOf(Largeint), @StrLenOrInd);
       Res:=SQLGetData(ODBCCursor.FSTMTHandle, FieldDef.Index+1, SQL_C_SBIGINT, buffer, SizeOf(Largeint), @StrLenOrInd);
@@ -962,6 +962,7 @@ var
   ColName,TypeName:string;
   ColName,TypeName:string;
   FieldType:TFieldType;
   FieldType:TFieldType;
   FieldSize:word;
   FieldSize:word;
+  AutoIncAttr: SQLINTEGER;
 begin
 begin
   ODBCCursor:=cursor as TODBCCursor;
   ODBCCursor:=cursor as TODBCCursor;
 
 
@@ -971,6 +972,7 @@ begin
     SQL_HANDLE_STMT, ODBCCursor.FSTMTHandle, 'Could not determine number of columns in result set.'
     SQL_HANDLE_STMT, ODBCCursor.FSTMTHandle, 'Could not determine number of columns in result set.'
   );
   );
 
 
+  AutoIncAttr:=SQL_FALSE;
   for i:=1 to ColumnCount do
   for i:=1 to ColumnCount do
   begin
   begin
     SetLength(ColName,ColNameDefaultLength); // also garantuees uniqueness
     SetLength(ColName,ColNameDefaultLength); // also garantuees uniqueness
@@ -1060,6 +1062,24 @@ begin
        (FieldSize >= dsMaxStringSize) then
        (FieldSize >= dsMaxStringSize) then
     begin
     begin
       FieldSize:=dsMaxStringSize-1;
       FieldSize:=dsMaxStringSize-1;
+    end
+    else
+    if (FieldType in [ftInteger]) and (AutoIncAttr=SQL_FALSE) then //if the column is an autoincrementing column
+                                                                   //any exact numeric type with scale 0 can have identity attr.
+                                                                   //only one column per table can have identity attr.
+    begin
+      ODBCCheckResult(
+        SQLColAttribute(ODBCCursor.FSTMTHandle, // statement handle
+                        i,                      // column number
+                        SQL_DESC_AUTO_UNIQUE_VALUE, // FieldIdentifier
+                        nil,                        // buffer
+                        0,                          // buffer size
+                        nil,                        // actual length
+                        @AutoIncAttr),              // NumericAttribute
+        SQL_HANDLE_STMT, ODBCCursor.FSTMTHandle, 'Could not get autoincrement attribute for column %d.',[i]
+      );
+      if AutoIncAttr=SQL_TRUE then
+        FieldType:=ftAutoInc;
     end;
     end;
 
 
     if FieldType=ftUnknown then // if unknown field type encountered, try finding more specific information about the ODBC SQL DataType
     if FieldType=ftUnknown then // if unknown field type encountered, try finding more specific information about the ODBC SQL DataType