예제 #1
0
        protected override void generateDbParameterList()
        {
            IList <string> nestedPropertyNameListToInsertUpdate = getNestedPropertyNameListToInsertUpdate();

            Dictionary <string, AutoSetColumnMapping> autoSetColumnMappingDictionary = null;

            Dictionary <string, string> returningColumnNamePropertyNameCache = null;
            Dictionary <string, string> insertUpdateColumnNameValueNameCacheForNonPrimaryKey = null;
            Dictionary <string, string> insertUpdateColumnNameValueNameCacheForPrimaryKey    = null;

            Dictionary <string, ValueProviderInfo> nestedPropertyValueProviderInfoCacheAtMapper = null;

            if (fillBackCommandMode == FillBackCommandMode.Insert)
            {
                autoSetColumnMappingDictionary = entityMapping.InsertAutoSetColumnMappingDictionary;
                insertUpdateColumnNameValueNameCacheForNonPrimaryKey = entityMapping.insertColumnNameValueNameForNonPrimaryKeyCacheAtMapper;
                insertUpdateColumnNameValueNameCacheForPrimaryKey    = entityMapping.insertColumnNameValueNameForPrimaryKeyCacheAtMapper;
                returningColumnNamePropertyNameCache         = entityMapping.insertReturningColumnNamePropertyNameCacheAtMapper;
                nestedPropertyValueProviderInfoCacheAtMapper = entityMapping.nestedPropertyValueProviderInfoForInsertCacheAtMapper;
            }
            else if (fillBackCommandMode == FillBackCommandMode.Update)
            {
                autoSetColumnMappingDictionary = entityMapping.UpdateAutoSetColumnMappingDictionary;
                insertUpdateColumnNameValueNameCacheForNonPrimaryKey = QueryMapping.updateColumnNameValueNameForNonPrimaryKeyCacheAtMapper;
                insertUpdateColumnNameValueNameCacheForPrimaryKey    = QueryMapping.updateColumnNameValueNameForPrimaryKeyCacheAtMapper;
                returningColumnNamePropertyNameCache         = QueryMapping.updateReturningColumnNamePropertyNameCacheAtMapper;
                nestedPropertyValueProviderInfoCacheAtMapper = QueryMapping.nestedPropertyValueProviderInfoForUpdateCacheAtMapper;
            }
            else if (fillBackCommandMode == FillBackCommandMode.LogicalDelete)
            {
                autoSetColumnMappingDictionary = entityMapping.LogicalDeleteAutoSetColumnMappingDictionary;
                insertUpdateColumnNameValueNameCacheForNonPrimaryKey = entityMapping.logicalDeleteColumnNameValueNameForNonPrimaryKeyCacheAtMapper;
                insertUpdateColumnNameValueNameCacheForPrimaryKey    = entityMapping.logicalDeleteColumnNameValueNameForPrimaryKeyCacheAtMapper;
                returningColumnNamePropertyNameCache         = entityMapping.logicalDeleteReturningColumnNamePropertyNameCacheAtMapper;
                nestedPropertyValueProviderInfoCacheAtMapper = entityMapping.nestedPropertyValueProviderInfoForLogicalDeleteCacheAtMapper;
            }

            insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Clear();
            insertUpdateColumnNameValueNameCacheForPrimaryKey.Clear();
            returningColumnNamePropertyNameCache.Clear();
            nestedPropertyValueProviderInfoCacheAtMapper.Clear();

            foreach (string nestedPropertyName in nestedPropertyNameListToInsertUpdate)
            {
                string               columnName           = entityMapping.NestedTableColumnNameDictionaryAtMapper[nestedPropertyName];
                ValueProviderSet     valueProviderSet     = null;
                ValueProvider        valueProvider        = 0;
                AutoSetColumnMapping autoSetColumnMapping = null;
                bool isForTimestamp        = false;
                bool isForPrimaryKey       = false;
                bool isDBNullableValueType = false;

                if (autoSetColumnMappingDictionary.TryGetValue(columnName, out autoSetColumnMapping))
                {
                    valueProviderSet      = autoSetColumnMapping.ValueProviderSet;
                    valueProvider         = valueProviderSet.ValueProvider;
                    isDBNullableValueType = entityMapping.ColumnMappingDictionary[columnName].IsDBNullableValueType;
                }
                else if (entityMapping.PrimaryKeyMapping.NestedPrimaryKeyPropertyNameList.Contains(nestedPropertyName))
                {
                    valueProviderSet = entityMapping.PrimaryKeyMapping.ValueProviderSet;
                    if (fillBackCommandMode == FillBackCommandMode.Insert)
                    {
                        valueProvider = valueProviderSet.ValueProvider;
                    }
                    else                     //update ve logicalDelete'de primary key yeni değer almayacağı için App gibi davranacak
                    {
                        valueProvider = ValueProvider.App;
                    }

                    isForPrimaryKey = true;
                }
                else if (entityMapping.TimestampMapping.ExistsForNestedPropertyName(nestedPropertyName))
                {
                    valueProviderSet = entityMapping.TimestampMapping.ValueProviderSet;
                    valueProvider    = valueProviderSet.ValueProvider;
                    isForTimestamp   = true;
                }
                else                  // diğer table column'lar için de default App, ancak logical delete'de bunlarla işimiz olmadığından es geç
                {
                    if (fillBackCommandMode == FillBackCommandMode.LogicalDelete)
                    {
                        continue;
                    }
                    else
                    {
                        valueProvider         = ValueProvider.App;
                        isDBNullableValueType = entityMapping.ColumnMappingDictionary[columnName].IsDBNullableValueType;
                    }
                }

                ValueProviderInfo valueProviderInfo = new ValueProviderInfo();
                valueProviderInfo.columnName            = columnName;
                valueProviderInfo.isDBNullableValueType = isDBNullableValueType;
                valueProviderInfo.isForPrimaryKey       = isForPrimaryKey;
                valueProviderInfo.isForTimestamp        = isForTimestamp;
                valueProviderInfo.valueProvider         = valueProvider;
                valueProviderInfo.valueProviderSet      = valueProviderSet;

                nestedPropertyValueProviderInfoCacheAtMapper.Add(nestedPropertyName, valueProviderInfo);

                if (valueProvider == ValueProvider.App)
                {
                    if (isForPrimaryKey)
                    {
                        insertUpdateColumnNameValueNameCacheForPrimaryKey.Add(columnName, entityMapping.ConnectionMapping.DBParameterPrefix + columnName);
                    }
                    else
                    {
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(columnName, entityMapping.ConnectionMapping.DBParameterPrefix + columnName);
                    }
                }
                else if (valueProvider == ValueProvider.AppFunctionDelegate)
                {
                    if (isForPrimaryKey)
                    {
                        insertUpdateColumnNameValueNameCacheForPrimaryKey.Add(columnName, entityMapping.ConnectionMapping.DBParameterPrefix + columnName);
                    }
                    else
                    {
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(columnName, entityMapping.ConnectionMapping.DBParameterPrefix + columnName);
                    }

                    returningColumnNamePropertyNameCache.Add(columnName, nestedPropertyName);
                }
                else if (valueProvider == ValueProvider.Sequence)
                {
                    if (isForPrimaryKey)
                    {
                        insertUpdateColumnNameValueNameCacheForPrimaryKey.Add(columnName, getSequenceNextValueSqlString(valueProviderSet.SequenceName));
                    }
                    else
                    {
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(columnName, getSequenceNextValueSqlString(valueProviderSet.SequenceName));
                    }

                    returningColumnNamePropertyNameCache.Add(columnName, nestedPropertyName);
                }
                else if (valueProvider == ValueProvider.DBFunction)
                {
                    if (isForPrimaryKey)
                    {
                        insertUpdateColumnNameValueNameCacheForPrimaryKey.Add(columnName, DB.schemaDotName(valueProviderSet.SchemaName, valueProviderSet.DBFunctionName) + "()");
                    }
                    else
                    {
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(columnName, DB.schemaDotName(valueProviderSet.SchemaName, valueProviderSet.DBFunctionName) + "()");
                    }

                    returningColumnNamePropertyNameCache.Add(columnName, nestedPropertyName);
                }
                else if (valueProvider == ValueProvider.DBTriggerredAutoValue)
                {
                    returningColumnNamePropertyNameCache.Add(columnName, nestedPropertyName);
                }
            }

            //Property mapsiz autoset columnlar için
            //ValueProvider deperlerine göre yapılacaklar:
            //AppFunctionDelegate = 2, //delegate invoke, bu değerle input dbparameter yarat, sqle kolonu koy
            //DBFunction = 4, //sqle dbfunction koy
            //DBTriggerredAutoValue = 5 //hiçbirşey yapma

            foreach (AutoSetColumnMapping autoSetColumnMapping in autoSetColumnMappingDictionary.Values)
            {
                if (!entityMapping.NestedTableColumnNameDictionaryAtMapper.Values.Contains(autoSetColumnMapping.ColumnName))
                {
                    DbParameter dbParameter = null;
                    if (autoSetColumnMapping.ValueProviderSet.ValueProvider == ValueProvider.AppFunctionDelegate)
                    {
                        object value = DB.tryInvokeAutoSetFunction(autoSetColumnMapping.ValueProviderSet.FunctionDelegate);
                        //TODO: mapsiz autoset column için type ve isDBNullableValueType bilgileri yok, parametre direkt set et, ancak çeşitli tiplerde sonucu test et
                        dbParameter = entityMapping.ConnectionMapping.CreateDbParameter(autoSetColumnMapping.ColumnName, value);
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(autoSetColumnMapping.ColumnName, entityMapping.ConnectionMapping.DBParameterPrefix + autoSetColumnMapping.ColumnName);
                    }
                    else if (autoSetColumnMapping.ValueProviderSet.ValueProvider == ValueProvider.DBFunction)
                    {
                        insertUpdateColumnNameValueNameCacheForNonPrimaryKey.Add(autoSetColumnMapping.ColumnName, DB.schemaDotName(autoSetColumnMapping.ValueProviderSet.SchemaName, autoSetColumnMapping.ValueProviderSet.DBFunctionName) + "()");
                    }
                }
            }

            if (fillBackCommandMode == FillBackCommandMode.Insert)
            {
                entityMapping.insertColumnNameCacheAtMapper.AddRange(entityMapping.insertColumnNameValueNameForPrimaryKeyCacheAtMapper.Keys);
                entityMapping.insertColumnNameCacheAtMapper.AddRange(entityMapping.insertColumnNameValueNameForNonPrimaryKeyCacheAtMapper.Keys);

                entityMapping.insertValueNameCacheAtMapper.AddRange(entityMapping.insertColumnNameValueNameForPrimaryKeyCacheAtMapper.Values);
                entityMapping.insertValueNameCacheAtMapper.AddRange(entityMapping.insertColumnNameValueNameForNonPrimaryKeyCacheAtMapper.Values);
            }
        }
