MCPcopy Create free account
hub / github.com/upper/db / PrimaryKeys

Method PrimaryKeys

adapter/sqlite/database.go:153–189  ·  view source on GitHub ↗
(sess sqladapter.Session, tableName string)

Source from the content-addressed store, hash-verified

151}
152
153func (*database) PrimaryKeys(sess sqladapter.Session, tableName string) ([]string, error) {
154 pk := make([]string, 0, 1)
155
156 stmt := exql.RawSQL(fmt.Sprintf("PRAGMA TABLE_INFO('%s')", tableName))
157
158 rows, err := sess.SQL().Query(stmt)
159 if err != nil {
160 return nil, err
161 }
162
163 columns := []struct {
164 Name string `db:"name"`
165 PK int `db:"pk"`
166 }{}
167
168 if err := sess.SQL().NewIterator(rows).All(&columns); err != nil {
169 return nil, err
170 }
171
172 maxValue := -1
173
174 for _, column := range columns {
175 if column.PK > 0 && column.PK > maxValue {
176 maxValue = column.PK
177 }
178 }
179
180 if maxValue > 0 {
181 for _, column := range columns {
182 if column.PK > 0 {
183 pk = append(pk, column.Name)
184 }
185 }
186 }
187
188 return pk, nil
189}

Callers

nothing calls this directly

Calls 5

RawSQLFunction · 0.92
QueryMethod · 0.65
SQLMethod · 0.65
AllMethod · 0.65
NewIteratorMethod · 0.65

Tested by

no test coverage detected