<s> import os <EOL> import sqlite3 <EOL> from . . import coins , util <EOL> KEY_VERSION = <NUM_LIT:1> <EOL> class DatabaseException ( Exception ) : pass <EOL> class Database ( object ) : <EOL> Columns = [ ] <EOL> Name = '<STR_LIT>' <EOL> Version = <NUM_LIT:1> <EOL> def __init__ ( self , data_dir = None , coin = coins . Bitcoin ) : <EOL> if data_dir is None : <EOL> data_dir = util . default_data_directory ( ) <EOL> self . __data_dir = data_dir <EOL> self . __coin = coin <EOL> self . sql_select = '<STR_LIT>' % ( '<STR_LIT:U+002C>' . join ( n for ( n , t , i ) in self . Columns ) , self . Name ) <EOL> offset = <NUM_LIT:0> <EOL> if len ( self . Columns ) and self . Columns [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:id>' : <EOL> offset = <NUM_LIT:1> <EOL> self . sql_insert = '<STR_LIT>' % ( <EOL> self . Name , <EOL> '<STR_LIT:U+002C>' . join ( n for ( n , t , i ) in self . Columns [ offset : ] ) , <EOL> '<STR_LIT:U+002C>' . join ( '<STR_LIT:?>' for c in self . Columns [ offset : ] ) <EOL> ) <EOL> data_dir = property ( lambda s : s . __data_dir ) <EOL> coin = property ( lambda s : s . __coin ) <EOL> def get_filename ( self , extra = '<STR_LIT>' ) : <EOL> return os . path . join ( self . data_dir , '<STR_LIT>' % ( self . coin . name , self . Name , extra ) ) <EOL> def get_connection ( self , extra = '<STR_LIT>' ) : <EOL> filename = self . get_filename ( extra ) <EOL> connection = sqlite3 . connect ( filename , timeout = <NUM_LIT:30> ) <EOL> connection . row_factory = sqlite3 . Row <EOL> self . initialize_database ( connection ) <EOL> return connection <EOL> def initialize_database ( self , connection ) : <EOL> cursor = connection . cursor ( ) <EOL> try : <EOL> sql = '<STR_LIT>' <EOL> cursor . execute ( sql ) <EOL> sql = '<STR_LIT>' <EOL> cursor . execute ( sql , ( KEY_VERSION , self . Version , None ) ) <EOL> column_defs = '<STR_LIT:U+002C>' . join ( "<STR_LIT>" % ( n , t ) for ( n , t , i ) in self . Columns ) <EOL> sql = '<STR_LIT>' % ( self . Name , column_defs ) <EOL> cursor . execute ( sql ) <EOL> for ( n , t , i ) in self . Columns : <EOL> if not i : continue <EOL> sql = "<STR_LIT>" % ( n , self . Name , n ) <EOL> cursor . execute ( sql ) <EOL> self . populate_database ( cursor ) <EOL> connection . commit ( ) <EOL> except sqlite3 . OperationalError , e : <EOL> if e . message != ( '<STR_LIT>' ) : <EOL> raise e <EOL> version = self . get_metadata ( cursor , KEY_VERSION ) <EOL> if version != self . Version : <EOL> raise DatabaseException ( '<STR_LIT>' % ( version , self . Version ) ) <EOL> def set_metadata ( self , cursor , key , value ) : <EOL> '''<STR_LIT>''' <EOL> if key == KEY_VERSION : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> if isinstance ( value , int ) : <EOL> cursor . execute ( sql , ( key , value , None ) ) <EOL> elif isinstance ( value , str ) : <EOL> cursor . execute ( sql , ( key , <NUM_LIT:0> , buffer ( value ) ) ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> def get_metadata ( self , cursor , key , default = None ) : <EOL> sql = '<STR_LIT>' <EOL> cursor . execute ( sql , ( key , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if row : <EOL> if row [ <NUM_LIT:1> ] is not None : <EOL> return str ( row [ <NUM_LIT:1> ] ) <EOL> return row [ <NUM_LIT:0> ] <EOL> return default <EOL> def populate_database ( self , cursor ) : <EOL> pass </s>
