Rename class programmatically

Primary tabs

How do I rename class programmatically?

This discussion touches on data move which, is not a concern in my case (BPL renames).

I also don't care about external references.

The best I got is:

  1. Get class text as string
  2. Replace class name in this string
  3. Save new string as new class
  4. Open new class as object
  5. Delete storage
  6. Compile class

Are there any better alternatives?

Also found this discussion.

Answers

Adapted from @krishna muthu  answer

ClassMethod Rename(oldClass, newClass) As %Status
{
    #dim sc As %Status = $$$OK
    
    quit:'##class(%Dictionary.ClassDefinition).%ExistsId(oldClass) $$$ERROR($$$GeneralError, "Old class does not exist")
    quit:##class(%Dictionary.ClassDefinition).%ExistsId(newClass) $$$ERROR($$$GeneralError, "New class already exists")
    
    merge ^oddDEF(newClass) = ^oddDEF(oldClass)
    $$$defClassKeySet(newClass, $$$cCLASSname, newClass) // Set class name
    $$$defClassKeyKill(newClass, $$$cCLASSstorage) // Kill old storage
    
    do UpdClsDef^%occLibrary(newClass)
    
    set sc = $system.OBJ.Compile(newClass, "/displaylog=0 /displayerror=0")
    quit:$$$ISERR(sc) sc
    
    set sc = ##class(%Dictionary.ClassDefinition).%DeleteId(oldClass)
    
    quit sc
}

Comments