Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2025-07

TStringField.Size

9 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Luca Olivetti

Hello,

In a project I'm using both an sql server database (TMSSQLConnection) and a postgresql one (TPQConnection).
One of the TStringField is defined as size 100, but in the sql server it has been changed to to 200.
Even without changing the TStringField.Size in the program I can get values longer than 100 characters.
I did a test with the TPQConnection and if I change the size of a field, the TStringField is truncated according to its size. Why the difference between the two database connectors? Shouldn't it depend exclusively on the TField implementation?

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 12 lines
Hello, In a project I'm using both an sql server database (TMSSQLConnection) and a postgresql one (TPQConnection). One of the TStringField is defined as size 100, but in the sql server it has been changed to to 200. Even without changing the TStringField.Size in the program I can get values longer than 100 characters. I did a test with the TPQConnection and if I change the size of a field, the TStringField is truncated according to its size. Why the difference between the two database connectors? Shouldn't it depend exclusively on the TField implementation?

No, the field size is calculated by the connection component and whether or not it uses UTF8 for the character set.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 14/7/25 a les 14:30, Michael Van Canneyt via fpc-pascal ha escrit:

Show quoted text 19 lines
On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: > Hello, > > In a project I'm using both an sql server database (TMSSQLConnection) > and a postgresql one (TPQConnection). > One of the TStringField is defined as size 100, but in the sql server > it has been changed to to 200. > Even without changing the TStringField.Size in the program I can get > values longer than 100 characters. > I did a test with the TPQConnection and if I change the size of a > field, the TStringField is truncated according to its size. > Why the difference between the two database connectors? Shouldn't it > depend exclusively on the TField implementation? No, the field size is calculated by the connection component and whether or not it uses UTF8 for the character set.

That's not what the documentation says

https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html (emphasis mine)

"it is the declared *maximum* size of the string (in *characters*) and is used to calculate the size of the dataset buffer."

In any case I would expect the same behaviour regardless of the database connector used.

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 28 lines
El 14/7/25 a les 14:30, Michael Van Canneyt via fpc-pascal ha escrit: > > > On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: > >> Hello, >> >> In a project I'm using both an sql server database (TMSSQLConnection) and >> a postgresql one (TPQConnection). >> One of the TStringField is defined as size 100, but in the sql server it >> has been changed to to 200. >> Even without changing the TStringField.Size in the program I can get >> values longer than 100 characters. >> I did a test with the TPQConnection and if I change the size of a field, >> the TStringField is truncated according to its size. >> Why the difference between the two database connectors? Shouldn't it >> depend exclusively on the TField implementation? > > No, the field size is calculated by the connection component and whether or > not it uses UTF8 for the character set. That's not what the documentation says https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html (emphasis mine) "it is the declared *maximum* size of the string (in *characters*) and is used to calculate the size of the dataset buffer."

