query_index_create.go (5526B)
1 package bun 2 3 import ( 4 "context" 5 "database/sql" 6 7 "github.com/uptrace/bun/internal" 8 "github.com/uptrace/bun/schema" 9 ) 10 11 type CreateIndexQuery struct { 12 whereBaseQuery 13 14 unique bool 15 fulltext bool 16 spatial bool 17 concurrently bool 18 ifNotExists bool 19 20 index schema.QueryWithArgs 21 using schema.QueryWithArgs 22 include []schema.QueryWithArgs 23 } 24 25 var _ Query = (*CreateIndexQuery)(nil) 26 27 func NewCreateIndexQuery(db *DB) *CreateIndexQuery { 28 q := &CreateIndexQuery{ 29 whereBaseQuery: whereBaseQuery{ 30 baseQuery: baseQuery{ 31 db: db, 32 conn: db.DB, 33 }, 34 }, 35 } 36 return q 37 } 38 39 func (q *CreateIndexQuery) Conn(db IConn) *CreateIndexQuery { 40 q.setConn(db) 41 return q 42 } 43 44 func (q *CreateIndexQuery) Model(model interface{}) *CreateIndexQuery { 45 q.setModel(model) 46 return q 47 } 48 49 func (q *CreateIndexQuery) Err(err error) *CreateIndexQuery { 50 q.setErr(err) 51 return q 52 } 53 54 func (q *CreateIndexQuery) Unique() *CreateIndexQuery { 55 q.unique = true 56 return q 57 } 58 59 func (q *CreateIndexQuery) Concurrently() *CreateIndexQuery { 60 q.concurrently = true 61 return q 62 } 63 64 func (q *CreateIndexQuery) IfNotExists() *CreateIndexQuery { 65 q.ifNotExists = true 66 return q 67 } 68 69 //------------------------------------------------------------------------------ 70 71 func (q *CreateIndexQuery) Index(query string) *CreateIndexQuery { 72 q.index = schema.UnsafeIdent(query) 73 return q 74 } 75 76 func (q *CreateIndexQuery) IndexExpr(query string, args ...interface{}) *CreateIndexQuery { 77 q.index = schema.SafeQuery(query, args) 78 return q 79 } 80 81 //------------------------------------------------------------------------------ 82 83 func (q *CreateIndexQuery) Table(tables ...string) *CreateIndexQuery { 84 for _, table := range tables { 85 q.addTable(schema.UnsafeIdent(table)) 86 } 87 return q 88 } 89 90 func (q *CreateIndexQuery) TableExpr(query string, args ...interface{}) *CreateIndexQuery { 91 q.addTable(schema.SafeQuery(query, args)) 92 return q 93 } 94 95 func (q *CreateIndexQuery) ModelTableExpr(query string, args ...interface{}) *CreateIndexQuery { 96 q.modelTableName = schema.SafeQuery(query, args) 97 return q 98 } 99 100 func (q *CreateIndexQuery) Using(query string, args ...interface{}) *CreateIndexQuery { 101 q.using = schema.SafeQuery(query, args) 102 return q 103 } 104 105 //------------------------------------------------------------------------------ 106 107 func (q *CreateIndexQuery) Column(columns ...string) *CreateIndexQuery { 108 for _, column := range columns { 109 q.addColumn(schema.UnsafeIdent(column)) 110 } 111 return q 112 } 113 114 func (q *CreateIndexQuery) ColumnExpr(query string, args ...interface{}) *CreateIndexQuery { 115 q.addColumn(schema.SafeQuery(query, args)) 116 return q 117 } 118 119 func (q *CreateIndexQuery) ExcludeColumn(columns ...string) *CreateIndexQuery { 120 q.excludeColumn(columns) 121 return q 122 } 123 124 //------------------------------------------------------------------------------ 125 126 func (q *CreateIndexQuery) Include(columns ...string) *CreateIndexQuery { 127 for _, column := range columns { 128 q.include = append(q.include, schema.UnsafeIdent(column)) 129 } 130 return q 131 } 132 133 func (q *CreateIndexQuery) IncludeExpr(query string, args ...interface{}) *CreateIndexQuery { 134 q.include = append(q.include, schema.SafeQuery(query, args)) 135 return q 136 } 137 138 //------------------------------------------------------------------------------ 139 140 func (q *CreateIndexQuery) Where(query string, args ...interface{}) *CreateIndexQuery { 141 q.addWhere(schema.SafeQueryWithSep(query, args, " AND ")) 142 return q 143 } 144 145 func (q *CreateIndexQuery) WhereOr(query string, args ...interface{}) *CreateIndexQuery { 146 q.addWhere(schema.SafeQueryWithSep(query, args, " OR ")) 147 return q 148 } 149 150 //------------------------------------------------------------------------------ 151 152 func (q *CreateIndexQuery) Operation() string { 153 return "CREATE INDEX" 154 } 155 156 func (q *CreateIndexQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) { 157 if q.err != nil { 158 return nil, q.err 159 } 160 161 b = append(b, "CREATE "...) 162 163 if q.unique { 164 b = append(b, "UNIQUE "...) 165 } 166 if q.fulltext { 167 b = append(b, "FULLTEXT "...) 168 } 169 if q.spatial { 170 b = append(b, "SPATIAL "...) 171 } 172 173 b = append(b, "INDEX "...) 174 175 if q.concurrently { 176 b = append(b, "CONCURRENTLY "...) 177 } 178 if q.ifNotExists { 179 b = append(b, "IF NOT EXISTS "...) 180 } 181 182 b, err = q.index.AppendQuery(fmter, b) 183 if err != nil { 184 return nil, err 185 } 186 187 b = append(b, " ON "...) 188 b, err = q.appendFirstTable(fmter, b) 189 if err != nil { 190 return nil, err 191 } 192 193 if !q.using.IsZero() { 194 b = append(b, " USING "...) 195 b, err = q.using.AppendQuery(fmter, b) 196 if err != nil { 197 return nil, err 198 } 199 } 200 201 b = append(b, " ("...) 202 for i, col := range q.columns { 203 if i > 0 { 204 b = append(b, ", "...) 205 } 206 b, err = col.AppendQuery(fmter, b) 207 if err != nil { 208 return nil, err 209 } 210 } 211 b = append(b, ')') 212 213 if len(q.include) > 0 { 214 b = append(b, " INCLUDE ("...) 215 for i, col := range q.include { 216 if i > 0 { 217 b = append(b, ", "...) 218 } 219 b, err = col.AppendQuery(fmter, b) 220 if err != nil { 221 return nil, err 222 } 223 } 224 b = append(b, ')') 225 } 226 227 if len(q.where) > 0 { 228 b = append(b, " WHERE "...) 229 b, err = appendWhere(fmter, b, q.where) 230 if err != nil { 231 return nil, err 232 } 233 } 234 235 return b, nil 236 } 237 238 //------------------------------------------------------------------------------ 239 240 func (q *CreateIndexQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) { 241 queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) 242 if err != nil { 243 return nil, err 244 } 245 246 query := internal.String(queryBytes) 247 248 res, err := q.exec(ctx, q, query) 249 if err != nil { 250 return nil, err 251 } 252 253 return res, nil 254 }