MCPcopy
hub / github.com/daptin/daptin / GetObjectByWhereClause

Method GetObjectByWhereClause

server/resource/dbmethods.go:2181–2230  ·  view source on GitHub ↗
(typeName string, column string, val interface{}, transaction *sqlx.Tx)

Source from the content-addressed store, hash-verified

2179}
2180
2181func (dbResource *DbResource) GetObjectByWhereClause(typeName string, column string, val interface{}, transaction *sqlx.Tx) (map[string]interface{}, error) {
2182 s, q, err := statementbuilder.Squirrel.Select("*").
2183 Prepared(true).From(typeName).Where(goqu.Ex{column: val}).ToSQL()
2184 if err != nil {
2185 return nil, err
2186 }
2187
2188 stmt1, err := transaction.Preparex(s)
2189
2190 if err != nil {
2191 log.Errorf("[1106] failed to prepare statment - [%v]: %v", s, err)
2192 return nil, err
2193 }
2194
2195 defer func(stmt1 *sqlx.Stmt) {
2196 err := stmt1.Close()
2197 if err != nil {
2198 log.Errorf("failed to close prepared statement: %v", err)
2199 }
2200 }(stmt1)
2201
2202 row, err := stmt1.Queryx(q...)
2203
2204 if err != nil {
2205 return nil, err
2206 }
2207
2208 defer func(row *sqlx.Rows) {
2209 err := row.Close()
2210 if err != nil {
2211 log.Errorf("[1029] failed to close result after value scan in defer")
2212 }
2213 }(row)
2214
2215 start := time.Now()
2216 responseArray, err := RowsToMap(row, dbResource.model.GetName())
2217 err = stmt1.Close()
2218 err = row.Close()
2219
2220 m, _, err := dbResource.ResultToArrayOfMapWithTransaction(responseArray, dbResource.Cruds[typeName].model.GetColumnMap(), nil, transaction)
2221 duration := time.Since(start)
2222 log.Tracef("[TIMING] GetObjectByWhere ResultToArray [1946]: %v", duration)
2223
2224 if len(m) == 0 {
2225 log.Printf("[1976] No result found for [%v] [%v][%v]", typeName, column, val)
2226 return nil, errors.New(fmt.Sprintf("no [%v=%v] object found", column, val))
2227 }
2228
2229 return m[0], err
2230}
2231
2232func (dbResource *DbResource) GetObjectByWhereClauseWithTransaction(
2233 typeName string, column string, val interface{}, transaction *sqlx.Tx) (map[string]interface{}, error) {

Callers 8

DaptinSmtpDbResourceFunction · 0.80
DoActionMethod · 0.80
getOutboxMailServerMethod · 0.80
DoActionMethod · 0.80
DoActionMethod · 0.80
RunMethod · 0.80
GetTLSConfigMethod · 0.80
ImportDataMapArrayFunction · 0.80

Calls 7

RowsToMapFunction · 0.85
QueryxMethod · 0.80
GetNameMethod · 0.80
SelectMethod · 0.65
PreparexMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected