public override void ValueFromParam(ClickHouseParameter parameter)
        {
            if (
#if NETCOREAPP11
                parameter.DbType == DbType.Integral || parameter.DbType == DbType.Float
#else
                parameter.DbType == DbType.Int16 ||
                parameter.DbType == DbType.Int32 ||
                parameter.DbType == DbType.Int64 ||
                parameter.DbType == DbType.UInt16 ||
                parameter.DbType == DbType.UInt32 ||
                parameter.DbType == DbType.UInt64 ||
                parameter.DbType == DbType.Single ||
                parameter.DbType == DbType.Decimal ||
                parameter.DbType == DbType.Double
#endif
                )
            {
                //anibal 20181231
                //if (((IDbDataParameter)parameter).IsNullable && parameter.Value == null)
                //{

                //}
                //else
                //{
                Data = new[] { (T)Convert.ChangeType(parameter.Value, typeof(T)) };
                //}
            }
Beispiel #2
0
        public void CopyTo()
        {
            var p1 = new ClickHouseParameter("p1")
            {
                ArrayRank               = 150,
                ClickHouseDbType        = ClickHouseDbType.IpV6,
                IsArray                 = true,
                IsNullable              = true,
                Precision               = 24,
                Scale                   = 255,
                Size                    = 123456,
                SourceColumn            = "aaaaa",
                SourceColumnNullMapping = true,
                StringEncoding          = Encoding.BigEndianUnicode,
                TimeZone                = TimeZoneInfo.Utc,
                Value                   = 123.456m
            };

            var collection = new ClickHouseParameterCollection {
                p1, new ClickHouseParameter("p2")
            };
            var p2 = collection["p2"];

            p1.CopyTo(p2);

            AssertParametersEqual(p1, p2, false);
        }
        public void ChangeParameterName()
        {
            var collection = new ClickHouseParameterCollection();
            var parameter  = new ClickHouseParameter("p1");

            Assert.Null(parameter.Collection);
            collection.Add(parameter);
            Assert.Same(collection, parameter.Collection);

            parameter.ParameterName = "{p1}";
            Assert.Same(collection, parameter.Collection);
            Assert.True(collection.TryGetValue(parameter.ParameterName, out var collectionParameter));
            Assert.Same(parameter, collectionParameter);

            parameter.ParameterName = "param123";
            Assert.Same(collection, parameter.Collection);
            Assert.True(collection.TryGetValue(parameter.ParameterName, out collectionParameter));
            Assert.Same(parameter, collectionParameter);

            parameter.ParameterName = "p1";
            Assert.Same(collection, parameter.Collection);
            Assert.True(collection.TryGetValue(parameter.ParameterName, out collectionParameter));
            Assert.Same(parameter, collectionParameter);

            parameter.ParameterName = "P1";
            Assert.Same(collection, parameter.Collection);
            Assert.True(collection.TryGetValue(parameter.ParameterName, out collectionParameter));
            Assert.Same(parameter, collectionParameter);
        }
        public void ReplaceParameter()
        {
            var collection = new ClickHouseParameterCollection();

            var parameters = new[] { "p1", "p2", "p3", "p4" }.Select(name => collection.AddWithValue(name, null, DbType.String)).ToArray();

            Assert.Equal(4, collection.Count);

            var newParam = new ClickHouseParameter("p5");

            collection[2] = newParam;
            Assert.Equal(4, collection.Count);
            Assert.Equal("p1", collection[0].ParameterName);
            Assert.Same(collection, collection[0].Collection);
            Assert.Equal("p2", collection[1].ParameterName);
            Assert.Same(collection, collection[1].Collection);
            Assert.Same(newParam, collection[2]);
            Assert.Same(collection, collection[2].Collection);
            Assert.Equal("p4", collection[3].ParameterName);
            Assert.Same(collection, collection[3].Collection);
            Assert.Null(parameters[2].Collection);

            collection["{p5}"] = parameters[2];
            Assert.Equal(4, collection.Count);
            for (int i = 0; i < parameters.Length; i++)
            {
                Assert.Same(parameters[i], collection[i]);
                Assert.Same(collection, parameters[i].Collection);
            }

            Assert.Null(newParam.Collection);

            collection["p1"] = parameters[0];
            Assert.Equal(4, collection.Count);
            for (int i = 0; i < parameters.Length; i++)
            {
                Assert.Same(parameters[i], collection[i]);
                Assert.Same(collection, parameters[i].Collection);
            }

            collection["{p5}"] = newParam;
            Assert.Equal(5, collection.Count);
            for (int i = 0; i < parameters.Length; i++)
            {
                Assert.Same(parameters[i], collection[i]);
                Assert.Same(collection, parameters[i].Collection);
            }

            Assert.Same(newParam, collection[4]);
            Assert.Same(collection, newParam.Collection);

            var ex = Assert.Throws <ArgumentException>(() => collection["p6"] = new ClickHouseParameter("p7"));

            Assert.Equal(5, collection.Count);
            Assert.Equal("parameterName", ex.ParamName);
        }
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     if (parameter.DbType == 0 || parameter.DbType == DbType.Object)
     {
         ValuesFromConst(new[] { parameter.Value as IEnumerable });
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Beispiel #6
0
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            Nulls = new[] { parameter.Value == null || parameter.Value == DBNull.Value };
            var value = parameter.Value;

            if (parameter.Value == DBNull.Value)
            {
                value = null;
            }
            InnerType.NullableValuesFromConst(new[] { value });
        }
Beispiel #7
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     if (_doubleFallback)
     {
         DataDouble = new[] { (double)Convert.ChangeType(parameter.Value, typeof(double)) }
     }
     ;
     else
     {
         DataDecimal = new[] { (decimal)Convert.ChangeType(parameter.Value, typeof(decimal)) }
     };
 }
