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

org.jooq.util.ase.ASEDatabase Maven / Gradle / Ivy

There is a newer version: 3.19.16
Show newest version
/**
 * Copyright (c) 2009-2013, Lukas Eder, [email protected]
 * All rights reserved.
 *
 * This software is licensed to you under the Apache License, Version 2.0
 * (the "License"); You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * . Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * . Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * . Neither the name "jOOQ" nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package org.jooq.util.ase;

import static java.util.Arrays.asList;
import static org.jooq.impl.DSL.concat;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.val;
import static org.jooq.util.ase.sys.tables.Sysindexes.SYSINDEXES;
import static org.jooq.util.ase.sys.tables.Sysusers.SYSUSERS;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record10;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import org.jooq.tools.JooqLogger;
import org.jooq.util.AbstractDatabase;
import org.jooq.util.ArrayDefinition;
import org.jooq.util.ColumnDefinition;
import org.jooq.util.DefaultRelations;
import org.jooq.util.EnumDefinition;
import org.jooq.util.PackageDefinition;
import org.jooq.util.RoutineDefinition;
import org.jooq.util.SchemaDefinition;
import org.jooq.util.SequenceDefinition;
import org.jooq.util.TableDefinition;
import org.jooq.util.UDTDefinition;
import org.jooq.util.ase.sys.tables.Sysindexes;
import org.jooq.util.ase.sys.tables.Sysreferences;
import org.jooq.util.ase.sys.tables.Sysusers;

/**
 * Sybase Adaptive Server implementation of {@link AbstractDatabase}
 *
 * @author Lukas Eder
 */
public class ASEDatabase extends AbstractDatabase {

    private static final JooqLogger log = JooqLogger.getLogger(ASEDatabase.class);

    @Override
    protected DSLContext create0() {
        return DSL.using(getConnection(), SQLDialect.ASE);
    }

    private SchemaDefinition getSchema() {
        List schemata = getSchemata();

        if (schemata.size() > 1) {
            log.error("NOT SUPPORTED", "jOOQ does not support multiple schemata in Sybase ASE.");
            log.error("-----------------------------------------------------------------------");

            // TODO [#1098] Support this also for Sybase ASE
        }

        return schemata.get(0);
    }

    @Override
    protected void loadPrimaryKeys(DefaultRelations relations) throws SQLException {
        for (Record record : fetchKeys(PK_INCL, PK_EXCL)) {
            String keyName = record.getValue(0, String.class);
            TableDefinition table = getTable(getSchema(), record.getValue(1, String.class));

            if (table != null) {
                for (int i = 0; i < 8; i++) {
                    if (record.getValue(2 + i) == null) {
                        break;
                    }

                    relations.addPrimaryKey(keyName, table.getColumn(record.getValue(2 + i, String.class)));
                }
            }
        }
    }

    @Override
    protected void loadUniqueKeys(DefaultRelations relations) throws SQLException {
        for (Record record : fetchKeys(UK_INCL, UK_EXCL)) {
            String keyName = record.getValue(0, String.class);
            TableDefinition table = getTable(getSchema(), record.getValue(1, String.class));

            if (table != null) {
                for (int i = 0; i < 8; i++) {
                    if (record.getValue(2 + i) == null) {
                        break;
                    }

                    relations.addUniqueKey(keyName, table.getColumn(record.getValue(2 + i, String.class)));
                }
            }
        }
    }

    private static final int PK_INCL = 2048;
    private static final int PK_EXCL = 64;
    private static final int UK_INCL = 4096 | 2;
    private static final int UK_EXCL = 2048 | 64;

