Пример #1
0
        public void Close()
        {
            try
            {
                if (_currentBlock != null && !_exceptionThrown)
                {
                    _clickHouseConnection.Formatter.ReadResponse();
                }
            }
            catch (ClickHouseException)
            {
                _exceptionThrown = true;
                _clickHouseConnection.MakeBroken();
            }
            catch (IOException)
            {
                _exceptionThrown = true;
                _clickHouseConnection.MakeBroken();
            }
#if !NETCOREAPP11
            if ((_behavior & CommandBehavior.CloseConnection) != 0 || _exceptionThrown)
            {
                _clickHouseConnection.Close();
            }
#endif

            _clickHouseConnection = null;
        }
        internal ClickHouseDataReader(ClickHouseConnection clickHouseConnection
#if !NETCOREAPP11
                                      , CommandBehavior behavior
#endif
                                      )
        {
            _clickHouseConnection = clickHouseConnection;
#if !NETCOREAPP11
            _behavior = behavior;
#endif
            NextResult();
        }
Пример #3
0
 public void Close()
 {
     if (_currentBlock != null)
     {
         _clickHouseConnection.Formatter.ReadResponse();
     }
     if (_clickHouseConnection != null && (_behavior & CommandBehavior.CloseConnection) != 0)
     {
         _clickHouseConnection.Close();
     }
     _clickHouseConnection = null;
 }
        public void Close()
        {
            if (_currentBlock != null)
            {
                _clickHouseConnection.Formatter.ReadResponse();
            }
#if !NETCOREAPP11
            if ((_behavior & CommandBehavior.CloseConnection) != 0)
            {
                _clickHouseConnection.Close();
            }
#endif
            _clickHouseConnection = null;
        }
Пример #5
0
        private void Execute(bool readResponse, ClickHouseConnection connection)
        {
            if (connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException("Connection isn't open");
            }

            var insertParser = new Impl.ATG.Insert.Parser(new Impl.ATG.Insert.Scanner(new MemoryStream(Encoding.UTF8.GetBytes(CommandText))));

            insertParser.errors.errorStream = new StringWriter();
            insertParser.Parse();

            if (insertParser.errors.count == 0)
            {
                var xText = new StringBuilder("INSERT INTO ");
                xText.Append(insertParser.tableName);
                if (insertParser.fieldList != null)
                {
                    xText.Append("(");
                    insertParser.fieldList.Aggregate(xText, (builder, fld) => builder.Append(fld).Append(','));
                    xText.Remove(xText.Length - 1, 1);
                    xText.Append(")");
                }
                xText.Append(" VALUES");

                connection.Formatter.RunQuery(xText.ToString(), QueryProcessingStage.Complete, null, null, null, false);
                var schema = connection.Formatter.ReadSchema();
                if (insertParser.oneParam != null)
                {
                    var table    = ((IEnumerable)Parameters[insertParser.oneParam].Value).OfType <IEnumerable>();
                    var colCount = table.First().Cast <object>().Count();
                    if (colCount != schema.Columns.Count)
                    {
                        throw new FormatException($"Column count in parameter table ({colCount}) doesn't match column count in schema ({schema.Columns.Count}).");
                    }
                    var cl = new List <List <object> >(colCount);
                    for (var i = 0; i < colCount; i++)
                    {
                        cl.Add(new List <object>());
                    }
                    var index = 0;
                    cl = table.Aggregate(cl, (colList, row) =>
                    {
                        index = 0;
                        foreach (var cval in row)
                        {
                            colList[index++].Add(cval);
                        }
                        return(colList);
                    });
                    index = 0;
                    foreach (var col in schema.Columns)
                    {
                        col.Type.ValuesFromConst(cl[index++]);
                    }
                }
                else
                {
                    if (schema.Columns.Count != insertParser.valueList.Count())
                    {
                        throw new FormatException($"Value count mismatch. Server expected {schema.Columns.Count} and query contains {insertParser.valueList.Count()}.");
                    }

                    var valueList = insertParser.valueList as List <Parser.ValueType> ?? insertParser.valueList.ToList();
                    for (var i = 0; i < valueList.Count; i++)
                    {
                        var val = valueList[i];
                        if (val.TypeHint == Parser.ConstType.Parameter)
                        {
                            schema.Columns[i].Type.ValueFromParam(Parameters[val.StringValue]);
                        }
                        else
                        {
                            schema.Columns[i].Type.ValueFromConst(val);
                        }
                    }
                }
                connection.Formatter.SendBlocks(new[] { schema });
            }
            else
            {
                connection.Formatter.RunQuery(SubstituteParameters(CommandText), QueryProcessingStage.Complete, null, null, null, false);
            }
            if (!readResponse)
            {
                return;
            }
            connection.Formatter.ReadResponse();
        }
Пример #6
0
 public ClickHouseCommand(ClickHouseConnection clickHouseConnection, string text) : this(clickHouseConnection)
 {
     CommandText = text;
 }
Пример #7
0
 public ClickHouseCommand(ClickHouseConnection clickHouseConnection)
 {
     _clickHouseConnection = clickHouseConnection;
 }
Пример #8
0
 internal ClickHouseDataReader(ClickHouseConnection clickHouseConnection, CommandBehavior behavior)
 {
     _clickHouseConnection = clickHouseConnection;
     _behavior             = behavior;
     NextResult();
 }
Пример #9
0
 public ClickHouseDbCommand(ClickHouseConnection clickHouseConnection)
 {
     _clickHouseConnection = clickHouseConnection;
     _parameters           = new ClickHouseParameterCollection();
 }