} // getConverters()

        // @Override
        public DataSet intercept(DataSet dataSet)
        {
            Dictionary<Column, TypeConverter<object, object>> converters = getConverters(dataSet);
            if (converters.IsEmpty())
            {
                return dataSet;
            }

            bool             hasConverter = false;
            List<SelectItem> selectItems  = NArrays.AsList<SelectItem>(dataSet.getSelectItems());
            TypeConverter<object, object>[] converterArray = new TypeConverter<object, object>[selectItems.Count];
            for (int i = 0; i < selectItems.Count; i++)
            {
                SelectItem selectItem = selectItems[i];
                Column     column     = selectItem.getColumn();
                if (column != null && selectItem.getAggregateFunction() == null)
                {
                    TypeConverter<object, object> converter = converters[column];
                    if (converter != null)
                    {
                        hasConverter = true;
                        converterArray[i] = converter;
                    }
                }
            }

            if (! hasConverter)
            {
                return dataSet;
            }

            return new ConvertedDataSet(dataSet, converterArray);
        } // intercept()
 public ScalarFunctionDataSet(List <SelectItem> scalarFunctionSelectItemsToEvaluate, DataSet dataSet) :
     base(CollectionUtils.concat(false, scalarFunctionSelectItemsToEvaluate,
                                 NArrays.AsList(dataSet.getSelectItems())))
 {
     _scalarFunctionSelectItemsToEvaluate = scalarFunctionSelectItemsToEvaluate;
     _dataSet = dataSet;
 } // contructor
Example #3
0
        // @Override
        public String toString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Relationship[");
            sb.Append("primaryTable=" + getPrimaryTable().getName());
            List <Column> columns = NArrays.AsList <Column>(getPrimaryColumns());

            sb.Append(",primaryColumns=[");
            for (int i = 0; i < columns.Count; i++)
            {
                if (i != 0)
                {
                    sb.Append(", ");
                }
                sb.Append(columns[i].getName());
            }
            sb.Append("]");
            sb.Append(",foreignTable=" + getForeignTable().getName());
            columns = NArrays.AsList <Column>(getForeignColumns());
            sb.Append(",foreignColumns=[");
            for (int i = 0; i < columns.Count; i++)
            {
                if (i != 0)
                {
                    sb.Append(", ");
                }
                sb.Append(columns[i].getName());
            }
            sb.Append("]");
            sb.Append("]");
            return(sb.ToString());
        }
        public static DataSet getSelection(List <SelectItem> selectItems, DataSet dataSet)
        {
            List <SelectItem> dataSetSelectItems = NArrays.AsList(dataSet.getSelectItems());

            // check if the selection is already the same
            if (selectItems.Equals(dataSetSelectItems))
            {
                // return the DataSet unmodified
                return(dataSet);
            }

            List <SelectItem> scalarFunctionSelectItemsToEvaluate = new List <SelectItem>();

            foreach (SelectItem selectItem in selectItems)
            {
                if (selectItem.getScalarFunction() != null)
                {
                    if (!dataSetSelectItems.Contains(selectItem) && dataSetSelectItems.Contains(selectItem.replaceFunction(
                                                                                                    null)))
                    {
                        scalarFunctionSelectItemsToEvaluate.Add(selectItem);
                    }
                }
            }

            if (scalarFunctionSelectItemsToEvaluate.IsEmpty())
            {
                return(new SubSelectionDataSet(selectItems, dataSet));
            }

            ScalarFunctionDataSet scalaFunctionDataSet = new ScalarFunctionDataSet(
                scalarFunctionSelectItemsToEvaluate, dataSet);

            return(new SubSelectionDataSet(selectItems, scalaFunctionDataSet));
        }     // getSelection()
 public static DataSet getDistinct(DataSet dataSet)
 {
     SelectItem[]  selectItems  = dataSet.getSelectItems();
     GroupByItem[] groupByItems = new GroupByItem[selectItems.Length];
     for (int i = 0; i < groupByItems.Length; i++)
     {
         groupByItems[i] = new GroupByItem(selectItems[i]);
     }
     return(getGrouped(NArrays.AsList(selectItems), dataSet, groupByItems));
 }     // getDistinct()
        } // constructor

        /**
         * Constructor appropriate for dataset implementations that wrap other
         * datasets, such as the {@link MaxRowsDataSet}, {@link FilteredDataSet} and
         * more.
         *
         * @param dataSet
         */
        public AbstractDataSet(DataSet dataSet)
        {
            if (dataSet is AbstractDataSet)
            {
                _header = ((AbstractDataSet)dataSet).getHeader();
            }
            else
            {
                _header = new CachingDataSetHeader(NArrays.AsList <SelectItem>(dataSet.getSelectItems()));
            }
        } // constructor
        } // getRelationshipCount()

        // @Override
        public IList <Relationship> getRelationships()
        {
            Table[]             tables = getTables(false);
            List <Relationship> result = new List <Relationship>();

            foreach (Table t in tables)
            {
                Relationship[] relations = t.getRelationships();
                result.AddRange(NArrays.AsList <Relationship>(relations));
            }
            //return getTables().stream()
            //        .flatMap(tab->tab.getRelationships().stream())
            //        .collect(new HashSet<Relationship>());
            return(result);
        } // getRelationships()
