Reputation: 8043
After enlarging a database table's VarChar
field from size 20
to size 50
, I have to update the Size
property of hundreds of design-time created TFields
and I would like to do that once for all (I don't want to do the same work again, as soon someone will decide to change the field's size again).
The smartest way that comes to my mind consists in setting the TField
's Size
property at runtime:
const
C_MY_FIELD_SIZE = 50;
...
MyField.Size := C_MY_FIELD_SIZE;
The only "problem" is that other programmers will see a misleading value displayed in the Size
property of the Object Inspector at design time.
I'm wondering if there is an equivalent design-time solution
Upvotes: 1
Views: 352
Reputation: 201
yes, I use SQL everywhere, not TTables and one of the problems is the max size of Edit boxes. In your case, that size is used for TField size. Well, what I do is at application start, do a query to the database that gets the size of all fields of the tables. In MsSqlServer is:
select table_name, column_name, character_maximum_length
from information_schema.columns where data_type='varchar' or data_type='nvarchar'
order by TABLE_NAME
that query returns: tableName, field, size in characters for all the fields. With that, build for example an array, or a List to get that info cached in your application.
For example, an array with:
aLongs[i].sTable:=uppercase(qAux.fields[0].asstring);
aLongs[i].sField:=uppercase(qAux.fields[1].asstring);
aLongs[i].iLong:=qAux.fields[2].asinteger;
and then: Function getSize(sMiFieldName:string):integer; that simply search in that array the length for the field. It's necesary to get the info cached because normally that querys retrieving columns size are slows.
Sorry, I can't publish my code completely, thats a resume, I hope you get the idea: Get the sizes in run-time and cache the info in an array, list, collection or whatever.
Upvotes: 1