Determining default value from db?

Posts   
 
    
benles
User
Posts: 62
Joined: 02-May-2005
# Posted on: 08-Feb-2007 17:41:01   

I would like to determine whether a default value exists on the given field in the database.

EntityFieldDefinition doesn't appear to have a 'HasDefaultValue' property or anything like that, but is there a way?

Thanks simple_smile

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 08-Feb-2007 18:04:48   

It's located in the mappedfield (the field in the table/view the entity field is mapped on).

Frans Bouma | Lead developer LLBLGen Pro
Miksovic avatar
Miksovic
User
Posts: 6
Joined: 16-Apr-2008
# Posted on: 16-Apr-2008 18:02:38   

Is it possible to determine wheather the default value exist in database using TDL?

I am generating validation code like this:

switch ((<[ CurrentEntityName ]>FieldIndex)fieldIndex)
{
    <[ Foreach EntityField ]>
        case <[ CurrentEntityName ]>FieldIndex.<[ EntityFieldName ]>:                   
            <[ If Not IsPrimaryKey ]>    <[ If Not IsForeignKey ]>                      
            fieldInfo = entityToValidate.Fields[(int)<[ CurrentEntityName ]>FieldIndex.<[ EntityFieldName ]>];                      
            if(!fieldInfo.IsNullable && IsNullOrEmpty(value))
            {
                msg = "<[ EntityFieldName ]> value is required";
                involvedEntity.SetEntityFieldError("<[ EntityFieldName ]>", msg, false);
            }
            //<[ MappedFieldHasDefaultValue ]>  //<[ EntityFieldName ]>                             

            <[ EndIf ]>     <[ EndIf ]>
            break;
    <[ NextForeach ]>
}

and would like to skip the validation code (don't generate it) when current field is

  • a primary key
  • a foreign key - there is a default value in db

something like

<[ If Not HasDefaultValue >]

Is there any way to solve that?

thanks for answer

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 17-Apr-2008 10:58:31   

The meta data is available for some databases. It's however not accessable through TDL, as there's no TDL statement to access it. (it's not used in our generated code). you can access it in an .lpt template. You can include .lpt templates inside TDL templates as a normal include template so that could solve your issue.

To check whether an entityfielddefinition in an entity has a default value: if(currentEntityField.MappedField.HasDefaultvalue) { // has default value }

Please check the TDL and .lpt sections in the SDK documentation how to write a simple .lpt template for this. Check the 'Grammar' section in the lpt section about what _activeObject is when an .lpt template is included inside a TDL template.

Frans Bouma | Lead developer LLBLGen Pro
sybjeb
User
Posts: 81
Joined: 20-Sep-2011
# Posted on: 20-Sep-2011 10:01:59   

As previous users, I need to know what the default value of a field is.

To achieve it, I follow your requirements :

  • I create a template bindings file
  • I create a** .lpt file** which I bound to the Custom_EntityInitializationTemplate

So, I'm able to add some code to the **InitClassEmpty **method of the entities.

Unfortunately, when I try to access to the **MappedField **of the **EntityFieldDefinition **object, the following error occured :

CS0246 : 'EntityFieldDefinition' Type or namespace can't be found (I translate the french message so...).

It seems that the code generator changed since 2008 and so the EntityFieldDefinition doesn't exist anymore.

Please, help me confused

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 20-Sep-2011 10:58:01   

I think you need to use the GetGroupableModelElementMapping as explained here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=18035&StartAtMessage=0&#102438

sybjeb
User
Posts: 81
Joined: 20-Sep-2011
# Posted on: 20-Sep-2011 12:18:51   

Thanks Walaa for your help.

I now have another little question. What I really want is generate a public method for each property of the entity which has a default value and I have been able to do it with the following source code but I use the Custom_EntityInitializationTemplate template ID so it generates the method in the InitClassEmpty method.

The question is : How can I generate this method directly in the entity itself? I tried the Custom_EntityAdapterTemplate (I'm in adapter mode) but no more method is generated in this case.

Thanks in advance.

// 1) Get the entity mapping <%EntityDefinition entity = ((Hashtable) _activeObject)["CurrentEntity"] as EntityDefinition; var entityMapping = _executingGenerator.ProjectDefinition.GetGroupableModelElementMapping(entity, _executingGenerator.DriverID);

// 2) For each field, get the associated mapping foreach(FieldElement fieldElement in entity.Fields) { if (fieldElement.Name != "LastChange" && fieldElement.Name != "IsActiveRecord") { FieldMapping fieldMapping = entityMapping.GetFieldMappingOfField(fieldElement);

    // 3) If the column has a default value, create a GetDefaultXXXXValue() method
    if (fieldMapping.MappedTarget.HasDefaultValue)
    {%>
        public <%=fieldMapping.MappedTarget.NETTypeAsString%> GetDefault<%=fieldElement.Name%>Value()
        {
            // 4) Need to manage string differently because it needs some "" around
            <%if (fieldMapping.MappedTarget.NETTypeAsString == "string")
            {%>         
                return "<%=fieldMapping.MappedTarget.DefaultValue%>";
            <%}
            else
            {%>
                return <%=fieldMapping.MappedTarget.DefaultValue%>;
            <%}%>
        }
    <%}
}

}%>

sybjeb
User
Posts: 81
Joined: 20-Sep-2011
# Posted on: 20-Sep-2011 13:00:43   

I found the matter : I checked the "Include only" so...