import re


def mk_primary_key(sqlCreateStr, colName):
    """Add a PRIMARY KEY statement to a column of a SQL create statement"""

    # Regular expressions to pick apart the create statement
    createRe =  re.compile('^(CREATE TABLE|create table) (\w+)\s*\((.+)\)\s*$')
    prikeyRe = re.compile(".*(PRIMARY KEY|primary key)$")

    sqlCreateNewStr = ""
    mch = createRe.match(sqlCreateStr)
    if not mch:
        return sqlCreateStr
    else:
        createStr = mch.group(1).strip()
        tableName = mch.group(2).strip()
        colsDefStr = mch.group(3).strip()
        colDefLst = []
        for colDefStr in colsDefStr.split(','):
            name, dataType = colDefStr.split(" ", 1)
            if name==colName:
                mch = prikeyRe.match(dataType)
                if not mch:
                    dataType += " PRIMARY KEY"
            colDefLst.append("%s %s" % (name, dataType))

        sqlCreateNewStr += createStr + " " + tableName + " ("
        sqlCreateNewStr += ",".join(colDefLst)
        sqlCreateNewStr += ")"

        return sqlCreateNewStr