# MindLake.DataLake

## 1. query() Method

Execute a SQL query or command.

{% hint style="info" %}
When referencing a table in your SQL query statement, you can specify the table owner with their wallet address, like this: `SELECT * FROM someWalletAddress.someTable`. If you omit the wallet address in your query, it defaults to your own wallet address. For example, `SELECT * FROM myTable` is equivalent to `SELECT * FROM myWalletAddress.myTable`.
{% endhint %}

{% hint style="info" %}
During testing, SQL queries will timeout after 5 minutes. Requests that take longer than that to process will not receive a response.
{% endhint %}

{% hint style="info" %}
To specify an upper-case or mixed-case name of table or column, you need to double-quote the name; else it will be folded to lower case.
{% endhint %}

```python
mindlake.datalake.query(executeSql: str) -> ResultType
# mindlake is an instance of MindLake
```

### Parameters

1. executeSql - `str`: the SQL statement used to query Mind Lake, such as `SELECT, INSERT, UPDATE, and DELETE`.&#x20;

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

* `data` - `dict`: The result of query execution.

  ```typescript
  {
    'columnList': [],
    'data': [][]
  }
  ```

  * columnList - `list<str>`: the column names in the specific order in which they appear.
  * data - `list<list<str>>`: The query data result is presented as a 2D array, with rows and columns.

### Example

```python
result = mind.datalake.query("SELECT token FROM test_table_enc")
assert result, result.message
print(result.data['columnList'][0])
for row in result.data['data']:
    result = mind.cryptor.decrypt(row[0])
    assert result, result.message
    print(result.data)
```

## 2. createTable() Method

Create a table in the user's own account in Mind Lake.

```python
mindlake.datalake.createTable(tableName: str, columns: list, primaryKey: list = None) -> ResultType
# mindlake is an instance of MindLake
```

### Parameters

1. `tableName` - `str`: the name of the table to be created.
2. `columns` - `list<`[`Column`](#3.-mindlake.datalake.column)`>`: defines the columns in the table. Each column should be defined as a [`MindLake.DataLake.Column`](#3.-mindlake.datalake.column) object.
3. `primaryKey` (Optional) - `list<str>`: defines the table's primary key by specifying one or more columns. Each column in the primary key should be identified by its column name.\
   If the `primaryKey` parameter is omitted, no primary key will be defined for the table.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

### Example

```python
result = mindlake.datalake.createTable('test_table_enc',
        [
            mindlake.datalake.Column('id', mindlake.DataType.int4, False),
            mindlake.datalake.Column('token', mindlake.DataType.text, True)
        ])
assert result, result.message
```

## 3. MindLake.DataLake.Column

`Column` type is used to represent a single column in a table. Each `Column` instance encapsulates properties about the column, including its name, data type, and encryption status. By creating a `Column` object, you can define the properties of a table column and use it to construct a table schema.

```python
class Column(dict):
    def __init__(self, columnName: str, dataType: DataType, encrypt: bool):
        self.columnName = columnName
        self.type = dataType
        self.encrypt = encrypt
    
    def toDict(self):
        return {
            'columnName': self.columnName,
            'type': self.type.value,
            'encrypt': self.encrypt
        }
```

### Fields

* `columnName` - `str`: The name of the column.
* `dataType` - [`MindLake.DataType`](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#3.-mindlake.datatype): The data type of the column, which should be define as [`MindLake.DataType`](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#3.-mindlake.datatype)
* `encrypt` - `bool`: A flag that indicates whether the column should be encrypted or not.

## 4. listTablesByCocoon() Method

Retrieve a list of names for the tables that belong to the user invoking the method, or in the specified cocoon.

```python
mindlake.datalake.listTablesByCocoon(cocoonName: str) -> ResultType
# mindlake is an instance of MindLake
```

### Parameters

1. `cocoonName` - `str`: specifies the name of the cocoon for which a list of tables should be returned.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

* `data` - `list<str>`: a list of names for the tables that belong to the user invoking the method, or in the specified cocoon.

### Example

```python
result = mindlake.datalake.listTablesByCocoon(cocoonName)
assert result, result.message
tableList = result.data
```

## 5. createCocoon() Method

Create a Cocoon in Mind Lake. Tables can be categorized into cocoons for classification purposes.

<pre class="language-python"><code class="lang-python"><strong>mindlake.datalake.createCocoon(cocoonName: str) -> ResultType
</strong># mindlake is an instance of MindLake
</code></pre>

### Parameters

1. `cocoonName` - `str`: the name of the cocoon to be created.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

### Example

```python
result = mindlake.datalake.createCocoon(cocoonName)
assert result, result.message
```

## 6. listCocoon() Method

Retrieve a list of names for the cocoons that belong to the user invoking the method.

```python
mindlake.datalake.listCocoon() -> ResultType
# mindlake is an instance of MindLake
```

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

* `data` - `list<str>`: a list of names for the cocoons that belong to the user invoking the method.

### Example

```python
result = mindlake.datalake.listCocoon()
assert result, result.message
cocoonList = result.data
```

## 7. linkTableToCocoon() Method

Assign the specified cocoon tag to the table.

```python
mindlake.datalake.linkTableToCocoon(tableName: str, cocoonName: str) -> ResultType
# mindlake is an instance of MindLake
```

### Parameters

1. `tableName` - `str`: the name of the table to be tagged with.
2. `cocoonName` - `str`: specifies the name of the cocoon for which the table should be tagged with.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

### Example

```python
result = mindlake.datalake.linkTableToCocoon(tableName, cocoonName)
assert result, result.message
```

## 8. dropCocoon() Method

Delete a Cocoon in Mind Lake.

<pre class="language-python"><code class="lang-python"><strong>mindlake.datalake.dropCocoon(cocoonName: str) -> ResultType
</strong># mindlake is an instance of MindLake
</code></pre>

### Parameters

1. `cocoonName` - `str`: the name of the cocoon to be dropped.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

### Example

```python
result = mindlake.datalake.dropCocoon(cocoonName)
assert result, result.message
```

## 9. dropTable() Method

Drop a table in the user's own account in Mind Lake.

```python
mindlake.datalake.dropTable(tableName: str, columns: list, primaryKey: list = None) -> ResultType
```

### Parameters

1. `tableName` - `str`: the name of the table to be dropped.

### Returns

An object of [`ResultType`](#user-content-fn-1)[^1]. [For more information](https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/pages/TDgQFnewZVDjZHEXSpFR#2.-mindlake.resulttype).

### Example

```python
result = mindlake.datalake.dropTable(cocoonName)
assert result, result.message
```

[^1]: ```python
    class ResultType:
        def __init__(self, code: int, message: str = None, data = None):
            self.code = code
            self.message = message
            self.data = data
                
        def __bool__(self):
            return self.code == 0
    ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mindnetwork.xyz/mind-lake-sdk/python-api-reference/mindlake.datalake.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
