# seSql

The seSql module allows you to connect to JDBC/ODBC from Python code to databases using 
Java [JDBC](http://java.sun.com/products/jdbc/overview.html) or [ODBC](https://learn.microsoft.com/et-ee/sql/connect/python/pyodbc/python-sql-driver-pyodbc?view=sql-server-2017).

## Install
```shell
pip install seSql
```

## Usage
### connect, query
- **_using JDBC_**
```python
from seSql import sql, logger

if __name__ == '__main__':
    oSql = sql()
    oSql.connect(
        server="SQL.site4now.net",
        port=1433,
        user="db_user",
        password="{pw}",
        trust="no",
        driverOverride="jdbc"
    )
    if oSql.isConnected:
        try:
            oSql.query("select @@version as version;")
            oSql.query("select @@versions as version;")
        except Exception as e:
            logger.error(f'Exception: {e}')
```

- **_using JDBC_ - output**
```shell
2024-04-01 00:00:00 | seSql | INFO     | {"driver-info": {"using": "jdbc", "odbc": {"loaded": true, "driver": "ODBC Driver 18 for SQL Server"}, "jdbc": {"loaded": true, "driver": "mssql-12.4.2"}}}
2024-04-01 00:00:00 | seSql | INFO     | {"jdbc-connect": {"connection_ms": 889.3, "connected": true, "connStr": {"driver": "mssql-12.4.2", "server": "jdbc:sqlserver://SQL.site4now.net", "port": 1433, "database": "", "user": "db_user", "password": "**********mee68", "encrypt": true, "trustServerCertificate": true}}}
2024-04-01 00:00:00 | seSql | INFO     | {"jdbc-query": {"connection_ms": 104.95, "query": "select @@version as version;"}}
2024-04-01 00:00:00 | seSql | ERROR    | {"jdbc-query": {"connection_ms": 59.0, "query": "select @@versions as version;", "error": "com.microsoft.sqlserver.jdbc.SQLServerException: Must declare the scalar variable \"@@versions\"."}}
2024-04-01 00:00:00 | seSql | ERROR    | Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Must declare the scalar variable "@@versions".
```

### connect, query
- **_using ODBC_**
```python
from seSql import sql, logger

if __name__ == '__main__':
    oSql = sql()
    oSql.connect(
        server="SQL.site4now.net",
        port=1433,
        user="db_user",
        password="{pw}",
        trust="no",
        driverOverride="odbc"
    )
    if oSql.isConnected:
        try:
            oSql.query("select @@version as version;")
            oSql.query("select @@versions as version;")
        except Exception as e:
            logger.error(f'Exception: {e}')
```
- **_using ODBC_ - output**
```shell
2024-04-01 00:00:00 | seSql | INFO     | {"driver-info": {"using": "odbc", "odbc": {"loaded": true, "driver": "ODBC Driver 18 for SQL Server"}, "jdbc": {"loaded": true, "driver": "mssql-12.4.2"}}}
2024-04-01 00:00:00 | seSql | INFO     | {"odbc-connect": {"connection_ms": 781.27, "connected": true, "connStr": {"driver": "ODBC Driver 18 for SQL Server", "server": "SQL.site4now.net", "port": 1433, "database": "", "user": "db_user", "password": "**********mee68", "trust": "no"}}}
2024-04-01 00:00:00 | seSql | INFO     | {"odbc-query": {"connection_ms": 56.09, "query": "select @@version as version;"}}
2024-04-01 00:00:00 | seSql | ERROR    | {"odbc-query": {"connection_ms": 56.53, "query": "select @@versions as version;", "error": "('42000', '[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Must declare the scalar variable \"@@versions\". (137) (SQLExecDirectW)')"}}
2024-04-01 00:00:00 | seSql | ERROR    | Exception: ('42000', '[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Must declare the scalar variable "@@versions". (137) (SQLExecDirectW)')
```

### hostName, hostIP, mask, seSqlVersion
```python
import json
from seSql import mask, seSqlVersion, hostName, hostIP, logger

if __name__ == '__main__':
    logger.info(json.dumps({
        'seSql-info': {
            'host': hostName(),
            'hostIP': hostIP(),
            'mask_all_but_last_12345': mask('all_but_last_12345'),
            'version': seSqlVersion
        }
    }, indent=4))
```
- output
```shell
2024-04-01 00:00:00 | seSql | INFO     | {
    "seSql-info": {
        "host": "MacBook-Pro",
        "hostIP": "127.0.0.1",
        "mask_all_but_last_12345": "*************12345",
        "version": "2024.4.0 build[59]"
    }
}

```