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

br.com.objectos.db.Dialect Maven / Gradle / Ivy

There is a newer version: 0.4.0
Show newest version
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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.
 */
package br.com.objectos.db;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class Dialect {

  protected Dialect() {
  }

  public final Database connect() {
    return DatabaseConfig.builder()
        .dialect(this)
        .server(string("server"))
        .port(intValue("port"))
        .db(string("db"))
        .user(string("user"))
        .password(string("password"))
        .build()
        .connect();
  }

  public abstract SqlBuilder sqlBuilder();

  protected abstract String prefix();

  protected String schemaName(String tableCat, String tableSchem) {
    return tableSchem;
  }

  protected abstract ResultSet tables(DatabaseMetaData db, String schemaName) throws SQLException;

  protected abstract Vendor vendor();

  Metadata metadata(Database database, DatabaseMetaData db) {
    String dbName = database.config().db();
    String catalogName = vendor().catalogName(dbName);
    String schemaName = vendor().schemaName(dbName);
    return new Metadata(this, db, catalogName, schemaName);
  }

  private int intValue(String key) {
    return Integer.getInteger(key(key), Integer.getInteger(jdbc(key), 0));
  }

  private String jdbc(String key) {
    return "jdbc" + "." + key;
  }

  private String key(String key) {
    return prefix() + "." + key;
  }

  private String string(String key) {
    return System.getProperty(key(key), System.getProperty(jdbc(key), ""));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy