-
Notifications
You must be signed in to change notification settings - Fork 150
METAMODEL-1225 Add ColumnTypingStrategies and use them in CSV module. #240
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
| import org.apache.metamodel.schema.ColumnType; | ||
| import org.apache.metamodel.schema.Table; | ||
|
|
||
| /** | ||
| * Defines the context for configuring the type for a single column in a | ||
| * {@link ColumnTypingStrategy} session. | ||
| */ | ||
| public interface ColumnTypingContext { | ||
|
||
|
|
||
| /** | ||
| * Gets the index of the column being configured. | ||
| * | ||
| * @return the column index | ||
| */ | ||
| int getColumnIndex(); | ||
|
|
||
| /** | ||
| * Gets the {@link Table} that the column is to pertain to. If the table is | ||
| * not yet available then this may return null. | ||
| * | ||
| * @return the associated table | ||
| */ | ||
| Table getTable(); | ||
|
|
||
| /** | ||
| * Gets the intrinsic column type, if this is defined in the datastore | ||
| * itself. This may be in the form of a header or such. | ||
| * | ||
| * @return the column type representing the datastore's column type | ||
| */ | ||
| ColumnType getIntrinsicColumnType(); | ||
kaspersorensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
| import org.apache.metamodel.schema.ColumnType; | ||
|
||
| import org.apache.metamodel.schema.Table; | ||
|
|
||
|
|
||
| /** | ||
| * An implementation of {@link ColumnTypingContext} that holds necessary context about the column being configured. | ||
| */ | ||
| public class ColumnTypingContextImpl implements ColumnTypingContext { | ||
|
|
||
| private final int columnIndex; | ||
|
|
||
| private final Table table; | ||
|
|
||
| private final ColumnType intrinsicColumnType; | ||
|
|
||
|
|
||
| /** | ||
| * Creates a context to conifgure a column for a specific table. | ||
| * @param table The table that contains the column | ||
| * @param intrinsicColumnType The column type that represents the type configured in the datastore | ||
| * @param columnIndex the index in the table of the column being configured. | ||
| */ | ||
| public ColumnTypingContextImpl( Table table, ColumnType intrinsicColumnType, int columnIndex ) { | ||
| this.table = table; | ||
| this.intrinsicColumnType = intrinsicColumnType; | ||
| this.columnIndex = columnIndex; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Creates a context a column to be configured. | ||
| * @param columnIndex the index in the table of the column being configured. | ||
| */ | ||
| public ColumnTypingContextImpl( int columnIndex ) { | ||
| this( null, null, columnIndex ); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public int getColumnIndex() { | ||
| return columnIndex; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public Table getTable() { | ||
| return table; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public ColumnType getIntrinsicColumnType() { | ||
| return intrinsicColumnType; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
|
|
||
| import org.apache.metamodel.schema.Column; | ||
| import org.apache.metamodel.schema.ColumnType; | ||
| import org.apache.metamodel.schema.Table; | ||
|
|
||
| import java.io.Closeable; | ||
|
|
||
| /** | ||
| * Represents a 'session' in which a single {@link Table}'s {@link Column}s are | ||
| * assigned {@link ColumnType}s. | ||
| */ | ||
| public interface ColumnTypingSession extends Closeable { | ||
|
|
||
| /** | ||
| * Provides the type to apply for a given column. | ||
| * | ||
| * @param ctx the context of the column naming taking place. This contains | ||
| * column index, intrinsic type etc. if available. | ||
| * @return the type to provide to the column. | ||
| */ | ||
| public ColumnType getNextColumnType( ColumnTypingContext ctx ); | ||
|
|
||
| /** | ||
| * Ends the column typing session. | ||
| */ | ||
| @Override | ||
| void close(); | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
| import org.apache.metamodel.schema.ColumnType; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Constructors and common utilities for {@link ColumnTypingStrategy} objects. | ||
| */ | ||
| public class ColumnTypingStrategies { | ||
|
|
||
| private static final DefaultColumnTypingStrategy DEFAULT_STRATEGY = new DefaultColumnTypingStrategy(); | ||
|
|
||
|
|
||
| private ColumnTypingStrategies() { | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * The default strategy assumes all column types are text. | ||
| * | ||
| * @return a strategy that defaults to text | ||
| */ | ||
| public static ColumnTypingStrategy defaultStrategy() { | ||
|
||
| return DEFAULT_STRATEGY; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Utility to create a {@link CustomColumnTypingStrategy}. | ||
| * | ||
| * @param columnTypes the types of each column | ||
| * @return a strategy for custom type configuration | ||
| */ | ||
| public static ColumnTypingStrategy customTypes( List<ColumnType> columnTypes ) { | ||
| return new CustomColumnTypingStrategy( columnTypes ); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Utility to create a {@link CustomColumnTypingStrategy}. | ||
| * | ||
| * @param columnTypes the types of each column | ||
| * @return a strategy for custom type configuration | ||
| */ | ||
| public static ColumnTypingStrategy customTypes( ColumnType... columnTypes ) { | ||
| return new CustomColumnTypingStrategy( columnTypes ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
|
|
||
| import java.io.Serializable; | ||
|
|
||
| /** | ||
| * A strategy that defines the data types of columns. Such strategies are | ||
| * mostly used when a particular datastore is not itself intrinsically | ||
| * specifying the column type. | ||
| */ | ||
| public interface ColumnTypingStrategy extends Serializable { | ||
|
|
||
| ColumnTypingSession startColumnTypingSession(); | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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.apache.metamodel.schema.typing; | ||
|
|
||
| import org.apache.metamodel.schema.ColumnType; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * A {@link ColumnTypingStrategy} that allows the user to supply his own list of column types | ||
| */ | ||
| public class CustomColumnTypingStrategy implements ColumnTypingStrategy { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final List<ColumnType> columnTypes; | ||
|
|
||
|
|
||
| /** | ||
| * Creates the strategy based on the provided types. | ||
| * | ||
| * @param columnTypes a list of column types to be applied to a table. | ||
| */ | ||
| public CustomColumnTypingStrategy( List<ColumnType> columnTypes ) { | ||
kaspersorensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.columnTypes = columnTypes; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Creates the strategy based on the provided types. | ||
| * | ||
| * @param columnTypes a list of column types to be applied to a table. | ||
| */ | ||
| public CustomColumnTypingStrategy( ColumnType... columnTypes ) { | ||
| this( Arrays.asList( columnTypes ) ); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public ColumnTypingSession startColumnTypingSession() { | ||
| final Iterator<ColumnType> iterator = columnTypes.iterator(); | ||
| return new ColumnTypingSession() { | ||
|
|
||
| @Override | ||
| public ColumnType getNextColumnType( ColumnTypingContext ctx ) { | ||
| if ( iterator.hasNext() ) { | ||
| return iterator.next(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
| }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The import org.apache.metamodel.schema.ColumnType is never used.