Browse Source

* Retrieve the string field size from the sqlite field description
* Create string fields using the size specified in the fielddef

git-svn-id: trunk@12766 -

blikblum 16 years ago
parent
commit
daa5fed573

+ 10 - 2
packages/fcl-db/src/sqlite/customsqliteds.pas

@@ -41,6 +41,9 @@ interface
 uses
   Classes, SysUtils, db;
 
+const
+  DefaultStringSize = 255;
+
 type
   PDataRecord = ^DataRecord;
   PPDataRecord = ^PDataRecord;
@@ -1579,7 +1582,7 @@ end;
 function TCustomSqliteDataset.CreateTable(const ATableName: String): Boolean;
 var
   SQLTemp: String;
-  i: Integer;
+  i, StrSize: Integer;
 begin
   {$ifdef DEBUG_SQLITEDS}
   WriteLn('##TCustomSqliteDataset.CreateTable##');
@@ -1599,7 +1602,12 @@ begin
         ftInteger:
           SQLTemp := SQLTemp + ' INTEGER';
         ftString:
-          SQLTemp := SQLTemp + ' VARCHAR';
+        begin
+          StrSize := FieldDefs[i].Size;
+          if StrSize = 0 then
+            StrSize := DefaultStringSize;
+          SQLTemp := SQLTemp + ' VARCHAR(' + IntToStr(StrSize) + ')';
+        end;
         ftBoolean:
           SQLTemp := SQLTemp + ' BOOL_INT';
         ftFloat:

+ 5 - 5
packages/fcl-db/src/sqlite/sqlite3ds.pas

@@ -62,7 +62,7 @@ type
 implementation
 
 uses
-  sqlite3, db;
+  sqlite3, db, strutils;
   
 function SqliteCode2Str(Code: Integer): String;
 begin
@@ -152,12 +152,10 @@ begin
 end;
 
 procedure TSqlite3Dataset.RetrieveFieldDefs;
-const
-  FieldSizeMap: array[Boolean] of Integer = (0, dsMaxStringSize);
 var
   vm: Pointer;
   ColumnStr: String;
-  i, ColumnCount: Integer;
+  i, ColumnCount, DataSize: Integer;
   AType: TFieldType;
 begin
   {$ifdef DEBUG_SQLITEDS}
@@ -174,6 +172,7 @@ begin
   SetLength(FGetSqlStr, ColumnCount);
   for i := 0 to ColumnCount - 1 do
   begin
+    DataSize := 0;
     ColumnStr := UpperCase(String(sqlite3_column_decltype(vm, i)));
     if (ColumnStr = 'INTEGER') or (ColumnStr = 'INT') then
     begin
@@ -187,6 +186,7 @@ begin
     end else if Pos('VARCHAR', ColumnStr) = 1 then
     begin
       AType := ftString;
+      DataSize := StrToIntDef(Trim(ExtractDelimited(2, ColumnStr, ['(', ')'])), DefaultStringSize);
     end else if Pos('BOOL', ColumnStr) = 1 then
     begin
       AType := ftBoolean;
@@ -233,7 +233,7 @@ begin
     begin
       AType := ftString;
     end;
-    FieldDefs.Add(String(sqlite3_column_name(vm, i)), AType, FieldSizeMap[AType = ftString]);
+    FieldDefs.Add(String(sqlite3_column_name(vm, i)), AType, DataSize);
     //Set the pchar2sql function
     if AType in [ftString, ftMemo] then
       FGetSqlStr[i] := @Char2SQLStr

+ 6 - 7
packages/fcl-db/src/sqlite/sqliteds.pas

@@ -64,7 +64,7 @@ type
 implementation
 
 uses
-  sqlite, db;
+  sqlite, db, strutils;
 
 //function sqlite_last_statement_changes(dbhandle:Pointer):longint;cdecl;external 'sqlite' name 'sqlite_last_statement_changes';
 
@@ -110,7 +110,7 @@ end;
 
 procedure TSqliteDataset.RetrieveFieldDefs;
 var
-  ColumnCount, i:Integer;
+  ColumnCount, i, DataSize:Integer;
   AType: TFieldType;
   vm: Pointer;
   ColumnNames, ColumnValues:PPChar;
@@ -131,6 +131,7 @@ begin
   // If the field contains another type, may have problems
   for i := 0 to ColumnCount - 1 do
   begin
+    DataSize := 0;
     ColumnStr := UpperCase(String(ColumnNames[i + ColumnCount]));
     if (ColumnStr = 'INTEGER') or (ColumnStr = 'INT') then
     begin
@@ -145,6 +146,7 @@ begin
     end else if Pos('VARCHAR', ColumnStr) = 1 then
     begin
       AType := ftString;
+      DataSize := StrToIntDef(Trim(ExtractDelimited(2, ColumnStr, ['(', ')'])), DefaultStringSize);
     end else if Pos('BOOL', ColumnStr) = 1 then
     begin
       AType := ftBoolean;
@@ -180,11 +182,8 @@ begin
     end else
     begin
       AType := ftString;
-    end;    
-    if AType = ftString then
-      FieldDefs.Add(String(ColumnNames[i]), AType, dsMaxStringSize)
-    else
-      FieldDefs.Add(String(ColumnNames[i]), AType);  
+    end;
+    FieldDefs.Add(String(ColumnNames[i]), AType, DataSize);
     //Set the pchar2sql function
     if AType in [ftString, ftMemo] then
       FGetSqlStr[i] := @Char2SQLStr