Exemplo n.º 1
0
        /// <summary>
        ///   Executes the query async.
        /// </summary>
        /// <typeparam name="T"> class representing the rows returned </typeparam>
        /// <returns> </returns>
        public async Task <CqlDataReader <T> > ExecuteReaderAsync <T>() where T : class, new()
        {
            var logger = _connection.LoggerManager.GetLogger("CqlSharp.CqlCommand.ExecuteReader");

            logger.LogVerbose("Waiting on Throttle");

            //wait until allowed
            _connection.Throttle.Wait();

            try
            {
                //capture current command state
                QueryExecutionState state = CaptureState();

                logger.LogVerbose("State captured, start executing query");

                ResultFrame result = await RunWithRetry(ExecuteInternalAsync, state, logger).ConfigureAwait(false);

                if (result.ResultOpcode != ResultOpcode.Rows)
                {
                    var ex = new CqlException("Can not create a DataReader for non-select query.");
                    logger.LogError("Error executing reader: {0}", ex);
                    throw ex;
                }
                var reader = new CqlDataReader <T>(result);

                logger.LogQuery("Query {0} returned {1} results", _cql, reader.Count);

                return(reader);
            }
            finally
            {
                _connection.Throttle.Release();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Executes the non-query async.
        /// </summary>
        /// <returns> A ICqlQueryResult of type rows, Void, SchemaChange or SetKeySpace </returns>
        /// <exception cref="CqlException">Unexpected type of result received</exception>
        public async Task <ICqlQueryResult> ExecuteNonQueryAsync()
        {
            var logger = _connection.LoggerManager.GetLogger("CqlSharp.CqlCommand.ExecuteNonQuery");

            logger.LogVerbose("Waiting on Throttle");

            //wait until allowed
            _connection.Throttle.Wait();

            try
            {
                //capture current command state
                QueryExecutionState state = CaptureState();

                logger.LogVerbose("State captured, start executing query");

                ResultFrame result = await RunWithRetry(ExecuteInternalAsync, state, logger).ConfigureAwait(false);

                switch (result.ResultOpcode)
                {
                case ResultOpcode.Rows:
                    var reader = new CqlDataReader(result);
                    logger.LogQuery("Query {0} returned {1} results", _cql, reader.Count);
                    return(reader);

                case ResultOpcode.Void:
                    logger.LogQuery("Query {0} executed succesfully", _cql);
                    return(new CqlVoid {
                        TracingId = result.TracingId
                    });

                case ResultOpcode.SchemaChange:
                    logger.LogQuery("Query {0} resulted in {1}.{2} {3}", _cql, result.Keyspace, result.Table, result.Change);
                    return(new CqlSchemaChange
                    {
                        TracingId = result.TracingId,
                        Keyspace = result.Keyspace,
                        Table = result.Table,
                        Change = result.Change
                    });

                case ResultOpcode.SetKeyspace:
                    logger.LogQuery("Query {0} resulted in keyspace set to {1}", _cql, result.Keyspace);
                    return(new CqlSetKeyspace
                    {
                        TracingId = result.TracingId,
                        Keyspace = result.Keyspace
                    });

                default:
                    throw new CqlException("Unexpected type of result received");
                }
            }
            finally
            {
                _connection.Throttle.Release();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the query async on the given connection
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="state">The state.</param>
        /// <param name="logger">The logger.</param>
        /// <returns></returns>
        /// <exception cref="CqlException">Unexpected frame received</exception>
        private async Task <ResultFrame> ExecuteInternalAsync(Connection connection, QueryExecutionState state, Logger logger)
        {
            Frame query;

            if (_prepared)
            {
                ResultFrame prepareResult = await PrepareInternalAsync(connection, state, logger).ConfigureAwait(false);

                query = new ExecuteFrame(prepareResult.PreparedQueryId, _level, state.Values);
                logger.LogVerbose("Sending execute {0} using {1}", _cql, connection);
            }
            else
            {
                query = new QueryFrame(_cql, _level);
                logger.LogVerbose("Sending query {0} using {1}", _cql, connection);
            }

            //update frame with tracing option if requested
            if (state.TracingEnabled)
            {
                query.Flags |= FrameFlags.Tracing;
            }

            Frame response = await connection.SendRequestAsync(query, logger, state.Load).ConfigureAwait(false);

            var result = response as ResultFrame;

            if (result != null)
            {
                //read all the data into a buffer, if requested
                if (state.UseBuffering)
                {
                    logger.LogVerbose("Buffering used, reading all data");
                    await result.BufferDataAsync().ConfigureAwait(false);
                }

                return(result);
            }

            throw new CqlException("Unexpected frame received " + response.OpCode);
        }
Exemplo n.º 4
0
        public MainPage()
        {
            this.Visual = VisualMarker.Material;
            NavigationPage.SetHasNavigationBar(this, false);

            this.BindingContext = new BaseViewModel();

            this.PartemLogo = new Image
            {
                Source        = ImageSource.FromResource("Icon.png"),
                HeightRequest = 50,
                WidthRequest  = 50
            };

            this.TitleLabel = new PartemLabelBold
            {
                Text      = "partem.tech",
                TextColor = Color.White
            };

            this.ActivityIndicator = new SfBusyIndicator()
            {
                AnimationType   = AnimationTypes.Ball,
                IsBusy          = true,
                TextColor       = Color.White,
                WidthRequest    = 40,
                HeightRequest   = 40,
                VerticalOptions = LayoutOptions.Center
            };
            this.ActivityIndicator.SetBinding(SfBusyIndicator.IsBusyProperty, new Binding("IsBusy"));

            this.AnalyzeFrame = new AnalyzeFrame();
            this.ResultFrame  = new ResultFrame();

            this.RootLayout = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition {
                        Height = new GridLength(1, GridUnitType.Star)
                    }
                }
            };

            this.CopyrightLabel = new PartemLabelLight
            {
                Text              = "copyright © partem 2020",
                FontSize          = 15,
                TextColor         = Color.White,
                HorizontalOptions = LayoutOptions.Center
            };

            this.MainLayout = new StackLayout
            {
                Children =
                {
                    new StackLayout
                    {
                        Orientation = StackOrientation.Horizontal,
                        Children    =
                        {
                            this.PartemLogo,
                            this.TitleLabel
                        },
                        Margin            = new Thickness(0, 40, 0, 10),
                        HorizontalOptions = LayoutOptions.Center
                    },
                    this.ActivityIndicator,
                    this.AnalyzeFrame,
                    //this.ResultFrame,
                    this.CopyrightLabel
                }
            };

            this.ScrollView = new ScrollView
            {
                Content = this.MainLayout
            };

            this.BackgroundImage = new Image
            {
                Source = ImageSource.FromResource("Background.png"),
                Aspect = Aspect.AspectFill,
            };

            this.RootLayout.Children.Add(this.BackgroundImage, 0, 0);
            this.RootLayout.Children.Add(this.ScrollView, 0, 0);

            this.Content = this.RootLayout;

            this.AnalyzeFrame.AnalyzeButton.Clicked += AnalyzeButton_Clicked;
            this.BindingContextChanged += MainPage_BindingContextChanged;

            MessagingCenter.Subscribe <object, string>(this, "OpenInUrl", (object obj, string s) =>
            {
                Device.BeginInvokeOnMainThread(() => {
                    (this.AnalyzeFrame.URLTextBox.InputView as Entry).Text = s;
                    this.AnalyzeButton_Clicked(this, new EventArgs());
                });
            });
        }
Exemplo n.º 5
0
        private async void AnalyzeButton_Clicked(object sender, EventArgs e)
        {
            this.AnalyzeFrame.URLTextBox.HasError = false;

            if (Uri.IsWellFormedUriString((this.AnalyzeFrame.URLTextBox.InputView as Entry).Text, UriKind.Absolute))
            {
                (this.BindingContext as BaseViewModel).IsBusy = true;
                this.AnalyzeFrame.AnalyzeButton.IsEnabled     = false;

                if (this.MainLayout.Children.Contains(this.ResultFrame))
                {
                    this.MainLayout.Children.Remove(this.ResultFrame);
                }
                var request = new ServiceRequest
                {
                    Url = (this.AnalyzeFrame.URLTextBox.InputView as Entry).Text
                };

                await Task.Delay(2000);

                var response = DependencyService.Get <ApiAdapter>().MakeRequest(request).Result;
                if (!response.Success)
                {
                    await DisplayAlert("Error", response.Error, "OK");

                    (this.BindingContext as BaseViewModel).IsBusy = false;
                    await Task.Delay(5000);

                    this.AnalyzeFrame.AnalyzeButton.IsEnabled = true;

                    return;
                }

                this.ResultFrame = new ResultFrame();
                this.ResultFrame.BindingContext = new ResultFrameViewModel
                {
                    LeftPercentage   = response.LeftPercentage,
                    CenterPercentage = response.CenterPercentage,
                    RightPercentage  = response.RightPercentage,
                    Headline         = response.Headline,
                    Source           = response.Source,
                    ArticleImage     = response.Image
                };
                this.MainLayout.Children.Insert(3, this.ResultFrame);

                this.ResultFrame.FadeTo(0, 0, Easing.CubicOut);
                await this.ResultFrame.TranslateTo(0, 200, 0, Easing.CubicOut);

                this.ResultFrame.TranslateTo(0, 0, 1000, Easing.CubicOut);
                await this.ResultFrame.FadeTo(1, 1000, Easing.CubicOut);

                this.ResultFrame.Graph.Animate();

                (this.BindingContext as BaseViewModel).IsBusy = false;

                this.ScrollView.ScrollToAsync(0, this.ResultFrame.Bounds.Y - 50, true);


                await Task.Delay(5000);

                this.AnalyzeFrame.AnalyzeButton.IsEnabled = true;
            }
            else
            {
                this.AnalyzeFrame.URLTextBox.HasError = true;
            }
        }
Exemplo n.º 6
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="CqlDataReader" /> class.
 /// </summary>
 /// <param name="frame"> The frame. </param>
 internal CqlDataReader(ResultFrame frame)
 {
     _frame = frame;
 }