/// <summary>
        /// Determines whether this instance [can generate key] the specified pk.
        /// </summary>
        /// <param name="pk">The pk.</param>
        /// <param name="pInfo">The p info.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="typeConverterStore">The type converter store.</param>
        /// <returns>
        ///     <c>true</c> if this instance [can generate key] the specified pk; otherwise, <c>false</c>.
        /// </returns>
        public static bool CanGenerateKey(this PrimaryKeyProperty pk, PropertyAccessor pInfo, object entity,
                                          ITypeConverterStore typeConverterStore)
        {
            if (pk.KeyGenerator == null)
            {
                return(false);
            }

            //now let's  check the value
            var idValue = pInfo.GetMethod(entity);

            if (idValue == null)
            {
                return(true);
            }

            var converter = typeConverterStore.GetConverterFactoryMethod(idValue.GetType());

            try
            {
                object unsavedVal = converter.Invoke(pk.UnsavedValueString);
                if (idValue.Equals(unsavedVal))
                {
                    return(true);
                }

                return(false);
            }
            catch (Exception ex)
            {
                throw new MappingException(
                          string.Format("Could not convert '{0}' to {1} for property {2}", pk.UnsavedValueString,
                                        idValue.GetType().FullName, pk.Key.PropertyName), ex);
            }
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigManager"/> class.
        /// </summary>
        /// <param name="config">The config.</param>
        /// <exception cref="System.ArgumentNullException">config</exception>
        public ConfigManager(MapConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            mainMap            = config;
            typeConverterStore = new TypeConverterStore();
        }
Example #3
0
        /// <summary>
        /// Overrides the type converter factory.
        /// </summary>
        /// <param name="converterStore">The type converter factory.</param>
        /// <returns></returns>
        public IConfigurationManager RegisterTypeConverterStore(ITypeConverterStore converterStore)
        {
            if (converterStore == null)
            {
                throw new ArgumentNullException("converterStore");
            }

            typeConverterStore = converterStore;
            return(this);
        }
        public static object GetUnsavedValue(this PrimaryKeyProperty pk, Type type, ITypeConverterStore typeConverterStore)
        {
            if (pk == null)
            {
                return(null);
            }


            if (string.IsNullOrWhiteSpace(pk.UnsavedValueString))
            {
                if (type.IsValueType && type.IsPrimitive)
                {
                    if (type == typeof(string))
                    {
                        return(string.Empty);
                    }
                    if ((type == typeof(int)) || (type == typeof(int?)))
                    {
                        return(0);
                    }
                    if ((type == typeof(long)) || (type == typeof(long?)))
                    {
                        return((long)0);
                    }
                    if ((type == typeof(short)) || (type == typeof(short?)))
                    {
                        return((short)0);
                    }
                    if ((type == typeof(double)) || (type == typeof(double?)))
                    {
                        return((double)0);
                    }
                    if ((type == typeof(float)) || (type == typeof(float?)))
                    {
                        return((float)0.0);
                    }
                    if ((type == typeof(Guid)) || (type == typeof(Guid?)))
                    {
                        return(Guid.Empty);
                    }
                }
            }
            else
            {
                var convertMethod = typeConverterStore.GetConverterFactoryMethod(type);
                var value         = convertMethod(pk.UnsavedValueString);
                return(value);
            }

            return(null);
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EntitySerializer"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <param name="typeConverterStore">The type converter.</param>
        public EntitySerializer(IDatabaseSettings settings, ITypeConverterStore typeConverterStore)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (typeConverterStore == null)
            {
                typeConverterStore = new TypeConverterStore();
            }

            this.settings      = settings;
            TypeConverterStore = typeConverterStore;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DataExporterAdapter" /> class.
        /// </summary>
        /// <param name="importDialect">The dialect.</param>
        /// <param name="exportDialect">The export dialect.</param>
        /// <param name="dbConnector">The database connector.</param>
        /// <param name="typeConverter">The type converter.</param>
        /// <exception cref="System.ArgumentNullException">
        /// </exception>
        public DataExporterAdapter(SqlDialect importDialect, SqlDialect exportDialect, IDbConnector dbConnector, ITypeConverterStore typeConverter)
        {
            if (importDialect == null)
            {
                throw new ArgumentNullException(nameof(importDialect));
            }
            if (dbConnector == null)
            {
                throw new ArgumentNullException(nameof(dbConnector));
            }
            if (typeConverter == null)
            {
                throw new ArgumentNullException(nameof(typeConverter));
            }

            this.importDialect = importDialect;
            this.exportDialect = exportDialect;
            this.dbConnector   = dbConnector;
            this.typeConverter = typeConverter;
        }
Example #7
0
        void ProcessRelation(Relation rel, object entity, EntityMap entityMap, InsertSqlInfo info, EntityAccessor entityAccessor, InsertSqlExecutionList executionList, ITypeConverterStore converterStore, ISession session)
        {
            if ((rel.RelationType != RelationshipType.ManyToOne) || rel.IsAutoGenerated)
            {
                return;
            }

            var paramName = ParameterNameBuilderHelper.QueryParamName(entityMap, rel.ColumnName);
            //first let's see if property is null or not  than we need to compare the primary key with unsaved value
            PropertyAccessor pinf;

            if (!entityAccessor.Properties.TryGetValue(rel.Name, out pinf))
            {
                throw new GoliathDataException("Property " + rel.Name + " not found in entity.");
            }

            object relValue = pinf.GetMethod(entity);

            if (relValue != null)
            {
                //now we need to get unsaved value;
                var relEntityType  = relValue.GetType();
                var relEntityMap   = session.SessionFactory.DbSettings.Map.GetEntityMap(relEntityType.FullName);
                var relEntAccessor = entityAccessorStore.GetEntityAccessor(relEntityType, relEntityMap);

                PropertyAccessor relPinf;
                if (!relEntAccessor.Properties.TryGetValue(rel.ReferenceProperty, out relPinf))
                {
                    throw new GoliathDataException("Reference " + rel.ReferenceProperty + " not found in entity.");
                }

                if (relEntityMap.PrimaryKey != null)
                {
                    foreach (var pk in relEntityMap.PrimaryKey.Keys)
                    {
                        object pkValue;
                        if (HasUnsavedValue(pk, relPinf, relValue, converterStore, out pkValue))
                        {
                            Build(relValue, relEntityType, relEntityMap, executionList, session);
                        }

                        QueryParam qParam;
                        if (!info.Parameters.TryGetValue(paramName, out qParam))
                        {
                            qParam = new QueryParam(paramName, rel.DbType);
                            info.Columns.Add(paramName, rel.ColumnName);
                            info.Parameters.Add(paramName, qParam);
                        }

                        qParam.Value = relPinf.GetMethod(relValue);
                    }
                }
            }
        }
Example #8
0
        void ProcessManyToManyRelation(Relation rel, object entity, EntityMap entityMap, InsertSqlInfo info, EntityAccessor entityAccessor, InsertSqlExecutionList executionList, ITypeConverterStore converterStore, ISession session)
        {
            if ((rel.RelationType != RelationshipType.ManyToMany) || !rel.Inverse)
            {
                return;
            }

            PropertyAccessor pinf;

            if (!entityAccessor.Properties.TryGetValue(rel.Name, out pinf))
            {
                throw new GoliathDataException("Property " + rel.Name + " not found in entity.");
            }

            var collection = pinf.GetMethod(entity) as System.Collections.IEnumerable;
            int counter    = 1;

            if (collection != null)
            {
                foreach (var relObj in collection)
                {
                    if (relObj == null)
                    {
                        continue;
                    }

                    var relEntityType  = relObj.GetType();
                    var relEntityMap   = session.SessionFactory.DbSettings.Map.GetEntityMap(relEntityType.FullName);
                    var relEntAccessor = entityAccessorStore.GetEntityAccessor(relEntityType, relEntityMap);
                    var paramName      = ParameterNameBuilderHelper.QueryParamName(entityMap, rel.MapColumn + counter);
                    var mapParamName   = ParameterNameBuilderHelper.QueryParamName(entityMap, rel.MapReferenceColumn + counter);

                    PropertyAccessor relPinf;
                    if (!relEntAccessor.Properties.TryGetValue(rel.ReferenceProperty, out relPinf))
                    {
                        throw new GoliathDataException("Reference " + rel.ReferenceProperty + " not found in entity.");
                    }

                    if (relEntityMap.PrimaryKey != null)
                    {
                        foreach (var pk in relEntityMap.PrimaryKey.Keys)
                        {
                            object pkValue;
                            if (HasUnsavedValue(pk, relPinf, relObj, converterStore, out pkValue))
                            {
                                Build(relObj, relEntityType, relEntityMap, executionList, session);
                            }

                            var manyToManyInfo = new InsertSqlInfo {
                                DelayExecute = true, TableName = rel.MapTableName
                            };

                            var param1 = new QueryParam(mapParamName, pk.Key.DbType)
                            {
                                Value = relPinf.GetMethod(relObj)
                            };
                            manyToManyInfo.Columns.Add(mapParamName, rel.MapReferenceColumn);
                            manyToManyInfo.Parameters.Add(mapParamName, param1);

                            PropertyAccessor mappedPinf;
                            if (!entityAccessor.Properties.TryGetValue(rel.MapPropertyName, out mappedPinf))
                            {
                                throw new GoliathDataException("Property " + rel.MapPropertyName + " not found in entity" + entityMap.FullName + ".");
                            }

                            var param2 = new QueryParam(paramName, rel.DbType)
                            {
                                Value = mappedPinf.GetMethod(entity)
                            };
                            manyToManyInfo.Columns.Add(paramName, rel.MapColumn);
                            manyToManyInfo.Parameters.Add(paramName, param2);

                            executionList.ExcuteStatement(session, manyToManyInfo, typeof(object));
                        }
                    }

                    counter++;
                }
            }
        }
Example #9
0
        bool HasUnsavedValue(PrimaryKeyProperty pk, PropertyAccessor pinf, object entity, ITypeConverterStore converterStore, out object pkValue)
        {
            if (!pk.UnsavedValueProcessed)
            {
                pk.UnsavedValue          = pk.GetUnsavedValue(pinf.PropertyType, converterStore);
                pk.UnsavedValueProcessed = true;
            }

            pkValue = pinf.GetMethod(entity);

            var rlt = object.Equals(pkValue, pk.UnsavedValue);

            return(rlt);
        }