<s> import urlparse <EOL> import urllib <EOL> from nomad . engine import BaseEngine , DBError <EOL> def unq ( s ) : <EOL> if isinstance ( s , str ) : <EOL> return urllib . unquote ( s ) <EOL> return s <EOL> class Connection ( object ) : <EOL> exc = None <EOL> def __init__ ( self ) : <EOL> self . connection = self . connect ( ) <EOL> def connect ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def prepare ( self , statement ) : <EOL> return statement <EOL> def fetch ( self , cursor ) : <EOL> return cursor . fetchall ( ) <EOL> def query ( self , statement , * args ) : <EOL> statement = self . prepare ( statement ) <EOL> c = self . connection . cursor ( ) <EOL> try : <EOL> c . execute ( statement , args ) <EOL> except self . exc as e : <EOL> raise DBError ( e ) <EOL> data = self . fetch ( c ) <EOL> c . close ( ) <EOL> return data <EOL> def begin ( self ) : <EOL> pass <EOL> def commit ( self ) : <EOL> return self . connection . commit ( ) <EOL> def rollback ( self ) : <EOL> return self . connection . rollback ( ) <EOL> class Sqlite ( Connection ) : <EOL> _conn = None <EOL> def __init__ ( self , path ) : <EOL> self . path = path . path [ <NUM_LIT:1> : ] <EOL> import sqlite3 <EOL> self . module = sqlite3 <EOL> self . exc = sqlite3 . Error <EOL> Connection . __init__ ( self ) <EOL> def connect ( self ) : <EOL> return self . module . connect ( self . path ) <EOL> class Mysql ( Connection ) : <EOL> _conn = None <EOL> def __init__ ( self , url ) : <EOL> self . parameters = { '<STR_LIT:host>' : unq ( url . hostname ) , <EOL> '<STR_LIT:port>' : url . port , <EOL> '<STR_LIT>' : unq ( url . path . lstrip ( '<STR_LIT:/>' ) ) , <EOL> '<STR_LIT:user>' : unq ( url . username ) , <EOL> '<STR_LIT>' : unq ( url . password ) } <EOL> for k , v in self . parameters . items ( ) : <EOL> if not v : <EOL> del self . parameters [ k ] <EOL> import MySQLdb <EOL> self . module = MySQLdb <EOL> self . exc = MySQLdb . MySQLError <EOL> Connection . __init__ ( self ) <EOL> def connect ( self ) : <EOL> return self . module . connect ( ** self . parameters ) <EOL> def prepare ( self , statement ) : <EOL> return statement . replace ( '<STR_LIT:?>' , '<STR_LIT:%s>' ) <EOL> def begin ( self ) : <EOL> return self . query ( '<STR_LIT>' ) <EOL> def commit ( self ) : <EOL> return self . query ( '<STR_LIT>' ) <EOL> def rollback ( self ) : <EOL> return self . query ( '<STR_LIT>' ) <EOL> class Pgsql ( Connection ) : <EOL> _conn = None <EOL> def __init__ ( self , url ) : <EOL> self . parameters = { '<STR_LIT:host>' : unq ( url . hostname ) , <EOL> '<STR_LIT:port>' : url . port , <EOL> '<STR_LIT>' : unq ( url . path . lstrip ( '<STR_LIT:/>' ) ) , <EOL> '<STR_LIT:user>' : unq ( url . username ) , <EOL> '<STR_LIT:password>' : unq ( url . password ) } <EOL> if url . query : <EOL> self . options = url . query . split ( '<STR_LIT:&>' ) <EOL> else : <EOL> self . options = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> import psycopg2 <EOL> self . module = psycopg2 <EOL> self . exc = psycopg2 . Error <EOL> Connection . __init__ ( self ) <EOL> def connect ( self ) : <EOL> try : <EOL> return self . module . connect ( ** self . parameters ) <EOL> except self . module . OperationalError as e : <EOL> raise DBError ( '<STR_LIT>' % e ) <EOL> def prepare ( self , statement ) : <EOL> return statement . replace ( '<STR_LIT:?>' , '<STR_LIT:%s>' ) <EOL> def fetch ( self , cursor ) : <EOL> if cursor . rowcount == - <NUM_LIT:1> : <EOL> return [ ] <EOL> try : <EOL> return cursor . fetchall ( ) <EOL> except self . module . ProgrammingError : <EOL> return [ ] <EOL> def begin ( self ) : <EOL> c = self . connection . cursor ( ) <EOL> c . executemany ( [ '<STR_LIT>' ] + [ '<STR_LIT>' + x for x in self . options ] , [ ] ) <EOL> c . close ( ) <EOL> CONNECTORS = { '<STR_LIT>' : Sqlite , '<STR_LIT>' : Mysql , <EOL> '<STR_LIT>' : Pgsql , '<STR_LIT>' : Pgsql , '<STR_LIT>' : Pgsql } <EOL> class DBEngine ( BaseEngine ) : <EOL> def connect ( self ) : <EOL> p = urlparse . urlparse ( self . url ) <EOL> if p . scheme not in CONNECTORS : <EOL> raise DBError ( '<STR_LIT>' % p . scheme ) <EOL> return CONNECTORS [ p . scheme ] ( p ) <EOL> def query ( self , statement , * args , ** kwargs ) : <EOL> return self . connection . query ( statement , * args ) <EOL> engine = DBEngine </s>