예제 #2
0
        protected override DbParameter[] createDbParameterList()
        {
            //Primary key, timestamp, property mapli autoset column ve diğer table columnlar için
            //ValueProvider deperlerine göre yapılacaklar:
            //App = 1, //property değeriyle input dbparameter yarat, sqle kolonu koy
            //AppFunctionDelegate = 2, //delegate invoke et, bu değerle inputoutput dbparameter yarat, sqle kolonu koy, sqlde return et/property set et
            //Sequence = 3, //boş değerle inputoutput dbparameter yarat, sqle sequence koy, sqlde return et/property set et
            //DBFunction = 4, //boş değerle inputoutput dbparameter yarat, sqle dbfunction koy, sqlde return et/property set et
            //DBTriggerredAutoValue = 5 //boş değerle inputoutput dbparameter yarat, sqle kolonu koyma, sqlde return et/property set et

            List <DbParameter> dbParameterList = new List <DbParameter>();
            //mappingde olmayan propertylar için birşey yapılmaz

            IList <string> nestedPropertyNameListToInsertUpdate = getNestedPropertyNameListToInsertUpdate();

            //update ve logicalDelete için diğer dbparameterlara ek olarak, OLD_ ile başlayan
            //mevcut timestamp property değeri için input dbparameter, App = 1 mantığıyla oluşturulacak
            if (fillBackCommandMode != FillBackCommandMode.Insert && nestedPropertyNameListToInsertUpdate.Contains(entityMapping.TimestampMapping.NestedPropertyName))
            {
                DbParameter dbParameterForCurrentTimestamp = null;
                //App = 1, //property değeriyle input dbparameter yarat, sqle kolonu koy
                //timestamp property değeri null ise dbparameter yaratma (selectten çekilmediği varsayılır, logicaldelete'de query ismi parametre olarak geçilmediği için buna bakılıyor)
                dbParameterForCurrentTimestamp = entityMapping.CreateAndSetInputDbParameterForOldTimestampFromTimestampNestedProperty("OLD_" + entityMapping.TimestampMapping.ColumnName, entity, entityMapping.TimestampMapping.NestedPropertyName);
                if (dbParameterForCurrentTimestamp != null)
                {
                    dbParameterList.Add(dbParameterForCurrentTimestamp);
                }
                else
                {
                    throw new MappingException("Old value for timestamp property can not be null.");
                }
            }

            Dictionary <string, AutoSetColumnMapping> autoSetColumnMappingDictionary = null;
            Dictionary <string, ValueProviderInfo>    nestedPropertyValueProviderInfoCacheAtMapper = null;

            if (fillBackCommandMode == FillBackCommandMode.Insert)
            {
                autoSetColumnMappingDictionary = entityMapping.InsertAutoSetColumnMappingDictionary;
                nestedPropertyValueProviderInfoCacheAtMapper = entityMapping.nestedPropertyValueProviderInfoForInsertCacheAtMapper;
            }
            else if (fillBackCommandMode == FillBackCommandMode.Update)
            {
                autoSetColumnMappingDictionary = entityMapping.UpdateAutoSetColumnMappingDictionary;
                nestedPropertyValueProviderInfoCacheAtMapper = QueryMapping.nestedPropertyValueProviderInfoForUpdateCacheAtMapper;
            }
            else if (fillBackCommandMode == FillBackCommandMode.LogicalDelete)
            {
                autoSetColumnMappingDictionary = entityMapping.LogicalDeleteAutoSetColumnMappingDictionary;
                nestedPropertyValueProviderInfoCacheAtMapper = entityMapping.nestedPropertyValueProviderInfoForLogicalDeleteCacheAtMapper;
            }

            foreach (string nestedPropertyName in nestedPropertyNameListToInsertUpdate)
            {
                ValueProviderInfo valueProviderInfo = null;
                if (!nestedPropertyValueProviderInfoCacheAtMapper.TryGetValue(nestedPropertyName, out valueProviderInfo))
                {
                    continue;
                }

                DbParameter dbParameter = null;

                if (valueProviderInfo.valueProvider == ValueProvider.App)
                {
                    //App = 1, //property değeriyle input dbparameter yarat, sqle kolonu koy
                    //timestamp için app tanımı yok..
                    dbParameter = entityMapping.CreateAndSetInputDbParameterFromNonTimestampNestedProperty(valueProviderInfo.columnName, entity, nestedPropertyName, valueProviderInfo.isDBNullableValueType);
                }
                else if (valueProviderInfo.valueProvider == ValueProvider.AppFunctionDelegate)
                {
                    //AppFunctionDelegate = 2, //delegate invoke et, bu değerle inputoutput dbparameter yarat, sqle kolonu koy, sqlde return et/property set et
                    object value = DB.tryInvokeAutoSetFunction(valueProviderInfo.valueProviderSet.FunctionDelegate);
                    //değer delegate'den geleceği için byte[] dönüştürmeden direkt set edilecek
                    dbParameter = entityMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForNonTimestamp(valueProviderInfo.columnName, value, nestedPropertyName, valueProviderInfo.isDBNullableValueType);
                }
                else if (valueProviderInfo.valueProvider == ValueProvider.Sequence)
                {
                    //Sequence = 3, //boş değerle inputoutput dbparameter yarat, sqle sequence koy, sqlde return et/property set et
                    dbParameter = entityMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForNonTimestamp(valueProviderInfo.columnName, null, nestedPropertyName, valueProviderInfo.isDBNullableValueType);
                }
                else if (valueProviderInfo.valueProvider == ValueProvider.DBFunction)
                {
                    //DBFunction = 4, //boş değerle inputoutput dbparameter yarat, sqle dbfunction koy, sqlde return et/property set et
                    if (valueProviderInfo.isForTimestamp)
                    {
                        dbParameter = entityMapping.ConnectionMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForTimestamp(valueProviderInfo.columnName, null);
                    }
                    else
                    {
                        dbParameter = entityMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForNonTimestamp(valueProviderInfo.columnName, null, nestedPropertyName, valueProviderInfo.isDBNullableValueType);
                    }
                }
                else if (valueProviderInfo.valueProvider == ValueProvider.DBTriggerredAutoValue)
                {
                    //DBTriggerredAutoValue = 5 //boş değerle inputoutput dbparameter yarat, sqle kolonu koyma, sqlde return et/property set et
                    if (valueProviderInfo.isForTimestamp)
                    {
                        dbParameter = entityMapping.ConnectionMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForTimestamp(valueProviderInfo.columnName, null);
                    }
                    else
                    {
                        dbParameter = entityMapping.CreateInputOutputDbParameterFromValueAndNestedPropertyTypeForNonTimestamp(valueProviderInfo.columnName, null, nestedPropertyName, valueProviderInfo.isDBNullableValueType);
                    }
                }

                if (dbParameter != null)
                {
                    dbParameterList.Add(dbParameter);
                }
            }

            //Property mapsiz autoset columnlar için
            //ValueProvider deperlerine göre yapılacaklar:
            //AppFunctionDelegate = 2, //delegate invoke, bu değerle input dbparameter yarat, sqle kolonu koy
            //DBFunction = 4, //sqle dbfunction koy
            //DBTriggerredAutoValue = 5 //hiçbirşey yapma

            foreach (AutoSetColumnMapping autoSetColumnMapping in autoSetColumnMappingDictionary.Values)
            {
                if (!entityMapping.NestedTableColumnNameDictionaryAtMapper.Values.Contains(autoSetColumnMapping.ColumnName))
                {
                    DbParameter dbParameter = null;
                    if (autoSetColumnMapping.ValueProviderSet.ValueProvider == ValueProvider.AppFunctionDelegate)
                    {
                        object value = DB.tryInvokeAutoSetFunction(autoSetColumnMapping.ValueProviderSet.FunctionDelegate);
                        //TODO: mapsiz autoset column için type ve isDBNullableValueType bilgileri yok, parametre direkt set et, ancak çeşitli tiplerde sonucu test et
                        dbParameter = entityMapping.ConnectionMapping.CreateDbParameter(autoSetColumnMapping.ColumnName, value);
                    }
                    else if (autoSetColumnMapping.ValueProviderSet.ValueProvider == ValueProvider.DBFunction)
                    {
                    }
                    if (dbParameter != null)
                    {
                        dbParameterList.Add(dbParameter);
                    }
                }
            }

            return(dbParameterList.ToArray());
        }