SQL Server field default value

Posts   
 
    
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 05-Jan-2011 12:52:49   

It's possible to determine the default value of a SQL Server table field?

I want to initialize an entity using the default values defined in the schema.

I'm using Adapter scenario.

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 05-Jan-2011 15:59:31   
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 05-Jan-2011 16:16:27   

Walaa wrote:

This issue was discussed here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=15323

(edit) Another relevant thread: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=17039

Yes, but how can I retrieve the default value set in the sql server schema?

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 05-Jan-2011 17:34:05   

When do you want to retrieve the default values? At design time/code generation or at runtime?

Please bare in mind that default values may change inside the database schema, and thus maintaining/synching these changes might be a nightmare.

Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 05-Jan-2011 17:48:55   

Walaa wrote:

When do you want to retrieve the default values? At design time/code generation or at runtime?

Please bare in mind that default values may change inside the database schema, and thus maintaining/synching these changes might be a nightmare.

if it were possible at runtime, I could set the default values for a new entity, when the entity is instantiated. BTW, retrieving the default values at code generation time would be also a suitable solution.

P.S. Changing the default values in the schema is extremely rare, and when I change a value, I could regenerate the data layer.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jan-2011 05:08:50   

IMHO, you should choose between doing defaults at DB or at runtime. If you choose DB, just create entities and don't fill the values you don't want to, the DB will pull default values to these fields. If you choose in-memory, you can do it into the OnInitialized method mentioned in the first thread posted by Walaa.

David Elizondo | LLBLGen Support Team
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 07-Jan-2011 09:27:06   

daelmo wrote:

IMHO, you should choose between doing defaults at DB or at runtime. If you choose DB, just create entities and don't fill the values you don't want to, the DB will pull default values to these fields. If you choose in-memory, you can do it into the OnInitialized method mentioned in the first thread posted by Walaa.

this does not answer my question, so i think isn't possibile to retrieve the values from the DB (unless I do not use, for example, the sp_columns stored procedure in SQLServer).

I already set default values in memory. I know that not setting the values in my entity, the DB will make the work for me.

But I have a llblgen template to generate the validator classes for all entities. I also have utility methods to validate not nullable fields, and if a field is not nullable but have a default value, the validator doesn't work properly. If I knew that the field has a default value, I could skip it in the validation process.

so I repeat my original question: it's possible to retrieve the default values from the db?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 07-Jan-2011 11:03:40   

which llblgen pro version: 3.0 or 2.6?

Frans Bouma | Lead developer LLBLGen Pro
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 07-Jan-2011 11:04:42   

Otis wrote:

which llblgen pro version: 3.0 or 2.6?

3.0

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39873
Joined: 17-Aug-2003
# Posted on: 07-Jan-2011 11:13:28   

Both versions pull default values from the DB (if possible, not all db's allow it). They're stored in the target field's 'DefaultValue' property. This value is obtainable in .lpt templates, by traversing the fields in the mapping of the entity you're working on.

The default value is however the exact string in the default value property of the table field. This means that if you call a db function there (e.g. GETDATE()), the default value is (GETDATE()).

In an lpt template, obtain the GroupableModelElementMapping object by calling currentProject.GetGroupableModelElementMapping(entity, _executingGenerator.DriverID).

Then traverse the FieldMappings property which contains FieldMapping instances. The MappedTarget is the db field (view/table field) which contains the DefaultValue property.

As said, not all db's retrieve these values. See the catalog explorer whether your fields have indeed default values retrieved from the db.

Frans Bouma | Lead developer LLBLGen Pro
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 07-Jan-2011 13:18:16   

Otis wrote:

Both versions pull default values from the DB (if possible, not all db's allow it). They're stored in the target field's 'DefaultValue' property. This value is obtainable in .lpt templates, by traversing the fields in the mapping of the entity you're working on.

The default value is however the exact string in the default value property of the table field. This means that if you call a db function there (e.g. GETDATE()), the default value is (GETDATE()).

In an lpt template, obtain the GroupableModelElementMapping object by calling currentProject.GetGroupableModelElementMapping(entity, _executingGenerator.DriverID).

Then traverse the FieldMappings property which contains FieldMapping instances. The MappedTarget is the db field (view/table field) which contains the DefaultValue property.

As said, not all db's retrieve these values. See the catalog explorer whether your fields have indeed default values retrieved from the db.

thank you Otis, I created a lpt template, something like:

<%  foreach(IFieldElementCore field in entityFields)
        {
            var fieldMapping = mapping.GetFieldMappingOfField(field);
            var targetField = fieldMapping.MappedTarget;
            if(targetField.HasDefaultValue) {%>
                ...
            <%}
        }%>

Now i can determine if a default value exists for each field, and skip it in validation. The retrieved value is not usable, because, as you said, is the exact string in SQL Server db.