예제 #1
0
def insertRoles(roleList):
    noOfEntries = len(roleList)

    if noOfEntries <= 0: 
        return 0

    dbHost, dbUser, dbPassword, dbName = getConfigParameters.getDBConnectionParameters()
    maxRecordsInOneCommit = getConfigParameters.getMaxInsertsInOneCommit()

    try:
        mysqlDB = MySQLdb.connect(dbHost, dbUser, dbPassword, dbName)
        mysqlDB.autocommit(False)
        cursor = mysqlDB.cursor()
    except:
        print 'Cannot open connection to database '
        return 0
        pass

    savepointStmt = "SAVEPOINT A" 
    count = 0
    noOfCommits = 0

    for entry in roleList:
        userName = entry[0]
        sphereType = entry[1]
        sphereName = entry[2]
        role = entry[3]

        SQLStatement = "INSERT INTO user_roles (user_name, sphere_type, sphere_name, role) VALUES ('" \
                            + userName + "', '" \
                            + sphereType + "', '"\
                            + sphereName + "', '" \
                            + role + "')"
        #print SQLStatement

        try:
            cursor.execute(SQLStatement)
            cursor.execute(savepointStmt)
            count = count + 1
        except MySQLdb.IntegrityError, message:
            #print 'Role exists for user(%s %s %s %s)' %(userName, sphereType, sphereName, role)
            pass

        if count >= maxRecordsInOneCommit:
            count = 0
            try:
                mysqlDB.commit()
                noOfCommits = noOfCommits + 1
            except:
                continue
예제 #2
0
def insertUserGroupMapping(userGroupList):
    dbHost, dbUser, dbPassword, dbName = getConfigParameters.getDBConnectionParameters()
    maxRecordsInOneCommit = getConfigParameters.getMaxInsertsInOneCommit()

    userName = ""
    groupName = ""
    count = 0

    listSize = len(userGroupList)

    if listSize <= 0:
        return 0

    savepointStmt = "SAVEPOINT A"
    try:
        mysqlDB = MySQLdb.connect(dbHost, dbUser, dbPassword, dbName)
        mysqlDB.autocommit(False)
        cursor = mysqlDB.cursor()
    except:
        return 0
    noOfCommits = 0

    cursor.execute(savepointStmt)

    for tuple in userGroupList:
        groupName = tuple[0]
        userName = tuple[1]

        SQLStatement = "INSERT INTO user_group_mapping (user_name, group_name) VALUES ('" \
                           + userName + "', '" \
                           + groupName + "')"

        try:
            cursor.execute(SQLStatement)
            cursor.execute(savepointStmt)
            count = count + 1
        except MySQLdb.IntegrityError, message:
            #print 'User %s is already present in group %s' % (userName, groupName)
            pass

        if count >= maxRecordsInOneCommit:
            count = 0
            try:
                mysqlDB.commit()
                noOfCommits = noOfCommits + 1
            except:
                continue 
예제 #3
0
def insertEGroups(eGroupList):
    noOfEntries = len(eGroupList)

    if noOfEntries <= 0:
        return 0

    dbHost, dbUser, dbPassword, dbName = getConfigParameters.getDBConnectionParameters()
    maxRecordsInOneCommit = getConfigParameters.getMaxInsertsInOneCommit()

    try:
        mysqlDB = MySQLdb.connect(dbHost, dbUser, dbPassword, dbName)
        mysqlDB.autocommit(False)
        cursor = mysqlDB.cursor()
    except:
        print 'Cannot open connection to database '
        return 0
        pass

    savepointStmt = "SAVEPOINT A"
    count = 0
    noOfCommits = 0

    for groupName in eGroupList:

        SQLStatement = "INSERT INTO egroups(name) VALUES ('" \
                            + groupName + "')"
        #print SQLStatement

        try:
            cursor.execute(SQLStatement)
            cursor.execute(savepointStmt)
            count = count + 1
        except MySQLdb.IntegrityError, message:
            #print 'E-group %s exists..' %(groupName)
            pass

        if count >= maxRecordsInOneCommit:
            count = 0
            try:
                mysqlDB.commit()
                noOfCommits = noOfCommits + 1
            except:
                continue