Seems the documentation has not been updated for the unicode case :-(

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 14/7/25 a les 14:59, Michael Van Canneyt via fpc-pascal ha escrit:

Show quoted text 10 lines
> > That's not what the documentation says > > https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html > (emphasis mine) > > "it is the declared *maximum* size of the string (in *characters*) and > is used to calculate the size of the dataset buffer." Seems the documentation has not been updated for the unicode case :-(

Well, even the "maximum" part isn't true, at least using the TMSSQLConnection.

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 15 lines
El 14/7/25 a les 14:59, Michael Van Canneyt via fpc-pascal ha escrit: >> >> That's not what the documentation says >> >> https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html >> (emphasis mine) >> >> "it is the declared *maximum* size of the string (in *characters*) and is >> used to calculate the size of the dataset buffer." > > Seems the documentation has not been updated for the unicode case :-( Well, even the "maximum" part isn't true, at least using the TMSSQLConnection.

How so ? the size comes from what is reported by MSSQL.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 14/7/25 a les 17:57, Michael Van Canneyt via fpc-pascal ha escrit:

Show quoted text 21 lines
On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: > El 14/7/25 a les 14:59, Michael Van Canneyt via fpc-pascal ha escrit: > >>> >>> That's not what the documentation says >>> >>> https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html >>> (emphasis mine) >>> >>> "it is the declared *maximum* size of the string (in *characters*) >>> and is used to calculate the size of the dataset buffer." >> >> Seems the documentation has not been updated for the unicode case :-( > > Well, even the "maximum" part isn't true, at least using the > TMSSQLConnection. How so ? the size comes from what is reported by MSSQL.

Well, in both cases (using TMSSQLConnection and TPQConnection) I have specified the size of the TStringField in the object inspector. When using the TMSSQLConnection the size I specified seems to be ignored (i.e. I have set it to 100, the field has been changed to varchar(200) and I get more than 100 characters) while with TPQConnection the length is truncated (i.e. even if I change the field to varchar(200) any record with a value longer than 100 characters is truncated to 100 characters). Again, the documentation says "Size is made published by the TStringField class so it can be set in the IDE" and I expected that to be the case, which it true with TPQConnection but false with TMSSQLConnection.

Bye

Luca

Reply by email

Michael Van Canneyt
to Luca Olivetti
Luca Olivetti wrote 33 lines
El 14/7/25 a les 17:57, Michael Van Canneyt via fpc-pascal ha escrit: > > > On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: > >> El 14/7/25 a les 14:59, Michael Van Canneyt via fpc-pascal ha escrit: >> >>>> >>>> That's not what the documentation says >>>> >>>> https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html >>>> (emphasis mine) >>>> >>>> "it is the declared *maximum* size of the string (in *characters*) and >>>> is used to calculate the size of the dataset buffer." >>> >>> Seems the documentation has not been updated for the unicode case :-( >> >> Well, even the "maximum" part isn't true, at least using the >> TMSSQLConnection. > > How so ? the size comes from what is reported by MSSQL. Well, in both cases (using TMSSQLConnection and TPQConnection) I have specified the size of the TStringField in the object inspector. When using the TMSSQLConnection the size I specified seems to be ignored (i.e. I have set it to 100, the field has been changed to varchar(200) and I get more than 100 characters) while with TPQConnection the length is truncated (i.e. even if I change the field to varchar(200) any record with a value longer than 100 characters is truncated to 100 characters). Again, the documentation says "Size is made published by the TStringField class so it can be set in the IDE" and I expected that to be the case, which it true with TPQConnection but false with TMSSQLConnection.

Under normal circumstances, it makes little or no sense to change the size.

The size is the size of the internal buffer reserved for the data of the field. This will truncate anything you try to save in it to that size.

The only connection to the declared field size in the database is that the 'size' property of the field is initially set to a size large enough to contain the maximum amount of characters in the database field.

The only reason that you can change it in the object inspector is that when the database field size changes, you can adjust the buffer size without needing to reinitialize all persistent fields.

Michael.

Reply by email

Luca Olivetti
to Michael Van Canneyt

El 15/7/25 a les 18:01, Michael Van Canneyt via fpc-pascal ha escrit:

Show quoted text 41 lines
On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: > El 14/7/25 a les 17:57, Michael Van Canneyt via fpc-pascal ha escrit: >> >> >> On Mon, 14 Jul 2025, Luca Olivetti via fpc-pascal wrote: >> >>> El 14/7/25 a les 14:59, Michael Van Canneyt via fpc-pascal ha escrit: >>> >>>>> >>>>> That's not what the documentation says >>>>> >>>>> https://www.freepascal.org/docs-html/fcl/db/tstringfield.size.html >>>>> (emphasis mine) >>>>> >>>>> "it is the declared *maximum* size of the string (in *characters*) >>>>> and is used to calculate the size of the dataset buffer." >>>> >>>> Seems the documentation has not been updated for the unicode case :-( >>> >>> Well, even the "maximum" part isn't true, at least using the >>> TMSSQLConnection. >> >> How so ? the size comes from what is reported by MSSQL. > > Well, in both cases (using TMSSQLConnection and TPQConnection) I have > specified the size of the TStringField in the object inspector. > When using the TMSSQLConnection the size I specified seems to be > ignored (i.e. I have set it to 100, the field has been changed to > varchar(200) and I get more than 100 characters) while with > TPQConnection the length is truncated (i.e. even if I change the field > to varchar(200) any record with a value longer than 100 characters is > truncated to 100 characters). > Again, the documentation says "Size is made published by the > TStringField class so it can be set in the IDE" and I expected that to > be the case, which it true with TPQConnection but false with > TMSSQLConnection. Under normal circumstances, it makes little or no sense to change the size.

I'm probably very bad at explaining myself: the size was automatically set when I double clicked on the (opened) query component and added the fields. Then somebody else doubled the size of the underlying field in the database...

The size is the size of the internal buffer reserved for the data of the field. This will truncate anything you try to save in it to that size.

...and the data *wasn't* truncated to the specified size when reading the data using a TMSSQLConnection, so, curious, I tried the same with postgresql and it does truncate the data.

Show quoted text 9 lines
The only connection to the declared field size in the database is that the 'size' property of the field is initially set to a size large enough to contain the maximum amount of characters in the database field. The only reason that you can change it in the object inspector is that when the database field size changes, you can adjust the buffer size without needing to reinitialize all persistent fields.

I don't even know how to do that :-D in any case what I see is that a TMSSQLConnection doesn't seem to respect the size set in the object inspector while a TPQConnection does and I find that inconsistent.

Bye

Luca

Reply by email