Esempio n. 1
0
        public AdvancedFilter(string filePath, string tablename, string uniqueColumnName, IEnumerable <long> uniqueRowIDs = null)
        {
            InitializeComponent();
            _filePath            = filePath;
            TableName            = tablename;
            SelectedKeys         = uniqueRowIDs;
            _selectionColumnName = uniqueColumnName;

            //handle events from the various specialized components
            TestWindow.ErrorsFound       += ErrorsFound;
            TestWindow.ParseSuccess      += DisplayResult;
            AvailableFunctions.TextToAdd += TextToInsert;
            //assign the proper grid alignment to the textblock that is behaving like a tooltip.
            Grid.SetRow(TestWindow.TextBlock, 3);
            MainGrid.Children.Add(TestWindow.TextBlock);
            //Add the names of the columns to populate the available columns array. (this is currently handled
            Database.Reader.SqLiteReader reader = new Database.Reader.SqLiteReader(filePath, tablename);
            _headers = reader.ColumnNames().ToList();
            TestWindow.SetHeaders = _headers;
            //Add the types of the columns to enforce type strictness.
            List <Type> headertypes = reader.ColumnTypes().ToList();

            TestWindow.SetHeaderTypes = headertypes;
            //Add the first row of data to the database as an object array so that the result will show the result for the first row.
            reader.Open();
            _FirstRow = reader.Row(0);
            if (_selectedKeys != null)
            {
                if (_selectedKeys.Count() > 0)
                {
                    AllowSelectionOption = true;
                    object[] tmp = reader.Column(_selectionColumnName);
                    _FirstSelectedRow = reader.Row(Array.IndexOf(tmp, _selectedKeys.First()));
                }
            }
            _totalRows = reader.RowCount();
            NotifyPropertyChanged(nameof(SelectedCount));
            reader.Close();
            TestWindow.SetDataForFirstRow = _FirstRow;
            //To create a selection, set the output type to boolean...
            TestWindow.SetOutputType = FieldCalculationParser.TypeEnum.Bool;
            ColumnType = "True or False";
            //Add the headers to the avaialblefields treeview. (i am adding type as a tooltip for reference)
            for (int i = 0; i < _headers.Count(); i++)
            {
                System.Windows.Controls.TreeViewItem tvi = new TreeViewItem();
                tvi.Header = _headers[i];
                System.Windows.Controls.ToolTip t = new System.Windows.Controls.ToolTip();
                t.Content   = headertypes[i].Name;
                tvi.ToolTip = t;
                AvailableFields.Items.Add(tvi);
            }
        }
        private List <PreviewRowItem> getPreviewRowItems()
        {
            List <PreviewRowItem> previewRowItems = new List <PreviewRowItem>();

            //this is where you would get the parse tree from the expressionwindow, and then execute the tree for each row.
            FieldCalculationParser.ParseTreeNode tree = TestWindow.GetTree;
            FieldCalculationParser.ParseTreeNode.Initialize();//clear all errors.

            if (tree != null)
            {
                Database.Reader.SqLiteReader reader = new Database.Reader.SqLiteReader(_filePath, _tableName);
                object[]      row;
                List <string> usedheaders   = tree.GetHeaderNames();
                string[]      uniqueheaders = getUniqueHeaders(usedheaders);
                tree.SetColNums(uniqueheaders.ToList());
                Int64      count        = reader.RowCount();
                List <int> selectedRows = null;

                List <object> output = new List <object>();

                reader.Open();
                object[] original    = reader.Column(_columnName);
                object[] primaryKeys = reader.Column(_selectionColumnName);
                //needs to operate on selected rows or not.
                //ifselected rows, update the entire column, but put the original value in for non selected rows.
                if (UseSelection)
                {
                    selectedRows = new List <int>();
                    object[] tmp = reader.Column(_selectionColumnName);
                    foreach (long uid in _selectedKeys)
                    {
                        selectedRows.Add(Array.IndexOf(tmp, uid));
                    }
                }

                for (int i = 0; i < count; i++)
                {
                    if (!UseSelection || selectedRows.Contains(i))
                    {
                        FieldCalculationParser.ParseTreeNode.RowOrCellNum = (int)i;//whatever.
                        if (uniqueheaders.Count() > 0)
                        {
                            row = reader.Row((int)i, uniqueheaders);
                            tree.Update(ref row);
                        }
                        output.Add(tree.Evaluate().GetResult);
                    }
                    else
                    {
                        output.Add(original[i]);
                    }

                    previewRowItems.Add(new PreviewRowItem(primaryKeys[i], original[i], output[i]));
                }

                reader.Close();
            }

            if (tree.GetComputeErrors.Count > 1)
            {
                //do not write to file.
                StringBuilder s = new StringBuilder();
                foreach (string o in tree.GetComputeErrors)
                {
                    s.AppendLine(o);
                }
                ErrorWindow err = new ErrorWindow(s.ToString());
                err.Title = "Output";
                err.ShowDialog();
            }

            return(previewRowItems);
        }
