#! /usr/bin/env python

'''
Python Interface to SAGA Tools Library
----------
- Category : Import/Export
- Name     : PostgreSQL
- ID       : db_pgsql

Description
----------
Accessing PostgreSQL databases.
'''

from PySAGA.helper import Tool_Wrapper

def Run_List_PostgreSQL_Connections(CONNECTIONS=None, Verbose=2):
    '''
    List PostgreSQL Connections
    ----------
    [db_pgsql.0]\n
    Lists all PostgreSQL sources.\n
    Arguments
    ----------
    - CONNECTIONS [`output table`] : Connections

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '0', 'List PostgreSQL Connections')
    if Tool.is_Okay():
        Tool.Set_Output('CONNECTIONS', CONNECTIONS)
        return Tool.Execute(Verbose)
    return False

def Run_Connect_to_PostgreSQL(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, Verbose=2):
    '''
    Connect to PostgreSQL
    ----------
    [db_pgsql.1]\n
    Connect to PostgreSQL data source.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: localhost
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User. Default: postgres
    - PG_PWD [`text`] : Password. Default: postgres

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '1', 'Connect to PostgreSQL')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        return Tool.Execute(Verbose)
    return False

def Run_Disconnect_from_PostgreSQL(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, TRANSACT=None, Verbose=2):
    '''
    Disconnect from PostgreSQL
    ----------
    [db_pgsql.2]\n
    Disconnect PostgreSQL data source.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - TRANSACT [`choice`] : Transactions. Available Choices: [0] rollback [1] commit Default: 1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '2', 'Disconnect from PostgreSQL')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('TRANSACT', TRANSACT)
        return Tool.Execute(Verbose)
    return False

def Run_Disconnect_All(TRANSACT=None, Verbose=2):
    '''
    Disconnect All
    ----------
    [db_pgsql.3]\n
    Disconnects all PostgreSQL connections.\n
    Arguments
    ----------
    - TRANSACT [`choice`] : Transactions. Available Choices: [0] rollback [1] commit Default: 1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '3', 'Disconnect All')
    if Tool.is_Okay():
        Tool.Set_Option('TRANSACT', TRANSACT)
        return Tool.Execute(Verbose)
    return False

def Run_Begin_Transaction(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, SAVEPOINT=None, Verbose=2):
    '''
    Begin Transaction
    ----------
    [db_pgsql.4]\n
    Begins a transaction, which will be finished later with a commit or rollback. Tries to add a save point, if already in transaction mode.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - SAVEPOINT [`text`] : Save Point. Default: SAVEPOINT_01

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '4', 'Begin Transaction')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('SAVEPOINT', SAVEPOINT)
        return Tool.Execute(Verbose)
    return False

def Run_CommitRollback_Transaction(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, TRANSACT=None, SAVEPOINT=None, Verbose=2):
    '''
    Commit/Rollback Transaction
    ----------
    [db_pgsql.5]\n
    Execute a commit or rollback on open transactions with PostgreSQL source.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - TRANSACT [`choice`] : Transactions. Available Choices: [0] rollback [1] commit Default: 1
    - SAVEPOINT [`text`] : Save Point

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '5', 'Commit/Rollback Transaction')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('TRANSACT', TRANSACT)
        Tool.Set_Option('SAVEPOINT', SAVEPOINT)
        return Tool.Execute(Verbose)
    return False