Beispiel #8
0
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            switch (parameter.DbType)
            {
            case DbType.Guid:
                Data = new[] { (Guid)Convert.ChangeType(parameter.Value, typeof(Guid)) };
                break;

            default:
                throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to Guid.");
            }
        }
Beispiel #9
0
        public void Clone()
        {
            var p1 = new ClickHouseParameter("p1")
            {
                Value = new[] { 42 }, ClickHouseDbType = ClickHouseDbType.VarNumeric, Precision = 19, Scale = 7, ParameterMode = ClickHouseParameterMode.Interpolate
            };

            var p2 = p1.Clone();

            Assert.NotSame(p1, p2);
            AssertParametersEqual(p1, p2);
            Assert.Equal("p1", p1.ParameterName);
            Assert.Equal(p1.Value, p2.Value);
            Assert.Equal(ClickHouseDbType.VarNumeric, p2.ClickHouseDbType);
            Assert.Equal(19, p2.Precision);
            Assert.Equal(7, p2.Scale);
            Assert.Equal(ClickHouseParameterMode.Interpolate, p2.ParameterMode);

            p2.TimeZone       = TimeZoneInfo.Local;
            p2.Size           = 35;
            p2.ArrayRank      = 3;
            p2.StringEncoding = Encoding.ASCII;
            p2.IsNullable     = true;

            var collection = new ClickHouseParameterCollection {
                p2
            };

            var p3 = p2.Clone();

            Assert.NotSame(p2, p3);
            Assert.Null(p3.Collection);
            Assert.Same(collection, p2.Collection);

            AssertParametersEqual(p2, p3);
            Assert.Equal(TimeZoneInfo.Local, p3.TimeZone);
            Assert.Equal(35, p3.Size);
            Assert.Equal(3, p3.ArrayRank);
            Assert.Equal(Encoding.ASCII, p3.StringEncoding);
            Assert.True(p3.IsNullable);

            p2.SourceColumn            = "some value";
            p2.SourceColumnNullMapping = true;

            var p4 = p2.Clone();

            Assert.NotSame(p2, p4);
            Assert.Null(p4.Collection);

            AssertParametersEqual(p2, p4);
            Assert.Equal("some value", p4.SourceColumn);
            Assert.True(p2.SourceColumnNullMapping);
        }
Beispiel #10
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     if (parameter.DbType == DbType.Guid
         )
     {
         Data = new[] { (Guid)Convert.ChangeType(parameter.Value, typeof(Guid)) };
     }
     else
     {
         throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to Guid.");
     }
 }
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     if (parameter.DbType == DbType.Date || parameter.DbType == DbType.DateTime || parameter.DbType == DbType.DateTime2 || parameter.DbType == DbType.DateTimeOffset)
     {
         Data = new[] { (DateTime)Convert.ChangeType(parameter.Value, typeof(DateTime)) }
     }
     ;
     else
     {
         throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to DateTime.");
     }
 }
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            if (
#if NETCOREAPP11
                parameter.DbType == DbType.Integral || parameter.DbType == DbType.Float
#else
                parameter.DbType == DbType.Int16 || parameter.DbType == DbType.Int32 || parameter.DbType == DbType.Int64 ||
                parameter.DbType == DbType.UInt16 || parameter.DbType == DbType.UInt32 || parameter.DbType == DbType.UInt64 ||
                parameter.DbType == DbType.Single || parameter.DbType == DbType.Decimal || parameter.DbType == DbType.Decimal
#endif
                )
            {
                Data = new[] { (T)Convert.ChangeType(parameter.Value, typeof(T)) };
            }
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            switch (parameter.DbType)
            {
            case DbType.Date:
            case DbType.DateTime:
            case DbType.DateTime2:
            case DbType.DateTimeOffset:
                Data = new[] { (DateTime)Convert.ChangeType(parameter.Value, typeof(DateTime)) };
                break;

            default:
                throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to Date.");
            }
        }
