Package org.jooq.tools.csv

Examples of org.jooq.tools.csv.CSVReader


            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);
        }
View Full Code Here


                  // [#1137] Avoid generating enum classes for enum types that
                  // are explicitly forced to another type
                    if (getConfiguredForcedType(columnDefinition) == null) {
                        DefaultEnumDefinition definition = new DefaultEnumDefinition(schema, name, comment);

                        CSVReader reader = new CSVReader(
                            new StringReader(columnType.replaceAll("(^enum\\()|(\\)$)", ""))
                           ,','  // Separator
                           ,'\'' // Quote character
                           ,true // Strict quotes
                        );

                        for (String string : reader.next()) {
                            definition.addLiteral(string);
                        }

                        result.add(definition);
                    }
View Full Code Here

            reader.close();
        }
    }

    private final void executeCSV() throws IOException {
        CSVReader reader = new CSVReader(data, separator, quote, ignoreRows);

        try {
            executeSQL(reader);
        }

        // SQLExceptions originating from rollbacks or commits are always fatal
        // They are propagated, and not swallowed
        catch (SQLException e) {
            throw Utils.translate(null, e);
        }
        finally {
            reader.close();
        }
    }
View Full Code Here

            DefaultEnumDefinition e = new DefaultEnumDefinition(getSchemata().get(0), name, null, true);

            String literals = enumType.getLiterals();

            try {
                CSVReader reader = new CSVReader(new StringReader(literals));
                e.addLiterals(reader.readNext());
            } catch (IOException ignore) {}

            result.add(e);
        }
View Full Code Here

        return this;
    }

    private final void executeCSV() throws IOException {
        CSVReader reader = new CSVReader(data, separator, quote, ignoreRows);

        try {
            String[] row = null;

            // TODO: When running in COMMIT_AFTER > 1 or COMMIT_ALL mode, then
            // it might be better to bulk load / merge n records
            rowloop: while ((row = reader.readNext()) != null) {
                processed++;
                InsertQuery<R> insert = create.insertQuery(table);

                for (int i = 0; i < row.length; i++) {
                    if (i < fields.length && fields[i] != null) {
                        addValue0(insert, fields[i], row[i]);
                    }
                }

                // TODO: This is only supported by some dialects. Let other
                // dialects execute a SELECT and then either an INSERT or UPDATE
                if (onDuplicate == ON_DUPLICATE_KEY_UPDATE) {
                    insert.onDuplicateKeyUpdate(true);

                    for (int i = 0; i < row.length; i++) {
                        if (i < fields.length && fields[i] != null && !mainKey[i]) {
                            addValueForUpdate0(insert, fields[i], row[i]);
                        }
                    }
                }

                // TODO: This can be implemented faster using a MERGE statement
                // in some dialects
                else if (onDuplicate == ON_DUPLICATE_KEY_IGNORE) {
                    SimpleSelectQuery<R> select = create.selectQuery(table);

                    for (int i = 0; i < row.length; i++) {
                        if (i < fields.length && mainKey[i]) {
                            select.addConditions(getCondition(fields[i], row[i]));
                        }
                    }

                    try {
                        if (select.execute() > 0) {
                            ignored++;
                            continue rowloop;
                        }
                    }
                    catch (DataAccessException e) {
                        errors.add(new LoaderErrorImpl(e, row, processed - 1, select));
                    }
                }

                // Don't do anything. Let the execution fail
                else if (onDuplicate == ON_DUPLICATE_KEY_ERROR) {
                }

                try {
                    insert.execute();
                    stored++;

                    if (commit == COMMIT_AFTER) {
                        if (processed % commitAfter == 0) {
                            create.getConnection().commit();
                        }
                    }
                }
                catch (DataAccessException e) {
                    errors.add(new LoaderErrorImpl(e, row, processed - 1, insert));
                    ignored++;

                    if (onError == ON_ERROR_ABORT) {
                        break rowloop;
                    }
                }
            }

            // Rollback on errors in COMMIT_ALL mode
            try {
                if (commit == COMMIT_ALL) {
                    if (!errors.isEmpty()) {
                        stored = 0;
                        create.getConnection().rollback();
                    }
                    else {
                        create.getConnection().commit();
                    }
                }

                // Commit remaining elements in COMMIT_AFTER mode
                else if (commit == COMMIT_AFTER) {
                    if (processed % commitAfter != 0) {
                        create.getConnection().commit();
                    }
                }
            }
            catch (DataAccessException e) {
                errors.add(new LoaderErrorImpl(e, null, processed - 1, null));
            }
        }

        // SQLExceptions originating from rollbacks or commits are always fatal
        // They are propagated, and not swallowed
        catch (SQLException e) {
            throw Util.translate("LoaderImpl.executeCSV", null, e);
        }
        finally {
            reader.close();
        }
    }
View Full Code Here

        return fetchFromCSV(string, ',');
    }

    @Override
    public Result<Record> fetchFromCSV(String string, char delimiter) {
        CSVReader reader = new CSVReader(new StringReader(string), delimiter);
        List<String[]> data = null;

        try {
            data = reader.readAll();
        }
        catch (IOException e) {
            throw new DataAccessException("Could not read the CSV string", e);
        }
        finally {
            try {
                reader.close();
            }
            catch (IOException ignore) {}
        }

        return fetchFromStringData(data);
View Full Code Here

TOP

Related Classes of org.jooq.tools.csv.CSVReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.