コード例 #1
0
ファイル: mysql_handler.py プロジェクト: TabbedOut/fake2db
    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
コード例 #2
0
    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
ファイル: mongodb_handler.py プロジェクト: akarca/fake2db
 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
ファイル: mongodb_handler.py プロジェクト: lenciel/fake2db
 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
ファイル: sqlite_handler.py プロジェクト: akarca/fake2db
    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
ファイル: postgresql_handler.py プロジェクト: akarca/fake2db
    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
ファイル: mysql_handler.py プロジェクト: akarca/fake2db
    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