def Run_Execute_SQL(TABLES=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, SQL=None, OUTPUT=None, STOP=None, Verbose=2):
    '''
    Execute SQL
    ----------
    [db_pgsql.6]\n
    Execute SQL commands on a connected PostgreSQL source. Separate different commands with a semicolon (';').\n
    Arguments
    ----------
    - TABLES [`output table list`] : Tables
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - SQL [`long text`] : SQL Statement. Default: CREATE TABLE myTable1 (Col1 VARCHAR(255) PRIMARY KEY, Col2 INTEGER);
INSERT INTO myTable1 (Col1, Col2) VALUES('First Value', 1);
DROP TABLE myTable1;

    - OUTPUT [`choice`] : Output. Available Choices: [0] none [1] message window [2] tables Default: 2
    - STOP [`boolean`] : Stop on Error. Default: 0

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '6', 'Execute SQL')
    if Tool.is_Okay():
        Tool.Set_Output('TABLES', TABLES)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('SQL', SQL)
        Tool.Set_Option('OUTPUT', OUTPUT)
        Tool.Set_Option('STOP', STOP)
        return Tool.Execute(Verbose)
    return False

def Run_List_Tables(TABLES=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, Verbose=2):
    '''
    List Tables
    ----------
    [db_pgsql.10]\n
    Lists all tables of an PostgreSQL data source.\n
    Arguments
    ----------
    - TABLES [`output table`] : Tables
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '10', 'List Tables')
    if Tool.is_Okay():
        Tool.Set_Output('TABLES', TABLES)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        return Tool.Execute(Verbose)
    return False

def Run_List_Table_Fields(TABLE=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, VERBOSE=None, Verbose=2):
    '''
    List Table Fields
    ----------
    [db_pgsql.11]\n
    Loads table information from PostgreSQL data source.\n
    Arguments
    ----------
    - TABLE [`output table`] : Field Description
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table
    - VERBOSE [`boolean`] : Verbose. Default: 0

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '11', 'List Table Fields')
    if Tool.is_Okay():
        Tool.Set_Output('TABLE', TABLE)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        Tool.Set_Option('VERBOSE', VERBOSE)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Table(TABLE=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, Verbose=2):
    '''
    Import Table
    ----------
    [db_pgsql.12]\n
    Imports a table from a PostgreSQL database.\n
    Arguments
    ----------
    - TABLE [`output table`] : Table
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '12', 'Import Table')
    if Tool.is_Okay():
        Tool.Set_Output('TABLE', TABLE)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        return Tool.Execute(Verbose)
    return False

