private ISqlStatements ConstructSqlStatements(EntityMapping entityMapping)
        {
            entityMapping.FreezeMapping();

            ISqlStatements       sqlStatements;
            IStatementSqlBuilder statementSqlBuilder;

            switch (entityMapping.Dialect)
            {
            case SqlDialect.MsSql:
                statementSqlBuilder = new MsSqlBuilder(this, entityMapping);
                break;

            case SqlDialect.MySql:
                statementSqlBuilder = new MySqlBuilder(this, entityMapping);
                break;

            case SqlDialect.PostgreSql:
                statementSqlBuilder = new PostgreSqlBuilder(this, entityMapping);
                break;

            case SqlDialect.SqLite:
                statementSqlBuilder = new SqLiteBuilder(this, entityMapping);
                break;

            default:
                throw new NotSupportedException($"Dialect {entityMapping.Dialect} is not supported");
            }

            sqlStatements = new GenericSqlStatements <TEntity>(statementSqlBuilder);
            return(sqlStatements);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the sql statements for an entity mapping, or the default one if the argument is null.
        /// </summary>
        public ISqlStatements GetSqlStatements(EntityMapping entityMapping = null)
        {
            entityMapping = entityMapping ?? DefaultEntityMapping;

            entityMapping.FreezeMapping();

            ISqlStatements       sqlStatements;
            IStatementSqlBuilder statementSqlBuilder;

            var originalRegisteredEntityMappings = _registeredEntityMappings;

            if (!originalRegisteredEntityMappings.TryGetValue(entityMapping, out sqlStatements))
            {
                switch (entityMapping.Dialect)
                {
                case SqlDialect.MsSql:
                    statementSqlBuilder = new MsSqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.MySql:
                    statementSqlBuilder = new MySqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.PostgreSql:
                    statementSqlBuilder = new PostgreSqlBuilder(this, entityMapping);
                    break;

                case SqlDialect.SqLite:
                    statementSqlBuilder = new SqLiteBuilder(this, entityMapping);
                    break;

                default:
                    throw new NotSupportedException($"Dialect {entityMapping.Dialect} is not supported");
                }

                sqlStatements = new GenericSqlStatements <TEntity>(statementSqlBuilder);

                // replace the original collection
                // rather than using a lock, we prefer the risk of missing a registration or two as they will get captured eventually
                _registeredEntityMappings = new Dictionary <EntityMapping, ISqlStatements>(originalRegisteredEntityMappings)
                {
                    [entityMapping] = sqlStatements
                };
            }
            return(sqlStatements);
        }