Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4895 from greenplum-db/external-table-ddl-creatio…
…n-162776271 GP: Add DDL generation for Greenplum's concept of external tables
- Loading branch information
Showing
9 changed files
with
643 additions
and
13 deletions.
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
...eaver.ext.greenplum/src/org/jkiss/dbeaver/ext/greenplum/model/GreenplumExternalTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* DBeaver - Universal Database Manager | ||
* Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) | ||
* Copyright (C) 2019 Dmitriy Dubson (ddubson@pivotal.io) | ||
* Copyright (C) 2019 Gavin Shaw (gshaw@pivotal.io) | ||
* Copyright (C) 2019 Zach Marcin (zmarcin@pivotal.io) | ||
* Copyright (C) 2019 Nikhil Pawar (npawar@pivotal.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jkiss.dbeaver.ext.greenplum.model; | ||
|
||
import org.jkiss.dbeaver.DBException; | ||
import org.jkiss.dbeaver.ext.postgresql.model.PostgreSchema; | ||
import org.jkiss.dbeaver.ext.postgresql.model.PostgreTableColumn; | ||
import org.jkiss.dbeaver.ext.postgresql.model.PostgreTableRegular; | ||
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils; | ||
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; | ||
|
||
import java.sql.ResultSet; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class GreenplumExternalTable extends PostgreTableRegular { | ||
public enum FormatType { | ||
c("CSV"), | ||
t("TEXT"); | ||
|
||
private String formatType; | ||
|
||
FormatType(String formatTypeString) { | ||
this.formatType = formatTypeString; | ||
} | ||
|
||
public String getValue() { | ||
return formatType; | ||
} | ||
} | ||
|
||
public enum RejectLimitType { | ||
r("ROWS"); | ||
|
||
private String rejectLimitType; | ||
|
||
RejectLimitType(String rejectLimitTypeString) { | ||
this.rejectLimitType = rejectLimitTypeString; | ||
} | ||
|
||
public String getValue() { | ||
return rejectLimitType; | ||
} | ||
} | ||
|
||
private final List<String> uriLocations; | ||
private final String execLocation; | ||
private final FormatType formatType; | ||
private final String formatOptions; | ||
private final String encoding; | ||
private final RejectLimitType rejectLimitType; | ||
private final int rejectLimit; | ||
|
||
public GreenplumExternalTable(PostgreSchema catalog, ResultSet dbResult) { | ||
super(catalog, dbResult); | ||
this.uriLocations = Arrays.asList(JDBCUtils.safeGetString(dbResult, "urilocation") | ||
.trim().split(",")); | ||
this.execLocation = JDBCUtils.safeGetString(dbResult, "execlocation"); | ||
this.formatType = FormatType.valueOf(JDBCUtils.safeGetString(dbResult, "fmttype")); | ||
this.formatOptions = JDBCUtils.safeGetString(dbResult, "fmtopts"); | ||
this.encoding = JDBCUtils.safeGetString(dbResult, "encoding"); | ||
|
||
this.rejectLimit = JDBCUtils.safeGetInt(dbResult, "rejectlimit"); | ||
String rejectlimittype = JDBCUtils.safeGetString(dbResult, "rejectlimittype"); | ||
if (rejectlimittype != null && rejectlimittype.length() > 0) { | ||
this.rejectLimitType = RejectLimitType.valueOf(rejectlimittype); | ||
} else { | ||
this.rejectLimitType = null; | ||
} | ||
|
||
} | ||
|
||
public List<String> getUriLocations() { | ||
return uriLocations; | ||
} | ||
|
||
public String getExecLocation() { | ||
return execLocation; | ||
} | ||
|
||
public FormatType getFormatType() { | ||
return formatType; | ||
} | ||
|
||
public String getFormatOptions() { | ||
return formatOptions; | ||
} | ||
|
||
public String getEncoding() { | ||
return encoding; | ||
} | ||
|
||
public RejectLimitType getRejectLimitType() { | ||
return rejectLimitType; | ||
} | ||
|
||
public int getRejectLimit() { | ||
return rejectLimit; | ||
} | ||
|
||
public String generateDDL(DBRProgressMonitor monitor) throws DBException { | ||
StringBuilder ddlBuilder = new StringBuilder("CREATE EXTERNAL TABLE " + this.getDatabase().getName() | ||
+ "." + this.getSchema().getName() + "." + this.getName() + " (\n"); | ||
|
||
List<PostgreTableColumn> tableColumns = this.getAttributes(monitor) | ||
.stream() | ||
.filter(field -> field.getOrdinalPosition() >= 0) | ||
.collect(Collectors.toList()); | ||
|
||
if (tableColumns.size() == 1) { | ||
PostgreTableColumn column = tableColumns.get(0); | ||
ddlBuilder.append("\t" + column.getName() + " " + column.getTypeName() + "\n)\n"); | ||
} else { | ||
ddlBuilder.append(tableColumns | ||
.stream() | ||
.map(field -> "\t" + field.getName() + " " + field.getTypeName()) | ||
.collect(Collectors.joining(",\n"))); | ||
ddlBuilder.append("\n)\n"); | ||
} | ||
|
||
if (this.getUriLocations() != null && !this.getUriLocations().isEmpty()) { | ||
ddlBuilder.append("LOCATION (\n"); | ||
|
||
ddlBuilder.append(this.getUriLocations() | ||
.stream() | ||
.map(location -> "\t'" + location + "'") | ||
.collect(Collectors.joining(",\n"))); | ||
|
||
ddlBuilder.append("\n) " + determineExecutionLocation() + "\n"); | ||
} | ||
|
||
ddlBuilder.append("FORMAT '" + this.getFormatType().getValue() + "' ( " + this.getFormatOptions() + " )"); | ||
|
||
if (this.getEncoding() != null && this.getEncoding().length() > 0) { | ||
ddlBuilder.append("\nENCODING '" + this.getEncoding() + "'"); | ||
} | ||
|
||
if (this.getRejectLimit() > 0 && this.getRejectLimitType() != null) { | ||
ddlBuilder.append("\nSEGMENT REJECT LIMIT " + this.getRejectLimit() + " " + this.getRejectLimitType().getValue()); | ||
} | ||
|
||
return ddlBuilder.toString(); | ||
} | ||
|
||
private String determineExecutionLocation() { | ||
if (this.getExecLocation() != null && this.getExecLocation().equalsIgnoreCase("MASTER_ONLY")) { | ||
return "ON MASTER"; | ||
} | ||
|
||
return "ON ALL"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...dbeaver.ext.greenplum/src/org/jkiss/dbeaver/ext/greenplum/model/GreenplumSchemaCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...r.ext.greenplum/src/org/jkiss/dbeaver/ext/greenplum/model/GreenplumWithClauseBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.