def Run_Export_Table(TABLE=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, TABLE_PK=None, TABLE_NN=None, TABLE_UQ=None, NAME=None, EXISTS=None, Verbose=2):
    '''
    Export Table
    ----------
    [db_pgsql.13]\n
    Exports a table to a PostgreSQL database.\n
    Arguments
    ----------
    - TABLE [`input table`] : Table
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - TABLE_PK [`table fields`] : Primary Key
    - TABLE_NN [`table fields`] : Not Null
    - TABLE_UQ [`table fields`] : Unique
    - NAME [`text`] : Table Name
    - EXISTS [`choice`] : If table exists.... Available Choices: [0] abort export [1] replace existing table [2] append records, if table structure allows Default: 0

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '13', 'Export Table')
    if Tool.is_Okay():
        Tool.Set_Input ('TABLE', TABLE)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('TABLE_PK', TABLE_PK)
        Tool.Set_Option('TABLE_NN', TABLE_NN)
        Tool.Set_Option('TABLE_UQ', TABLE_UQ)
        Tool.Set_Option('NAME', NAME)
        Tool.Set_Option('EXISTS', EXISTS)
        return Tool.Execute(Verbose)
    return False

def Run_Drop_Table(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, Verbose=2):
    '''
    Drop Table
    ----------
    [db_pgsql.14]\n
    Deletes a table from a PostgreSQL database.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '14', 'Drop Table')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Table_from_SQL_Query(TABLE=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, TABLES=None, FIELDS=None, WHERE=None, GROUP=None, HAVING=None, ORDER=None, DISTINCT=None, Verbose=2):
    '''
    Import Table from SQL Query
    ----------
    [db_pgsql.15]\n
    Import a SQL table from a PostgreSQL database.\n
    Arguments
    ----------
    - TABLE [`output table`] : Query Result
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - TABLES [`text`] : Tables
    - FIELDS [`text`] : Fields
    - WHERE [`text`] : Where
    - GROUP [`text`] : Group by
    - HAVING [`text`] : Having
    - ORDER [`text`] : Order by
    - DISTINCT [`boolean`] : Distinct. Default: 0

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '15', 'Import Table from SQL Query')
    if Tool.is_Okay():
        Tool.Set_Output('TABLE', TABLE)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('TABLES', TABLES)
        Tool.Set_Option('FIELDS', FIELDS)
        Tool.Set_Option('WHERE', WHERE)
        Tool.Set_Option('GROUP', GROUP)
        Tool.Set_Option('HAVING', HAVING)
        Tool.Set_Option('ORDER', ORDER)
        Tool.Set_Option('DISTINCT', DISTINCT)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Table_from_SQL_Query_GUI(TABLE=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, WHERE=None, HAVING=None, ORDER=None, DISTINCT=None, Verbose=2):
    '''
    Import Table from SQL Query (GUI)
    ----------
    [db_pgsql.16]\n
    Import a SQL table from a PostgreSQL database.\n
    Arguments
    ----------
    - TABLE [`output table`] : Query Result
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - WHERE [`text`] : Where
    - HAVING [`text`] : Having
    - ORDER [`text`] : Order by
    - DISTINCT [`boolean`] : Distinct. Default: 0

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '16', 'Import Table from SQL Query (GUI)')
    if Tool.is_Okay():
        Tool.Set_Output('TABLE', TABLE)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('WHERE', WHERE)
        Tool.Set_Option('HAVING', HAVING)
        Tool.Set_Option('ORDER', ORDER)
        Tool.Set_Option('DISTINCT', DISTINCT)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Shapes_from_PostGIS(SHAPES=None, COLLECTION=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, Verbose=2):
    '''
    Import Shapes from PostGIS
    ----------
    [db_pgsql.20]\n
    Imports shapes from a PostGIS database.\n
    Arguments
    ----------
    - SHAPES [`output shapes`] : Shapes
    - COLLECTION [`output shapes list`] : Shapes
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '20', 'Import Shapes from PostGIS')
    if Tool.is_Okay():
        Tool.Set_Output('SHAPES', SHAPES)
        Tool.Set_Output('COLLECTION', COLLECTION)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        return Tool.Execute(Verbose)
    return False

def Run_Export_Shapes_to_PostGIS(SHAPES=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, SHAPES_PK=None, SHAPES_NN=None, SHAPES_UQ=None, NAME=None, EXISTS=None, CRS_EPSG=None, Verbose=2):
    '''
    Export Shapes to PostGIS
    ----------
    [db_pgsql.21]\n
    Exports shapes to a PostGIS database.\n
    Arguments
    ----------
    - SHAPES [`input shapes`] : Shapes
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - SHAPES_PK [`table fields`] : Primary Key
    - SHAPES_NN [`table fields`] : Not Null
    - SHAPES_UQ [`table fields`] : Unique
    - NAME [`text`] : Table Name. if empty shapes layers's name is used as table name
    - EXISTS [`choice`] : If table exists.... Available Choices: [0] abort export [1] replace existing table [2] append records, if table structure allows Default: 0
    - CRS_EPSG [`integer number`] : EPSG Code. Minimum: -1 Default: -1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '21', 'Export Shapes to PostGIS')
    if Tool.is_Okay():
        Tool.Set_Input ('SHAPES', SHAPES)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('SHAPES_PK', SHAPES_PK)
        Tool.Set_Option('SHAPES_NN', SHAPES_NN)
        Tool.Set_Option('SHAPES_UQ', SHAPES_UQ)
        Tool.Set_Option('NAME', NAME)
        Tool.Set_Option('EXISTS', EXISTS)
        Tool.Set_Option('CRS_EPSG', CRS_EPSG)
        return Tool.Execute(Verbose)
    return False

