11package sqobj
22
33import (
4-
5- // Import Namespaces
64 "context"
75 "fmt"
86 "reflect"
97
8+ // Import Namespaces
109 . "github.com/djthorpe/go-errors"
1110 . "github.com/djthorpe/go-sqlite"
1211
1312 // Packages
14- "github.com/djthorpe/go-sqlite/pkg/sqlite3"
13+ sqlite3 "github.com/djthorpe/go-sqlite/pkg/sqlite3"
1514)
1615
1716///////////////////////////////////////////////////////////////////////////////
1817// TYPES
1918
2019type Objects struct {
21- * sqlite3.Conn
22-
2320 schema string
2421 m map [reflect.Type ]* Class
22+ p SQPool
23+ c SQConnection
2524}
2625
2726///////////////////////////////////////////////////////////////////////////////
2827// LIFECYCLE
2928
30- // With creates an SQObjects with an existing database connection and named schema
31- func With (conn * sqlite3.Conn , schema string , classes ... SQClass ) (* Objects , error ) {
29+ // WithPool creates an SQObjects with a connection pool and named schema
30+ func WithPool (pool SQPool , schema string , classes ... SQClass ) (* Objects , error ) {
31+ objects := new (Objects )
32+
33+ // Check parameters
34+ if pool == nil || len (classes ) == 0 {
35+ return nil , ErrBadParameter .With ("WithPool" )
36+ } else {
37+ objects .p = pool
38+ }
39+
40+ return objects .with (schema , classes ... )
41+ }
42+
43+ // With creates an SQObjects with a database connection and named schema
44+ func With (conn SQConnection , schema string , classes ... SQClass ) (* Objects , error ) {
3245 objects := new (Objects )
3346
3447 // Check parameters
3548 if conn == nil || len (classes ) == 0 {
3649 return nil , ErrBadParameter .With ("With" )
50+ } else {
51+ objects .c = conn
52+ }
53+
54+ return objects .with (schema , classes ... )
55+ }
56+
57+ ///////////////////////////////////////////////////////////////////////////////
58+ // STRINGIFY
59+
60+ func (obj * Objects ) String () string {
61+ str := "<sqobjects"
62+ str += fmt .Sprintf (" schema=%q" , obj .schema )
63+ for _ , c := range obj .m {
64+ str += " " + c .String ()
65+ }
66+ str += fmt .Sprint (" " , obj .SQConnection )
67+ return str + ">"
68+ }
69+
70+ ///////////////////////////////////////////////////////////////////////////////
71+ // PUBLIC METHODS
72+
73+ // Write objects (insert or update) to the database
74+ func (obj * Objects ) Write (ctx context.Context , v ... interface {}) error {
75+ return obj .c .Do (ctx , SQLITE_NONE , func (txn SQTransaction ) error {
76+ for _ , v := range v {
77+ rv := ValueOf (v )
78+ class , exists := obj .m [rv .Type ()]
79+ if ! exists {
80+ return ErrBadParameter .Withf ("Write: %v" , v )
81+ }
82+ if r , err := class .UpsertKeys (txn , v ); err != nil {
83+ return err
84+ } else {
85+ // TODO: Pass rowid and primary keys to next object
86+ fmt .Println (r [0 ])
87+ }
88+ }
89+ return nil
90+ })
91+ }
92+
93+ ///////////////////////////////////////////////////////////////////////////////
94+ // PRIVATE METHODS
95+
96+ func (objects * Objects ) conn (ctx context.Context ) SQConnection {
97+
98+ }
99+
100+ func (objects * Objects ) with (schema string , classes ... SQClass ) (* Objects , error ) {
101+ if schema == "" {
102+ schema = sqlite3 .DefaultSchema
37103 }
104+
105+ // Set connection, classes
106+ objects .m = make (map [reflect.Type ]* Class , len (classes ))
107+ objects .schema = schema
108+
38109 if schema == "" {
39110 schema = sqlite3 .DefaultSchema
40111 }
41112
42113 // Set connection, classes
43- objects .Conn = conn
114+ objects .c = conn
44115 objects .m = make (map [reflect.Type ]* Class , len (classes ))
45116 objects .schema = schema
46117
@@ -49,6 +120,11 @@ func With(conn *sqlite3.Conn, schema string, classes ...SQClass) (*Objects, erro
49120 return nil , ErrNotFound .Withf ("schema %q" , schema )
50121 }
51122
123+ // Error if foreign keys not supported
124+ if ! conn .Flags ().Is (SQLITE_OPEN_FOREIGNKEYS ) {
125+ return nil , ErrBadParameter .With ("SQLITE_OPEN_FOREIGNKEYS" )
126+ }
127+
52128 // Register classes
53129 for _ , class := range classes {
54130 if class , ok := class .(* Class ); ! ok {
@@ -58,11 +134,6 @@ func With(conn *sqlite3.Conn, schema string, classes ...SQClass) (*Objects, erro
58134 }
59135 }
60136
61- // Set foreign keys on
62- if err := conn .SetForeignKeyConstraints (true ); err != nil {
63- return nil , err
64- }
65-
66137 // Create schema - tables, indexes
67138 if err := conn .Do (context .Background (), SQLITE_TXN_NO_FOREIGNKEY_CONSTRAINTS , func (txn SQTransaction ) error {
68139 // Create all classes
@@ -79,38 +150,3 @@ func With(conn *sqlite3.Conn, schema string, classes ...SQClass) (*Objects, erro
79150 // Return success
80151 return objects , nil
81152}
82-
83- ///////////////////////////////////////////////////////////////////////////////
84- // STRINGIFY
85-
86- func (obj * Objects ) String () string {
87- str := "<sqobjects"
88- str += fmt .Sprintf (" schema=%q" , obj .schema )
89- for _ , c := range obj .m {
90- str += " " + c .String ()
91- }
92- str += " " + obj .Conn .String ()
93- return str + ">"
94- }
95-
96- ///////////////////////////////////////////////////////////////////////////////
97- // WRITE OBJECTS
98-
99- // Write objects (insert or update) to the database
100- func (obj * Objects ) Write (ctx context.Context , v ... interface {}) error {
101- return obj .Conn .Do (ctx , SQLITE_NONE , func (txn SQTransaction ) error {
102- for _ , v := range v {
103- rv := ValueOf (v )
104- class , exists := obj .m [rv .Type ()]
105- if ! exists {
106- return ErrBadParameter .Withf ("Write: %v" , v )
107- }
108- if r , err := class .UpsertKeys (txn , v ); err != nil {
109- return err
110- } else {
111- fmt .Println (r [0 ])
112- }
113- }
114- return nil
115- })
116- }
0 commit comments