<s> """<STR_LIT>""" <EOL> from bayeslite . exception import BQLError <EOL> from bayeslite . sqlite3_util import sqlite3_quote_name <EOL> from bayeslite . util import casefold <EOL> from bayeslite . util import cursor_value <EOL> def bayesdb_has_table ( bdb , name ) : <EOL> """<STR_LIT>""" <EOL> qt = sqlite3_quote_name ( name ) <EOL> cursor = bdb . sql_execute ( '<STR_LIT>' % ( qt , ) ) <EOL> try : <EOL> cursor . next ( ) <EOL> except StopIteration : <EOL> return False <EOL> else : <EOL> return True <EOL> def bayesdb_table_column_names ( bdb , table ) : <EOL> """<STR_LIT>""" <EOL> bayesdb_table_guarantee_columns ( bdb , table ) <EOL> sql = '''<STR_LIT>''' <EOL> return [ str ( row [ <NUM_LIT:0> ] ) for row in bdb . sql_execute ( sql , ( table , ) ) ] <EOL> def bayesdb_table_has_column ( bdb , table , name ) : <EOL> """<STR_LIT>""" <EOL> bayesdb_table_guarantee_columns ( bdb , table ) <EOL> sql = '<STR_LIT>' <EOL> return cursor_value ( bdb . sql_execute ( sql , ( table , name ) ) ) <EOL> def bayesdb_table_column_name ( bdb , table , colno ) : <EOL> """<STR_LIT>""" <EOL> bayesdb_table_guarantee_columns ( bdb , table ) <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , ( table , colno ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( table ) , colno ) ) <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_table_column_number ( bdb , table , name ) : <EOL> """<STR_LIT>""" <EOL> bayesdb_table_guarantee_columns ( bdb , table ) <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , ( table , name ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( table ) , repr ( name ) ) ) <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_table_guarantee_columns ( bdb , table ) : <EOL> """<STR_LIT>""" <EOL> with bdb . savepoint ( ) : <EOL> qt = sqlite3_quote_name ( table ) <EOL> insert_column_sql = '''<STR_LIT>''' <EOL> nrows = <NUM_LIT:0> <EOL> for row in bdb . sql_execute ( '<STR_LIT>' % ( qt , ) ) : <EOL> nrows += <NUM_LIT:1> <EOL> colno , name , _sqltype , _notnull , _default , _primary_key = row <EOL> bdb . sql_execute ( insert_column_sql , ( table , colno , name ) ) <EOL> if nrows == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( table ) , ) ) <EOL> def bayesdb_has_generator ( bdb , name ) : <EOL> """<STR_LIT>""" <EOL> sql = '<STR_LIT>' <EOL> return <NUM_LIT:0> != cursor_value ( bdb . sql_execute ( sql , ( name , ) ) ) <EOL> def bayesdb_has_generator_default ( bdb , name ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> return <NUM_LIT:0> != cursor_value ( bdb . sql_execute ( sql , { '<STR_LIT:name>' : name } ) ) <EOL> def bayesdb_get_generator ( bdb , name ) : <EOL> """<STR_LIT>""" <EOL> sql = '<STR_LIT>' <EOL> cursor = bdb . sql_execute ( sql , ( name , ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( name ) , ) ) <EOL> else : <EOL> assert isinstance ( row [ <NUM_LIT:0> ] , int ) <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_get_generator_default ( bdb , name ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , { '<STR_LIT:name>' : name } ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( name ) , ) ) <EOL> else : <EOL> assert isinstance ( row [ <NUM_LIT:0> ] , int ) <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_name ( bdb , id ) : <EOL> """<STR_LIT>""" <EOL> sql = '<STR_LIT>' <EOL> cursor = bdb . sql_execute ( sql , ( id , ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( id ) , ) ) <EOL> else : <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_metamodel ( bdb , id ) : <EOL> """<STR_LIT>""" <EOL> sql = '<STR_LIT>' <EOL> cursor = bdb . sql_execute ( sql , ( id , ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( id ) , ) ) <EOL> else : <EOL> if row [ <NUM_LIT:0> ] not in bdb . metamodels : <EOL> name = bayesdb_generator_name ( bdb , id ) <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( name ) , repr ( row [ <NUM_LIT:0> ] ) ) ) <EOL> return bdb . metamodels [ row [ <NUM_LIT:0> ] ] <EOL> def bayesdb_generator_table ( bdb , id ) : <EOL> """<STR_LIT>""" <EOL> sql = '<STR_LIT>' <EOL> cursor = bdb . sql_execute ( sql , ( id , ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> raise ValueError ( '<STR_LIT>' % ( repr ( id ) , ) ) <EOL> else : <EOL> assert len ( row ) == <NUM_LIT:1> <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_column_names ( bdb , generator_id ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> return [ str ( row [ <NUM_LIT:0> ] ) for row in bdb . sql_execute ( sql , ( generator_id , ) ) ] <EOL> def bayesdb_generator_column_stattype ( bdb , generator_id , colno ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , ( generator_id , colno ) ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> generator = bayesdb_generator_name ( bdb , generator_id ) <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : colno , <EOL> } ) <EOL> if cursor_value ( cursor ) == <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( generator , colno ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( generator , colno ) ) <EOL> else : <EOL> assert len ( row ) == <NUM_LIT:1> <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_has_column ( bdb , generator_id , column_name ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : column_name , <EOL> } ) <EOL> return cursor_value ( cursor ) <EOL> def bayesdb_generator_column_name ( bdb , generator_id , colno ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : colno , <EOL> } ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> generator = bayesdb_generator_name ( bdb , generator_id ) <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( generator ) , colno ) ) <EOL> else : <EOL> assert len ( row ) == <NUM_LIT:1> <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_column_number ( bdb , generator_id , column_name ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> cursor = bdb . sql_execute ( sql , { <EOL> '<STR_LIT>' : generator_id , <EOL> '<STR_LIT>' : column_name , <EOL> } ) <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> generator = bayesdb_generator_name ( bdb , generator_id ) <EOL> raise ValueError ( '<STR_LIT>' % <EOL> ( repr ( generator ) , repr ( column_name ) ) ) <EOL> else : <EOL> assert len ( row ) == <NUM_LIT:1> <EOL> assert isinstance ( row [ <NUM_LIT:0> ] , int ) <EOL> return row [ <NUM_LIT:0> ] <EOL> def bayesdb_generator_column_numbers ( bdb , generator_id ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> return [ row [ <NUM_LIT:0> ] for row in bdb . sql_execute ( sql , ( generator_id , ) ) ] <EOL> def bayesdb_generator_has_model ( bdb , generator_id , modelno ) : <EOL> """<STR_LIT>""" <EOL> sql = '''<STR_LIT>''' <EOL> return cursor_value ( bdb . sql_execute ( sql , ( generator_id , modelno ) ) ) <EOL> def bayesdb_generator_modelnos ( bdb , generator_id ) : <EOL> sql = '''<STR_LIT>''' <EOL> return [ row [ <NUM_LIT:0> ] for row in bdb . sql_execute ( sql , ( generator_id , ) ) ] <EOL> def bayesdb_generator_cell_value ( bdb , generator_id , rowid , colno ) : <EOL> table_name = bayesdb_generator_table ( bdb , generator_id ) <EOL> colname = bayesdb_generator_column_name ( bdb , generator_id , colno ) <EOL> qt = sqlite3_quote_name ( table_name ) <EOL> qcn = sqlite3_quote_name ( colname ) <EOL> value_sql = '<STR_LIT>' % ( qcn , qt ) <EOL> value_cursor = bdb . sql_execute ( value_sql , ( rowid , ) ) <EOL> value = None <EOL> try : <EOL> row = value_cursor . next ( ) <EOL> except StopIteration : <EOL> generator = bayesdb_generator_name ( bdb , generator_id ) <EOL> raise BQLError ( bdb , '<STR_LIT>' % <EOL> ( repr ( generator ) , rowid ) ) <EOL> else : <EOL> assert len ( row ) == <NUM_LIT:1> <EOL> value = row [ <NUM_LIT:0> ] <EOL> return value <EOL> def bayesdb_generator_row_values ( bdb , generator_id , rowid ) : <EOL> table_name = bayesdb_generator_table ( bdb , generator_id ) <EOL> column_names = bayesdb_generator_column_names ( bdb , generator_id ) <EOL> qt = sqlite3_quote_name ( table_name ) <EOL> qcns = '<STR_LIT:U+002C>' . join ( map ( sqlite3_quote_name , column_names ) ) <EOL> select_sql = ( '<STR_LIT>' % ( qcns , qt ) ) <EOL> cursor = bdb . sql_execute ( select_sql , ( rowid , ) ) <EOL> row = None <EOL> try : <EOL> row = cursor . next ( ) <EOL> except StopIteration : <EOL> generator = bayesdb_generator_table ( bdb , generator_id ) <EOL> raise BQLError ( bdb , '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( repr ( table_name ) , repr ( generator ) , repr ( rowid ) ) ) <EOL> try : <EOL> cursor . next ( ) <EOL> except StopIteration : <EOL> pass <EOL> else : <EOL> generator = bayesdb_generator_table ( bdb , generator_id ) <EOL> raise BQLError ( bdb , '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( repr ( table_name ) , repr ( generator ) , repr ( rowid ) ) ) <EOL> return row <EOL> def bayesdb_generator_fresh_row_id ( bdb , generator_id ) : <EOL> table_name = bayesdb_generator_table ( bdb , generator_id ) <EOL> qt = sqlite3_quote_name ( table_name ) <EOL> cursor = bdb . sql_execute ( '<STR_LIT>' % ( qt , ) ) <EOL> max_rowid = cursor_value ( cursor ) <EOL> if max_rowid is None : <EOL> max_rowid = <NUM_LIT:0> <EOL> return max_rowid + <NUM_LIT:1> <EOL> _STATTYPE_TO_AFFINITY = dict ( ( casefold ( st ) , casefold ( af ) ) for st , af in ( <EOL> ( '<STR_LIT>' , '<STR_LIT:text>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) ) <EOL> def bayesdb_stattype_affinity ( _bdb , stattype ) : <EOL> return _STATTYPE_TO_AFFINITY [ casefold ( stattype ) ] </s>
