示例#1
0
        protected static Column createColumn(string name, View view, Column.DataType type = Column.DataType.Text)
        {
            Column column = new Column();

            column.name = name;
            column.Type = type;
            return(view.AddColumn(column));
        }
示例#2
0
 public RecordComparer(int index, Column.DataType type)
 {
     this.type  = type;
     this.index = index;
 }
示例#3
0
        /**
         * Query:
         * SELECT [*] [col_1, col_2]
         * FROM table_1
         * [WHERE] ((col_3 = val_3) AND (col_4 = col_5))
         * [ORDER BY] col_name [ASC|DESC]
         */
        private void SelectRecordsFromTable()
        {
            ParseTreeNode topNode = root.ChildNodes[0];

            //Selecting the table
            ParseTreeNode tableListNode = topNode
                                          .ChildNodes[4].ChildNodes[1];
            String tableName = tableListNode.ChildNodes[0].ChildNodes[0].Token.ValueString;


            //Selecting the columns
            ParseTreeNode colList = topNode.ChildNodes[2].ChildNodes[0];
            List <int>    selectedColumns;

            if (colList.ChildNodes.Count == 0)            //'*' clause
            {
                selectedColumns = subQueryHandler.GetColumnIndicesFromName(tableName, new List <string>());
            }
            else
            {
                List <String> colNames = new List <string>();
                foreach (var colNameNode in colList.ChildNodes)
                {
                    String colName = colNameNode.ChildNodes[0].ChildNodes[0].ChildNodes[0].Token.ValueString;
                    colNames.Add(colName);
                }
                selectedColumns = subQueryHandler.GetColumnIndicesFromName(tableName, colNames);
            }


            //Selecting the records
            Dictionary <int, Record> possibleRecords;

            if (topNode.ChildNodes[5].ChildNodes.Count > 1)
            {
                ParseTreeNode binExpr = topNode.ChildNodes[5].ChildNodes[1];
                possibleRecords = SolveWhereClause(tableName, binExpr);
            }
            else
            {
                possibleRecords = subQueryHandler.SelectRecordsFromTable(tableName, null);
            }


            //Getting the order by clause
            List <Record> orderedList = new List <Record>(possibleRecords.Values);

            if (topNode.ChildNodes[8].ChildNodes.Count > 1)
            {
                ParseTreeNode orderByNode    = topNode.ChildNodes[8].ChildNodes[2];
                String        orderByColName = orderByNode.ChildNodes[0]
                                               .ChildNodes[0].ChildNodes[0].Token.ValueString;

                List <String> orderByList = new List <string>(1);
                orderByList.Add(orderByColName);
                int             toSortIndex = subQueryHandler.GetColumnIndicesFromName(tableName, orderByList)[0];
                Column.DataType toSortType  = subQueryHandler.DescribeTable(tableName).Columns[toSortIndex].Type;

                String orderByType = "asc";
                if (orderByNode.ChildNodes[0].ChildNodes[1].ChildNodes.Count > 0)
                {
                    orderByType = orderByNode.ChildNodes[0].ChildNodes[1]
                                  .ChildNodes[0].Token.ValueString;
                }

                orderedList.Sort(new RecordComparer(toSortIndex, toSortType));
                if (orderByType == "desc")
                {
                    orderedList.Reverse();
                }
            }


            //Displaying the heading of selected records
            List <String> sortedSelectedCols = subQueryHandler.SortColumnsInOrder(tableName, selectedColumns);

            foreach (String colName in sortedSelectedCols)
            {
                _messenger.Message(colName + "|");
            }


            //Dispaly the records in proper format
            foreach (Record record in orderedList)
            {
                List <String> fields      = record.Fields;
                String        recAsString = "";
                foreach (int index in selectedColumns)
                {
                    recAsString += (fields[index] + " | ");
                }
                _messenger.Message(recAsString);
            }
        }