    /**
     * The underlying query of this method was found here:
     * 

* http://www.dbforums.com/sybase/1625012-sysindexes-question-testing- * unique-clustered-indexes.html */ private Result> fetchKeys(int incl, int excl) { Field table = field("object_name(id)", String.class); Field key = field("name", String.class); return create().select( concat(table, val("__"), key), table, field("index_col(object_name(id), indid, 1)", String.class), field("index_col(object_name(id), indid, 2)", String.class), field("index_col(object_name(id), indid, 3)", String.class), field("index_col(object_name(id), indid, 4)", String.class), field("index_col(object_name(id), indid, 5)", String.class), field("index_col(object_name(id), indid, 6)", String.class), field("index_col(object_name(id), indid, 7)", String.class), field("index_col(object_name(id), indid, 8)", String.class)) .from(SYSINDEXES) .where("status & ? = 0", excl) .and("status & ? <> 0", incl) .orderBy(Sysindexes.ID) .fetch(); } @Override protected void loadForeignKeys(DefaultRelations relations) throws SQLException { Field fkTable = field("object_name(tableid)", String.class); Field fk = field("object_name(constrid)", String.class); Field pkTable = field("object_name(reftabid)", String.class); Field pk = field("index_name(pmrydbid, reftabid, indexid)", String.class); for (Record record : create().select( fkTable.as("fk_table"), concat(fkTable, val("__"), fk).as("fk"), concat(pkTable, val("__"), pk).as("pk"), field("col_name(tableid, fokey1)", String.class), field("col_name(tableid, fokey2)", String.class), field("col_name(tableid, fokey3)", String.class), field("col_name(tableid, fokey4)", String.class), field("col_name(tableid, fokey5)", String.class), field("col_name(tableid, fokey6)", String.class), field("col_name(tableid, fokey7)", String.class), field("col_name(tableid, fokey8)", String.class), field("col_name(tableid, fokey9)", String.class), field("col_name(tableid, fokey10)", String.class), field("col_name(tableid, fokey11)", String.class), field("col_name(tableid, fokey12)", String.class), field("col_name(tableid, fokey13)", String.class), field("col_name(tableid, fokey14)", String.class), field("col_name(tableid, fokey15)", String.class), field("col_name(tableid, fokey16)", String.class), field("object_owner_id(tableid)")) .from(Sysreferences.SYSREFERENCES) .fetch()) { TableDefinition referencingTable = getTable(getSchema(), record.getValue("fk_table", String.class)); if (referencingTable != null) { for (int i = 0; i < 16; i++) { if (record.getValue(i + 3) == null) { break; } String foreignKeyName = record.getValue("fk", String.class); String foreignKeyColumnName = record.getValue(i + 3, String.class); String uniqueKeyName = record.getValue("pk", String.class); ColumnDefinition column = referencingTable.getColumn(foreignKeyColumnName); relations.addForeignKey(foreignKeyName, uniqueKeyName, column, getSchema()); } } } } @Override protected List getSchemata0() throws SQLException { List result = new ArrayList(); for (String name : create() .select(Sysusers.NAME) .from(SYSUSERS) .fetch(Sysusers.NAME)) { result.add(new SchemaDefinition(this, name, "")); } return result; } @Override protected List getSequences0() throws SQLException { List result = new ArrayList(); return result; } @Override protected List getTables0() throws SQLException { List result = new ArrayList(); for (Record record : fetchTables()) { SchemaDefinition schema = getSchema(record.getValue("Owner", String.class)); String name = record.getValue("Name", String.class); result.add(new ASETableDefinition(schema, name, null)); } return result; } private List fetchTables() { List result = new ArrayList(); for (Record record : create().fetch("sp_help")) { if (asList("view", "user table", "system table").contains(record.getValue("Object_type", String.class))) { if (getInputSchemata().contains(record.getValue("Owner", String.class))) { result.add(record); } } } return result; } @SuppressWarnings("unused") private List fetchRoutines() { List result = new ArrayList(); for (Record record : create().fetch("sp_help")) { if (asList("stored procedure").contains(record.getValue("Object_type", String.class))) { if (getInputSchemata().contains(record.getValue("Owner", String.class))) { result.add(record); } } } return result; } @Override protected List getRoutines0() throws SQLException { List result = new ArrayList(); // [#1507] This was contributed by Mark. It will be correctly // implemented at a later stage // for (Record record : fetchRoutines()) { // SchemaDefinition schema = getSchema(record.getValue("Owner", String.class)); // String name = record.getValue("Name", String.class); // result.add(new ASERoutineDefinition(schema, null, name, null, null, null)); // } return result; } @Override protected List getPackages0() throws SQLException { List result = new ArrayList(); return result; } @Override protected List getEnums0() throws SQLException { List result = new ArrayList(); return result; } @Override protected List getUDTs0() throws SQLException { List result = new ArrayList(); return result; } @Override protected List getArrays0() throws SQLException { List result = new ArrayList(); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy