}     // getPrimaryKeys()

        public Column getColumnByName(String columnName)
        {
            if (columnName == null)
            {
                return(null);
            }

            List <Column> foundColumns       = new List <Column>(1);
            List <string> found_column_names = new List <string>(1);

            // Search for column matches, case insensitive.
            foreach (Column column in getColumns())
            {
                String candidateName = column.getName();
                if (columnName.Equals(candidateName, StringComparison.CurrentCultureIgnoreCase))
                {
                    foundColumns.Add(column);
                    found_column_names.Add(candidateName);
                }
            }

            int numColumns = foundColumns.Count;

            if (logger.isDebugEnabled())
            {
                //logger.debug("Found {} column(s) matching '{}': {}", new Object[] { numColumns, columnName, foundColumns });
                logger.debug("Found {0} column(s) matching '{1}': {2}", numColumns, columnName, string.Join(",", found_column_names));
            }

            if (numColumns == 0)
            {
                return(null);
            }
            else if (numColumns == 1)
            {
                // if there's only one, return it.
                return(foundColumns[0]);
            }

            // If more matches are found, search case sensitive
            foreach (Column column in foundColumns)
            {
                if (columnName.Equals(column.getName()))
                {
                    return(column);
                }
            }

            // if none matches case sensitive, pick the first one.
            return(foundColumns[0]);
        }     // getColumnByName()
        } // getTableCount()

        // @Override
        public virtual Table getTableByName(String tableName)
        {
            if (tableName == null)
            {
                return(null);
            }

            List <string> found_table_names = new List <string>(1);
            List <Table>  foundTables       = new List <Table>(1);

            // Search for table matches, case insensitive.
            foreach (Table table in getTables(false))
            {
                if (tableName.Equals(table.getName(), StringComparison.CurrentCultureIgnoreCase))
                {
                    foundTables.Add(table);
                    found_table_names.Add(table.getName());
                }
            }

            int numTables = foundTables.Count;

            if (logger.isDebugEnabled())
            {
                logger.debug("Found {0} tables(s) matching '{1}': {2}", numTables, tableName, found_table_names);
            }

            if (numTables == 0)
            {
                return(null);
            }
            else if (numTables == 1)
            {
                return(foundTables[0]);
            }

            // If more matches are found, search case sensitive
            foreach (Table table in foundTables)
            {
                if (tableName.Equals(table.getName()))
                {
                    return(table);
                }
            }

            // if none matches case sensitive, pick the first one.
            return(foundTables[0]);
        } // getTableByName()
        } // 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()
        // @Override
        public Column findColumn(String columnName) // throws IllegalArgumentException
        {
            if (columnName == null)
            {
                throw new ArgumentException("columnName cannot be null");
            }

            List <FromItem>   fromItems   = _query.getFromClause().getItems();
            List <SelectItem> selectItems = _query.getSelectClause().getItems();

            Column column = null;

            int dotIndex = columnName.IndexOf('.');

            if (dotIndex != -1)
            {
                // check aliases of from items
                String aliasPart  = columnName.Substring(0, dotIndex);
                String columnPart = columnName.Substring(dotIndex + 1);

                foreach (FromItem fromItem in fromItems)
                {
                    column = null;
                    column = findColumnInAliasedTable(column, fromItem, aliasPart, columnPart);
                    if (column != null)
                    {
                        return(column);
                    }
                }
            }

            // check columns already in select clause
            foreach (SelectItem item in selectItems)
            {
                column = item.getColumn();
                if (column != null)
                {
                    if (columnName.Equals(column.getName()))
                    {
                        return(column);
                    }
                }
            }

            foreach (FromItem fromItem in fromItems)
            {
                Table table = fromItem.getTable();
                if (table != null)
                {
                    column = table.getColumnByName(columnName);
                    if (column != null)
                    {
                        return(column);
                    }
                }
            }

            column = _dataContext.getColumnByQualifiedLabel(columnName);
            if (column != null)
            {
                return(column);
            }

            ArgumentException exception = new ArgumentException("Could not find column: " + columnName);

            if (logger.isDebugEnabled())
            {
                logger.debug("findColumn('" + columnName + "') could not resolve a column", exception);
                foreach (FromItem fromItem in fromItems)
                {
                    Table table = fromItem.getTable();
                    if (table != null)
                    {
                        logger.debug("Table available in FROM item: {}. Column names: {}", table,
                                     NArrays.ArrayAsString(table.getColumnNames()));
                    }
                }
            }

            throw exception;
        }
Beispiel #5
0
        } // readAsString()

        public static void safeClose(params Object[] objects)
        {
            bool debug_enabled = logger.isDebugEnabled();

            if (objects == null || objects.Length == 0)
            {
                logger.info("safeClose(...) was invoked with null or empty array: {}", objects);
                return;
            }

            foreach (Object obj in objects)
            {
                if (obj != null)
                {
                    if (debug_enabled)
                    {
                        logger.debug("Trying to safely close {}", obj);
                    }

                    if (obj is NFlushable)
                    {
                        try
                        {
                            ((NFlushable)obj).flush();
                        }
                        catch (Exception e)
                        {
                            if (debug_enabled)
                            {
                                logger.debug("Flushing Flushable failed", e);
                            }
                        }
                    }

                    if (obj is IDisposable)
                    {
                        try
                        {
                            ((IDisposable)obj).Dispose();
                        }
                        catch (Exception e)
                        {
                            if (debug_enabled)
                            {
                                logger.debug("Closing AutoCloseable failed", e);
                            }
                        }
                    }
                    else
                    {
                        logger.info("obj was not AutoCloseable, trying to find close() method via reflection.");

                        try
                        {
                            MethodInfo method = obj.GetType().GetMethod("close");
                            if (method == null)
                            {
                                logger.info("obj did not have a close() method, ignoring");
                            }
                            else
                            {
                                //method.setAccessible(true);
                                method.Invoke(obj, null);
                            }
                        }
                        catch (TargetInvocationException e)
                        {
                            logger.warn("Invoking close() by reflection threw exception", e);
                        }
                        catch (Exception e)
                        {
                            logger.warn("Could not invoke close() by reflection", e);
                        }
                    }
                } // foreach (Object obj in objects)
            }
        }         // safeClose()