## tables.BaseColumns


Base class for column enumeration-like access.


Usage

``` python
tables.BaseColumns(table)
```


Provides dictionary-like access to table columns.


## Attributes

| Name | Description |
|----|----|
| [detached](#detached) | Columns in the database that [restore_user_defined](tables.BaseColumns.md#mikeplus.tables.BaseColumns.restore_user_defined) can restore. |
| [user_defined](#user_defined) | Names of the user-defined columns currently shown in MIKE+. |

------------------------------------------------------------------------


#### detached


Columns in the database that [restore_user_defined](tables.BaseColumns.md#mikeplus.tables.BaseColumns.restore_user_defined) can restore.


`detached: list[str]`


These are database columns of the table that are neither standard MIKE+ columns nor currently user-defined, for example after [remove_user_defined](tables.BaseColumns.md#mikeplus.tables.BaseColumns.remove_user_defined).


------------------------------------------------------------------------


#### user_defined


Names of the user-defined columns currently shown in MIKE+.


`user_defined: list[str]`


## Methods

| Name | Description |
|----|----|
| [__contains__()](#__contains__) | Check for a column using case-insensitive matching. |
| [__getitem__()](#__getitem__) | Resolve a column name to its canonical MIKE+ casing. |
| [__init__()](#__init__) | Initialize with a reference to the parent table. |
| [__iter__()](#__iter__) | Make the columns iterable. |
| [add_user_defined()](#add_user_defined) | Add a user-defined column, like "Add user defined column" in MIKE+. |
| [db_types()](#db_types) | Map casefolded column names to their database types. |
| [remove_user_defined()](#remove_user_defined) | Remove a user-defined column, like "Remove column" in MIKE+. |
| [restore_user_defined()](#restore_user_defined) | Restore a detached column with its existing data type and data. |

------------------------------------------------------------------------


#### \_\_contains\_\_()


Check for a column using case-insensitive matching.


Usage

``` python
__contains__(item)
```


##### Parameters


`item: str`  
Column name in any casing.


##### Returns


`bool`  
Whether a matching column exists.


------------------------------------------------------------------------


#### \_\_getitem\_\_()


Resolve a column name to its canonical MIKE+ casing.


Usage

``` python
__getitem__(column_name)
```


##### Parameters


`column_name: str`  
Column name in any casing.


##### Returns


`str`  
The canonical MIKE+ column name.


##### Raises


`KeyError`  
If no column matches `column_name`.


------------------------------------------------------------------------


#### \_\_init\_\_()


Initialize with a reference to the parent table.


Usage

``` python
__init__(table)
```


##### Parameters


`table: BaseTable`  
Reference to the parent BaseTable instance


------------------------------------------------------------------------


#### \_\_iter\_\_()


Make the columns iterable.


Usage

``` python
__iter__()
```


##### Returns


`Iterator[str]`  
Iterator over column names


------------------------------------------------------------------------


#### add_user_defined()


Add a user-defined column, like "Add user defined column" in MIKE+.


Usage

``` python
add_user_defined(name, data_type, header=None)
```


Safe to call repeatedly: the column is created if it doesn't exist, restored if it is in [detached](tables.BaseColumns.md#mikeplus.tables.BaseColumns.detached), and left unchanged if it is already user-defined.


##### Parameters


`name: str`  
Field name of the column. Case-insensitive: an existing or detached column matches regardless of casing. A new or restored column is stored with the casing given here.

`data_type: str`  
One of 'integer', 'double', 'string', 'datetime', in any casing. If the column already exists, it must match the existing type.

`header: str or None = None`  
Header shown in the MIKE+ GUI. Defaults to [name](tables.BaseTable.md#mikeplus.tables.BaseTable.name). Ignored if the column is already user-defined.


##### Returns


`str`  
MUID of the column's record in `m_UserDefinedColumn`, the same whether the column was created, restored or already there.


##### Raises


`ValueError`  
If [name](tables.BaseTable.md#mikeplus.tables.BaseTable.name) is a standard MIKE+ column, or if `data_type` is invalid or doesn't match the existing column.


##### Examples

``` python
>>> pipes = db.tables.msm_Link
>>> pipes.columns.add_user_defined("install_year", "integer")
```

'udf_1'

------------------------------------------------------------------------


#### db_types()


Map casefolded column names to their database types.


Usage

``` python
db_types()
```


Read from the .NET table on each call, unlike the cached names, so user-defined columns added after construction are included.


##### Returns


`dict[str, DbType]`  
Casefolded column names mapped to their `DbType`.


------------------------------------------------------------------------


#### remove_user_defined()


Remove a user-defined column, like "Remove column" in MIKE+.


Usage

``` python
remove_user_defined(name)
```


The column is no longer shown in MIKE+, but it stays in the database with its data and appears in [detached](tables.BaseColumns.md#mikeplus.tables.BaseColumns.detached). Restore it with [restore_user_defined](tables.BaseColumns.md#mikeplus.tables.BaseColumns.restore_user_defined).


##### Parameters


`name: str`  
Field name of the column. Case-insensitive.


##### Returns


`str`  
MUID of the removed `m_UserDefinedColumn` record.


##### Raises


`KeyError`  
If [name](tables.BaseTable.md#mikeplus.tables.BaseTable.name) isn't a user-defined column of this table.


##### Examples

``` python
>>> pipes = db.tables.msm_Link
>>> pipes.columns.add_user_defined("install_year", "integer")
```

'udf_1'

``` python
>>> pipes.columns.remove_user_defined("install_year")
```

'udf_1'

``` python
>>> pipes.columns.detached
```

\['install_year'\]

------------------------------------------------------------------------


#### restore_user_defined()


Restore a detached column with its existing data type and data.


Usage

``` python
restore_user_defined(name)
```


##### Parameters


`name: str`  
Field name of a column in [detached](tables.BaseColumns.md#mikeplus.tables.BaseColumns.detached). Case-insensitive; the restored column is stored with the casing given here.


##### Returns


`str`  
MUID of the column's new record in `m_UserDefinedColumn`.


##### Raises


`KeyError`  
If [name](tables.BaseTable.md#mikeplus.tables.BaseTable.name) isn't in [detached](tables.BaseColumns.md#mikeplus.tables.BaseColumns.detached).


##### Examples

``` python
>>> pipes = db.tables.msm_Link
>>> pipes.columns.add_user_defined("install_year", "integer")
```

'udf_1'

``` python
>>> pipes.columns.remove_user_defined("install_year")
```

'udf_1'

``` python
>>> pipes.columns.restore_user_defined("install_year")
```

'udf_2'
