Skip to content
This repository was archived by the owner on Jun 29, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.Table;


/**
* Defines the context for configuring the type for a single column in a
* {@link ColumnTypingStrategy} session.
*/
public class ColumnTypingContext {

private final int columnIndex;

private final Table table;


/**
* Creates a context to conifgure a column for a specific table.
*
* @param table The table that contains the column
* @param columnIndex the index in the table of the column being configured.
*/
public ColumnTypingContext(final Table table, final int columnIndex) {
this.table = table;
this.columnIndex = columnIndex;
}


/**
* Creates a context a column to be configured.
*
* @param columnIndex the index in the table of the column being configured.
*/
public ColumnTypingContext(final int columnIndex) {
this(null, columnIndex);
}


/**
* Gets the index of the column being configured.
*
* @return the column index
*/
public int getColumnIndex() {
return columnIndex;
}


/**
* 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
*/
public Table getTable() {
return table;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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.
*/
ColumnType getNextColumnType(ColumnTypingContext ctx);

/**
* Ends the column typing session.
*/
@Override
default 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 getDefaultStrategy() {
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 getCustomStrategy(final 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 getCustomStrategy(final 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,73 @@
/**
* 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.util.SimpleTableDef;

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(final List<ColumnType> columnTypes) {
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(final ColumnType... columnTypes) {
this(Arrays.asList(columnTypes));
}


public CustomColumnTypingStrategy(final SimpleTableDef tableDef) {
this(Arrays.asList(tableDef.getColumnTypes()));
}


@Override
public ColumnTypingSession startColumnTypingSession() {
final Iterator<ColumnType> iterator = columnTypes.iterator();
return ctx -> {
if (iterator.hasNext()) {
return iterator.next();
}
return null;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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;

public class DefaultColumnTypingStrategy implements ColumnTypingStrategy {

private static final long serialVersionUID = 1L;


public DefaultColumnTypingStrategy() {
}


@Override
public ColumnTypingSession startColumnTypingSession() {
return ctx -> ColumnType.STRING;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/
/**
* API for column data type configuration
*/
package org.apache.metamodel.schema.typing;

Loading