Example #1
0
 public DapperWriter(ILoggerFactory loggerFactory,
                     IDependencyResolver dependencyResolver)
     : base(loggerFactory, dependencyResolver)
 {
     _logger = loggerFactory.CreateLogger <DapperWriter>();
     DommelMapper.AddSqlBuilder(typeof(SqlConnection), new CustomSqlBuilder());
 }
Example #2
0
        public void InsertGuidPrimaryKey()
        {
            if (CI.IsTravis)
            {
                // Don't run SQL Server test on Linux
                return;
            }

            using (var con = new SqlServerDatabaseDriver().GetConnection())
            {
                con.Execute("CREATE TABLE dbo.Bazs (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(), Name VARCHAR(255));");

                object identity;
                try
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new GuidSqlServerSqlBuilder());
                    identity = con.Insert(new Baz {
                        Name = "blah"
                    });
                }
                finally
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new DommelMapper.SqlServerSqlBuilder());
                }

                Assert.NotNull(identity);
                var id  = Assert.IsType <Guid>(identity);
                var baz = con.Get <Baz>(id);
                Assert.NotNull(baz);
                Assert.Equal("blah", baz.Name);
                Assert.Equal(id, baz.Id);
                con.Execute("DROP TABLE dbo.Bazs");
            }
        }
Example #3
0
        public ParameterPrefixTest()
        {
            mock.As <IDbConnection>().SetupDapper(x => x.QueryFirstOrDefault <FooParameterPrefix>(It.IsAny <string>(), It.IsAny <object>(), null, null, null))
            .Returns(new FooParameterPrefix());

            var connectionType = mock.Object.GetType();

            // Change the default Sql Connection
            DommelMapper.AddSqlBuilder(connectionType, new ExampleBuilderBuilder());
        }
Example #4
0
        /// <summary>
        /// Configures Dommel JSON support using the specified <see cref="DommelJsonOptions"/>.
        /// </summary>
        /// <param name="options"></param>
        public static void AddJson(DommelJsonOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (options.EntityAssemblies == null || options.EntityAssemblies.Length == 0)
            {
                throw new ArgumentException("No entity assemblies specified.", nameof(options));
            }

            // Add SQL builders with JSON value support
            DommelMapper.AddSqlBuilder("sqlconnection", new SqlServerSqlBuilder());
            DommelMapper.AddSqlBuilder("sqlceconnection", new SqlServerCeSqlBuilder());
            DommelMapper.AddSqlBuilder("sqliteconnection", new SqliteSqlBuilder());
            DommelMapper.AddSqlBuilder("npgsqlconnection", new PostgresSqlBuiler());
            DommelMapper.AddSqlBuilder("mysqlconnection", new MySqlSqlBuilder());

            // Add a custom SqlExpression<T> factory with JSON support
            DommelMapper.SqlExpressionFactory = (type, sqlBuilder) =>
            {
                if (!(sqlBuilder is IJsonSqlBuilder))
                {
                    throw new InvalidOperationException($"The specified SQL builder type should be assignable from {nameof(IJsonSqlBuilder)}.");
                }

                var sqlExpression = typeof(JsonSqlExpression <>).MakeGenericType(type);
                return(Activator.CreateInstance(sqlExpression, sqlBuilder));
            };

            // Add a Dapper type mapper with JSON support for
            // properties annotated with the [JsonData] attribute.
            var jsonTypeHander = options.JsonTypeHandler?.Invoke() ?? new JsonObjectTypeHandler();
            var jsonTypes      = new List <Type>();

            foreach (var assembly in options.EntityAssemblies)
            {
                foreach (var type in assembly.ExportedTypes)
                {
                    foreach (var property in type.GetRuntimeProperties())
                    {
                        var jsonAttr = property.GetCustomAttribute <JsonDataAttribute>();
                        if (jsonAttr != null)
                        {
                            SqlMapper.AddTypeHandler(property.PropertyType, jsonTypeHander);
                            jsonTypes.Add(property.PropertyType);
                        }
                    }
                }
            }

            // Set a property resolver which considers the types discovered above
            // as primitive types so they will be used in insert and update queries.
            DommelMapper.SetPropertyResolver(new JsonPropertyResolver(jsonTypes));
        }
Example #5
0
        static GuildConfigRepository()
        {
            FluentMapper.Initialize(config =>
            {
                config
                .AddConvention <LowerCaseConvention>()
                .ForEntity <GuildConfigEntity>();

                config.ForDommel();

                DommelMapper.SetColumnNameResolver(new LowerCaseConvention());
                DommelMapper.AddSqlBuilder(typeof(NpgsqlConnection), new PostgresSqlBuilder());
            });
        }
Example #6
0
        static YuGiOhRepository()
        {
            FluentMapper.Initialize(config =>
            {
                config
                .AddConvention <LowerCaseConvention>()
                .ForEntity <CardEntity>()
                .ForEntity <BoosterPackEntity>();

                config.AddMap(new CardEntityMapper());
                config.AddMap(new BoosterPackEntityMapper());
                config.ForDommel();

                var resolver = new LowerCaseConvention();

                DommelMapper.SetColumnNameResolver(resolver);
                DommelMapper.AddSqlBuilder(typeof(NpgsqlConnection), new PostgresSqlBuilder());
            });
        }
        public async Task InsertGuidPrimaryKey()
        {
            if (CI.IsTravis)
            {
                // Don't run SQL Server test on Linux
                return;
            }

            using var con = new SqlServerDatabaseDriver().GetConnection();
            await con.ExecuteAsync("CREATE TABLE dbo.Quxs (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(), Name VARCHAR(255));");

            try
            {
                object identity;
                try
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new GuidSqlServerSqlBuilder());
                    identity = await con.InsertAsync(new Qux { Name = "blah" });
                }
                finally
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new SqlServerSqlBuilder());
                }

                Assert.NotNull(identity);
                var id  = Assert.IsType <Guid>(identity);
                var baz = await con.GetAsync <Qux>(id);

                Assert.NotNull(baz);
                Assert.Equal("blah", baz.Name);
                Assert.Equal(id, baz.Id);
            }
            finally
            {
                await con.ExecuteAsync("DROP TABLE dbo.Quxs");
            }
        }