def Run_Update_Shapes_SRID(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, CRS_EPSG=None, Verbose=2):
    '''
    Update Shapes SRID
    ----------
    [db_pgsql.22]\n
    Change the SRID of all geometries in the user-specified column and table.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table
    - CRS_EPSG [`integer number`] : EPSG Code. Minimum: -1 Default: -1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '22', 'Update Shapes SRID')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        Tool.Set_Option('CRS_EPSG', CRS_EPSG)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Shapes_with_Joined_Data_from_PostGIS_GUI(SHAPES=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, GEO_TABLE=None, GEO_KEY=None, JOIN_TABLE=None, JOIN_KEY=None, WHERE=None, Verbose=2):
    '''
    Import Shapes with Joined Data from PostGIS (GUI)
    ----------
    [db_pgsql.23]\n
    Imports shapes with joined data from a PostGIS database.\n
    Arguments
    ----------
    - SHAPES [`output shapes`] : Shapes
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - GEO_TABLE [`choice`] : Geometry Table. Available Choices: Default: 0
    - GEO_KEY [`choice`] : Key. Available Choices: Default: 0
    - JOIN_TABLE [`choice`] : Join Table. Available Choices: Default: 0
    - JOIN_KEY [`choice`] : Key. Available Choices: Default: 0
    - WHERE [`text`] : Where

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '23', 'Import Shapes with Joined Data from PostGIS (GUI)')
    if Tool.is_Okay():
        Tool.Set_Output('SHAPES', SHAPES)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('GEO_TABLE', GEO_TABLE)
        Tool.Set_Option('GEO_KEY', GEO_KEY)
        Tool.Set_Option('JOIN_TABLE', JOIN_TABLE)
        Tool.Set_Option('JOIN_KEY', JOIN_KEY)
        Tool.Set_Option('WHERE', WHERE)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Raster_from_PostGIS(GRIDS=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, WHERE=None, MULTIPLE=None, Verbose=2):
    '''
    Import Raster from PostGIS
    ----------
    [db_pgsql.30]\n
    Imports grids from a PostGIS database.\n
    Arguments
    ----------
    - GRIDS [`output grid list`] : Grids
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table
    - WHERE [`text`] : Where
    - MULTIPLE [`choice`] : Multiple Bands. Available Choices: [0] single grids [1] grid collection [2] automatic Default: 2

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '30', 'Import Raster from PostGIS')
    if Tool.is_Okay():
        Tool.Set_Output('GRIDS', GRIDS)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        Tool.Set_Option('WHERE', WHERE)
        Tool.Set_Option('MULTIPLE', MULTIPLE)
        return Tool.Execute(Verbose)
    return False

def Run_Export_Raster_to_PostGIS(GRIDS=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, GRID_SYSTEM=None, TABLE=None, NAME=None, GRID_NAME=None, CRS_EPSG=None, Verbose=2):
    '''
    Export Raster to PostGIS
    ----------
    [db_pgsql.31]\n
    Exports grids to a PostGIS database.\n
    Arguments
    ----------
    - GRIDS [`input grid list`] : Bands
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - GRID_SYSTEM [`grid system`] : Grid System
    - TABLE [`choice`] : Add to Table. Available Choices: Default: 0
    - NAME [`text`] : Table Name
    - GRID_NAME [`boolean`] : Band Name Field. Default: 1
    - CRS_EPSG [`integer number`] : EPSG Code. Minimum: -1 Default: -1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '31', 'Export Raster to PostGIS')
    if Tool.is_Okay():
        Tool.Set_Input ('GRIDS', GRIDS)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('GRID_SYSTEM', GRID_SYSTEM)
        Tool.Set_Option('TABLE', TABLE)
        Tool.Set_Option('NAME', NAME)
        Tool.Set_Option('GRID_NAME', GRID_NAME)
        Tool.Set_Option('CRS_EPSG', CRS_EPSG)
        return Tool.Execute(Verbose)
    return False

