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

gu.sql2java.generator.Index Maven / Gradle / Ivy

The newest version!
package gu.sql2java.generator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import gu.sql2java.generator.Column;
import gu.sql2java.generator.IndexColumn;
import gu.sql2java.generator.Table;

public class Index {
	private Table table;
	private String name;
	private boolean unique;
	private Map columns;

	public Index() {
		this("");
	}

	public Index(String name) {
		this(name, null);
	}

	public Index(String name, Table table) {
		this(name, table, new HashMap());
	}

	public Index(String name, Table table, Map columns) {
		this.name = name;
		this.table = table;
		this.columns = columns;
		if (null != this.table) {
			this.table.addIndex(this);
		}
	}

	public void addIndexColumn(IndexColumn column) {
		if (null != column) {
			this.columns.put(column.getName().toLowerCase(), column);
		}
	}

	public void removeIndexColumn(Column column) {
		if (null != column) {
			this.columns.remove(column.getName().toLowerCase());
		}
	}

	public Table getTable() {
		return this.table;
	}

	public String getName() {
		return this.name;
	}

	public boolean isUnique() {
		return this.unique;
	}

	public Map getIndexColumns() {
		return this.columns;
	}

	public List getIndexColumnsList() {
		ArrayList list = new ArrayList();
		list.addAll(this.columns.values());
		Collections.sort(list);
		return list;
	}
	public Column getIndexColumn() {
		if (this.columns.size() != 1) {
			throw new RuntimeException("the index "+this.getName() + "of Table " + this.getTable().getName() + " is a composite key");
		}
		return getIndexColumnsList().get(0);	
	}
	public void setName(String name) {
		this.name = name;
	}

	public void setUnique(boolean unique) {
		this.unique = unique;
	}

	public void setTable(Table table) {
		this.table = table;
	}
	
	public String asIndexName(){
		StringBuilder buf=new StringBuilder("index");
		List list = getIndexColumnsList();
		for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy