-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PostgreSQL Connector implementation (#53)
* PostgreSQL Connector implementation * Removing the old Postgres connector
- Loading branch information
1 parent
f4f4a95
commit 35c5c57
Showing
40 changed files
with
1,662 additions
and
54,133 deletions.
There are no files selected for viewing
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
158 changes: 158 additions & 0 deletions
158
...at-connector-jdbc/src/main/java/com/netflix/metacat/connector/jdbc/JdbcTypeConverter.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,158 @@ | ||
/* | ||
* | ||
* Copyright 2017 Netflix, Inc. | ||
* | ||
* 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 | ||
* | ||
* https://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 com.netflix.metacat.connector.jdbc; | ||
|
||
import com.netflix.metacat.common.server.connectors.ConnectorTypeConverter; | ||
import com.netflix.metacat.common.type.BaseType; | ||
import com.netflix.metacat.common.type.CharType; | ||
import com.netflix.metacat.common.type.DecimalType; | ||
import com.netflix.metacat.common.type.Type; | ||
import com.netflix.metacat.common.type.VarbinaryType; | ||
import com.netflix.metacat.common.type.VarcharType; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Arrays; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Type converter utilities for JDBC connectors. | ||
* | ||
* @author tgianos | ||
* @since 0.1.52 | ||
*/ | ||
@Slf4j | ||
public abstract class JdbcTypeConverter implements ConnectorTypeConverter { | ||
|
||
private static final Pattern TYPE_PATTERN = Pattern.compile( | ||
"^\\s*?" | ||
+ "(\\w+(?:\\s(?:precision|varying))?)" | ||
+ "\\s*?(\\[\\](?:\\[\\])?)?" | ||
+ "\\s*?" | ||
+ "(?:\\(\\s*?(\\d+)(?:\\s*?,\\s*?(\\d+))?\\s*?\\))?" | ||
+ "(?:\\s*?(\\w+(?:\\s\\w+)*))?$" | ||
); | ||
|
||
protected String[] splitType(final String type) { | ||
final Matcher matcher = TYPE_PATTERN.matcher(type); | ||
final int numGroups = matcher.groupCount(); | ||
if (matcher.find()) { | ||
final String[] split = new String[numGroups]; | ||
for (int i = 0; i < numGroups; i++) { | ||
split[i] = matcher.group(i + 1); | ||
} | ||
return split; | ||
} else { | ||
throw new IllegalArgumentException("Unable to parse " + type); | ||
} | ||
} | ||
|
||
protected Type toMetacatBitType(@Nonnull final String[] bit) { | ||
// No size parameter | ||
if (bit[2] == null || Integer.parseInt(bit[2]) == 1) { | ||
return BaseType.BOOLEAN; | ||
} else { | ||
final int bytes = (int) Math.ceil(Double.parseDouble(bit[2]) / 8.0); | ||
return VarbinaryType.createVarbinaryType(bytes); | ||
} | ||
|
||
|
||
} | ||
|
||
protected DecimalType toMetacatDecimalType(@Nonnull final String[] splitType) { | ||
if (splitType[2] == null && splitType[3] == null) { | ||
return DecimalType.createDecimalType(); | ||
} else if (splitType[2] != null) { | ||
final int precision = Integer.parseInt(splitType[2]); | ||
if (splitType[3] == null) { | ||
return DecimalType.createDecimalType(precision); | ||
} else { | ||
return DecimalType.createDecimalType(precision, Integer.parseInt(splitType[3])); | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Illegal definition of a decimal type: " + Arrays.toString(splitType)); | ||
} | ||
} | ||
|
||
protected Type toMetacatCharType(@Nonnull final String[] splitType) { | ||
if (splitType[2] == null) { | ||
throw new IllegalArgumentException("Must have size for char type"); | ||
} | ||
|
||
final int size = Integer.parseInt(splitType[2]); | ||
// Check if we're dealing with binary or not | ||
if (splitType[4] != null) { | ||
if (!splitType[4].equals("binary")) { | ||
throw new IllegalArgumentException( | ||
"Unrecognized extra field in char type: " + splitType[4] + ". Expected 'binary'." | ||
); | ||
} | ||
return VarbinaryType.createVarbinaryType(size); | ||
} else { | ||
return CharType.createCharType(size); | ||
} | ||
} | ||
|
||
protected Type toMetacatVarcharType(@Nonnull final String[] splitType) { | ||
if (splitType[2] == null) { | ||
throw new IllegalArgumentException("Must have size for varchar type"); | ||
} | ||
|
||
final int size = Integer.parseInt(splitType[2]); | ||
// Check if we're dealing with binary or not | ||
if (splitType[4] != null) { | ||
if (!splitType[4].equals("binary")) { | ||
throw new IllegalArgumentException( | ||
"Unrecognized extra field in varchar type: " + splitType[4] + ". Expected 'binary'." | ||
); | ||
} | ||
return VarbinaryType.createVarbinaryType(size); | ||
} else { | ||
return VarcharType.createVarcharType(size); | ||
} | ||
} | ||
|
||
protected VarbinaryType toMetacatVarbinaryType(@Nonnull final String[] splitType) { | ||
if (!splitType[0].equals("varbinary") && !splitType[0].equals("binary")) { | ||
// Blob | ||
return VarbinaryType.createVarbinaryType(Integer.MAX_VALUE); | ||
} | ||
if (splitType[2] == null) { | ||
throw new IllegalArgumentException("Must have size for varbinary type"); | ||
} | ||
|
||
return VarbinaryType.createVarbinaryType(Integer.parseInt(splitType[2])); | ||
} | ||
|
||
protected Type toMetacatTimeType(@Nonnull final String[] splitType) { | ||
if (splitType[4] != null && splitType[4].equals("with time zone")) { | ||
return BaseType.TIME_WITH_TIME_ZONE; | ||
} else { | ||
return BaseType.TIME; | ||
} | ||
} | ||
|
||
protected Type toMetacatTimestampType(@Nonnull final String[] splitType) { | ||
if (splitType[4] != null && splitType[4].equals("with time zone")) { | ||
return BaseType.TIMESTAMP_WITH_TIME_ZONE; | ||
} else { | ||
return BaseType.TIMESTAMP; | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
dependencies { | ||
compile project(":metacat-connector-jdbc") | ||
compile ("mysql:mysql-connector-java:${mysql_connector_version}") | ||
runtime ("mysql:mysql-connector-java:${mysql_connector_version}") | ||
} |
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.