github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/queryinfo.go (about) 1 package function 2 3 import ( 4 "fmt" 5 6 "github.com/dolthub/go-mysql-server/sql" 7 "github.com/dolthub/go-mysql-server/sql/expression" 8 "github.com/dolthub/go-mysql-server/sql/types" 9 ) 10 11 // RowCount implements the ROW_COUNT() function 12 type RowCount struct{} 13 14 func (r RowCount) IsNonDeterministic() bool { 15 return true 16 } 17 18 func NewRowCount() sql.Expression { 19 return RowCount{} 20 } 21 22 var _ sql.FunctionExpression = RowCount{} 23 var _ sql.CollationCoercible = RowCount{} 24 25 // Description implements sql.FunctionExpression 26 func (r RowCount) Description() string { 27 return "returns the number of rows updated." 28 } 29 30 // Resolved implements sql.Expression 31 func (r RowCount) Resolved() bool { 32 return true 33 } 34 35 // String implements sql.Expression 36 func (r RowCount) String() string { 37 return fmt.Sprintf("%s()", r.FunctionName()) 38 } 39 40 // Type implements sql.Expression 41 func (r RowCount) Type() sql.Type { 42 return types.Int64 43 } 44 45 // CollationCoercibility implements the interface sql.CollationCoercible. 46 func (RowCount) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 47 return sql.Collation_binary, 5 48 } 49 50 // IsNullable implements sql.Expression 51 func (r RowCount) IsNullable() bool { 52 return false 53 } 54 55 // Eval implements sql.Expression 56 func (r RowCount) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 57 return ctx.GetLastQueryInfo(sql.RowCount), nil 58 } 59 60 // Children implements sql.Expression 61 func (r RowCount) Children() []sql.Expression { 62 return nil 63 } 64 65 // WithChildren implements sql.Expression 66 func (r RowCount) WithChildren(children ...sql.Expression) (sql.Expression, error) { 67 return sql.NillaryWithChildren(r, children...) 68 } 69 70 // FunctionName implements sql.FunctionExpression 71 func (r RowCount) FunctionName() string { 72 return "row_count" 73 } 74 75 // LastInsertId implements the LAST_INSERT_ID() function 76 type LastInsertId struct { 77 expression.UnaryExpression 78 } 79 80 func (r LastInsertId) IsNonDeterministic() bool { 81 return true 82 } 83 84 func NewLastInsertId(children ...sql.Expression) (sql.Expression, error) { 85 switch len(children) { 86 case 0: 87 return LastInsertId{}, nil 88 case 1: 89 return LastInsertId{UnaryExpression: expression.UnaryExpression{Child: children[0]}}, nil 90 default: 91 return nil, sql.ErrInvalidArgumentNumber.New("LastInsertId", len(children), 1) 92 } 93 } 94 95 var _ sql.FunctionExpression = LastInsertId{} 96 var _ sql.CollationCoercible = LastInsertId{} 97 98 // Description implements sql.FunctionExpression 99 func (r LastInsertId) Description() string { 100 return "returns value of the AUTOINCREMENT column for the last INSERT." 101 } 102 103 // Resolved implements sql.Expression 104 func (r LastInsertId) Resolved() bool { 105 return true 106 } 107 108 // String implements sql.Expression 109 func (r LastInsertId) String() string { 110 return fmt.Sprintf("%s(%s)", r.FunctionName(), r.Child) 111 } 112 113 // Type implements sql.Expression 114 func (r LastInsertId) Type() sql.Type { 115 return types.Uint64 116 } 117 118 // CollationCoercibility implements the interface sql.CollationCoercible. 119 func (LastInsertId) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 120 return sql.Collation_binary, 5 121 } 122 123 // IsNullable implements sql.Expression 124 func (r LastInsertId) IsNullable() bool { 125 return false 126 } 127 128 // Eval implements sql.Expression 129 func (r LastInsertId) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 130 // With no arguments, just return the last insert id for this session 131 if len(r.Children()) == 0 { 132 lastInsertId := ctx.GetLastQueryInfo(sql.LastInsertId) 133 unsigned, _, err := types.Uint64.Convert(lastInsertId) 134 if err != nil { 135 return nil, err 136 } 137 return unsigned, nil 138 } 139 140 // If an expression is provided, we set the next insert id for this session as well as returning it 141 res, err := r.Child.Eval(ctx, row) 142 if err != nil { 143 return nil, err 144 } 145 id, _, err := types.Int64.Convert(res) 146 if err != nil { 147 return nil, err 148 } 149 150 ctx.SetLastQueryInfo(sql.LastInsertId, id.(int64)) 151 return id, nil 152 } 153 154 // Children implements sql.Expression 155 func (r LastInsertId) Children() []sql.Expression { 156 if r.Child == nil { 157 return nil 158 } 159 return []sql.Expression{r.Child} 160 } 161 162 // WithChildren implements sql.Expression 163 func (r LastInsertId) WithChildren(children ...sql.Expression) (sql.Expression, error) { 164 return NewLastInsertId(children...) 165 } 166 167 // FunctionName implements sql.FunctionExpression 168 func (r LastInsertId) FunctionName() string { 169 return "last_insert_id" 170 } 171 172 // FoundRows implements the FOUND_ROWS() function 173 type FoundRows struct{} 174 175 func (r FoundRows) IsNonDeterministic() bool { 176 return true 177 } 178 179 func NewFoundRows() sql.Expression { 180 return FoundRows{} 181 } 182 183 var _ sql.FunctionExpression = FoundRows{} 184 var _ sql.CollationCoercible = FoundRows{} 185 186 // FunctionName implements sql.FunctionExpression 187 func (r FoundRows) FunctionName() string { 188 return "found_rows" 189 } 190 191 // Description implements sql.Expression 192 func (r FoundRows) Description() string { 193 return "for a SELECT with a LIMIT clause, returns the number of rows that would be returned were there no LIMIT clause." 194 } 195 196 // Resolved implements sql.Expression 197 func (r FoundRows) Resolved() bool { 198 return true 199 } 200 201 // String implements sql.Expression 202 func (r FoundRows) String() string { 203 return fmt.Sprintf("%s()", r.FunctionName()) 204 } 205 206 // Type implements sql.Expression 207 func (r FoundRows) Type() sql.Type { 208 return types.Int64 209 } 210 211 // CollationCoercibility implements the interface sql.CollationCoercible. 212 func (FoundRows) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 213 return sql.Collation_binary, 5 214 } 215 216 // IsNullable implements sql.Expression 217 func (r FoundRows) IsNullable() bool { 218 return false 219 } 220 221 // Eval implements sql.Expression 222 func (r FoundRows) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 223 return ctx.GetLastQueryInfo(sql.FoundRows), nil 224 } 225 226 // Children implements sql.Expression 227 func (r FoundRows) Children() []sql.Expression { 228 return nil 229 } 230 231 // WithChildren implements sql.Expression 232 func (r FoundRows) WithChildren(children ...sql.Expression) (sql.Expression, error) { 233 return sql.NillaryWithChildren(r, children...) 234 }