Beispiel #14
0
        public void AsSubstitudeDate()
        {
            //Arrange
            ClickHouseParameter parameter = new ClickHouseParameter()
            {
                DbType = System.Data.DbType.Date,
                Value  = new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc)
            };

            //Act
            var res = parameter.AsSubstitute();

            //Assert
            Assert.AreEqual("toDate('2020-01-01', 'UTC')", res);
        }
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            if (parameter.DbType == 0
#if !NETCOREAPP11
                || parameter.DbType == System.Data.DbType.Object
#endif
                )
            {
                ValuesFromConst(new[] { parameter.Value as IEnumerable });
            }
            else
            {
                throw new NotSupportedException();
            }
        }
Beispiel #16
0
        public void NotSupportedProperties()
        {
            var p = new ClickHouseParameter();

            Assert.Throws <NotSupportedException>(() => p.Direction = ParameterDirection.InputOutput);
            Assert.Throws <NotSupportedException>(() => p.Direction = ParameterDirection.Output);
            Assert.Throws <NotSupportedException>(() => p.Direction = ParameterDirection.ReturnValue);

            // Inherited behaviour
            p.SourceVersion = DataRowVersion.Current;
            Assert.Equal(DataRowVersion.Default, p.SourceVersion);

            p.SourceVersion = DataRowVersion.Original;
            Assert.Equal(DataRowVersion.Default, p.SourceVersion);

            p.SourceVersion = DataRowVersion.Proposed;
            Assert.Equal(DataRowVersion.Default, p.SourceVersion);
        }
Beispiel #17
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     if (parameter.DbType == DbType.String || parameter.DbType == DbType.StringFixedLength || parameter.DbType == DbType.AnsiString || parameter.DbType == DbType.AnsiStringFixedLength)
     {
         Data = new[] { Values.First(x => x.Item1 == parameter.Value?.ToString()).Item2 }
     }
     ;
     else if (parameter.DbType == DbType.Int16 || parameter.DbType == DbType.Int32 || parameter.DbType == DbType.Int64 || parameter.DbType == DbType.UInt16 ||
              parameter.DbType == DbType.UInt32 || parameter.DbType == DbType.UInt64)
     {
         Data = new[] { (int)Convert.ChangeType(parameter.Value, typeof(int)) }
     }
     ;
     else
     {
         throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to Enum.");
     }
 }
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            switch (parameter.DbType)
            {
            case DbType.String:
            case DbType.AnsiString:
                Data = new[] { IPAddress.Parse(parameter.Value.ToString()) };
                break;

            case DbType.UInt32:
                byte[] rawIPAddress = BitConverter.GetBytes(Convert.ToUInt32(parameter.Value)).Reverse().ToArray();
                Data = new[] { new IPAddress(rawIPAddress) };
                break;

            default:
                throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to {typeof(IPAddress).Name}.");
            }
        }
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            switch (parameter.DbType)
            {
            case DbType.Int16:
            case DbType.Int32:
            case DbType.Int64:
            case DbType.UInt16:
            case DbType.UInt32:
            case DbType.UInt64:
            case DbType.Single:
            case DbType.Decimal:
            case DbType.Double:
                Data = new[] { (T)Convert.ChangeType(parameter.Value, typeof(T)) };
                break;

            default:
                throw new
                      InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to {typeof(T).Name}.");
            }
        }
        public void AddParameter()
        {
            var parameterNames = new[] { "abc", "{abc}", "@abc" };

            foreach (var name in parameterNames)
            {
                var collection = new ClickHouseParameterCollection();
                var parameter  = new ClickHouseParameter(name);
                collection.Add(parameter);

                Assert.Single(collection);
                Assert.Same(parameter, collection[0]);

                foreach (var altName in parameterNames)
                {
                    Assert.True(collection.Contains(altName));
                    Assert.True(collection.TryGetValue(altName, out var collectionParameter));
                    Assert.Same(parameter, collectionParameter);
                }
            }
        }
