001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.oozie.store; 020 021import java.sql.Blob; 022import java.sql.Timestamp; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import org.apache.oozie.util.db.Schema; 029import org.apache.oozie.util.db.Schema.Column; 030import org.apache.oozie.util.db.Schema.DBType; 031import org.apache.oozie.util.db.Schema.Index; 032import org.apache.oozie.util.db.Schema.Table; 033 034public class OozieSchema { 035 036 private static String oozieDbName; 037 038 private static final String OOZIE_VERSION = "0.1"; 039 040 public static final Map<Table, List<Column>> TABLE_COLUMNS = new HashMap<Table, List<Column>>(); 041 042 static { 043 for (Column column : OozieColumn.values()) { 044 List<Column> tColumns = TABLE_COLUMNS.get(column.table()); 045 if (tColumns == null) { 046 tColumns = new ArrayList<Column>(); 047 TABLE_COLUMNS.put(column.table(), tColumns); 048 } 049 tColumns.add(column); 050 } 051 } 052 053 public static void setOozieDbName(String dbName) { 054 oozieDbName = dbName; 055 } 056 057 public static enum OozieTable implements Table { 058 WORKFLOWS, 059 ACTIONS, 060 WF_PROCESS_INSTANCE, 061 VERSION; 062 063 @Override 064 public String toString() { 065 return oozieDbName + "." + name().toUpperCase(); 066 } 067 } 068 069 public static enum OozieIndex implements Index { 070 IDX_WF_APPNAME(OozieColumn.WF_appName), 071 IDX_WF_USER(OozieColumn.WF_userName), 072 IDX_WF_GROUP(OozieColumn.WF_groupName), 073 IDX_WF_STATUS(OozieColumn.WF_status), 074 IDX_WF_EXTERNAL_ID(OozieColumn.WF_externalId), 075 076 IDX_ACTIONS_BEGINTIME(OozieColumn.ACTIONS_pendingAge), 077 IDX_ACTIONS_WFID(OozieColumn.ACTIONS_wfId); 078 079 final Column column; 080 081 OozieIndex(Column column) { 082 this.column = column; 083 } 084 085 public Column column() { 086 return column; 087 } 088 } 089 090 public static enum OozieColumn implements Column { 091 // Process Instance Table 092 PI_wfId(OozieTable.WF_PROCESS_INSTANCE, String.class, true, 100), 093 PI_state(OozieTable.WF_PROCESS_INSTANCE, Blob.class, false), 094 095 // WorkflowJob Table 096 WF_id(OozieTable.WORKFLOWS, String.class, true, 100), 097 WF_externalId(OozieTable.WORKFLOWS, String.class, false, 100), 098 WF_appName(OozieTable.WORKFLOWS, String.class, false, 100), 099 WF_appPath(OozieTable.WORKFLOWS, String.class, false, 255), 100 WF_conf(OozieTable.WORKFLOWS, String.class, false), 101 WF_protoActionConf(OozieTable.WORKFLOWS, String.class, false), 102 WF_logToken(OozieTable.WORKFLOWS, String.class, false, 100), 103 WF_status(OozieTable.WORKFLOWS, String.class, false, 100), 104 WF_run(OozieTable.WORKFLOWS, Long.class, false), 105 WF_lastModTime(OozieTable.WORKFLOWS, Timestamp.class, false), 106 WF_createdTime(OozieTable.WORKFLOWS, Timestamp.class, false), 107 WF_startTime(OozieTable.WORKFLOWS, Timestamp.class, false), 108 WF_endTime(OozieTable.WORKFLOWS, Timestamp.class, false), 109 WF_userName(OozieTable.WORKFLOWS, String.class, false, 100), 110 WF_groupName(OozieTable.WORKFLOWS, String.class, false, 100), 111 WF_authToken(OozieTable.WORKFLOWS, String.class, false), 112 113 // Actions Table 114 ACTIONS_id(OozieTable.ACTIONS, String.class, true, 100), 115 ACTIONS_name(OozieTable.ACTIONS, String.class, false, 100), 116 ACTIONS_type(OozieTable.ACTIONS, String.class, false, 100), 117 ACTIONS_wfId(OozieTable.ACTIONS, String.class, false, 100), 118 ACTIONS_conf(OozieTable.ACTIONS, String.class, false), 119 ACTIONS_status(OozieTable.ACTIONS, String.class, false, 100), 120 ACTIONS_externalStatus(OozieTable.ACTIONS, String.class, false, 100), 121 ACTIONS_errorCode(OozieTable.ACTIONS, String.class, false, 100), 122 ACTIONS_errorMessage(OozieTable.ACTIONS, String.class, false), 123 ACTIONS_transition(OozieTable.ACTIONS, String.class, false, 100), 124 ACTIONS_retries(OozieTable.ACTIONS, Long.class, false), 125 ACTIONS_startTime(OozieTable.ACTIONS, Timestamp.class, false), 126 ACTIONS_endTime(OozieTable.ACTIONS, Timestamp.class, false), 127 ACTIONS_lastCheckTime(OozieTable.ACTIONS, Timestamp.class, false), 128 ACTIONS_data(OozieTable.ACTIONS, String.class, false), 129 ACTIONS_externalId(OozieTable.ACTIONS, String.class, false, 100), 130 ACTIONS_trackerUri(OozieTable.ACTIONS, String.class, false, 100), 131 ACTIONS_consoleUrl(OozieTable.ACTIONS, String.class, false, 100), 132 ACTIONS_executionPath(OozieTable.ACTIONS, String.class, false, 255), 133 ACTIONS_pending(OozieTable.ACTIONS, Boolean.class, false), 134 ACTIONS_pendingAge(OozieTable.ACTIONS, Timestamp.class, false), 135 ACTIONS_signalValue(OozieTable.ACTIONS, String.class, false, 100), 136 ACTIONS_logToken(OozieTable.ACTIONS, String.class, false, 100), 137 138 // Version Table 139 VER_versionNumber(OozieTable.VERSION, String.class, false, 255); 140 141 final Table table; 142 final Class<?> type; 143 int length = -1; 144 final boolean isPrimaryKey; 145 146 OozieColumn(Table table, Class<?> type, boolean isPrimaryKey) { 147 this(table, type, isPrimaryKey, -1); 148 } 149 150 OozieColumn(Table table, Class<?> type, boolean isPrimaryKey, int length) { 151 this.table = table; 152 this.type = type; 153 this.isPrimaryKey = isPrimaryKey; 154 this.length = length; 155 } 156 157 private String getName() { 158 String tName = table.name(); 159 return tName + "." + columnName(); 160 } 161 162 public String columnName() { 163 return name().split("_")[1].toLowerCase(); 164 } 165 166 @Override 167 public String toString() { 168 return getName(); 169 } 170 171 public Table table() { 172 return table; 173 } 174 175 public Class<?> getType() { 176 return type; 177 } 178 179 public int getLength() { 180 return length; 181 } 182 183 public String asLabel() { 184 return name().toUpperCase(); 185 } 186 187 public boolean isPrimaryKey() { 188 return isPrimaryKey; 189 } 190 } 191 192 /** 193 * Generates the create table SQL Statement 194 * 195 * @param table 196 * @param dbType 197 * @return SQL Statement to create the table 198 */ 199 public static String generateCreateTableScript(Table table, DBType dbType) { 200 return Schema.generateCreateTableScript(table, dbType, TABLE_COLUMNS.get(table)); 201 } 202 203 /** 204 * Gets the query that will be used to validate the connection 205 * 206 * @param dbName 207 * @return 208 */ 209 public static String getValidationQuery(String dbName) { 210 return "select count(" + OozieColumn.VER_versionNumber.columnName() + ") from " + dbName + "." 211 + OozieTable.VERSION.name().toUpperCase(); 212 } 213 214 /** 215 * Generates the Insert statement to insert the OOZIE_VERSION to table 216 * 217 * @param dbName 218 * @return 219 */ 220 public static String generateInsertVersionScript(String dbName) { 221 return "INSERT INTO " + dbName + "." + OozieTable.VERSION.name().toUpperCase() + "(" 222 + OozieColumn.VER_versionNumber.columnName() + ") VALUES(" + OOZIE_VERSION + ")"; 223 } 224 225 /** 226 * Gets the Oozie Schema Version 227 * 228 * @return 229 */ 230 public static String getOozieVersion() { 231 return OOZIE_VERSION; 232 } 233}