All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jooq.impl.MetaDataFieldProvider Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
/**
 * Copyright (c) 2009-2014, Data Geekery GmbH (http://www.datageekery.com)
 * All rights reserved.
 *
 * This work is dual-licensed
 * - under the Apache Software License 2.0 (the "ASL")
 * - under the jOOQ License and Maintenance Agreement (the "jOOQ License")
 * =============================================================================
 * You may choose which license applies to you:
 *
 * - If you're using this work with Open Source databases, you may choose
 *   either ASL or jOOQ License.
 * - If you're using this work with at least one commercial database, you must
 *   choose jOOQ License
 *
 * For more information, please visit http://www.jooq.org/licenses
 *
 * Apache Software License 2.0:
 * -----------------------------------------------------------------------------
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * jOOQ License and Maintenance Agreement:
 * -----------------------------------------------------------------------------
 * Data Geekery grants the Customer the non-exclusive, timely limited and
 * non-transferable license to install and use the Software under the terms of
 * the jOOQ License and Maintenance Agreement.
 *
 * This library is distributed with a LIMITED WARRANTY. See the jOOQ License
 * and Maintenance Agreement for more details: http://www.jooq.org/licensing
 */
package org.jooq.impl;

import static org.jooq.impl.DSL.field;

import java.io.Serializable;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jooq.Configuration;
import org.jooq.DataType;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.tools.JooqLogger;

/**
 * A FieldProvider providing fields for a JDBC
 * {@link ResultSetMetaData} object.
 * 

* This can be used when a Cursor doesn't know its fields before * actually fetching data from the database. Use-cases for this are: *

    *
  • Plain SQL tables
  • *
  • CURSOR type OUT parameters from stored procedures
  • *
  • CURSOR type return values from stored functions
  • *
* * @author Lukas Eder */ class MetaDataFieldProvider implements Serializable { /** * Generated UID */ private static final long serialVersionUID = -8482521025536063609L; private static final JooqLogger log = JooqLogger.getLogger(MetaDataFieldProvider.class); private final Fields fields; MetaDataFieldProvider(Configuration configuration, ResultSetMetaData meta) { this.fields = init(configuration, meta); } private Fields init(Configuration configuration, ResultSetMetaData meta) { List> fieldList = new ArrayList>(); int columnCount = 0; try { columnCount = meta.getColumnCount(); } // This happens in Oracle for empty cursors returned from stored // procedures / functions catch (SQLException e) { log.info("Cannot fetch column count for cursor : " + e.getMessage()); fieldList.add(field("dummy")); } try { for (int i = 1; i <= columnCount; i++) { String name = meta.getColumnLabel(i); int precision = meta.getPrecision(i); int scale = meta.getScale(i); DataType dataType = SQLDataType.OTHER; String type = meta.getColumnTypeName(i); try { dataType = DefaultDataType.getDataType(configuration.dialect().family(), type, precision, scale); if (dataType.hasPrecision()) { dataType = dataType.precision(precision); } if (dataType.hasScale()) { dataType = dataType.scale(scale); } if (dataType.hasLength()) { // JDBC doesn't distinguish between precision and length dataType = dataType.length(precision); } } // [#650, #667] All types should be known at this point, but in plain // SQL environments, it is possible that user-defined types, or vendor-specific // types (e.g. such as PostgreSQL's json type) will cause this exception. catch (SQLDialectNotSupportedException ignore) { log.debug("Not supported by dialect", ignore.getMessage()); } fieldList.add(field(name, dataType)); } } catch (SQLException e) { throw Utils.translate(null, e); } return new Fields(fieldList); } final Field[] getFields() { return fields.fields(); } // ------------------------------------------------------------------------- // The Object API // ------------------------------------------------------------------------- @Override public String toString() { return fields.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy