}     // 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()
        public DefaultTable(IEnumerable enumerable)
        {
            _enumerable = enumerable;

            Type eleType = _enumerable.GetType();
            if (eleType.IsGenericType)
                _rowCount = (int)eleType.GetProperty("Count").GetValue(_enumerable, null);
            else
            {
                if (eleType.IsArray)
                    _rowCount = (int)eleType.GetProperty("Length").GetValue(_enumerable, null);
            }

            int _rowsIndex = -1;
            int _columnIndex = -1;
            PropertyInfo[] _properties = null;
            _rows = new DefaultRow[_rowCount];

            foreach (var item in _enumerable)
            {
                _rowsIndex++;
                if (_rowsIndex == 0)
                {
                    _columnsHashMap = new Hashtable();
                    _properties = item.GetType().GetProperties();
                    _columns = new DataColumn[_properties.Length];
                    foreach (var property in _properties)
                    {
                        _columnIndex++;
                        _columnsHashMap.Add(_columnIndex, property.Name);
                        _columns[_columnIndex] = new DataColumn(property.Name, property.ReflectedType);
                    }
                }
                _rows[_rowsIndex] = new DefaultRow(this, item);
            }
        }
예제 #3
0
        private void    OnLogReceived(Client sender, Packet command)
        {
            LogPacket log = command as LogPacket;

            // Simulate a Log Entry.
            LogEntry logEntry = new LogEntry();

            logEntry.condition = log.condition + "\n" + log.stackTrace;

            if (log.logType == UnityEngine.LogType.Log)
            {
                logEntry.mode = Mode.ScriptingLog | Mode.MayIgnoreLineNumber;
            }
            else if (log.logType == UnityEngine.LogType.Warning)
            {
                logEntry.mode = Mode.ScriptingWarning | Mode.MayIgnoreLineNumber;
            }
            else if (log.logType == UnityEngine.LogType.Error)
            {
                logEntry.mode = Mode.ScriptingError | Mode.MayIgnoreLineNumber;
            }
            else if (log.logType == UnityEngine.LogType.Exception ||
                     log.logType == UnityEngine.LogType.Assert)
            {
                if (HQ.Settings.Get <GeneralSettings>().differentiateException == false)
                {
                    logEntry.mode = Mode.ScriptingError | Mode.MayIgnoreLineNumber;
                }
                else
                {
                    logEntry.mode = Mode.ScriptingError | Mode.ScriptingException | Mode.Log;
                }
            }

            if (string.IsNullOrEmpty(log.stackTrace))
            {
                logEntry.mode |= Mode.DontExtractStacktrace;
            }
            else
            {
                int fileStart = log.stackTrace.IndexOf(") (at ");

                if (fileStart != -1)
                {
                    int comma = log.stackTrace.IndexOf(':', fileStart);

                    if (comma != -1)
                    {
                        int par = log.stackTrace.IndexOf(')', comma);

                        if (par != -1)
                        {
                            logEntry.file = log.stackTrace.Substring(fileStart + 6, comma - fileStart - 6);
                            logEntry.line = int.Parse(log.stackTrace.Substring(comma + 1, par - comma - 1));
                        }
                    }
                }
            }

            DefaultRow row = new DefaultRow();

            row.Init(this.console, logEntry);

            this.AddRow(row);
        }
        }     // getGrouped()

        /**
         * Applies aggregate values to a dataset. This method is to be invoked AFTER
         * any filters have been applied.
         *
         * @param workSelectItems
         *            all select items included in the processing of the query
         *            (including those originating from other clauses than the
         *            SELECT clause).
         * @param dataSet
         * @return
         */
        public static DataSet getAggregated(List <SelectItem> workSelectItems, DataSet dataSet)
        {
            List <SelectItem> functionItems = getAggregateFunctionSelectItems(workSelectItems);

            if (functionItems.IsEmpty())
            {
                return(dataSet);
            }

            AggregateBuilder <Object> t;
            Dictionary <SelectItem, AggregateBuilder <Object> > aggregateBuilders = new Dictionary <SelectItem, AggregateBuilder <Object> >();

            foreach (SelectItem item in functionItems)
            {
                aggregateBuilders.Add(item, item.getAggregateFunction().createAggregateBuilder <object>());
            }

            DataSetHeader header;
            bool          onlyAggregates;

            if (functionItems.Count != workSelectItems.Count)
            {
                onlyAggregates = false;
                header         = new CachingDataSetHeader(workSelectItems);
            }
            else
            {
                onlyAggregates = true;
                header         = new SimpleDataSetHeader(workSelectItems);
            }

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

            while (dataSet.next())
            {
                Row inputRow = dataSet.getRow();
                foreach (SelectItem item in functionItems)
                {
                    AggregateBuilder <object> aggregateBuilder = aggregateBuilders[item];
                    Column column = item.getColumn();
                    if (column != null)
                    {
                        Object value = inputRow.getValue(new SelectItem(column));
                        aggregateBuilder.add(value);
                    }
                    else if (SelectItem.isCountAllItem(item))
                    {
                        // Just use the empty string, since COUNT(*) don't
                        // evaluate values (but null values should be prevented)
                        aggregateBuilder.add("");
                    }
                    else
                    {
                        throw new ArgumentException("Expression function not supported: " + item);
                    }
                }

                // If the result should also contain non-aggregated values, we
                // will keep those in the rows list
                if (!onlyAggregates)
                {
                    Object[] values = new Object[header.size()];
                    for (int i = 0; i < header.size(); i++)
                    {
                        Object value = inputRow.getValue(header.getSelectItem(i));
                        if (value != null)
                        {
                            values[i] = value;
                        }
                    }
                    resultRows.Add(new DefaultRow(header, values));
                }
            }
            dataSet.close();

            // Collect the aggregates
            Dictionary <SelectItem, Object> functionResult = new Dictionary <SelectItem, Object>();

            foreach (SelectItem item in functionItems)
            {
                AggregateBuilder <object> aggregateBuilder = aggregateBuilders[item];
                Object result = aggregateBuilder.getAggregate();
                functionResult.Add(item, result);
            }

            // if there are no result rows (no matching records at all), we still
            // need to return a record with the aggregates
            bool noResultRows = resultRows.IsEmpty();

            if (onlyAggregates || noResultRows)
            {
                // We will only create a single row with all the aggregates
                Object[] values = new Object[header.size()];
                for (int i = 0; i < header.size(); i++)
                {
                    values[i] = functionResult[header.getSelectItem(i)];
                }
                Row row = new DefaultRow(header, values);
                resultRows.Add(row);
            }
            else
            {
                // We will create the aggregates as well as regular values
                for (int i = 0; i < resultRows.Count; i++)
                {
                    Row      row    = resultRows[i];
                    Object[] values = row.getValues();
                    foreach (KeyValuePair <SelectItem, Object> entry in functionResult)
                    {
                        SelectItem item      = entry.Key;
                        int        itemIndex = row.indexOf(item);
                        if (itemIndex != -1)
                        {
                            Object value = entry.Value;
                            values[itemIndex] = value;
                        }
                    }
                    resultRows[i] = new DefaultRow(header, values);
                }
            }
            return(new InMemoryDataSet(header, resultRows));
        }     // getAggregated()