jdbcschema.java

来自「数据仓库展示程序」· Java 代码 · 共 1,385 行 · 第 1/3 页

JAVA
1,385
字号
            /** Radix (typically either 10 or 2). */
            private int numPrecRadix;

            /** For char types the maximum number of bytes in the column. */
            private int charOctetLength;

            /**
             * False means the column definitely does not allow NULL values.
             */
            private boolean isNullable;

            public final MondrianDef.Column column;

            private final List usages;

            /**
             * This contains the enums of all of the column's usages.
             */
            private int columnType;

            private Column(final String name) {
                this.name = name;
                this.column = new MondrianDef.Column(
                                        JdbcSchema.Table.this.getName(),
                                        name);
                this.usages = new ArrayList();
            }
            /**
             * For testing ONLY
            JdbcSchema.Table.Column copy() {
                Column column = new Column(name);
                column.type = type;
                column.typeName = typeName;
                column.columnSize = columnSize;
                column.decimalDigits = decimalDigits;
                column.numPrecRadix = numPrecRadix;
                column.charOctetLength = charOctetLength;
                column.isNullable = isNullable;

                return column;
            }
             */
            /**
             * For testing ONLY
            void clearUsages() {
                // empty
            }
             */


            /**
             * This is the column's name in the database, not a symbolic name.
             *
             * @return
             */
            public String getName() {
                return name;
            }

            /**
             * Set the columns java.sql.Type enun of the column.
             *
             * @param type
             */
            private void setType(final int type) {
                this.type = type;
            }

            /**
             * Get the columns java.sql.Type enun of the column.
             *
             * @return
             */
            public int getType() {
                return type;
            }

            /**
             * Set the columns java.sql.Type name.
             *
             * @param typeName
             */
            private void setTypeName(final String typeName) {
                this.typeName = typeName;
            }

            /**
             * Get the columns java.sql.Type name.
             *
             * @return
             */
            public String getTypeName() {
                return typeName;
            }

            /**
             * Get this column's table.
             *
             * @return
             */
            public Table getTable() {
                return JdbcSchema.Table.this;
            }

            /**
             * Return true if this column is numeric.
             *
             * @return
             */
            public boolean isNumeric() {
                return JdbcSchema.isNumeric(getType());
            }

            /**
             * Set the size in bytes of the column in the database.
             *
             * @param columnSize
             */
            private void setColumnSize(final int columnSize) {
                this.columnSize = columnSize;
            }

            /**
             * Get the size in bytes of the column in the database.
             *
             *
             * @return
             */
            public int getColumnSize() {
                return columnSize;
            }

            /**
             * Set number of fractional digits.
             *
             * @param decimalDigits
             */
            private void setDecimalDigits(final int decimalDigits) {
                this.decimalDigits = decimalDigits;
            }

            /**
             * Get number of fractional digits.
             *
             * @return
             */
            public int getDecimalDigits() {
                return decimalDigits;
            }

            /**
             * Set Radix (typically either 10 or 2).
             *
             * @param numPrecRadix
             */
            private void setNumPrecRadix(final int numPrecRadix) {
                this.numPrecRadix = numPrecRadix;
            }

            /**
             * Get Radix (typically either 10 or 2).
             *
             * @return
             */
            public int getNumPrecRadix() {
                return numPrecRadix;
            }

            /**
             * For char types the maximum number of bytes in the column.
             *
             * @param charOctetLength
             */
            private void setCharOctetLength(final int charOctetLength) {
                this.charOctetLength = charOctetLength;
            }

            /**
             * For char types the maximum number of bytes in the column.
             *
             * @return
             */
            public int getCharOctetLength() {
                return charOctetLength;
            }

            /**
             * False means the column definitely does not allow NULL values.
             *
             * @param isNullable
             */
            private void setIsNullable(final boolean isNullable) {
                this.isNullable = isNullable;
            }

            /**
             * False means the column definitely does not allow NULL values.
             *
             * @return
             */
            public boolean isNullable() {
                return isNullable;
            }

            /**
             * How many usages does this column have. A column has
             * between 0 and N usages. It has no usages if it is some
             * administrative column. It has one usage if, for example, its
             * the fact_count column or a level column (for a collapsed
             * dimension aggregate). It might have 2 usages if its a foreign key
             * that is also used as a measure. If its a column used in N
             * measures, then it will have N usages.
             *
             * @return
             */
            public int numberOfUsages() {
                return usages.size();
            }

            /**
             * Return true if the column has at least one usage.
             *
             * @return
             */
            public boolean hasUsage() {
                return (usages.size() != 0);
            }

            /**
             * Return true if the column has at least one usage of the given
             * column type.
             *
             * @param columnType
             * @return
             */
            public boolean hasUsage(final int columnType) {
                return ((this.columnType & columnType) != 0);
            }

            /**
             * Get an iterator over all usages.
             *
             * @return
             */
            public Iterator getUsages() {
                return usages.iterator();
            }

            /**
             * Get an iterator over all usages of the given column type.
             *
             * @param columnType
             * @return
             */
            public Iterator getUsages(final int columnType) {

                // Yes, this is legal.
                class ColumnTypeIterator implements Iterator {
                    private final Iterator it;
                    private final int columnType;
                    private Object nextObject;

                    ColumnTypeIterator(final Iterator it,
                                       final int columnType) {
                        this.it = it;
                        this.columnType = columnType;
                    }
                    public boolean hasNext() {
                        while (it.hasNext()) {
                            Object o = it.next();
                            if (isColumnType(o, columnType)) {
                                nextObject = o;
                                return true;
                            }

                        }
                        nextObject = null;
                        return false;
                    }
                    protected boolean isColumnType(Object o, int columnType) {
                        return ((Usage)o).isColumnType(columnType);
                    }

                    public Object next() {
                        return nextObject;
                    }
                    public void remove() {
                        it.remove();
                    }
                }

                return new ColumnTypeIterator(getUsages(), columnType);
            }

            /**
             * Create a new usage of a given column type.
             *
             * @param columnType
             * @return
             */
            public Usage newUsage(int columnType) {
                this.columnType |= columnType;

                Usage usage = new Usage(columnType);
                usages.add(usage);
                return usage;
            }

            public String toString() {
                StringWriter sw = new StringWriter(256);
                PrintWriter pw = new PrintWriter(sw);
                print(pw, "");
                pw.flush();
                return sw.toString();
            }

            public void print(final PrintWriter pw, final String prefix) {
                pw.print(prefix);
                pw.print("name=");
                pw.print(getName());
                pw.print(", typename=");
                pw.print(getTypeName());
                pw.print(", size=");
                pw.print(getColumnSize());

                switch (getType()) {
                case Types.TINYINT :
                case Types.SMALLINT :
                case Types.INTEGER :
                case Types.BIGINT :
                case Types.FLOAT :
                case Types.REAL :
                case Types.DOUBLE :
                    break;
                case Types.NUMERIC :
                case Types.DECIMAL :
                    pw.print(", decimalDigits=");
                    pw.print(getDecimalDigits());
                    pw.print(", numPrecRadix=");
                    pw.print(getNumPrecRadix());
                    break;
                case Types.CHAR :
                case Types.VARCHAR :
                    pw.print(", charOctetLength=");
                    pw.print(getCharOctetLength());
                    break;
                case Types.LONGVARCHAR :
                case Types.DATE :
                case Types.TIME :
                case Types.TIMESTAMP :
                case Types.BINARY :
                case Types.VARBINARY :
                case Types.LONGVARBINARY :
                default:
                    break;
                }
                pw.print(", isNullable=");
                pw.print(isNullable());

                if (hasUsage()) {
                    pw.print(" Usages [");
                    for (Iterator it = getUsages(); it.hasNext(); ) {
                        Usage u = (Usage) it.next();
                        pw.print('(');
                        u.print(pw, prefix);
                        pw.print(')');
                    }
                    pw.println("]");
                }
            }
        }

        /** Name of table. */
        private final String name;
        /** Map from column name to column. */
        private Map columnMap;
        /** Sum of all of the table's column's column sizes. */
        private int totalColumnSize;
        /**
         * Is the table a fact, aggregate or other table type.
         * Note: this assumes that a table has only ONE usage.
         */
        private int tableUsage;

        /**
         * Typical table types are: "TABLE", "VIEW", "SYSTEM TABLE",
         * "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
         */
        private String tableType;

        // mondriandef stuff
        public MondrianDef.Table table;

        private boolean allColumnsLoaded;

        private Table(final String name) {
            this.name = name;
            this.tableUsage = UNKNOWN_TABLE_USAGE;
            this.tableType = UNKNOWN_TABLE_TYPE;
        }

        public void load() throws SQLException {
            loadColumns();
        }

        /**
         * For testing ONLY
        JdbcSchema.Table copy() {
            Table table = new Table(name);
            table.totalColumnSize = totalColumnSize;
            table.tableUsage = tableUsage;
            table.tableType = tableType;

            Map m = table.getColumnMap();
            for (Iterator it = getColumns(); it.hasNext(); ) {
                Column column = (Column) it.next();
                m.put(column.getName(), column.copy());
            }

            return table;
        }
         */
        /**
         * For testing ONLY
        void clearUsages() {
            this.tableUsage = UNKNOWN_TABLE_USAGE;
            for (Iterator it = getColumns(); it.hasNext(); ) {
                Column column = (Column) it.next();
                column.clearUsages();
            }
        }
         */

        /**
         * Get the name of the table.
         *
         * @return
         */
        public String getName() {
            return name;
        }

        /**
         * Get the total size of a row (sum of the column sizes).
         *
         * @return
         */
        public int getTotalColumnSize() {
            return totalColumnSize;
        }

        /**
         * Get the number of rows in the table.
         *
         * @return
         */
        public int getNumberOfRows() {
            return -1;
        }

        /**
         * Iterate of the table's columns.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?