示例#1
0
    def createDatabase(self):
        sqlfile = file("data/scripts/openihmdb_mysql.sql", "r")
        commands = sqlfile.read()
        commandlist = commands.split(";")
        sqlfile.close()
        try:
            dbinfo = self.config.superuser_dbinfo().copy()
            del dbinfo["database"]
            db = Connect(**dbinfo)
            cursor = db.cursor()

            for command in commandlist:
                if not command.isspace():
                    cursor.execute(command)

            updatestr = "latest update on %s" % (date.today().isoformat())
            query = "INSERT INTO dbupdate VALUES('%s')" % updatestr
            cursor.execute(query)
            db.commit()

            cursor.close()
            db.close()

            return True

        except errors.InterfaceError, e:
            return False
示例#2
0
 def _ddl_command(self, query, params=None):
     config = self.config.superuser_dbinfo().copy()
     config['database'] = 'mysql'
     db = Connect(**config)
     cursor = db.cursor()
     cursor.execute(query, params)
     db.commit()
     db.close()
示例#3
0
 def execute_instruction(self, query, data=None):
     """
     Yet another wrapper around execute
     """
     db = Connect(**self.config.superuser_dbinfo().copy())
     cursor = db.cursor()
     cursor.execute(query, data)
     db.commit()
     db.close()
     return cursor
示例#4
0
    def updateDatabase(self):
        # if database is already up to date return
        if self.databaseUpToDate():

            return True
        # else update the database
        else:
            sqlfile = file("data/scripts/openihmdb_mysql_update.sql", "r")
            commands = sqlfile.read()
            commandlist = commands.split(";")
            sqlfile.close()
            try:
                dbinfo = self.config.superuser_dbinfo().copy()
                db = Connect(**dbinfo)
                cursor = db.cursor()

                for command in commandlist:
                    if not command.isspace():
                        cursor.execute(command)

                updatestr = "latest update on %s" % (date.today().isoformat())

                query = "UPDATE dbupdate SET lastupdate='%s'" % updatestr
                cursor.execute(query)
                db.commit()

                cursor.close()
                db.close()

                return True

            except errors.InterfaceError, e:
                print e
                return False

            except (errors.OperationalError, errors.ProgrammingError) as e:
                print e
                return False
示例#5
0
    def databaseUpToDate(self):
        # check if the database is already up to date: i.e. there is "summary" in standardofliving
        #   checks in assets

        config = self.config
        dbinfo = config.dbinfo().copy()
        db = Connect(**dbinfo)
        cursor = db.cursor()

        query = "SHOW TABLES"

        cursor.execute(query)
        rows = cursor.fetchall()

        upToDate = False
        for row in rows:
            if row[0] == "dbupdate":
                query = "SELECT lastupdate FROM dbupdate"
                cursor.execute(query)
                rows = cursor.fetchall()

                if len(rows) != 0:
                    for row in rows:
                        if row[0] > self.latestupdatestring:
                            upToDate = True
                else:
                    updatestr = "latest update on %s" % (date.today().isoformat())
                    query = "INSERT INTO dbupdate VALUES('%s')" % updatestr
                    cursor.execute(query)
                    db.commit()

                    upToDate = True

        cursor.close()
        db.close()

        return upToDate
    def createDatabase(self):
        sqlfile = file('data/scripts/openihmdb_mysql.sql', 'r')
        commands = sqlfile.read()
        commandlist = commands.split(';')
        sqlfile.close()
        try:
            dbinfo = self.config.superuser_dbinfo().copy()
            database = dbinfo['database']
            del dbinfo['database']
            db = Connect(**dbinfo)
            cursor = db.cursor()
            # FIXME: long term we should look at changing the character set.
            cursor.execute("CREATE SCHEMA IF NOT EXISTS `%s` DEFAULT CHARACTER SET latin1 ;" % database)
            cursor.execute("USE `%s`;" % database)

            for command in commandlist:
                if ( not command.isspace() ):
                    cursor.execute(command)
            try:
                if self.config.user != self.config.superuser:
                    cursor.execute("GRANT ALL ON %s.* TO %s@localhost IDENTIFIED BY '%s';" % 
                                    (database, self.config.user, self.config.password)) 
            except ( errors.OperationalError,  errors.ProgrammingError) as e:
                print e.msg
                
            updatestr = "latest update on %s" % (date.today().isoformat())      
            query = "INSERT INTO dbupdate VALUES('%s')" % updatestr
            cursor.execute(query)
            db.commit()
            
            cursor.close()
            db.close()
            
            return True
        
        except errors.InterfaceError,  e:
            return False