def Run_Update_Raster_SRID(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, TABLES=None, CRS_EPSG=None, Verbose=2):
    '''
    Update Raster SRID
    ----------
    [db_pgsql.32]\n
    Change the SRID of all rasters in the user-specified column and table.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - TABLES [`choice`] : Tables. Available Choices: Default: 0
    - CRS_EPSG [`integer number`] : EPSG Code. Minimum: -1 Default: -1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '32', 'Update Raster SRID')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('TABLES', TABLES)
        Tool.Set_Option('CRS_EPSG', CRS_EPSG)
        return Tool.Execute(Verbose)
    return False

def Run_Import_Single_Raster_Band_from_PostGIS(GRID=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, DB_TABLE=None, RID=None, Verbose=2):
    '''
    Import Single Raster Band from PostGIS
    ----------
    [db_pgsql.33]\n
    Imports grids from a PostGIS database.\n
    Arguments
    ----------
    - GRID [`output data object`] : Grid
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - DB_TABLE [`text`] : Table
    - RID [`text`] : Band Identifier

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '33', 'Import Single Raster Band from PostGIS')
    if Tool.is_Okay():
        Tool.Set_Output('GRID', GRID)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('DB_TABLE', DB_TABLE)
        Tool.Set_Option('RID', RID)
        return Tool.Execute(Verbose)
    return False

def Run_Export_Grid_Collection_to_PostGIS(GRIDS=None, PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, GRID_SYSTEM=None, NAME=None, EXISTS=None, CRS_EPSG=None, Verbose=2):
    '''
    Export Grid Collection to PostGIS
    ----------
    [db_pgsql.34]\n
    Exports a grid collection to a PostGIS database.\n
    Arguments
    ----------
    - GRIDS [`input grid collection`] : Grid Collection
    - PG_HOST [`text`] : Host. Default: 127.0.0.1
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database
    - PG_USER [`text`] : User
    - PG_PWD [`text`] : Password
    - GRID_SYSTEM [`grid system`] : Grid System
    - NAME [`text`] : Name
    - EXISTS [`choice`] : If table exists.... Available Choices: [0] abort [1] replace Default: 0
    - CRS_EPSG [`integer number`] : EPSG Code. Minimum: -1 Default: -1

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '34', 'Export Grid Collection to PostGIS')
    if Tool.is_Okay():
        Tool.Set_Input ('GRIDS', GRIDS)
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        Tool.Set_Option('GRID_SYSTEM', GRID_SYSTEM)
        Tool.Set_Option('NAME', NAME)
        Tool.Set_Option('EXISTS', EXISTS)
        Tool.Set_Option('CRS_EPSG', CRS_EPSG)
        return Tool.Execute(Verbose)
    return False

def Run_Create_Database(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, Verbose=2):
    '''
    Create Database
    ----------
    [db_pgsql.35]\n
    Creates a new PostgreSQL Database.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: localhost
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database. Default: dbname
    - PG_USER [`text`] : User. Default: postgres
    - PG_PWD [`text`] : Password. Default: postgres

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '35', 'Create Database')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        return Tool.Execute(Verbose)
    return False

def Run_Drop_Database(PG_HOST=None, PG_PORT=None, PG_DB=None, PG_USER=None, PG_PWD=None, Verbose=2):
    '''
    Drop Database
    ----------
    [db_pgsql.36]\n
    Deletes a PostgreSQL Database.\n
    Arguments
    ----------
    - PG_HOST [`text`] : Host. Default: localhost
    - PG_PORT [`integer number`] : Port. Minimum: 0 Default: 5432
    - PG_DB [`text`] : Database. Default: dbname
    - PG_USER [`text`] : User. Default: postgres
    - PG_PWD [`text`] : Password. Default: postgres

    - Verbose [`integer number`] : Verbosity level, 0=silent, 1=tool name and success notification, 2=complete tool output.\n
    Returns
    ----------
    `boolean` : `True` on success, `False` on failure.
    '''
    Tool = Tool_Wrapper('db_pgsql', '36', 'Drop Database')
    if Tool.is_Okay():
        Tool.Set_Option('PG_HOST', PG_HOST)
        Tool.Set_Option('PG_PORT', PG_PORT)
        Tool.Set_Option('PG_DB', PG_DB)
        Tool.Set_Option('PG_USER', PG_USER)
        Tool.Set_Option('PG_PWD', PG_PWD)
        return Tool.Execute(Verbose)
    return False

