Skip to content

Commit

Permalink
refactor: Fix v syntax errors (#135)
Browse files Browse the repository at this point in the history
Some minor cleanup with V language and formatting changes.
  • Loading branch information
amirrezasalimi authored Dec 12, 2022
1 parent 6119845 commit 8af2ddf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
7 changes: 4 additions & 3 deletions vsql/cast.v
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ fn cast(conn &Connection, msg string, v Value, to Type) !Value {
return new_null_value(to.typ)
}

key := '$v.typ.typ AS $to.typ'
if key in conn.cast_rules {
return conn.cast_rules[key](conn, v, to)
key := '${v.typ.typ} AS ${to.typ}'
if fnc := conn.cast_rules[key] {
cast_fn := fnc as CastFunc
return cast_fn(conn, v, to)!
}

return sqlstate_42846(v.typ, to)
Expand Down
60 changes: 32 additions & 28 deletions vsql/eval.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn new_expr_operation(conn &Connection, params map[string]Value, select_list Sel
for _, table in tables {
columns << table.columns
for column in table.columns {
exprs << DerivedColumn{new_identifier('"${table.name}.$column.name"'), new_identifier('"$column.name"')}
exprs << DerivedColumn{new_identifier('"${table.name}.${column.name}"'), new_identifier('"${column.name}"')}
}
}
}
Expand All @@ -37,14 +37,14 @@ fn new_expr_operation(conn &Connection, params map[string]Value, select_list Sel
return sqlstate_3f000(parts[0]) // scheme does not exist
}
} else {
table_name = 'PUBLIC.$table_name'
table_name = 'PUBLIC.${table_name}'
}

// panic(table_name)
table := tables[table_name] or { return sqlstate_42p01(table_name) }
columns = table.columns
for column in table.columns {
exprs << DerivedColumn{new_identifier('"${table.name}.$column.name"'), new_identifier('"$column.name"')}
exprs << DerivedColumn{new_identifier('"${table.name}.${column.name}"'), new_identifier('"${column.name}"')}
}
}
[]DerivedColumn {
Expand All @@ -61,7 +61,7 @@ fn new_expr_operation(conn &Connection, params map[string]Value, select_list Sel

columns << Column{column_name, eval_as_type(conn, empty_row, expr, params)!, false}

exprs << DerivedColumn{expr, new_identifier('"$column_name"')}
exprs << DerivedColumn{expr, new_identifier('"${column_name}"')}
}
}
}
Expand All @@ -70,7 +70,7 @@ fn new_expr_operation(conn &Connection, params map[string]Value, select_list Sel
}

fn (o ExprOperation) str() string {
return 'EXPR ($o.columns())'
return 'EXPR (${o.columns()})'
}

fn (o ExprOperation) columns() Columns {
Expand All @@ -95,7 +95,7 @@ fn eval_row(conn &Connection, data Row, exprs []Expr, params map[string]Value) !
mut col_number := 1
mut row := map[string]Value{}
for expr in exprs {
row['COL$col_number'] = eval_as_value(conn, data, expr, params)!
row['COL${col_number}'] = eval_as_value(conn, data, expr, params)!
col_number++
}

Expand Down Expand Up @@ -149,12 +149,12 @@ fn eval_as_type(conn &Connection, data Row, e Expr, params map[string]Value) !Ty
return eval_as_type(conn, data, e.left, params)
}
Identifier {
col := data.data[e.name] or { return sqlstate_42601('unknown column: $e.name') }
col := data.data[e.name] or { return sqlstate_42601('unknown column: ${e.name}') }

return col.typ
}
NoExpr, QualifiedAsteriskExpr, QueryExpression, RowExpr {
return sqlstate_42601('invalid expression provided: $e.str()')
return sqlstate_42601('invalid expression provided: ${e.str()}')
}
CurrentDateExpr {
return new_type('DATE', 0)
Expand Down Expand Up @@ -242,14 +242,14 @@ fn eval_as_value(conn &Connection, data Row, e Expr, params map[string]Value) !V
}
CurrentTimeExpr {
if e.prec > 6 {
return sqlstate_42601('$e: cannot have precision greater than 6')
return sqlstate_42601('${e}: cannot have precision greater than 6')
}

return new_time_value(time_value(conn, e.prec, true))
}
CurrentTimestampExpr {
if e.prec > 6 {
return sqlstate_42601('$e: cannot have precision greater than 6')
return sqlstate_42601('${e}: cannot have precision greater than 6')
}

now, _ := conn.options.now()
Expand All @@ -258,14 +258,14 @@ fn eval_as_value(conn &Connection, data Row, e Expr, params map[string]Value) !V
}
LocalTimeExpr {
if e.prec > 6 {
return sqlstate_42601('$e: cannot have precision greater than 6')
return sqlstate_42601('${e}: cannot have precision greater than 6')
}

return new_time_value(time_value(conn, e.prec, false))
}
LocalTimestampExpr {
if e.prec > 6 {
return sqlstate_42601('$e: cannot have precision greater than 6')
return sqlstate_42601('${e}: cannot have precision greater than 6')
}

now, _ := conn.options.now()
Expand Down Expand Up @@ -321,7 +321,7 @@ fn eval_as_bool(conn &Connection, data Row, e Expr, params map[string]Value) !bo
}

