aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/parallelai/spyglass/jdbc/TableDesc.java
blob: b0aaea7a080d25d2c36b27afb25e59c262117d08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2009 Concurrent, Inc.
 *
 * This work has been released into the public domain
 * by the copyright holder. This applies worldwide.
 *
 * In case this is not legally possible:
 * The copyright holder grants any entity the right
 * to use this work for any purpose, without any
 * conditions, unless such conditions are required by law.
 */

package parallelai.spyglass.jdbc;

import cascading.util.Util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Class TableDesc describes a SQL based table, this description is used by the {@link JDBCTap} when
 * creating a missing table.
 *
 * @see JDBCTap
 * @see JDBCScheme
 */
public class TableDesc implements Serializable {
    /** Field tableName */
    String tableName;
    /** Field columnNames */
    String[] columnNames;
    /** Field columnDefs */
    String[] columnDefs;
    /** Field primaryKeys */
    String[] primaryKeys;

    /**
     * Constructor TableDesc creates a new TableDesc instance.
     *
     * @param tableName of type String
     */
    public TableDesc( String tableName ) {
        this.tableName = tableName;
    }

    /**
     * Constructor TableDesc creates a new TableDesc instance.
     *
     * @param tableName   of type String
     * @param columnNames of type String[]
     * @param columnDefs  of type String[]
     * @param primaryKeys of type String
     */
    public TableDesc( String tableName, String[] columnNames, String[] columnDefs, String[] primaryKeys ) {
        this.tableName = tableName;
        this.columnNames = columnNames;
        this.columnDefs = columnDefs;
        this.primaryKeys = primaryKeys;
    }

    public String getTableName() {
        return tableName;
    }

    public String[] getColumnNames() {
        return columnNames;
    }

    public String[] getColumnDefs() {
        return columnDefs;
    }

    public String[] getPrimaryKeys() {
        return primaryKeys;
    }

    /**
     * Method getTableCreateStatement returns the tableCreateStatement of this TableDesc object.
     *
     * @return the tableCreateStatement (type String) of this TableDesc object.
     */
    public String getCreateTableStatement() {
        List<String> createTableStatement = new ArrayList<String>();

        createTableStatement = addCreateTableBodyTo( createTableStatement );

        return String.format( getCreateTableFormat(), tableName, Util.join( createTableStatement, ", " ) );
    }

    protected List<String> addCreateTableBodyTo( List<String> createTableStatement ) {
        createTableStatement = addDefinitionsTo( createTableStatement );
        createTableStatement = addPrimaryKeyTo( createTableStatement );

        return createTableStatement;
    }

    protected String getCreateTableFormat() {
        return "CREATE TABLE %s ( %s )";
    }

    protected List<String> addDefinitionsTo( List<String> createTableStatement ) {
        for( int i = 0; i < columnNames.length; i++ ) {
            String columnName = columnNames[ i ];
            String columnDef = columnDefs[ i ];

            createTableStatement.add( columnName + " " + columnDef );
        }

        return createTableStatement;
    }

    protected List<String> addPrimaryKeyTo( List<String> createTableStatement ) {
        if( hasPrimaryKey() )
            createTableStatement.add( String.format( "PRIMARY KEY( %s )", Util.join( primaryKeys, ", " ) ) );

        return createTableStatement;
    }

    /**
     * Method getTableDropStatement returns the tableDropStatement of this TableDesc object.
     *
     * @return the tableDropStatement (type String) of this TableDesc object.
     */
    public String getTableDropStatement() {
        return String.format( getDropTableFormat(), tableName );
    }

    protected String getDropTableFormat() {
        return "DROP TABLE %s";
    }

    /**
     * Method getTableExistsQuery returns the tableExistsQuery of this TableDesc object.
     *
     * @return the tableExistsQuery (type String) of this TableDesc object.
     */
    public String getTableExistsQuery() {
        return String.format( "select 1 from %s where 1 = 0", tableName );
    }

    private boolean hasPrimaryKey() {
        return primaryKeys != null && primaryKeys.length != 0;
    }

    @Override
    public String toString() {
        return "TableDesc{" + "tableName='" + tableName + '\'' + ", columnNames=" + ( columnNames == null ? null : Arrays.asList( columnNames ) ) + ", columnDefs=" + ( columnDefs == null ? null : Arrays.asList( columnDefs ) ) + ", primaryKeys=" + ( primaryKeys == null ? null : Arrays.asList( primaryKeys ) ) + '}';
    }

    @Override
    public boolean equals( Object object ) {
        if( this == object )
            return true;
        if( !( object instanceof TableDesc ) )
            return false;

        TableDesc tableDesc = (TableDesc) object;

        if( !Arrays.equals( columnDefs, tableDesc.columnDefs ) )
            return false;
        if( !Arrays.equals( columnNames, tableDesc.columnNames ) )
            return false;
        if( !Arrays.equals( primaryKeys, tableDesc.primaryKeys ) )
            return false;
        if( tableName != null ? !tableName.equals( tableDesc.tableName ) : tableDesc.tableName != null )
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = tableName != null ? tableName.hashCode() : 0;
        result = 31 * result + ( columnNames != null ? Arrays.hashCode( columnNames ) : 0 );
        result = 31 * result + ( columnDefs != null ? Arrays.hashCode( columnDefs ) : 0 );
        result = 31 * result + ( primaryKeys != null ? Arrays.hashCode( primaryKeys ) : 0 );
        return result;
    }
}