@ -2,12 +2,12 @@ package template
// FindOne defines find row by id.
// FindOne defines find row by id.
var FindOne = `
var FindOne = `
func ( m * default { { . upperStartCamelObject } } Model ) FindOne ( { { . lowerStartCamelPrimaryKey } } { { . dataType } } ) ( * { { . upperStartCamelObject } } , error ) {
func ( m * default { { . upperStartCamelObject } } Model ) FindOne ( ctx context . Context , { { . lowerStartCamelPrimaryKey } } { { . dataType } } ) ( * { { . upperStartCamelObject } } , error ) {
{ { if . withCache } } { { . cacheKey } }
{ { if . withCache } } { { . cacheKey } }
var resp { { . upperStartCamelObject } }
var resp { { . upperStartCamelObject } }
err := m . QueryRow ( & resp , { { . cacheKeyVariable } } , func ( conn sqlx . SqlConn , v interface { } ) error {
err := m . QueryRow Ctx ( ctx , & resp , { { . cacheKeyVariable } } , func ( ctx context . Context , conn sqlx . SqlConn , v interface { } ) error {
query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
return conn . QueryRow ( v , query , { { . lowerStartCamelPrimaryKey } } )
return conn . QueryRow Ctx ( ctx , v , query , { { . lowerStartCamelPrimaryKey } } )
} )
} )
switch err {
switch err {
case nil :
case nil :
@ -18,7 +18,7 @@ func (m *default{{.upperStartCamelObject}}Model) FindOne({{.lowerStartCamelPrima
return nil , err
return nil , err
} { { else } } query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
} { { else } } query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryKey}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
var resp { { . upperStartCamelObject } }
var resp { { . upperStartCamelObject } }
err := m . conn . QueryRow ( & resp , query , { { . lowerStartCamelPrimaryKey } } )
err := m . conn . QueryRow Ctx ( ctx , & resp , query , { { . lowerStartCamelPrimaryKey } } )
switch err {
switch err {
case nil :
case nil :
return & resp , nil
return & resp , nil
@ -35,9 +35,9 @@ var FindOneByField = `
func ( m * default { { . upperStartCamelObject } } Model ) FindOneBy { { . upperField } } ( { { . in } } ) ( * { { . upperStartCamelObject } } , error ) {
func ( m * default { { . upperStartCamelObject } } Model ) FindOneBy { { . upperField } } ( { { . in } } ) ( * { { . upperStartCamelObject } } , error ) {
{ { if . withCache } } { { . cacheKey } }
{ { if . withCache } } { { . cacheKey } }
var resp { { . upperStartCamelObject } }
var resp { { . upperStartCamelObject } }
err := m . QueryRowIndex ( & resp , { { . cacheKeyVariable } } , m . formatPrimary , func ( conn sqlx . SqlConn , v interface { } ) ( i interface { } , e error ) {
err := m . QueryRowIndex Ctx ( ctx , & resp , { { . cacheKeyVariable } } , m . formatPrimary , func ( ctx context . Context , conn sqlx . SqlConn , v interface { } ) ( i interface { } , e error ) {
query := fmt . Sprintf ( "select %s from %s where {{.originalField}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
query := fmt . Sprintf ( "select %s from %s where {{.originalField}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
if err := conn . QueryRow ( & resp , query , { { . lowerStartCamelField } } ) ; err != nil {
if err := conn . QueryRow Ctx ( ctx , & resp , query , { { . lowerStartCamelField } } ) ; err != nil {
return nil , err
return nil , err
}
}
return resp . { { . upperStartCamelPrimaryKey } } , nil
return resp . { { . upperStartCamelPrimaryKey } } , nil
@ -52,7 +52,7 @@ func (m *default{{.upperStartCamelObject}}Model) FindOneBy{{.upperField}}({{.in}
}
}
} { { else } } var resp { { . upperStartCamelObject } }
} { { else } } var resp { { . upperStartCamelObject } }
query := fmt . Sprintf ( "select %s from %s where {{.originalField}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
query := fmt . Sprintf ( "select %s from %s where {{.originalField}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
err := m . conn . QueryRow ( & resp , query , { { . lowerStartCamelField } } )
err := m . conn . QueryRow Ctx ( ctx , & resp , query , { { . lowerStartCamelField } } )
switch err {
switch err {
case nil :
case nil :
return & resp , nil
return & resp , nil
@ -70,14 +70,14 @@ func (m *default{{.upperStartCamelObject}}Model) formatPrimary(primary interface
return fmt . Sprintf ( "%s%v" , { { . primaryKeyLeft } } , primary )
return fmt . Sprintf ( "%s%v" , { { . primaryKeyLeft } } , primary )
}
}
func ( m * default { { . upperStartCamelObject } } Model ) queryPrimary ( c onn sqlx . SqlConn , v , primary interface { } ) error {
func ( m * default { { . upperStartCamelObject } } Model ) queryPrimary ( c tx context . Context , c onn sqlx . SqlConn , v , primary interface { } ) error {
query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
query := fmt . Sprintf ( "select %s from %s where {{.originalPrimaryField}} = {{if .postgreSql}}$1{{else}}?{{end}} limit 1" , { { . lowerStartCamelObject } } Rows , m . table )
return conn . QueryRow ( v , query , primary )
return conn . QueryRow Ctx ( ctx , v , query , primary )
}
}
`
`
// FindOneMethod defines find row method.
// FindOneMethod defines find row method.
var FindOneMethod = ` FindOne( {{ .lowerStartCamelPrimaryKey }} {{ .dataType }} ) (* {{ .upperStartCamelObject }} , error) `
var FindOneMethod = ` FindOne( ctx context.Context, {{ .lowerStartCamelPrimaryKey }} {{ .dataType }} ) (* {{ .upperStartCamelObject }} , error) `
// FindOneByFieldMethod defines find row by field method.
// FindOneByFieldMethod defines find row by field method.
var FindOneByFieldMethod = ` FindOneBy {{ .upperField }} ( {{ .in }} ) (* {{ .upperStartCamelObject }} , error) `
var FindOneByFieldMethod = ` FindOneBy {{ .upperField }} ( ctx context.Context, {{ .in }} ) (* {{ .upperStartCamelObject }} , error) `