fn eval_identifier(data Row, e Identifier) !Value {
value := data.data[e.name] or { return sqlstate_42601('unknown column: $e.name') }
value := data.data[e.name] or { return sqlstate_42601('unknown column: ${e.name}') }

return value
}
Expand All @@ -341,15 +341,16 @@ fn eval_call(conn &Connection, data Row, e CallExpr, params map[string]Value) !V
}

if e.args.len != func.arg_types.len {
return sqlstate_42883('$func_name has $e.args.len ${pluralize(e.args.len, 'argument')} but needs $func.arg_types.len ${pluralize(func.arg_types.len,
return sqlstate_42883('${func_name} has ${e.args.len} ${pluralize(e.args.len,
'argument')} but needs ${func.arg_types.len} ${pluralize(func.arg_types.len,
'argument')}')
}

mut args := []Value{}
mut i := 0
for typ in func.arg_types {
arg := eval_as_value(conn, data, e.args[i], params)!
args << cast(conn, 'argument ${i + 1} in $func_name', arg, typ)!
args << cast(conn, 'argument ${i + 1} in ${func_name}', arg, typ)!
i++
}

Expand Down Expand Up @@ -473,8 +474,8 @@ fn eval_like(conn &Connection, data Row, e LikeExpr, params map[string]Value) !V
'\\)').replace('[', '\\[').replace('{', '\\{').replace('_', '.').replace('%',
'.*')

mut re := regex.regex_opt('^$escaped_regex$') or {
return error('cannot compile regexp: ^$escaped_regex$: $err')
mut re := regex.regex_opt('^${escaped_regex}$') or {
return error('cannot compile regexp: ^${escaped_regex}$: ${err}')
}
result := re.matches_string(left.string_value)

Expand Down Expand Up @@ -516,27 +517,29 @@ fn eval_substring(conn &Connection, data Row, e SubstringExpr, params map[string
return new_varchar_value(value.string_value.substr(from, from + @for), 0)
}

fn eval_binary(conn &Connection, data Row, e BinaryExpr, params map[string]Value) !Value {
fn eval_binary(conn Connection, data Row, e BinaryExpr, params map[string]Value) !Value {
left := eval_as_value(conn, data, e.left, params)!
right := eval_as_value(conn, data, e.right, params)!

key := '$left.typ.typ $e.op $right.typ.typ'
if key in conn.binary_operators {
return conn.binary_operators[key](conn, left, right)
key := '${left.typ.typ} ${e.op} ${right.typ.typ}'
if fnc := conn.binary_operators[key] {
op_fn:=fnc as BinaryOperatorFunc
return op_fn(conn, left, right)
}

return sqlstate_42883('operator does not exist: $left.typ $e.op $right.typ')
return sqlstate_42883('operator does not exist: ${left.typ} ${e.op} ${right.typ}')
}

fn eval_unary(conn &Connection, data Row, e UnaryExpr, params map[string]Value) !Value {
value := eval_as_value(conn, data, e.expr, params)!

key := '$e.op $value.typ.typ'
if key in conn.unary_operators {
return conn.unary_operators[key](conn, value)
key := '${e.op} ${value.typ.typ}'
if fnc:= conn.unary_operators[key] {
unary_fn:=fnc as UnaryOperatorFunc
return unary_fn(conn, value)!
}

return sqlstate_42883('operator does not exist: $key')
return sqlstate_42883('operator does not exist: ${key}')
}

fn eval_between(conn &Connection, data Row, e BetweenExpr, params map[string]Value) !Value {
Expand Down Expand Up @@ -572,7 +575,9 @@ fn eval_similar(conn &Connection, data Row, e SimilarExpr, params map[string]Val

regexp := '^${right.string_value.replace('.', '\\.').replace('_', '.').replace('%',
'.*')}$'
mut re := regex.regex_opt(regexp) or { return error('cannot compile regexp: $regexp: $err') }
mut re := regex.regex_opt(regexp) or {
return error('cannot compile regexp: ${regexp}: ${err}')
}
result := re.matches_string(left.string_value)

if e.not {
Expand All @@ -591,6 +596,5 @@ fn eval_as_nullable_value(conn &Connection, typ SQLType, data Row, e Expr, param
if e is UntypedNullExpr {
return new_null_value(typ)
}

return eval_as_value(conn, data, e, params)
}
8 changes: 5 additions & 3 deletions vsql/row.v
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,11 @@ fn new_row_from_bytes(t Table, data []u8, tid int, table_name string) Row {
if !v.is_null || v.typ.typ == .is_boolean {
match col.typ.typ {
.is_boolean {
v.bool_value = Boolean(buf.read_u8())
if v.bool_value == .is_unknown {
v.is_null = true
unsafe{
v.bool_value = Boolean(buf.read_u8())
if v.bool_value == .is_unknown {
v.is_null = true
}
}
}
.is_bigint {
Expand Down

0 comments on commit 8af2ddf

Please sign in to comment.