001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.dataAccess.db;
019
020import javax.persistence.Column;
021import java.util.ArrayList;
022import java.util.List;
023
024import static io.stallion.utils.Literals.list;
025
026/**
027 * Used internally to represent the schema of a database table. Model classes can
028 * be parsed into schemas, the schemas are then used to build the INSERT or UPDATE
029 * sql when doing database operations.
030 */
031public class Schema {
032    private List<Col> columns;
033    private String name;
034    private Class clazz;
035    private List<String> keyNames;
036    private List<String> extraKeyDefinitions = list();
037
038    public Schema(String name, Class clazz) {
039        columns = new ArrayList<Col>();
040        this.name = name;
041        this.clazz = clazz;
042    }
043
044    public List<Col> getColumns() {
045        return columns;
046    }
047
048    public Schema setColumns(List<Col> columns) {
049        this.columns = columns;
050        return this;
051    }
052
053    public String getName() {
054        return name;
055    }
056
057    public Schema setName(String name) {
058        this.name = name;
059        return this;
060    }
061
062    public Class getClazz() {
063        return clazz;
064    }
065
066    public void setClazz(Class clazz) {
067        this.clazz = clazz;
068    }
069
070    public List<String> getKeyNames() {
071        if (keyNames == null) {
072            keyNames = new ArrayList<>();
073            for(Col col: columns) {
074                if (col.getAlternativeKey()) {
075                    keyNames.add(col.getPropertyName());
076                }
077            }
078        }
079        return this.keyNames;
080    }
081
082
083    public List<String> getExtraKeyDefinitions() {
084        return extraKeyDefinitions;
085    }
086
087    public Schema setExtraKeyDefinitions(List<String> extraKeyDefinitions) {
088        this.extraKeyDefinitions = extraKeyDefinitions;
089        return this;
090    }
091}