github.com/kunlun-qilian/sqlx/v3@v3.0.0/generator/model_list.go (about) 1 package generator 2 3 import ( 4 "fmt" 5 6 "github.com/go-courier/codegen" 7 ) 8 9 func (m *Model) WriteCount(file *codegen.File) { 10 file.WriteBlock( 11 codegen.Func( 12 codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3", "DBExecutor")), "db"), 13 codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "SqlCondition")), "condition"), 14 codegen.Var(codegen.Ellipsis(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Addition"))), "additions"), 15 ). 16 Named("Count"). 17 MethodOf(codegen.Var(m.PtrType(), "m")). 18 Return( 19 codegen.Var(codegen.Int), 20 codegen.Var(codegen.Error), 21 ). 22 Do( 23 codegen.Expr(` 24 count := -1 25 26 table := db.T(m) 27 _ = table 28 `), 29 30 func() codegen.Snippet { 31 if m.HasDeletedAt { 32 return codegen.Expr( 33 `condition = ?(condition, table.F("`+m.FieldKeyDeletedAt+`").Eq(0))`, 34 codegen.Id(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "And")), 35 ) 36 } 37 return nil 38 }(), 39 40 codegen.Expr(` 41 42 finalAdditions := []`+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Addition")+`{ 43 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Where")+`(condition), 44 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Comment")+`(?), 45 } 46 47 if len(additions) > 0 { 48 finalAdditions = append(finalAdditions, additions...) 49 } 50 51 err := db.QueryExprAndScan( 52 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Select")+`( 53 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Count")+`(), 54 ). 55 From(db.T(m), finalAdditions...), 56 &count, 57 ) 58 59 return count, err 60 `, 61 file.Val(m.StructName+".Count"), 62 ), 63 ), 64 ) 65 } 66 67 func (m *Model) WriteList(file *codegen.File) { 68 file.WriteBlock( 69 codegen.Func( 70 codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3", "DBExecutor")), "db"), 71 codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "SqlCondition")), "condition"), 72 codegen.Var(codegen.Ellipsis(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Addition"))), "additions"), 73 ). 74 Named("List"). 75 MethodOf(codegen.Var(m.PtrType(), "m")). 76 Return( 77 codegen.Var(codegen.Slice(codegen.Type(m.StructName))), 78 codegen.Var(codegen.Error), 79 ). 80 Do( 81 codegen.Expr(` 82 list := make([]`+m.StructName+`, 0) 83 84 table := db.T(m) 85 _ = table 86 `), 87 88 func() codegen.Snippet { 89 if m.HasDeletedAt { 90 return codegen.Expr( 91 `condition = ?(condition, table.F("`+m.FieldKeyDeletedAt+`").Eq(0))`, 92 codegen.Id(file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "And")), 93 ) 94 } 95 return nil 96 }(), 97 98 codegen.Expr(` 99 100 finalAdditions := []`+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Addition")+`{ 101 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Where")+`(condition), 102 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Comment")+`(?), 103 } 104 105 if len(additions) > 0 { 106 finalAdditions = append(finalAdditions, additions...) 107 } 108 109 err := db.QueryExprAndScan( 110 `+file.Use("github.com/kunlun-qilian/sqlx/v3/builder", "Select")+`(nil). 111 From(db.T(m), finalAdditions...), 112 &list, 113 ) 114 115 return list, err 116 `, 117 file.Val(m.StructName+".List"), 118 ), 119 ), 120 ) 121 } 122 123 func (m *Model) WriteBatchList(file *codegen.File) { 124 indexedFields := m.IndexFieldNames() 125 126 for _, field := range indexedFields { 127 method := fmt.Sprintf("BatchFetchBy%sList", field) 128 129 typ := m.FieldType(file, field) 130 131 file.WriteBlock( 132 codegen.Func( 133 codegen.Var(codegen.Type(file.Use("github.com/kunlun-qilian/sqlx/v3", "DBExecutor")), "db"), 134 codegen.Var(codegen.Slice(typ), "values"), 135 ). 136 Named(method). 137 MethodOf(codegen.Var(m.PtrType(), "m")). 138 Return( 139 codegen.Var(codegen.Slice(codegen.Type(m.StructName))), 140 codegen.Var(codegen.Error), 141 ). 142 Do( 143 codegen.Expr(` 144 if len(values) == 0 { 145 return nil, nil 146 } 147 148 table := db.T(m) 149 150 condition := table.F("` + field + `").In(values) 151 152 return m.List(db, condition) 153 `), 154 ), 155 ) 156 } 157 }