github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/sqlx/gen/model_list.go (about) 1 package gen 2 3 import ( 4 "bytes" 5 "fmt" 6 "go/types" 7 "strings" 8 ) 9 10 func (m *Model) methodsForList() string { 11 return strings.Join([]string{ 12 m.methodsForFetchList(), 13 m.methodsForBatchList(), 14 }, "\n\n") 15 } 16 17 func (m *Model) methodsForFetchList() string { 18 buf := &bytes.Buffer{} 19 20 m.ParseTo(buf, fmt.Sprintf( 21 ` 22 {{ $method := "FetchList" }} 23 type {{ .StructName }}List []{{ .StructName }} 24 25 // deprecated 26 func ({{ var .StructName }}List *{{ .StructName }}List) {{ $method }}(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB, size int32, offset int32, conditions ...*{{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.Condition) (count int32, err error) { 27 *{{ var .StructName }}List, count, err = (&{{ .StructName }}{}).FetchList(db, size, offset, conditions...) 28 return 29 } 30 31 func ({{ var .StructName }} *{{ .StructName }}) {{ $method }}(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB, size int32, offset int32, conditions ...*{{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.Condition) ({{ var .StructName }}List {{ .StructName }}List, count int32, err error) { 32 {{ var .StructName }}List = {{ .StructName }}List{} 33 34 table := {{ var .StructName }}.T() 35 36 condition := {{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.And(conditions...) 37 {{ if .HasSoftDelete }} 38 condition = {{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.And(condition, table.F("{{ .FieldSoftDelete }}").Eq({{ use .ConstSoftDeleteTrue }})) 39 {{ end }} 40 41 stmt := table.Select(). 42 Comment("{{ .StructName }}.{{ $method }}"). 43 Where(condition) 44 45 errForCount := db.Do(stmt.For({{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.Count({{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.Star()))).Scan(&count).Err() 46 if errForCount != nil { 47 err = errForCount 48 return 49 } 50 51 stmt = stmt.Limit(size).Offset(offset) 52 {{ if .HasCreatedAt }} 53 stmt = stmt.OrderDescBy(table.F("{{ .FieldCreatedAt }}")) 54 {{ end }} 55 56 err = db.Do(stmt).Scan(&{{ var .StructName }}List).Err() 57 58 return 59 } 60 `)) 61 62 m.ParseTo(buf, fmt.Sprintf( 63 ` 64 {{ $method := "List" }} 65 66 func ({{ var .StructName }} *{{ .StructName }}) {{ $method }}(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB, condition *{{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.Condition) ({{ var .StructName }}List {{ .StructName }}List, err error) { 67 {{ var .StructName }}List = {{ .StructName }}List{} 68 69 table := {{ var .StructName }}.T() 70 71 {{ if .HasSoftDelete }} 72 condition = {{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.And(condition, table.F("{{ .FieldSoftDelete }}").Eq({{ use .ConstSoftDeleteTrue }})) 73 {{ end }} 74 75 stmt := table.Select(). 76 Comment("{{ .StructName }}.{{ $method }}"). 77 Where(condition) 78 79 err = db.Do(stmt).Scan(&{{ var .StructName }}List).Err() 80 81 return 82 } 83 `)) 84 85 m.ParseTo(buf, fmt.Sprintf( 86 ` 87 {{ $method := "ListByStruct" }} 88 89 func ({{ var .StructName }} *{{ .StructName }}) {{ $method }}(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB) ({{ var .StructName }}List {{ .StructName }}List, err error) { 90 {{ var .StructName }}List = {{ .StructName }}List{} 91 92 table := {{ var .StructName }}.T() 93 94 condition := {{ var .StructName }}.ConditionByStruct() 95 96 {{ if .HasSoftDelete }} 97 condition = {{ use "github.com/johnnyeven/libtools/sqlx/builder" }}.And(condition, table.F("{{ .FieldSoftDelete }}").Eq({{ use .ConstSoftDeleteTrue }})) 98 {{ end }} 99 100 stmt := table.Select(). 101 Comment("{{ .StructName }}.{{ $method }}"). 102 Where(condition) 103 104 err = db.Do(stmt).Scan(&{{ var .StructName }}List).Err() 105 106 return 107 } 108 `)) 109 110 return buf.String() 111 } 112 113 func ForEachField(typeStruct *types.Struct, cb func(field *types.Var)) { 114 for i := 0; i < typeStruct.NumFields(); i++ { 115 f := typeStruct.Field(i) 116 if f.Anonymous() { 117 if s, ok := f.Type().Underlying().(*types.Struct); ok { 118 ForEachField(s, cb) 119 } else { 120 cb(f) 121 } 122 } else { 123 cb(f) 124 } 125 } 126 } 127 128 func (m *Model) FieldType(name string) (typ string) { 129 typeStruct := m.TypeName.Type().Underlying().(*types.Struct) 130 131 ForEachField(typeStruct, func(field *types.Var) { 132 if field.Name() == name { 133 typ = field.Type().String() 134 if strings.Contains(typ, ".") { 135 typ = m.Use(field.Type().String()) 136 } 137 } 138 }) 139 140 return 141 } 142 143 func (m *Model) methodsForBatchList() string { 144 buf := &bytes.Buffer{} 145 146 indexedFields := m.IndexFieldNames() 147 148 for _, field := range indexedFields { 149 m.ParseTo(buf, fmt.Sprintf(` 150 {{ $field := "%s" }} 151 {{ $fieldType := "%s" }} 152 153 // deprecated 154 func ({{ var .StructName }}List *{{ .StructName }}List) BatchFetchBy{{ $field }}List(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB, {{ var $field }}List []{{ $fieldType }}) (err error) { 155 *{{ var .StructName }}List, err = (&{{ .StructName }}{}).BatchFetchBy{{ $field }}List(db, {{ var $field }}List) 156 return 157 } 158 159 func ({{ var .StructName }} *{{ .StructName }}) BatchFetchBy{{ $field }}List(db *{{ use "github.com/johnnyeven/libtools/sqlx" }}.DB, {{ var $field }}List []{{ $fieldType }}) ({{ var .StructName }}List {{ .StructName }}List, err error) { 160 if len({{ var $field }}List) == 0 { 161 return {{ .StructName }}List{}, nil 162 } 163 164 table := {{ var .StructName }}.T() 165 166 condition := table.F("{{ $field }}").In({{ var $field }}List) 167 168 {{ if .HasSoftDelete }} 169 condition = condition.And(table.F("{{ .FieldSoftDelete }}").Eq({{ use .ConstSoftDeleteTrue }})) 170 {{ end }} 171 172 stmt := table.Select(). 173 Comment("{{ .StructName }}.BatchFetchBy{{ $field }}List"). 174 Where(condition) 175 176 err = db.Do(stmt).Scan(&{{ var .StructName }}List).Err() 177 178 return 179 } 180 `, 181 field, 182 m.FieldType(field), 183 )) 184 } 185 186 return buf.String() 187 }