Skip to content

Commit

Permalink
chore: Refactor <unsigned value specification>
Browse files Browse the repository at this point in the history
As a longer term effort to remove ast.v and eval.v, this moves
<unsigned value specification> and its subtypes into their respective
locations.
  • Loading branch information
elliotchance committed Oct 26, 2023
1 parent 9c801c0 commit fe8e00a
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 93 deletions.
51 changes: 2 additions & 49 deletions vsql/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ type Expr = BinaryExpr
| CastExpr
| CoalesceExpr
| CountAllExpr
| CurrentCatalogExpr
| CurrentDateExpr
| CurrentSchemaExpr
| CurrentTimeExpr
| CurrentTimestampExpr
| Identifier
Expand All @@ -39,7 +37,6 @@ type Expr = BinaryExpr
| NextValueExpr
| NoExpr
| NullIfExpr
| Parameter
| Predicate
| QualifiedAsteriskExpr
| QueryExpression
Expand All @@ -48,6 +45,7 @@ type Expr = BinaryExpr
| TrimExpr
| TruthExpr
| UnaryExpr
| UnsignedValueSpecification
| UntypedNullExpr
| Value

Expand All @@ -57,7 +55,7 @@ fn (e Expr) str() string {

fn (e Expr) pstr(params map[string]Value) string {
return match e {
Predicate {
Predicate, UnsignedValueSpecification {
e.pstr(params)
}
BinaryExpr {
Expand All @@ -75,12 +73,6 @@ fn (e Expr) pstr(params map[string]Value) string {
CountAllExpr {
e.pstr(params)
}
CurrentCatalogExpr {
e.str()
}
CurrentSchemaExpr {
e.str()
}
CurrentDateExpr {
e.str()
}
Expand Down Expand Up @@ -108,9 +100,6 @@ fn (e Expr) pstr(params map[string]Value) string {
NullIfExpr {
e.pstr(params)
}
Parameter {
e.pstr(params)
}
QualifiedAsteriskExpr {
e.str()
}
Expand Down Expand Up @@ -628,26 +617,6 @@ fn (c Correlation) str() string {
return s
}

// Parameter is :foo. The colon is not included in the name. Parameters are case
// sensitive.
struct Parameter {
name string
}

fn (e Parameter) str() string {
return ':${e.name}'
}

fn (e Parameter) pstr(params map[string]Value) string {
p := params[e.name]

if p.typ.typ != .is_numeric && (p.typ.uses_string() || p.typ.uses_time()) {
return '\'${p.str()}\''
}

return p.str()
}

struct UniqueConstraintDefinition {
columns []Identifier
}
Expand Down Expand Up @@ -909,22 +878,6 @@ fn (e NextValueExpr) str() string {
return 'NEXT VALUE FOR ${e.name}'
}

// CURRENT_CATALOG
struct CurrentCatalogExpr {
}

fn (e CurrentCatalogExpr) str() string {
return 'CURRENT_CATALOG'
}

// CURRENT_SCHEMA
struct CurrentSchemaExpr {
}

fn (e CurrentSchemaExpr) str() string {
return 'CURRENT_SCHEMA'
}

// SET SCHEMA
struct SetSchemaStmt {
schema_name Expr
Expand Down
21 changes: 5 additions & 16 deletions vsql/eval.v
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ fn eval_as_type(conn &Connection, data Row, e Expr, params map[string]Value) !Ty
TruthExpr {
return new_type('BOOLEAN', 0, 0)
}
Parameter {
p := params[e.name] or { return sqlstate_42p02(e.name) }

return eval_as_type(conn, data, p, params)
}
Value {
return e.typ
}
Expand Down Expand Up @@ -166,7 +161,10 @@ fn eval_as_type(conn &Connection, data Row, e Expr, params map[string]Value) !Ty
LocalTimestampExpr {
return new_type('TIMESTAMP WITHOUT TIME ZONE', 0, 0)
}
SubstringExpr, TrimExpr, CurrentCatalogExpr, CurrentSchemaExpr {
UnsignedValueSpecification {
return e.eval_type(conn, data, params)
}
SubstringExpr, TrimExpr {
return new_type('CHARACTER VARYING', 0, 0)
}
UntypedNullExpr {
Expand All @@ -177,7 +175,7 @@ fn eval_as_type(conn &Connection, data Row, e Expr, params map[string]Value) !Ty

fn eval_as_value(mut conn Connection, data Row, e Expr, params map[string]Value) !Value {
match e {
Predicate {
Predicate, UnsignedValueSpecification {
return e.eval(mut conn, data, params)
}
BinaryExpr {
Expand All @@ -204,9 +202,6 @@ fn eval_as_value(mut conn Connection, data Row, e Expr, params map[string]Value)
NullIfExpr {
return eval_nullif(mut conn, data, e, params)
}
Parameter {
return params[e.name] or { return sqlstate_42p02(e.name) }
}
SubstringExpr {
return eval_substring(mut conn, data, e, params)
}
Expand All @@ -224,17 +219,11 @@ fn eval_as_value(mut conn Connection, data Row, e Expr, params map[string]Value)
// ValuesOperation.
return sqlstate_42601('missing or invalid expression provided')
}
CurrentCatalogExpr {
return new_varchar_value(conn.current_catalog)
}
CurrentDateExpr {
now, _ := conn.now()

return new_date_value(now.strftime('%Y-%m-%d'))
}
CurrentSchemaExpr {
return new_varchar_value(conn.current_schema)
}
CurrentTimeExpr {
if e.prec > 6 {
return sqlstate_42601('${e}: cannot have precision greater than 6')
Expand Down
13 changes: 6 additions & 7 deletions vsql/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn expr_is_agg(conn &Connection, e Expr, row Row, params map[string]Value) !bool
return nested_agg_unsupported(e)
}
}
Predicate {
Predicate, UnsignedValueSpecification {
return e.is_agg(conn, row, params)
}
CallExpr {
Expand All @@ -36,9 +36,9 @@ fn expr_is_agg(conn &Connection, e Expr, row Row, params map[string]Value) !bool
CountAllExpr {
return true
}
Identifier, Parameter, Value, NoExpr, RowExpr, QualifiedAsteriskExpr, QueryExpression,
Identifier, Value, NoExpr, RowExpr, QualifiedAsteriskExpr, QueryExpression,
CurrentDateExpr, CurrentTimeExpr, CurrentTimestampExpr, LocalTimeExpr, LocalTimestampExpr,
UntypedNullExpr, NextValueExpr, CurrentCatalogExpr, CurrentSchemaExpr {
UntypedNullExpr, NextValueExpr {
return false
}
CoalesceExpr {
Expand Down Expand Up @@ -107,7 +107,7 @@ fn resolve_identifiers(conn &Connection, e Expr, tables map[string]Table) !Expr
return BinaryExpr{resolve_identifiers(conn, e.left, tables)!, e.op, resolve_identifiers(conn,
e.right, tables)!}
}
Predicate {
Predicate, UnsignedValueSpecification {
return e.resolve_identifiers(conn, tables)
}
CallExpr {
Expand Down Expand Up @@ -159,9 +159,8 @@ fn resolve_identifiers(conn &Connection, e Expr, tables map[string]Table) !Expr
QualifiedAsteriskExpr {
return QualifiedAsteriskExpr{resolve_identifiers(conn, e.table_name, tables)! as Identifier}
}
CountAllExpr, Parameter, Value, NoExpr, QueryExpression, CurrentDateExpr, CurrentTimeExpr,
CurrentTimestampExpr, LocalTimeExpr, LocalTimestampExpr, UntypedNullExpr,
CurrentSchemaExpr, CurrentCatalogExpr {
CountAllExpr, Value, NoExpr, QueryExpression, CurrentDateExpr, CurrentTimeExpr,
CurrentTimestampExpr, LocalTimeExpr, LocalTimestampExpr, UntypedNullExpr {
// These don't have any Expr properties to recurse.
return e
}
Expand Down
Loading

0 comments on commit fe8e00a

Please sign in to comment.