Exemplo n.º 1
0
 public static FileStream createTempFile(string prefix, string suffix)
 {
     try
     {
         return(File.Open(prefix + '.' + suffix, FileMode.Create));  //File.createTempFile(prefix, suffix);
     }
     catch (IOException e)
     {
         logger.error("Could not create tempFile", e);
         string tempDir = getTempDir();
         return(File.Open(tempDir + "\\" + prefix + '.' + suffix, FileMode.Create));
     }
 } // createTempFile()
Exemplo n.º 2
0
        } // getComparable()

        public int Compare(Object o1, Object o2)
        {
            if (o1 == null && o2 == null)
            {
                return(0);
            }
            if (o1 == null)
            {
                return(-1);
            }
            if (o2 == null)
            {
                return(1);
            }
            try
            {
                NDate dt1 = toDate(o1);
                NDate dt2 = toDate(o2);
                return(NDate.CompareTo(dt1, dt2));
            }
            catch (Exception e)
            {
                logger.error("Could not compare {} and {}", o1, o2);
                throw new NSystemException(e.Message);
            }
        } // Compare()
        } // convertColumnType()

        public static ColumnType valueOf(string fieldName)
        {
            try
            {
                FieldInfo column_type_field = NTypeUtils.getField(typeof(ColumnType), fieldName);
                if (column_type_field != null)
                {
                    // columnTypeField.setAccessible(true);
                    object o = new object();
                    column_type_field.GetValue(o);
                    ColumnType columnType = NSystem.Cast(o, typeof(ColumnType));
                    return((ColumnType)columnType);
                }
            }
            catch (Exception e)
            {
                logger.error("Failed to resolve JDBC type in ColumnType constants: " + fieldName, e);
            }
            return(null);
        } // valueOf()
        }                                                           // getGrouped()

        public static DataSet getGrouped(List <SelectItem> selectItems, DataSet dataSet, GroupByItem[] groupByItems)
        {
            DataSet result = dataSet;

            if (groupByItems != null && groupByItems.Length > 0)
            {
                Dictionary <Row, Dictionary <SelectItem, List <Object> > > uniqueRows = new Dictionary <Row, Dictionary <SelectItem, List <Object> > >();

                SelectItem[] groupBySelects = new SelectItem[groupByItems.Length];
                for (int i = 0; i < groupBySelects.Length; i++)
                {
                    groupBySelects[i] = groupByItems[i].getSelectItem();
                }
                DataSetHeader groupByHeader = new CachingDataSetHeader(groupBySelects);

                // Creates a list of SelectItems that have aggregate functions
                List <SelectItem> functionItems = getAggregateFunctionSelectItems(selectItems);

                // Loop through the dataset and identify groups
                while (dataSet.next())
                {
                    Row row = dataSet.getRow();

                    // Subselect a row prototype with only the unique values that
                    // define the group
                    Row uniqueRow = row.getSubSelection(groupByHeader);

                    // function input is the values used for calculating aggregate
                    // functions in the group
                    Dictionary <SelectItem, List <Object> > functionInput;
                    if (!uniqueRows.ContainsKey(uniqueRow))
                    {
                        // If this group already exist, use an existing function
                        // input
                        functionInput = new Dictionary <SelectItem, List <Object> >();
                        foreach (SelectItem item in functionItems)
                        {
                            functionInput.Add(item, new List <Object>());
                        }
                        uniqueRows.Add(uniqueRow, functionInput);
                    }
                    else
                    {
                        // If this is a new group, create a new function input
                        functionInput = uniqueRows[uniqueRow];
                    }

                    // Loop through aggregate functions to check for validity
                    foreach (SelectItem item in functionItems)
                    {
                        List <Object> objects = functionInput[item];
                        Column        column  = item.getColumn();
                        if (column != null)
                        {
                            Object value = row.getValue(new SelectItem(column));
                            objects.Add(value);
                        }
                        else if (SelectItem.isCountAllItem(item))
                        {
                            // Just use the empty string, since COUNT(*) don't
                            // evaluate values (but null values should be prevented)
                            objects.Add("");
                        }
                        else
                        {
                            throw new ArgumentException("Expression function not supported: " + item);
                        }
                    }
                }

                dataSet.close();
                List <Row>    resultData   = new List <Row>();
                DataSetHeader resultHeader = new CachingDataSetHeader(selectItems);

                int count = uniqueRows.Count;
                // Loop through the groups to generate aggregates
                foreach (KeyValuePair <Row, Dictionary <SelectItem, List <Object> > > key_value in uniqueRows)
                {
                    Row row = key_value.Key;
                    Dictionary <SelectItem, List <Object> > functionInput = key_value.Value;
                    Object[] resultRow = new Object[selectItems.Count];
                    // Loop through select items to generate a row
                    int i = 0;
                    foreach (SelectItem item in selectItems)
                    {
                        int uniqueRowIndex = row.indexOf(item);
                        if (uniqueRowIndex != -1)
                        {
                            // If there's already a value for the select item in the
                            // row, keep it (it's one of the grouped by columns)
                            resultRow[i] = row.getValue(uniqueRowIndex);
                        }
                        else
                        {
                            // Use the function input to calculate the aggregate
                            // value
                            List <Object> objects = functionInput[item];
                            if (objects != null)
                            {
                                Object functionResult = item.getAggregateFunction().evaluate(objects.ToArray());
                                resultRow[i] = functionResult;
                            }
                            else
                            {
                                if (item.getAggregateFunction() != null)
                                {
                                    logger.error("No function input found for SelectItem: {}", item);
                                }
                            }
                        }
                        i++;
                    }
                    resultData.Add(new DefaultRow(resultHeader, resultRow, null));
                }

                if (resultData.IsEmpty())
                {
                    result = new EmptyDataSet(selectItems);
                }
                else
                {
                    result = new InMemoryDataSet(resultHeader, resultData);
                }
            }
            result = getSelection(selectItems, result);
            return(result);
        }     // getGrouped()