Example #8
0
 // @Override
 public bool containsColumnPair(Column pkColumn, Column fkColumn)
 {
     if (pkColumn != null && fkColumn != null)
     {
         List <Column> primaryColumns = NArrays.AsList <Column>(getPrimaryColumns());
         List <Column> foreignColumns = NArrays.AsList <Column>(getForeignColumns());
         for (int i = 0; i < primaryColumns.Count; i++)
         {
             if (pkColumn.Equals(primaryColumns[i]) &&
                 fkColumn.Equals(foreignColumns[i]))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        }     // getCarthesianProduct()

        #endregion getCarthesianProduct()

        /**
         * Executes a simple nested loop join. The innerLoopDs will be copied in an
         * in-memory dataset.
         *
         */
        public static InMemoryDataSet nestedLoopJoin(DataSet innerLoopDs, DataSet outerLoopDs, IEnumerable <FilterItem> filtersIterable)
        {
            List <FilterItem> filters = new List <FilterItem>();

            foreach (FilterItem fi in filtersIterable)
            {
                filters.Add(fi);
            }
            List <Row> innerRows = innerLoopDs.toRows();

            List <SelectItem> allItems = new List <SelectItem>(NArrays.AsList <SelectItem>(outerLoopDs.getSelectItems()));

            allItems.AddRange(NArrays.AsList(innerLoopDs.getSelectItems()));

            HashSet <FilterItem> applicable_filters = applicableFilters(filters, allItems);

            DataSetHeader jointHeader = new CachingDataSetHeader(allItems);

            List <Row> resultRows = new List <Row>();

            foreach (Row outerRow in outerLoopDs)
            {
                foreach (Row innerRow in innerRows)
                {
                    Object[] joinedRowObjects = new Object[outerRow.getValues().Length + innerRow.getValues().Length];

                    Array.Copy(outerRow.getValues(), 0, joinedRowObjects, 0, outerRow.getValues().Length);
                    Array.Copy(innerRow.getValues(), 0, joinedRowObjects, outerRow.getValues().Length,
                               innerRow.getValues().Length);

                    Row joinedRow = new DefaultRow(jointHeader, joinedRowObjects);
                    IEnumerable <FilterItem> selected_items = applicable_filters.Where(fi => isJoinedRowAccepted(fi, joinedRow));

                    if (applicable_filters.IsEmpty() || (selected_items != null && selected_items.Count <FilterItem>() != 0))
                    {
                        resultRows.Add(joinedRow);
                    }
                }
            }

            return(new InMemoryDataSet(jointHeader, resultRows));
        }     // nestedLoopJoin()
        }     // getCarthesianProduct()

        public static DataSet getCarthesianProduct(DataSet[] fromDataSets, IEnumerable <FilterItem> whereItems)
        {
            Debug.Assert(fromDataSets.Length > 0);
            // First check if carthesian product is even nescesary
            if (fromDataSets.Length == 1)
            {
                return(getFiltered(fromDataSets[0], whereItems));
            }
            // do a nested loop join, no matter what
            IEnumerator <DataSet> dsIter = NArrays.AsList(fromDataSets).GetEnumerator();

            DataSet joined = dsIter.Current;

            while (dsIter.MoveNext())
            {
                joined = nestedLoopJoin(dsIter.Current, joined, (whereItems));
            }

            return(joined);
        }     // getCarthesianProduct()
Example #11
0
        } // constructor

        /**
         * Constructs a row from an array of SelectItems and an array of
         * corresponding values
         *
         * @param items
         *            the array of SelectItems
         * @param values
         *            the array of values
         * @param styles
         *            an optional array of styles
         * @deprecated use {@link #DefaultRow(DataSetHeader, Object[])} or
         *             {@link #DefaultRow(DataSetHeader, Object[], Style[])}
         *             instead.
         */
        // @Deprecated
        public DefaultRow(SelectItem[] items, Object[] values, Style[] styles) : this(NArrays.AsList(items), values, styles)
        {
        } // constructor
        } // filter()

        public static List <O> map <I, SuperI, O> (I[] items, NFunc <SuperI, O> func) where I : SuperI
        {
            return(map(NArrays.AsList(items), func));
        } // map()
        } // executePrimaryKeyLookupQuery()

        protected DataSet materializeFromItem(FromItem fromItem, List <SelectItem> selectItems)
        {
            DataSet  dataSet;
            JoinType joinType = fromItem.getJoin();

            if (fromItem.getTable() != null)
            {
                // We need to materialize a single table
                Table             table = fromItem.getTable();
                List <SelectItem> selectItemsToMaterialize = new List <SelectItem>();

                foreach (SelectItem selectItem in selectItems)
                {
                    FromItem selectedFromItem = selectItem.getFromItem();
                    if (selectedFromItem != null)
                    {
                        if (selectedFromItem.equals(fromItem))
                        {
                            selectItemsToMaterialize.Add(selectItem.replaceFunction(null));
                        }
                    }
                    else
                    {
                        // the select item does not specify a specific
                        // from-item
                        Column selectedColumn = selectItem.getColumn();
                        if (selectedColumn != null)
                        {
                            // we assume that if the table matches, we will use the
                            // column
                            if (selectedColumn.getTable() != null && selectedColumn.getTable().Equals(table))
                            {
                                selectItemsToMaterialize.Add(selectItem.replaceFunction(null));
                            }
                        }
                    }
                }

                if (logger.isDebugEnabled())
                {
                    logger.debug("calling materializeTable(" + table.getName() + "," + selectItemsToMaterialize + ",1,-1");
                }

                // Dispatching to the concrete subclass of
                // QueryPostprocessDataContextStrategy
                dataSet = materializeTable(table, selectItemsToMaterialize, 1, -1);
            }
            else if (joinType != JoinType.None)
            {
                // We need to (recursively) materialize a joined FromItem
                if (fromItem.getLeftSide() == null || fromItem.getRightSide() == null)
                {
                    throw new ArgumentException("Joined FromItem requires both left and right side: " + fromItem);
                }
                DataSet[] fromItemDataSets = new DataSet[2];

                // materialize left side
                List <SelectItem> leftOn = NArrays.AsList(fromItem.getLeftOn());
                fromItemDataSets[0] = materializeFromItem(fromItem.getLeftSide(),
                                                          CollectionUtils.concat(true, selectItems, leftOn));

                // materialize right side
                List <SelectItem> rightOn = NArrays.AsList(fromItem.getRightOn());
                fromItemDataSets[1] = materializeFromItem(fromItem.getRightSide(),
                                                          CollectionUtils.concat(true, selectItems, rightOn));

                FilterItem[] onConditions = new FilterItem[leftOn.Count];
                for (int i = 0; i < onConditions.Length; i++)
                {
                    FilterItem whereItem = new FilterItem(leftOn[i], OperatorType.EQUALS_TO, rightOn[i]);
                    onConditions[i] = whereItem;
                }

                switch (joinType)
                {
                case JoinType.INNER:
                    dataSet = MetaModelHelper.getCarthesianProduct(fromItemDataSets, onConditions);
                    break;

                case JoinType.LEFT:
                    dataSet = MetaModelHelper.getLeftJoin(fromItemDataSets[0], fromItemDataSets[1], onConditions);
                    break;

                case JoinType.RIGHT:
                    dataSet = MetaModelHelper.getRightJoin(fromItemDataSets[0], fromItemDataSets[1], onConditions);
                    break;

                default:
                    throw new ArgumentException("FromItem type not supported: " + fromItem);
                }
            }
            else if (fromItem.getSubQuery() != null)
            {
                // We need to (recursively) materialize a subquery
                dataSet = executeQuery(fromItem.getSubQuery());
            }
            else
            {
                throw new ArgumentException("FromItem type not supported: " + fromItem);
            }
            if (dataSet == null)
            {
                throw new ArgumentException("FromItem was not succesfully materialized: " + fromItem);
            }
            return(dataSet);
        } // materializeFromItem()
        }                                                                          // getFiltered()

        public static DataSet getFiltered(DataSet dataSet, params FilterItem[] filterItems)
        {
            return(getFiltered(dataSet, NArrays.AsList(filterItems)));
        }
        }     // executeSingleRowQuery()

        /**
         * Creates a subset array of columns, where only columns that are contained
         * within the specified table are included.
         *
         * @param table
         * @param columns
         * @return an array containing the columns that exist in the table
         */
        public static Column[] getTableColumns(Table table, Column[] columns)
        {
            return(getTableColumns(table, NArrays.AsList(columns)));
        }     // getTableColumns()
        }     // getDistinct()

        public static Table[] getTables(Column[] columns)
        {
            return(getTables(NArrays.AsList <Column>(columns)));
        }     // getTables()
        } // constructor

        public AbstractDataSet(SelectItem[] selectItems) : this(NArrays.AsList <SelectItem>(selectItems))
        {
            Debug.WriteLine("new AbstractDataSet(SelectItem[])");
        } // constructor
        }     // getSelection()

        public static DataSet getSelection(SelectItem[] selectItems, DataSet dataSet)
        {
            return(getSelection(NArrays.AsList(selectItems), dataSet));
        }
Example #19
0
        } // constructor

        public SimpleDataSetHeader(SelectItem[] selectItems) : this(NArrays.AsList <SelectItem>(selectItems))
        {
        } // constructor
Example #20
0
 public InMemoryDataSet(params Row[] rows) : this(NArrays.AsList(rows))
 {
 } // constructor
 public static List <E> filter <E, SuperE> (E[] items, Predicate <SuperE> predicate) where E : SuperE
 {
     return(filter(NArrays.AsList(items), predicate));
 } // filter()
Example #22
0
        } // constructor

        public InMemoryDataSet(DataSetHeader header, params Row[] rows) :  base(header)
        {
            _rows = NArrays.AsList(rows);
        } // constructor
        } // map()

        // https://stackoverflow.com/questions/36449343/what-is-the-c-sharp-equivalent-of-java-8-java-util-function-consumer
        public static void forEach <E> (E[] items, NConsumer <E> action)
        {
            forEach(NArrays.AsList(items), action);
        } // forEach()
Example #24
0
        } // constructor

        public CachingDataSetHeader(SelectItem[] items) : this(NArrays.AsList(items))
        {
        } // constructor