Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
| ... | ... |
@@ -79,46 +79,43 @@ func NewDatabase(conn *sql.DB) (*Database, error) {
|
| 79 | 79 |
} |
| 80 | 80 |
db := &Database{conn: conn}
|
| 81 | 81 |
|
| 82 |
- if _, err := conn.Exec(createEntityTable); err != nil {
|
|
| 82 |
+ // Create root entities |
|
| 83 |
+ tx, err := conn.Begin() |
|
| 84 |
+ if err != nil {
|
|
| 83 | 85 |
return nil, err |
| 84 | 86 |
} |
| 85 |
- if _, err := conn.Exec(createEdgeTable); err != nil {
|
|
| 87 |
+ |
|
| 88 |
+ if _, err := tx.Exec(createEntityTable); err != nil {
|
|
| 86 | 89 |
return nil, err |
| 87 | 90 |
} |
| 88 |
- if _, err := conn.Exec(createEdgeIndices); err != nil {
|
|
| 91 |
+ if _, err := tx.Exec(createEdgeTable); err != nil {
|
|
| 89 | 92 |
return nil, err |
| 90 | 93 |
} |
| 91 |
- |
|
| 92 |
- rollback := func() {
|
|
| 93 |
- conn.Exec("ROLLBACK")
|
|
| 94 |
- } |
|
| 95 |
- |
|
| 96 |
- // Create root entities |
|
| 97 |
- if _, err := conn.Exec("BEGIN"); err != nil {
|
|
| 94 |
+ if _, err := tx.Exec(createEdgeIndices); err != nil {
|
|
| 98 | 95 |
return nil, err |
| 99 | 96 |
} |
| 100 | 97 |
|
| 101 |
- if _, err := conn.Exec("DELETE FROM entity where id = ?", "0"); err != nil {
|
|
| 102 |
- rollback() |
|
| 98 |
+ if _, err := tx.Exec("DELETE FROM entity where id = ?", "0"); err != nil {
|
|
| 99 |
+ tx.Rollback() |
|
| 103 | 100 |
return nil, err |
| 104 | 101 |
} |
| 105 | 102 |
|
| 106 |
- if _, err := conn.Exec("INSERT INTO entity (id) VALUES (?);", "0"); err != nil {
|
|
| 107 |
- rollback() |
|
| 103 |
+ if _, err := tx.Exec("INSERT INTO entity (id) VALUES (?);", "0"); err != nil {
|
|
| 104 |
+ tx.Rollback() |
|
| 108 | 105 |
return nil, err |
| 109 | 106 |
} |
| 110 | 107 |
|
| 111 |
- if _, err := conn.Exec("DELETE FROM edge where entity_id=? and name=?", "0", "/"); err != nil {
|
|
| 112 |
- rollback() |
|
| 108 |
+ if _, err := tx.Exec("DELETE FROM edge where entity_id=? and name=?", "0", "/"); err != nil {
|
|
| 109 |
+ tx.Rollback() |
|
| 113 | 110 |
return nil, err |
| 114 | 111 |
} |
| 115 | 112 |
|
| 116 |
- if _, err := conn.Exec("INSERT INTO edge (entity_id, name) VALUES(?,?);", "0", "/"); err != nil {
|
|
| 117 |
- rollback() |
|
| 113 |
+ if _, err := tx.Exec("INSERT INTO edge (entity_id, name) VALUES(?,?);", "0", "/"); err != nil {
|
|
| 114 |
+ tx.Rollback() |
|
| 118 | 115 |
return nil, err |
| 119 | 116 |
} |
| 120 | 117 |
|
| 121 |
- if _, err := conn.Exec("COMMIT"); err != nil {
|
|
| 118 |
+ if err := tx.Commit(); err != nil {
|
|
| 122 | 119 |
return nil, err |
| 123 | 120 |
} |
| 124 | 121 |
|
| ... | ... |
@@ -135,33 +132,32 @@ func (db *Database) Set(fullPath, id string) (*Entity, error) {
|
| 135 | 135 |
db.mux.Lock() |
| 136 | 136 |
defer db.mux.Unlock() |
| 137 | 137 |
|
| 138 |
- rollback := func() {
|
|
| 139 |
- db.conn.Exec("ROLLBACK")
|
|
| 140 |
- } |
|
| 141 |
- if _, err := db.conn.Exec("BEGIN EXCLUSIVE"); err != nil {
|
|
| 138 |
+ tx, err := db.conn.Begin() |
|
| 139 |
+ if err != nil {
|
|
| 142 | 140 |
return nil, err |
| 143 | 141 |
} |
| 142 |
+ |
|
| 144 | 143 |
var entityID string |
| 145 |
- if err := db.conn.QueryRow("SELECT id FROM entity WHERE id = ?;", id).Scan(&entityID); err != nil {
|
|
| 144 |
+ if err := tx.QueryRow("SELECT id FROM entity WHERE id = ?;", id).Scan(&entityID); err != nil {
|
|
| 146 | 145 |
if err == sql.ErrNoRows {
|
| 147 |
- if _, err := db.conn.Exec("INSERT INTO entity (id) VALUES(?);", id); err != nil {
|
|
| 148 |
- rollback() |
|
| 146 |
+ if _, err := tx.Exec("INSERT INTO entity (id) VALUES(?);", id); err != nil {
|
|
| 147 |
+ tx.Rollback() |
|
| 149 | 148 |
return nil, err |
| 150 | 149 |
} |
| 151 | 150 |
} else {
|
| 152 |
- rollback() |
|
| 151 |
+ tx.Rollback() |
|
| 153 | 152 |
return nil, err |
| 154 | 153 |
} |
| 155 | 154 |
} |
| 156 | 155 |
e := &Entity{id}
|
| 157 | 156 |
|
| 158 | 157 |
parentPath, name := splitPath(fullPath) |
| 159 |
- if err := db.setEdge(parentPath, name, e); err != nil {
|
|
| 160 |
- rollback() |
|
| 158 |
+ if err := db.setEdge(parentPath, name, e, tx); err != nil {
|
|
| 159 |
+ tx.Rollback() |
|
| 161 | 160 |
return nil, err |
| 162 | 161 |
} |
| 163 | 162 |
|
| 164 |
- if _, err := db.conn.Exec("COMMIT"); err != nil {
|
|
| 163 |
+ if err := tx.Commit(); err != nil {
|
|
| 165 | 164 |
return nil, err |
| 166 | 165 |
} |
| 167 | 166 |
return e, nil |
| ... | ... |
@@ -179,7 +175,7 @@ func (db *Database) Exists(name string) bool {
|
| 179 | 179 |
return e != nil |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 |
-func (db *Database) setEdge(parentPath, name string, e *Entity) error {
|
|
| 182 |
+func (db *Database) setEdge(parentPath, name string, e *Entity, tx *sql.Tx) error {
|
|
| 183 | 183 |
parent, err := db.get(parentPath) |
| 184 | 184 |
if err != nil {
|
| 185 | 185 |
return err |
| ... | ... |
@@ -188,7 +184,7 @@ func (db *Database) setEdge(parentPath, name string, e *Entity) error {
|
| 188 | 188 |
return fmt.Errorf("Cannot set self as child")
|
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 |
- if _, err := db.conn.Exec("INSERT INTO edge (parent_id, name, entity_id) VALUES (?,?,?);", parent.id, name, e.id); err != nil {
|
|
| 191 |
+ if _, err := tx.Exec("INSERT INTO edge (parent_id, name, entity_id) VALUES (?,?,?);", parent.id, name, e.id); err != nil {
|
|
| 192 | 192 |
return err |
| 193 | 193 |
} |
| 194 | 194 |
return nil |
| ... | ... |
@@ -371,18 +367,15 @@ func (db *Database) Purge(id string) (int, error) {
|
| 371 | 371 |
db.mux.Lock() |
| 372 | 372 |
defer db.mux.Unlock() |
| 373 | 373 |
|
| 374 |
- rollback := func() {
|
|
| 375 |
- db.conn.Exec("ROLLBACK")
|
|
| 376 |
- } |
|
| 377 |
- |
|
| 378 |
- if _, err := db.conn.Exec("BEGIN"); err != nil {
|
|
| 374 |
+ tx, err := db.conn.Begin() |
|
| 375 |
+ if err != nil {
|
|
| 379 | 376 |
return -1, err |
| 380 | 377 |
} |
| 381 | 378 |
|
| 382 | 379 |
// Delete all edges |
| 383 |
- rows, err := db.conn.Exec("DELETE FROM edge WHERE entity_id = ?;", id)
|
|
| 380 |
+ rows, err := tx.Exec("DELETE FROM edge WHERE entity_id = ?;", id)
|
|
| 384 | 381 |
if err != nil {
|
| 385 |
- rollback() |
|
| 382 |
+ tx.Rollback() |
|
| 386 | 383 |
return -1, err |
| 387 | 384 |
} |
| 388 | 385 |
|
| ... | ... |
@@ -392,14 +385,15 @@ func (db *Database) Purge(id string) (int, error) {
|
| 392 | 392 |
} |
| 393 | 393 |
|
| 394 | 394 |
// Delete entity |
| 395 |
- if _, err := db.conn.Exec("DELETE FROM entity where id = ?;", id); err != nil {
|
|
| 396 |
- rollback() |
|
| 395 |
+ if _, err := tx.Exec("DELETE FROM entity where id = ?;", id); err != nil {
|
|
| 396 |
+ tx.Rollback() |
|
| 397 | 397 |
return -1, err |
| 398 | 398 |
} |
| 399 | 399 |
|
| 400 |
- if _, err := db.conn.Exec("COMMIT"); err != nil {
|
|
| 400 |
+ if err := tx.Commit(); err != nil {
|
|
| 401 | 401 |
return -1, err |
| 402 | 402 |
} |
| 403 |
+ |
|
| 403 | 404 |
return int(changes), nil |
| 404 | 405 |
} |
| 405 | 406 |
|