Esempio n. 3
0
        private void CmdExecute_Click(object sender, RoutedEventArgs e)
        {
            //this is where you would get the parse tree from the expressionwindow, and then execute the tree for each row.
            FieldCalculationParser.ParseTreeNode tree = TestWindow.GetTree;
            FieldCalculationParser.ParseTreeNode.Initialize();//clear all errors.

            if (tree != null)
            {
                Database.Reader.SqLiteReader reader = new Database.Reader.SqLiteReader(_filePath, _tableName);
                object[]      row;
                List <string> usedheaders   = tree.GetHeaderNames();         //will include duplicates
                string[]      uniqueheaders = getUniqueHeaders(usedheaders); //removes duplicates
                tree.SetColNums(uniqueheaders.ToList());                     //lets the tree know what index in the array of object to look for for each header.
                Int64 count = reader.RowCount();

                List <long> output = new List <long>();

                reader.Open();
                //needs to operate on selected rows or not.

                //ifselected rows, only iterate on the current selection.
                if (UseSelection)
                {
                    object[] tmp = reader.Column(_selectionColumnName);

                    List <int> items = new List <int>();
                    int        idx   = 0;
                    foreach (long uid in _selectedKeys)
                    {
                        idx = Array.IndexOf(tmp, uid);
                        FieldCalculationParser.ParseTreeNode.RowOrCellNum = idx;
                        if (uniqueheaders.Count() > 0)
                        {
                            row = reader.Row(idx, uniqueheaders);
                            tree.Update(ref row);
                        }
                        if ((bool)tree.Evaluate().GetResult)
                        {
                            //select it!
                            output.Add(uid);
                        }
                    }
                }
                else
                {
                    object[] uniqueIDS = reader.Column(_selectionColumnName);
                    for (Int64 i = 0; i < count; i++)
                    {
                        FieldCalculationParser.ParseTreeNode.RowOrCellNum = (int)i;//whatever.
                        if (uniqueheaders.Count() > 0)
                        {
                            row = reader.Row((int)i, uniqueheaders);
                            tree.Update(ref row);
                        }
                        if ((bool)tree.Evaluate().GetResult)
                        {
                            //select it!
                            output.Add((long)uniqueIDS[i]);
                        }
                    }
                }

                reader.Close();
                if (tree.GetComputeErrors.Count > 1)
                {
                    //do not update selection
                    StringBuilder s = new StringBuilder();
                    foreach (string o in tree.GetComputeErrors)
                    {
                        s.AppendLine(o);
                    }
                    ErrorWindow err = new ErrorWindow(s.ToString());
                    err.Title = "Output";
                    err.ShowDialog();
                }
                else
                {
                    //update _selectedKeys;
                    SelectedKeys = output;
                    //raise event?
                    FilterWasSuccessful = true;
                    Close();
                }
            }
        }