Пример #1
0
def get_tables(service, session_handle, database_name='.*'):
    req = TGetTablesReq(sessionHandle=session_handle,
                        schemaName=database_name,
                        tableName='.*')
    resp = service.GetTables(req)
    err_if_rpc_not_ok(resp)
    return resp.operation_handle
Пример #2
0
def fetch_results(service, operation_handle, schema=None, max_rows=100,
                  orientation=TFetchOrientation.FETCH_NEXT):
    if not operation_handle.hasResultSet:
        return None

    # the schema is necessary to pull the proper values (i.e., coalesce)
    if schema is None:
        schema = get_result_schema(service, operation_handle)

    req = TFetchResultsReq(operationHandle=operation_handle,
                           orientation=orientation,
                           maxRows=max_rows)
    resp = service.FetchResults(req)
    err_if_rpc_not_ok(resp)

    rows = []
    for trow in resp.results.rows:
        row = []
        for (i, col_val) in enumerate(trow.colVals):
            type_ = schema[i][1]
            value = _TTypeId_to_TColumnValue_getters[type_](col_val).value
            if type_ == 'TIMESTAMP_TYPE':
                value = _parse_timestamp(value)
            row.append(value)
        rows.append(tuple(row))

    return rows
Пример #3
0
def get_functions(service, session_handle, database_name='.*'):
    # TODO: need to test this one especially
    req = TGetFunctionsReq(sessionHandle=session_handle,
                           schemaName=database_name,
                           functionName='.*')
    resp = service.GetFunctions(req)
    err_if_rpc_not_ok(resp)
    return resp.operationHandle
Пример #4
0
def ping(service, session_handle):
    req = TGetInfoReq(sessionHandle=session_handle,
                      infoType=TGetInfoType.CLI_SERVER_NAME)
    try:
        resp = service.GetInfo(req)
    except TTransportException as e:
        return False

    try:
        err_if_rpc_not_ok(resp)
    except RPCError as e:
        return False
    return True
Пример #5
0
def database_exists(service, session_handle, db_name):
    req = TGetSchemasReq(sessionHandle=session_handle, schemaName=db_name)
    resp = service.GetSchemas(req)
    err_if_rpc_not_ok(resp)
    operation_handle = resp.operationHandle
    # this only fetches default max_rows, but there should only be one row ideally
    results = fetch_results(service=service, operation_handle=operation_handle)
    exists = False
    for result in results:
        if result[0] == db_name:
            exists = True
    close_operation(service, operation_handle)
    return exists
Пример #6
0
def get_result_schema(service, operation_handle):
    if not operation_handle.hasResultSet:
        return None
    req = TGetResultSetMetadataReq(operationHandle=operation_handle)
    resp = service.GetResultSetMetadata(req)
    err_if_rpc_not_ok(resp)

    schema = []
    for column in resp.schema.columns:
        name = column.columnName
        type_ = TTypeId._VALUES_TO_NAMES[
                column.typeDesc.types[0].primitiveEntry.type]
        schema.append((name, type_))

    return schema
Пример #7
0
def get_log(service, operation_handle):
    req = TGetLogReq(operationHandle=operation_handle)
    resp = service.GetLog(req)
    err_if_rpc_not_ok(resp)
    return resp.log
Пример #8
0
def close_operation(service, operation_handle):
    req = TCloseOperationReq(operationHandle=operation_handle)
    resp = service.CloseOperation(req)
    err_if_rpc_not_ok(resp)
Пример #9
0
def cancel_operation(service, operation_handle):
    req = TCancelOperationReq(operationHandle=operation_handle)
    resp = service.CancelOperation(req)
    err_if_rpc_not_ok(resp)
Пример #10
0
def get_operation_status(service, operation_handle):
    req = TGetOperationStatusReq(operationHandle=operation_handle)
    resp = service.GetOperationStatus(req)
    err_if_rpc_not_ok(resp)
    return TOperationState._VALUES_TO_NAMES[resp.operationState]
Пример #11
0
def get_databases(service, session_handle):
    req = TGetSchemasReq(sessionHandle=session_handle, schemaName='.*')
    resp = service.GetSchemas(req)
    err_if_rpc_not_ok(resp)
    return resp.operation_handle
Пример #12
0
def execute_statement(service, session_handle, statement, configuration=None):
    req = TExecuteStatementReq(sessionHandle=session_handle,
                               statement=statement, confOverlay=configuration)
    resp = service.ExecuteStatement(req)
    err_if_rpc_not_ok(resp)
    return resp.operationHandle
Пример #13
0
def close_session(service, session_handle):
    req = TCloseSessionReq(sessionHandle=session_handle)
    resp = service.CloseSession(req)
    err_if_rpc_not_ok(resp)
Пример #14
0
def open_session(service, user, configuration=None):
    req = TOpenSessionReq(username=user, configuration=configuration)
    resp = service.OpenSession(req)
    err_if_rpc_not_ok(resp)
    return resp.sessionHandle