Search in sources :

Example 1 with CSVReader

use of org.jooq.tools.csv.CSVReader in project jOOQ by jOOQ.

the class MySQLDatabase method getEnums0.

@Override
protected List<EnumDefinition> getEnums0() throws SQLException {
    List<EnumDefinition> result = new ArrayList<EnumDefinition>();
    Result<Record5<String, String, String, String, String>> records = create().select(Columns.TABLE_SCHEMA, Columns.COLUMN_COMMENT, Columns.TABLE_NAME, Columns.COLUMN_NAME, Columns.COLUMN_TYPE).from(COLUMNS).where(Columns.COLUMN_TYPE.like("enum(%)").and(Columns.TABLE_SCHEMA.in(getInputSchemata()))).orderBy(Columns.TABLE_SCHEMA.asc(), Columns.TABLE_NAME.asc(), Columns.COLUMN_NAME.asc()).fetch();
    for (Record record : records) {
        SchemaDefinition schema = getSchema(record.get(Columns.TABLE_SCHEMA));
        String comment = record.get(Columns.COLUMN_COMMENT);
        String table = record.get(Columns.TABLE_NAME);
        String column = record.get(Columns.COLUMN_NAME);
        String name = table + "_" + column;
        String columnType = record.get(Columns.COLUMN_TYPE);
        // [#1237] Don't generate enum classes for columns in MySQL tables
        // that are excluded from code generation
        TableDefinition tableDefinition = getTable(schema, table);
        if (tableDefinition != null) {
            ColumnDefinition columnDefinition = tableDefinition.getColumn(column);
            if (columnDefinition != null) {
                // are explicitly forced to another type
                if (getConfiguredForcedType(columnDefinition, columnDefinition.getType()) == null) {
                    DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment);
                    CSVReader reader = new CSVReader(new StringReader(columnType.replaceAll("(^enum\\()|(\\)$)", "")), // Separator
                    ',', // Quote character
                    '\'', // Strict quotes
                    true);
                    for (String string : reader.next()) {
                        definition.addLiteral(string);
                    }
                    result.add(definition);
                }
            }
        }
    }
    return result;
}
Also used : SchemaDefinition(org.jooq.util.SchemaDefinition) CSVReader(org.jooq.tools.csv.CSVReader) ArrayList(java.util.ArrayList) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) EnumDefinition(org.jooq.util.EnumDefinition) DefaultEnumDefinition(org.jooq.util.DefaultEnumDefinition) ColumnDefinition(org.jooq.util.ColumnDefinition) StringReader(java.io.StringReader) TableDefinition(org.jooq.util.TableDefinition) Record(org.jooq.Record) Record5(org.jooq.Record5)

Example 2 with CSVReader

use of org.jooq.tools.csv.CSVReader in project jOOQ by jOOQ.

the class DefaultDSLContext method fetchFromCSV.

@Override
public Result<Record> fetchFromCSV(String string, boolean header, char delimiter) {
    CSVReader reader = new CSVReader(new StringReader(string), delimiter);
    List<String[]> list = null;
    try {
        list = reader.readAll();
    } catch (IOException e) {
        throw new DataAccessException("Could not read the CSV string", e);
    } finally {
        try {
            reader.close();
        } catch (IOException ignore) {
        }
    }
    return fetchFromStringData(list, header);
}
Also used : CSVReader(org.jooq.tools.csv.CSVReader) StringReader(java.io.StringReader) IOException(java.io.IOException) DataAccessException(org.jooq.exception.DataAccessException)

Example 3 with CSVReader

use of org.jooq.tools.csv.CSVReader in project jOOQ by jOOQ.

the class AbstractDatabase method getConfiguredEnums.

private final List<EnumDefinition> getConfiguredEnums() {
    List<EnumDefinition> result = new ArrayList<EnumDefinition>();
    for (EnumType enumType : configuredEnumTypes) {
        String name = enumType.getName();
        DefaultEnumDefinition e = new DefaultEnumDefinition(getSchemata().get(0), name, null, true);
        String literals = enumType.getLiterals();
        try {
            @SuppressWarnings("resource") CSVReader reader = new CSVReader(new StringReader(literals));
            e.addLiterals(reader.readNext());
        } catch (IOException ignore) {
        }
        result.add(e);
    }
    return result;
}
Also used : CSVReader(org.jooq.tools.csv.CSVReader) EnumType(org.jooq.util.jaxb.EnumType) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 4 with CSVReader

use of org.jooq.tools.csv.CSVReader in project jOOQ by jOOQ.

the class LoaderImpl method executeCSV.

private final void executeCSV() throws IOException {
    CSVReader reader = null;
    try {
        if (ignoreRows == 1) {
            reader = new CSVReader(data.reader(), separator, quote, 0);
            source = Tools.fieldsByName(reader.next());
        } else {
            reader = new CSVReader(data.reader(), separator, quote, ignoreRows);
        }
        executeSQL(reader);
    }// They are propagated, and not swallowed
     catch (SQLException e) {
        throw Tools.translate(null, e);
    } finally {
        if (reader != null)
            reader.close();
    }
}
Also used : CSVReader(org.jooq.tools.csv.CSVReader) SQLException(java.sql.SQLException)

Aggregations

CSVReader (org.jooq.tools.csv.CSVReader)4 StringReader (java.io.StringReader)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 SQLException (java.sql.SQLException)1 Record (org.jooq.Record)1 Record5 (org.jooq.Record5)1 DataAccessException (org.jooq.exception.DataAccessException)1 ColumnDefinition (org.jooq.util.ColumnDefinition)1 DefaultEnumDefinition (org.jooq.util.DefaultEnumDefinition)1 EnumDefinition (org.jooq.util.EnumDefinition)1 SchemaDefinition (org.jooq.util.SchemaDefinition)1 TableDefinition (org.jooq.util.TableDefinition)1 EnumType (org.jooq.util.jaxb.EnumType)1