MCPcopy Index your code
hub / github.com/upper/db / Insert

Method Insert

adapter/sqlite/collection.go:35–70  ·  view source on GitHub ↗
(col sqladapter.Collection, item interface{})

Source from the content-addressed store, hash-verified

33}
34
35func (*collectionAdapter) Insert(col sqladapter.Collection, item interface{}) (interface{}, error) {
36 columnNames, columnValues, err := sqlbuilder.Map(item, nil)
37 if err != nil {
38 return nil, err
39 }
40
41 pKey, err := col.PrimaryKeys()
42 if err != nil {
43 return nil, err
44 }
45
46 q := col.SQL().InsertInto(col.Name()).
47 Columns(columnNames...).
48 Values(columnValues...)
49
50 var res sql.Result
51 if res, err = q.Exec(); err != nil {
52 return nil, err
53 }
54
55 if len(pKey) <= 1 {
56 return res.LastInsertId()
57 }
58
59 keyMap := db.Cond{}
60
61 for i := range columnNames {
62 for j := 0; j < len(pKey); j++ {
63 if pKey[j] == columnNames[i] {
64 keyMap[pKey[j]] = columnValues[i]
65 }
66 }
67 }
68
69 return keyMap, nil
70}

Callers

nothing calls this directly

Calls 8

MapFunction · 0.92
PrimaryKeysMethod · 0.65
ValuesMethod · 0.65
ColumnsMethod · 0.65
InsertIntoMethod · 0.65
SQLMethod · 0.65
NameMethod · 0.65
ExecMethod · 0.65

Tested by

no test coverage detected