1// Copyright 2017 The Xorm Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package statements
6
7import (
8	"fmt"
9	"reflect"
10
11	"xorm.io/builder"
12	"xorm.io/xorm/schemas"
13)
14
15var (
16	ptrPkType  = reflect.TypeOf(&schemas.PK{})
17	pkType     = reflect.TypeOf(schemas.PK{})
18	stringType = reflect.TypeOf("")
19	intType    = reflect.TypeOf(int64(0))
20	uintType   = reflect.TypeOf(uint64(0))
21)
22
23// ErrIDConditionWithNoTable represents an error there is no reference table with an ID condition
24type ErrIDConditionWithNoTable struct {
25	ID schemas.PK
26}
27
28func (err ErrIDConditionWithNoTable) Error() string {
29	return fmt.Sprintf("ID condition %#v need reference table", err.ID)
30}
31
32// IsIDConditionWithNoTableErr return true if the err is ErrIDConditionWithNoTable
33func IsIDConditionWithNoTableErr(err error) bool {
34	_, ok := err.(ErrIDConditionWithNoTable)
35	return ok
36}
37
38// ID generate "where id = ? " statement or for composite key "where key1 = ? and key2 = ?"
39func (statement *Statement) ID(id interface{}) *Statement {
40	switch t := id.(type) {
41	case *schemas.PK:
42		statement.idParam = *t
43	case schemas.PK:
44		statement.idParam = t
45	case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
46		statement.idParam = schemas.PK{id}
47	default:
48		idValue := reflect.ValueOf(id)
49		idType := idValue.Type()
50
51		switch idType.Kind() {
52		case reflect.String:
53			statement.idParam = schemas.PK{idValue.Convert(stringType).Interface()}
54		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
55			statement.idParam = schemas.PK{idValue.Convert(intType).Interface()}
56		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
57			statement.idParam = schemas.PK{idValue.Convert(uintType).Interface()}
58		case reflect.Slice:
59			if idType.ConvertibleTo(pkType) {
60				statement.idParam = idValue.Convert(pkType).Interface().(schemas.PK)
61			}
62		case reflect.Ptr:
63			if idType.ConvertibleTo(ptrPkType) {
64				statement.idParam = idValue.Convert(ptrPkType).Elem().Interface().(schemas.PK)
65			}
66		}
67	}
68
69	if statement.idParam == nil {
70		statement.LastError = fmt.Errorf("ID param %#v is not supported", id)
71	}
72
73	return statement
74}
75
76// ProcessIDParam handles the process of id condition
77func (statement *Statement) ProcessIDParam() error {
78	if statement.idParam == nil {
79		return nil
80	}
81
82	if statement.RefTable == nil {
83		return ErrIDConditionWithNoTable{statement.idParam}
84	}
85
86	if len(statement.RefTable.PrimaryKeys) != len(statement.idParam) {
87		return fmt.Errorf("ID condition is error, expect %d primarykeys, there are %d",
88			len(statement.RefTable.PrimaryKeys),
89			len(statement.idParam),
90		)
91	}
92
93	for i, col := range statement.RefTable.PKColumns() {
94		var colName = statement.colName(col, statement.TableName())
95		statement.cond = statement.cond.And(builder.Eq{colName: statement.idParam[i]})
96	}
97	return nil
98}
99