How to detect if column is required or not (nullable)

Posts   
 
    
ivanc
User
Posts: 36
Joined: 29-Apr-2004
# Posted on: 23-Jun-2004 11:50:01   

Is there a way to determine if a column in a collection is required (is not nullable). I have tried using something like this:


((EntityField)_someEntity.Fields[FieldName]).SourceColumnIsNullable

but it doesn't seem to work as I thought it would (Invalid cast exception).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 23-Jun-2004 11:58:40   

Are you using Adapter? If you're using SelfServicing, you don't have to cast, the properties are there automatically, as the indexer in Fields already returns a correct object.

Frans Bouma | Lead developer LLBLGen Pro
ivanc
User
Posts: 36
Joined: 29-Apr-2004
# Posted on: 24-Jun-2004 18:50:37   

Yes, I'm using adapter.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 24-Jun-2004 20:13:39   

You can derive a class from DataAccessAdapter and retrieve the persistence information for a given field by creating an override of GetFieldPersistenceInfo and simply calling hte base class' method, passing in the field. You can then return the IFieldPersistenceInfo object which contains the information about the database field, like nullability, precision, scale etc.

If you need help with this, let me know.

Frans Bouma | Lead developer LLBLGen Pro
eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 21-Feb-2005 17:20:22   

Dear Frans,

is it possible for you to post an example of how this can be accomplished? My objective is to have a method that would return an array of all properties of an entity that map to a non-nullable field in the DB.

Thank you very much in advance.

Have a nice evening!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 21-Feb-2005 17:53:09   

Something like this?


public ArrayList NonNullableFields(IEntity2 entity)
{
    IFieldPersistenceInfo[] infos = base.GetFieldPersistenceInfos(entity);
    
    ArrayList toReturn = new ArrayList();
    for(int i=0;i<entity.Fields.Count;i++)
    {
        if(infos[i].SourceColumnIsNullable)
        {
            toReturn.Add(entity.Fields[i]);
        }
    }
    
    return toReturn;
}

Frans Bouma | Lead developer LLBLGen Pro
eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 21-Feb-2005 18:12:42   

Dear Frans,

I did manage to get the IFieldPersistenceInfo array out of the inherited Adapter. What I need now is the following:
- I need to find all fields that are not nullable - Sort field out that are auto-filled (Identity, timestamps, Fields with defaults) - Check for User-changes: TestOriginalFieldValueForNull works only on fetched entities and not on new ones, for new Entites I use IsChanged

As you can see from the code I did manage some of these issues what remains is:

  • SourceColumnDbType returns an integer. How can I interpret this integer? do you use some enum to identify types?
  • I don't know if the PersistenceInfo kann tell me if a column has a default value, is this possible?
            
Dim _fieldInfos As IFieldPersistenceInfo()
Dim _fieldInfo As IFieldPersistenceInfo
Dim _error As EntityValidationError
Dim i As Integer

_fieldInfos = New PersistenceInfoAdapter().GetPersistanceInfo(Me.Auftragsposition.LLBLGenProEntityName)

For i = _fieldInfos.GetLowerBound(0) To _fieldInfos.GetUpperBound(0)
   If Not _fieldInfos(i).SourceColumnIsNullable AndAlso _fieldInfos(i).IsIdentity = False AndAlso _fieldInfos(i).SourceColumnDbType <>  SourceColumnDbTypeEnumShouldoneExist AndAlso _
                (Me.Auftragsposition.TestOriginalFieldValueForNull(Me.Auftragsposition.Fields(_fieldInfos(i).SourceColumnName).FieldIndex) _
                Or Me.Auftragsposition.Fields(_fieldInfos(i).SourceColumnName).IsChanged = False) Then
                    _error = New EntityValidationError
                    _error.Message = "Feld darf nicht leer sein"
                    _error.MessageCode = "Validate_Auftragsposition_03"
                    _error.Source = _fieldInfos(i).SourceColumnName
                    MyBase.ErrorCol.Add(MyBase.ErrorCol.Count, _error)
                End If
            Next

Again, thank you very much for your support.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39862
Joined: 17-Aug-2003
# Posted on: 21-Feb-2005 21:13:21   

eugene wrote:

Dear Frans,

I did manage to get the IFieldPersistenceInfo array out of the inherited Adapter. What I need now is the following:
- I need to find all fields that are not nullable - Sort field out that are auto-filled (Identity, timestamps, Fields with defaults) - Check for User-changes: TestOriginalFieldValueForNull works only on fetched entities and not on new ones, for new Entites I use IsChanged

As you can see from the code I did manage some of these issues what remains is:

  • SourceColumnDbType returns an integer. How can I interpret this integer? do you use some enum to identify types?
  • I don't know if the PersistenceInfo kann tell me if a column has a default value, is this possible?

SourceColumnDbType is the enum value of the .NET provider type. So if you're using SqlServer, it's a SqlDbTypes value. Cast to that type and you know the db type simple_smile

Default value is not stored in PersistenceInfo, it is in the project though. You could create a .lpt template and generate a class which holds this info, add it to the database specific project and consult that class for that info simple_smile

Frans Bouma | Lead developer LLBLGen Pro