"""
Example 36
No SQL lock-free meta data requests introduced in Exasol 7.0, WebSocket protocol v2
"""

import pyexasol
import _config as config

import sys

import pprint
printer = pprint.PrettyPrinter(indent=4, width=140)


try:
    C = pyexasol.connect(dsn=config.dsn, user=config.user, password=<PASSWORD>, schema=config.schema
                         , protocol_version=pyexasol.PROTOCOL_V2)

except pyexasol.ExaRequestError as e:
    if e.message == 'Could not create WebSocket protocol version 2':
        print('Old Exasol versions cannot process protocol_version 2, skipping meta_nosql checks')
        sys.exit(0)
    else:
        raise

if C.protocol_version() < pyexasol.PROTOCOL_V2:
    print('Actual protocol version is less than 2, skipping meta_nosql checks')
    sys.exit(0)


# Schema exists
val = C.meta.schema_exists(C.current_schema())
print(f'Schema exists: {val}')

# Schema does not exist
val = C.meta.schema_exists('abcabcabc')
print(f'Schema exists: {val}')


# Table exists
val = C.meta.table_exists('users')
print(f'Table exists: {val}')

# Table exists (with schema name)
val = C.meta.table_exists((C.current_schema(), 'users'))
print(f'Table exists: {val}')

# Table does not exist
val = C.meta.table_exists('abcabcabc')
print(f'Table exists: {val}')


# View exists
val = C.meta.view_exists('users_view')
print(f'View exists: {val}')

# View exists (with schema name)
val = C.meta.view_exists((C.current_schema(), 'users_view'))
print(f'View exists: {val}')

# View does not exist
val = C.meta.view_exists('abcabcabc')
print(f'View exists: {val}')


# List schemas
st = C.meta.execute_meta_nosql('getSchemas')
printer.pprint(st.fetchall())

# List schemas with filters
st = C.meta.execute_meta_nosql('getSchemas', {
    'schema': 'PYEXASOL%'
})
printer.pprint(st.fetchall())


# List tables with filters
st = C.meta.execute_meta_nosql('getTables', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'tableTypes': ['TABLE'],
})
printer.pprint(st.fetchall())


# List views with filters
st = C.meta.execute_meta_nosql('getTables', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'tableTypes': ['VIEW'],
})
printer.pprint(st.fetchall())


# List columns with filters
st = C.meta.execute_meta_nosql('getColumns', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'column': '%_ID%',
})
printer.pprint(st.fetchall())


# List keywords
val = C.meta.list_sql_keywords()
printer.pprint(val[0:10])


# Exception handling
try:
    st = C.meta.execute_meta_nosql('getColumns', {
        'schema': 'PYEXASOL%',
        'table': 'USERS%',
        'column': ['%_ID%'],
    })
except pyexasol.ExaRequestError as e:
    print(e)

"""
Example 36
No SQL lock-free meta data requests introduced in Exasol 7.0, WebSocket protocol v2
"""

import pyexasol
import _config as config

import sys

import pprint
printer = pprint.PrettyPrinter(indent=4, width=140)


try:
    C = pyexasol.connect(dsn=config.dsn, user=config.user, password=<PASSWORD>, schema=config.schema
                         , protocol_version=pyexasol.PROTOCOL_V2)

except pyexasol.ExaRequestError as e:
    if e.message == 'Could not create WebSocket protocol version 2':
        print('Old Exasol versions cannot process protocol_version 2, skipping meta_nosql checks')
        sys.exit(0)
    else:
        raise

if C.protocol_version() < pyexasol.PROTOCOL_V2:
    print('Actual protocol version is less than 2, skipping meta_nosql checks')
    sys.exit(0)


# Schema exists
val = C.meta.schema_exists(C.current_schema())
print(f'Schema exists: {val}')

# Schema does not exist
val = C.meta.schema_exists('abcabcabc')
print(f'Schema exists: {val}')


# Table exists
val = C.meta.table_exists('users')
print(f'Table exists: {val}')

# Table exists (with schema name)
val = C.meta.table_exists((C.current_schema(), 'users'))
print(f'Table exists: {val}')

# Table does not exist
val = C.meta.table_exists('abcabcabc')
print(f'Table exists: {val}')


# View exists
val = C.meta.view_exists('users_view')
print(f'View exists: {val}')

# View exists (with schema name)
val = C.meta.view_exists((C.current_schema(), 'users_view'))
print(f'View exists: {val}')

# View does not exist
val = C.meta.view_exists('abcabcabc')
print(f'View exists: {val}')


# List schemas
st = C.meta.execute_meta_nosql('getSchemas')
printer.pprint(st.fetchall())

# List schemas with filters
st = C.meta.execute_meta_nosql('getSchemas', {
    'schema': 'PYEXASOL%'
})
printer.pprint(st.fetchall())


# List tables with filters
st = C.meta.execute_meta_nosql('getTables', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'tableTypes': ['TABLE'],
})
printer.pprint(st.fetchall())


# List views with filters
st = C.meta.execute_meta_nosql('getTables', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'tableTypes': ['VIEW'],
})
printer.pprint(st.fetchall())


# List columns with filters
st = C.meta.execute_meta_nosql('getColumns', {
    'schema': 'PYEXASOL%',
    'table': 'USERS%',
    'column': '%_ID%',
})
printer.pprint(st.fetchall())


# List keywords
val = C.meta.list_sql_keywords()
printer.pprint(val[0:10])


# Exception handling
try:
    st = C.meta.execute_meta_nosql('getColumns', {
        'schema': 'PYEXASOL%',
        'table': 'USERS%',
        'column': ['%_ID%'],
    })
except pyexasol.ExaRequestError as e:
    print(e)

Example 36 No SQL lock-free meta data requests introduced in Exasol 7.0, WebSocket protocol v2 # Schema exists # Schema does not exist # Table exists # Table exists (with schema name) # Table does not exist # View exists # View exists (with schema name) # View does not exist # List schemas # List schemas with filters # List tables with filters # List views with filters # List columns with filters # List keywords # Exception handling