Beispiel #21
0
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            if (parameter.DbType == DbType.String
#if !NETCOREAPP11
                || parameter.DbType == DbType.StringFixedLength || parameter.DbType == DbType.AnsiString || parameter.DbType == DbType.AnsiStringFixedLength
#endif
                )
            {
                Data = new[] { Values.First(x => x.Item1 == parameter.Value?.ToString()).Item2 }
            }
            ;
            else if (
#if NETCOREAPP11
                parameter.DbType == DbType.Integral
#else
                parameter.DbType == DbType.Int16 || parameter.DbType == DbType.Int32 || parameter.DbType == DbType.Int64 ||
                parameter.DbType == DbType.UInt16 || parameter.DbType == DbType.UInt32 || parameter.DbType == DbType.UInt64
#endif
                )
            {
                Data = new[] { (int)Convert.ChangeType(parameter.Value, typeof(int)) }
            }
            ;
Beispiel #22
0
        public override void ValueFromParam(ClickHouseParameter parameter)
        {
            switch (parameter.DbType)
            {
            case DbType.String:
            case DbType.StringFixedLength:
            case DbType.AnsiString:
            case DbType.AnsiStringFixedLength:
                Data = new[] { Values.First(x => x.Item1 == parameter.Value?.ToString()).Item2 };
                break;

            case DbType.Int16:
            case DbType.Int32:
            case DbType.Int64:
            case DbType.UInt16:
            case DbType.UInt32:
            case DbType.UInt64:
                Data = new[] { (int)Convert.ChangeType(parameter.Value, typeof(int)) };
                break;

            default:
                throw new InvalidCastException($"Cannot convert parameter with type {parameter.DbType} to Enum.");
            }
        }
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     Data = new[] { parameter.Value?.ToString() };
 }
Beispiel #24
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
 }
        public async Task InsertFromSelect()
        {
            if (KeepConnection)
            {
                await using var connection = new ClickHouseConnection(_connectionSettings);
                await connection.OpenAsync();

                for (int i = 0; i < Rows; i++)
                {
                    await using var cmd = connection.CreateCommand("INSERT INTO ColumnWriterInsertBenchmarks(id, str, dt, val) SELECT {id:UUID}, {str}, {dt}, {val}");

                    var idParam = new ClickHouseParameter("id")
                    {
                        Value = _id[i].ToString(), ClickHouseDbType = ClickHouseDbType.String
                    };
                    cmd.Parameters.Add(idParam);

                    var strParam = new ClickHouseParameter("str")
                    {
                        Value = _str[i], ClickHouseDbType = ClickHouseDbType.String
                    };
                    cmd.Parameters.Add(strParam);

                    var dtParam = new ClickHouseParameter("dt")
                    {
                        Value = _dt[i], ClickHouseDbType = ClickHouseDbType.DateTime
                    };
                    cmd.Parameters.Add(dtParam);

                    var valParam = new ClickHouseParameter("val")
                    {
                        Value = _val[i], ClickHouseDbType = ClickHouseDbType.Decimal
                    };
                    cmd.Parameters.Add(valParam);

                    await cmd.ExecuteNonQueryAsync();
                }
            }
            else
            {
                for (int i = 0; i < Rows; i++)
                {
                    await using var connection = new ClickHouseConnection(_connectionSettings);
                    await connection.OpenAsync();

                    await using var cmd = connection.CreateCommand("INSERT INTO ColumnWriterInsertBenchmarks(id, str, dt, val) SELECT {id:UUID}, {str}, {dt}, {val}");

                    var idParam = new ClickHouseParameter("id")
                    {
                        Value = _id[i].ToString(), ClickHouseDbType = ClickHouseDbType.String
                    };
                    cmd.Parameters.Add(idParam);

                    var strParam = new ClickHouseParameter("str")
                    {
                        Value = _str[i], ClickHouseDbType = ClickHouseDbType.String
                    };
                    cmd.Parameters.Add(strParam);

                    var dtParam = new ClickHouseParameter("dt")
                    {
                        Value = _dt[i], ClickHouseDbType = ClickHouseDbType.DateTime
                    };
                    cmd.Parameters.Add(dtParam);

                    var valParam = new ClickHouseParameter("val")
                    {
                        Value = _val[i], ClickHouseDbType = ClickHouseDbType.Decimal
                    };
                    cmd.Parameters.Add(valParam);

                    await cmd.ExecuteNonQueryAsync();

                    if (!KeepConnection)
                    {
                        await cmd.DisposeAsync();

                        await connection.DisposeAsync();
                    }
                }
            }
        }
Beispiel #26
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     throw new NotImplementedException();
 }
Beispiel #27
0
 public abstract void ValueFromParam(ClickHouseParameter parameter);
Beispiel #28
0
 public override void ValueFromParam(ClickHouseParameter parameter)
 {
     Nulls = new[] { parameter.Value == null };
     InnerType.ValueFromParam(parameter);
 }
 public override void ValueFromParam(ClickHouseParameter parameter) => Data = new[] { (decimal)Convert.ChangeType(parameter.Value, typeof(decimal)) };