示例#1
0
    def database_caller_creator(self, host, port, password, name=None):
        '''creates a mysql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None

        try:
            if name:
                db = name
            else:
                db = 'mysql_' + str_generator(self)

            conn = mysql.connector.connect(user='******', host=host, port=port, password=password)
            cursor = conn.cursor()
            cursor.execute('CREATE DATABASE IF NOT EXISTS ' + db)
            cursor.execute('USE ' + db)
            logger.warning('Database created and opened succesfully: %s' % db, extra=extra_information)

        except mysql.connector.Error as err:
            logger.error(err.msg, extra=extra_information)
            sys.exit(1)

        return cursor, conn
    def database_caller_creator(self, host, port, name=None):
        '''creates a postgresql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None
        username = getpass.getuser()

        try:
            if name:
                db = name
            else:
                db = 'postgresql_' + str_generator(self)

            subprocess.Popen("createdb --no-password --owner " + username +
                             " " + db,
                             shell=True)
            time.sleep(1)
            conn = psycopg2.connect("dbname=" + db + " user="******" host=" + host + " port=" + port)
            cursor = conn.cursor()
            logger.warning('Database created and opened succesfully: %s' % db,
                           extra=d)
        except Exception as err:
            logger.error(err, extra=d)

        return cursor, conn
示例#3
0
    def database_caller_creator(self, username, password, host, port, name=None):
        '''creates a postgresql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None

        if name:
            dbname = name
        else:
            dbname = 'postgresql_' + str_generator(self).lower()
        try:
            # createdb
            conn = psycopg2.connect(
                user=username, password=password, host=host, port=port)
            conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
            cur = conn.cursor()
            cur.execute('CREATE DATABASE %s;' % dbname)
            cur.close()
            conn.close()
            # reconnect to the new database
            conn = psycopg2.connect(user=username, password=password,
                                    host=host, port=port, database=dbname)
            cursor = conn.cursor()
            logger.warning('Database created and opened succesfully: %s' % dbname, extra=d)
        except Exception as err:
            logger.error(err, extra=d)
            raise

        return cursor, conn
示例#4
0
    def database_caller_creator(self, host, port, password, name=None):
        '''creates a mysql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None

        try:
            if name:
                db = name
            else:
                db = 'mysql_' + str_generator(self)

            conn = mysql.connector.connect(user='******', host=host, port=port, password=password)
            cursor = conn.cursor()
            cursor.execute('CREATE DATABASE IF NOT EXISTS ' + db)
            cursor.execute('USE ' + db)
            logger.warning('Database created and opened succesfully: %s' % db, extra=extra_information)

        except mysql.connector.Error as err:
            logger.error(err.msg, extra=extra_information)
            sys.exit(1)

        return cursor, conn
示例#5
0
 def database_caller_creator(self):
     '''creates a mongodb database
     returns the related connection object
     which will be later used to spawn the cursor
     '''
             
     client = pymongo.MongoClient()
     db = client['mongodb_' + str_generator(self)]
     
     return db
示例#6
0
    def database_caller_creator(self,
                                number_of_rows,
                                username,
                                password,
                                host,
                                port,
                                name=None,
                                custom=None):
        '''creates a postgresql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None

        if name:
            dbname = name
        else:
            dbname = 'postgresql_' + str_generator(self).lower()
        try:
            # createdb
            conn = psycopg2.connect(user=username,
                                    password=password,
                                    host=host,
                                    port=port)
            conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
            cur = conn.cursor()
            cur.execute('CREATE DATABASE %s;' % dbname)
            cur.close()
            conn.close()
            # reconnect to the new database
            conn = psycopg2.connect(user=username,
                                    password=password,
                                    host=host,
                                    port=port,
                                    database=dbname)
            cursor = conn.cursor()
            logger.warning('Database created and opened succesfully: %s' %
                           dbname,
                           extra=d)
        except Exception as err:
            logger.error(err, extra=d)
            raise

        if custom:
            self.custom_db_creator(number_of_rows, cursor, conn, custom)
            cursor.close()
            conn.close()
            sys.exit(0)

        return cursor, conn
示例#7
0
 def database_caller_creator(self, host, port, name=None):
     '''creates a mongodb database
     returns the related connection object
     which will be later used to spawn the cursor
     '''
             
     client = pymongo.MongoClient(host, port)
     
     if name:
         db = client[name]
     else:
         db = client['mongodb_' + str_generator(self)]
     
     return db
示例#8
0
    def database_caller_creator(self, host, port, name=None):
        '''creates a mongodb database
        returns the related connection object
        which will be later used to spawn the cursor
        '''

        client = pymongo.MongoClient(host, port)

        if name:
            db = client[name]
        else:
            db = client['mongodb_' + str_generator(self)]

        return db
示例#9
0
    def database_caller_creator(self):
        '''creates a sqlite3 db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        database = ''
        try:
            database = 'sqlite_' + str_generator(self) + '.db'
            conn = sqlite3.connect(database)
            logger.warning('Database created and opened succesfully: %s' % database, extra=d)
        except:
            logger.error('Failed to connect or create database / sqlite3', extra=d)
            raise DbConnException

        return conn
示例#10
0
    def database_caller_creator(self, name=None):
        '''creates a sqlite3 db
        returns the related connection object
        which will be later used to spawn the cursor
        '''

        try:
            if name:
                database = name + '.db'
            else:
                database = 'sqlite_' + str_generator(self) + '.db'

            conn = sqlite3.connect(database)
            logger.warning('Database created and opened succesfully: %s' % database, extra=d)
        except Exception:
            logger.error('Failed to connect or create database / sqlite3', extra=d)
            raise DbConnException

        return conn
示例#11
0
    def database_caller_creator(self):
        '''creates a postgresql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None
        username = getpass.getuser()
        
        try:
            db = 'postgresql_' + str_generator(self)
            subprocess.Popen("createdb --no-password --owner " + username + " " + db, shell=True)
            time.sleep(1)
            conn = psycopg2.connect("dbname=" + db + " user=" + username)
            cursor = conn.cursor()
            logger.warning('Database created and opened succesfully: %s' % db, extra=d)
        except Exception as err:
            logger.error(err, extra=d)

        return cursor, conn
示例#12
0
    def database_caller_creator(self):
        '''creates a mysql db
        returns the related connection object
        which will be later used to spawn the cursor
        '''
        cursor = None
        conn = None
        
        try:
            db = 'mysql_' + str_generator(self)
            conn = mysql.connector.connect(user='******', host='localhost')
            cursor = conn.cursor()
            cursor.execute('CREATE DATABASE IF NOT EXISTS ' + db)
            cursor.execute('USE ' + db)
            logger.warning('Database created and opened succesfully: %s' % db, extra=d)

        except mysql.connector.Error as err:
            logger.error(err.message, extra=d)

        return cursor, conn