/// <summary> /// This is here for performance reasons since in some cases we will have already resolved the value from the db /// and want to just give this object the value so it doesn't go re-look it up from the database. /// </summary> /// <param name="val"></param> /// <param name="strDbType"></param> void IDataValueSetter.SetValue(object val, string strDbType) { //We need to ensure that val is not a null value, if it is then we'll convert this to an empty string. //The reason for this is because by default the DefaultData.Value property returns an empty string when // there is no value, this is based on the PropertyDataDto.GetValue return value which defaults to an // empty string (which is called from this class's method LoadValueFromDatabase). //Some legacy implementations of DefaultData are expecting an empty string when there is // no value so we need to keep this consistent. if (val == null) { val = string.Empty; } _value = val; //now that we've set our value, we can update our BaseDataType object with the correct values from the db //instead of making it query for itself. This is a peformance optimization enhancement. var dbType = BaseDataType.GetDBType(strDbType); var fieldName = BaseDataType.GetDataFieldName(dbType); //if misconfigured (datatype created in the tree, but save button never clicked), the datatype will be null if (_dataType != null) { _dataType.SetDataTypeProperties(fieldName, dbType); } //ensures that it doesn't go back to the db _valueLoaded = true; }
/// <summary> /// This is here for performance reasons since in some cases we will have already resolved the value from the db /// and want to just give this object the value so it doesn't go re-look it up from the database. /// </summary> /// <param name="val"></param> /// <param name="strDbType"></param> void IDataValueSetter.SetValue(object val, string strDbType) { _value = val; //now that we've set our value, we can update our BaseDataType object with the correct values from the db //instead of making it query for itself. This is a peformance optimization enhancement. var dbType = BaseDataType.GetDBType(strDbType); var fieldName = BaseDataType.GetDataFieldName(dbType); _dataType.SetDataTypeProperties(fieldName, dbType); //ensures that it doesn't go back to the db _valueLoaded = true; }
/// <summary> /// Loads the data value from the database. /// </summary> protected virtual void LoadValueFromDatabase() { //this is an optimized version of this query. In one call it will return the data type //and the values, this will then set the underlying db type and value of the BaseDataType object //instead of having it query the database itself. var sql = @" SELECT dataInt, dataDate, dataNvarchar, dataNtext, dbType FROM cmsPropertyData INNER JOIN cmsPropertyType ON cmsPropertyType.id = cmsPropertyData.propertytypeid INNER JOIN cmsDataType ON cmsDataType.nodeId = cmsPropertyType.dataTypeId WHERE cmsPropertyData.id = " + m_PropertyId; using (var r = SqlHelper.ExecuteReader(sql)) { if (r.Read()) { //the type stored in the cmsDataType table var strDbType = r.GetString("dbType"); //get the enum of the data type var dbType = BaseDataType.GetDBType(strDbType); //get the column name in the cmsPropertyData table that stores the correct information for the data type var fieldName = BaseDataType.GetDataFieldName(dbType); //get the value for the data type, if null, set it to an empty string m_Value = r.GetObject(fieldName) ?? string.Empty; //now that we've set our value, we can update our BaseDataType object with the correct values from the db //instead of making it query for itself. This is a peformance optimization enhancement. _dataType.SetDataTypeProperties(fieldName, dbType); } } }
/// <summary> /// Loads the data value from the database. /// </summary> protected virtual void LoadValueFromDatabase() { var sql = new Sql(); sql.Select("*") .From <PropertyDataDto>() .InnerJoin <PropertyTypeDto>() .On <PropertyTypeDto, PropertyDataDto>(x => x.Id, y => y.PropertyTypeId) .InnerJoin <DataTypeDto>() .On <DataTypeDto, PropertyTypeDto>(x => x.DataTypeId, y => y.DataTypeId) .Where <PropertyDataDto>(x => x.Id == _propertyId); var dto = Database.Fetch <PropertyDataDto, PropertyTypeDto, DataTypeDto>(sql).FirstOrDefault(); if (dto != null) { //the type stored in the cmsDataType table var strDbType = dto.PropertyTypeDto.DataTypeDto.DbType; //get the enum of the data type var dbType = BaseDataType.GetDBType(strDbType); //get the column name in the cmsPropertyData table that stores the correct information for the data type var fieldName = BaseDataType.GetDataFieldName(dbType); //get the value for the data type, if null, set it to an empty string _value = dto.GetValue; //now that we've set our value, we can update our BaseDataType object with the correct values from the db //instead of making it query for itself. This is a peformance optimization enhancement. _dataType.SetDataTypeProperties(fieldName, dbType); } }