在Go中实现基本的ORM

I'm trying to design a basic ORM for a data app that I'm working on. My question about the model I came up with is 2-fold:

  • Is this the 'best' most 'efficient' model for database tracking?
  • Is this idiomatic Go?

The idea is to load a complete model of the db into memory at app start. Use this model to generate a map with a crc32 hash of each object (corresponding to a row in the database). This is what we use to compare with model to find specifically where changes have been made when .save() is called.

The first part of the following generates the crc32 map, the second part introduces a random change, and the last part would be part of .save() to write db changes to disk

The code:

func main() {
    // Read data off of disk into memory
    memDB := ddb

    // Create hash map of memDB
    peopleMap := make(map[int]uint32)
    for _, v := range memDB.people {
        // make row into byte array, looks kludgy
        hash := []byte(fmt.Sprintf("%#v", v))
        peopleMap[v.pID] = crc32.ChecksumIEEE(hash)
        fmt.Printf("%v: %v %v \t(%v %v) - crc sum: %v
",
            v.pID, v.fName, v.lName, v.job, v.location,
            peopleMap[v.pID])
    }
    fmt.Println("
# of people in memory:", len(memDB.people))

    // Sometime later, we need to delete Danielle, so
    // Delete in memory:
    var tmpSlice []ddPerson
    for _, v := range memDB.people {
        if v.fName == "Danielle" {
            continue
        }
        tmpSlice = append(tmpSlice, v)
    }
    memDB.people = tmpSlice
    fmt.Println("# of people in memory:", len(memDB.people))

    // Okay, we save in-memory representation mem.DB back
    // to disk with some kind of .save() assertion
    // a len(peopleMap) comparison to len(memDB.people) will
    // tell us there has been a change for INSERTS and
    // DELETES, but it won't tell us about updates or which
    // record was inserted or deleted

    // First, check for additions
    if len(peopleMap) < len(memDB.people) {
        // Code to find and add person to disk db ddb here
        fmt.Println("Adding someone to disk database...")
    } else if len(peopleMap) > len(memDB.people) {
        // Check for deletions
        fmt.Println("Purging someone from disk database...")
    }

    // in any case, recheck hashes
    tMap := make(map[int]uint32)
    for _, v := range memDB.people {
        hash := []byte(fmt.Sprintf("%#v", v))
        t := crc32.ChecksumIEEE(hash)
        // Add to temporary map
        tMap[v.pID] = t
        // And check for changes
        if t != peopleMap[v.pID] {
            fmt.Println("Change detected in in-memory model...")
            fmt.Println("Writing changes to disk db now")
            // Writing any changes to DB here
            ddb.people = memDB.people
        }
    }

    // 'Fix' our hashmap checker deal
    peopleMap = tMap

    // Carry on
}

There is a working version at: http://play.golang.org/p/XMTmynNy7t

You've implemented an in memory cache of your db which isn't really the same thing as an ORM.

There are several problems with what you have:

  • If some other process/application ever modifies the database your in memory model will be out of date. This could cause your writes to clobber writes to the db because you didn't know your cache was stale.
  • If the db gets large you're application will get correspondingly large.

Most ORM's provide a way to map a type (eg. structs) to a db read and write. This allows you to read a single object from the database modify it and then write that object back to the database. That is probably a better approach for this.

As for how idiomatic your go is I didn't see obviously non idiomatic go but there also isn't much going on in it. At the top memDb := ddb is a function call but you don't have parens after it. This is a syntax error.

Generating a checksum when loading the data and again when saving it seems pretty inefficient to me.

Most web applications that use a database backend make changes directly to the database. But apparently you want to have a separate "Save" operation so that the user can back out of his changes…?

Have you considered using transactions? You could issue a BEGIN TRANSACTION command when the user starts editing the data. Then when he presses "Save", you would issue a COMMIT command, permanently storing the changes in the database. If the user backs out of his changes or quits without saving, you would issue a ROLLBACK command instead.

The issue here isn't so much about idiomatic use of Go as about idiomatic use of SQL.