001/*
002 *  Copyright 2019 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.odf.ose.db.column;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.odf.ose.db.parameter.ValuedQueryParameter;
024
025/**
026 * The definition of a colum with a name, a SQL type and if it's nullable.
027 * Also, we can define a view column name for the associated SRC_* view.
028 */
029public class DefaultColumn implements Column
030{
031    /** The name */
032    protected String _name;
033    /** The SQL type */
034    protected String _type;
035    /** Column is nullable ? */
036    protected boolean _isNullable;
037    /** The column name in the view */
038    protected String _viewName;
039
040    /**
041     * Constructor.
042     * @param name The name of the column
043     * @param type The SQL type of the column
044     * @param isNullable <code>true</code> if it's nullable
045     * @param viewName The name of the column in the associated SRC_* view
046     */
047    public DefaultColumn(String name, String type, boolean isNullable, String viewName)
048    {
049        this._name = name;
050        this._type = type;
051        this._isNullable = isNullable;
052        this._viewName = viewName;
053    }
054    
055    /**
056     * Constructor
057     * @param name The name of the column (it will also be used for the associated SRC_* view)
058     * @param type The SQL type of the column
059     * @param isNullable <code>true</code> if it's nullable
060     */
061    public DefaultColumn(String name, String type, boolean isNullable)
062    {
063        this(name, type, isNullable, name);
064    }
065
066    @Override
067    public String getColumnDescription()
068    {
069        StringBuilder sb = new StringBuilder(_name);
070        sb.append(" ");
071        sb.append(_type);
072        
073        if (!_isNullable)
074        {
075            sb.append(" NOT NULL");
076        }
077        
078        return sb.toString();
079    }
080
081    @Override
082    public String getViewDescription()
083    {
084        if (_viewName == null)
085        {
086            return StringUtils.EMPTY;
087        }
088        
089        StringBuilder sb = new StringBuilder();
090        sb.append("TBL.");
091        sb.append(_name);
092        sb.append(" AS ");
093        sb.append(_viewName);
094        
095        return sb.toString();
096    }
097
098    @Override
099    public List<ValuedQueryParameter> getViewParameters()
100    {
101        return new ArrayList<>();
102    }
103}