Beispiel #1
0
 def conn(cls):
     """
     return the mysql connection handle to the configured server
     """
     config = cls.config
     try:
         if cls.config.get('password'):
             conn = mysql.connector.Connect(user=config['username'],
                                            password=config['password'],
                                            host=config['host'],
                                            port=config['port'])
         else:
             conn = mysql.connector.Connect(user=config['username'],
                                            host=config['host'],
                                            port=config['port'])
     except mysql.connector.InterfaceError, ex:
         raise DbError(
             "Cannot connect to MySQL Db (1): %s\n"
             "Ensure that the server is running and you can connect normally"
             % ex)
Beispiel #2
0
class PostgresDb(Db):
    DEFAULT_PORT = 5432

    @classmethod
    def new(cls, config):
        super(PostgresDb, cls).new(config)
        if 'revision_schema_name' in cls.config:
            cls.history_table_name = cls.config['history_table_name']
            cls.full_table_name = '"%s"."%s"' % (
                cls.config['revision_schema_name'],
                cls.config['history_table_name'])
        else:
            raise DbError(
                'No schema found in config file. Please add one with the key: '
                'revision_schema_name')

        return cls

    @classmethod
    def init_conn(cls):
        try:
            psycopg2
        except NameError:
            raise DbError(
                'Postgres module not found/loaded. Please make sure psycopg2 is installed\n'
            )

        cls.conn = cls.conn()
        cls.cursor = cls.conn.cursor()

        cls.conn_initialized = True
        return cls

    @classmethod
    def execute(cls, query, data=None):
        if not cls.conn_initialized:
            cls.init_conn()
        try:
            cursor = cls.cursor
            cursor.execute('SET search_path TO %s' % cls.config['schema_name'])
            if data:
                cursor.execute(query, data)
            else:
                cursor.execute(query)
            results = []
            # If rowcount == 0, just return None.
            #
            # Note from psycopg2 docs:
            #
            # The rowcount attribute specifies the number of rows that the
            # last execute*() produced (for DQL statements like SELECT) or
            # affected (for DML statements like UPDATE or INSERT).
            #
            # http://initd.org/psycopg/docs/cursor.html
            #
            # Thus, it is possible that fetchone/fetchall will fail despite
            # rowcount being > 0.  That error is caught below and None is
            # returned.
            if cursor.rowcount > 0:
                try:
                    results = cursor.fetchall()
                except psycopg2.ProgrammingError, e:
                    if str(e) != 'no results to fetch':
                        raise psycopg2.ProgrammingError(e.message)
            cls.conn.commit()
            return results
        except Exception, e:
            raise DbError(
                'Psycopg2 execution error: %s\n. Query: %s - Data: %s\n.' %
                (e.message, query, str(data)))
Beispiel #3
0
        config = cls.config
        try:
            if cls.config.get('password'):
                conn = mysql.connector.Connect(user=config['username'],
                                               password=config['password'],
                                               host=config['host'],
                                               port=config['port'])
            else:
                conn = mysql.connector.Connect(user=config['username'],
                                               host=config['host'],
                                               port=config['port'])
        except mysql.connector.InterfaceError, ex:
            raise DbError("Cannot connect to MySQL Db (1): %s\n"
                          "Ensure that the server is running and you can connect normally" % ex)
        except mysql.connector.ProgrammingError, ex:
            raise DbError("Cannot connect to MySQL Db (2): %s" % ex)
        except db_errors.DatabaseError, er:
            base = 'Cannot not connect to MySQL Db (3): %s, %s\n\n' % (er.errno, er.msg)
            if er.errno == -1 and re.compile('.*insecure.*').match(er.msg) is not None:
                # print some instructions on connecting with new mode
                additional = ("Your MySQL version may be running with old_password compatibility mode."
                              "\nPlease check your CNF files and if necessary change the setting, restart,"
                              "\nand create a new-user or update your existing users to use new auth.")
                raise DbError(base, additional)
            else:
                raise DbError(base)

        return conn

    @classmethod
    def run_file_cmd(cls):