/
table_sync.go
44 lines (37 loc) · 1.41 KB
/
table_sync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package database
import (
"fmt"
"reflect"
"github.com/Gophercraft/phylactery/database/storage"
)
func (table *Table) Schema() *storage.TableSchemaStructure {
return table.container.engine.Schema(table.table)
}
func (table *Table) SyncSchema(schema *storage.TableSchemaStructure) error {
// Tell engine to update the underlying table storage (may be quite slow)
return table.container.engine.SyncTable(table.table, schema)
}
func (table *Table) SyncType(structure_type reflect.Type) error {
// but if that isn't pointing to a struct, that's an error case
if structure_type.Kind() != reflect.Struct {
return fmt.Errorf("passed structure type isn't a structure: '%s'", structure_type.String())
}
// Schematize (transform Go type into a persistent format)
schematized_structure, err := storage.SchematizeStructureType(structure_type)
if err != nil {
return err
}
// Sync
return table.SyncSchema(schematized_structure)
}
// Sync schematizes a struct type and applies that schema to the underlying table.
// Warning: may be slow.
func (table *Table) Sync(prototype any) error {
structure_type := reflect.TypeOf(prototype)
// Typically you would pass a pointer of an empty struct here.
// That's what we did when we were using Xorm ¯\_(ツ)_/¯
if structure_type.Kind() == reflect.Pointer {
structure_type = structure_type.Elem()
}
return table.SyncType(structure_type)
}