Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for V ORM #198

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ btree-test: oldv
sql-test: oldv
$(V) -stats $(BUILD_OPTIONS) vsql/sql_test.v

orm-test: oldv
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just adding a note here for me to add this to the docs.

$(V) -stats $(BUILD_OPTIONS) vsql/orm_test.v

# CLI Tests

cli-test: bin/vsql
Expand Down
10 changes: 5 additions & 5 deletions cmd/vsql/bench.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import vsql

fn register_bench_command(mut cmd cli.Command) {
mut bench_cmd := cli.Command{
name: 'bench'
name: 'bench'
description: 'Run benchmark'
execute: bench_command
execute: bench_command
}
bench_cmd.add_flag(cli.Flag{
flag: .string
name: 'file'
abbrev: 'f'
flag: .string
name: 'file'
abbrev: 'f'
description: 'File path that will be deleted and created for the test. You can use :memory: as well (default bench.vsql)'
})
cmd.add_command(bench_cmd)
Expand Down
8 changes: 4 additions & 4 deletions cmd/vsql/cli.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import vsql

fn register_cli_command(mut cmd cli.Command) {
mut cli_cmd := cli.Command{
name: 'cli'
usage: '<file>'
name: 'cli'
usage: '<file>'
required_args: 1
description: 'Run SQL in a vsql file'
execute: cli_command
description: 'Run SQL in a vsql file'
execute: cli_command
}
cmd.add_command(cli_cmd)
}
Expand Down
18 changes: 9 additions & 9 deletions cmd/vsql/in.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import vsql

fn register_in_command(mut cmd cli.Command) {
mut in_cmd := cli.Command{
name: 'in'
usage: '<file>'
name: 'in'
usage: '<file>'
required_args: 1
description: 'Import schema and data'
execute: in_command
description: 'Import schema and data'
execute: in_command
}

in_cmd.add_flag(cli.Flag{
flag: .bool
name: 'continue-on-error'
flag: .bool
name: 'continue-on-error'
description: 'Continue when errors occur'
})

in_cmd.add_flag(cli.Flag{
flag: .bool
name: 'verbose'
abbrev: 'v'
flag: .bool
name: 'verbose'
abbrev: 'v'
description: 'Show result of each command'
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/vsql/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import os

fn main() {
mut cmd := cli.Command{
name: 'vsql'
name: 'vsql'
description: 'vsql is a single-file or PostgeSQL-compatible SQL database written in V.\nhttps://github.com/elliotchance/vsql'
execute: unknown_command
execute: unknown_command
}

register_bench_command(mut cmd)
Expand Down
12 changes: 6 additions & 6 deletions cmd/vsql/out.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import vsql

fn register_out_command(mut cmd cli.Command) {
mut out_cmd := cli.Command{
name: 'out'
usage: '<file>'
name: 'out'
usage: '<file>'
required_args: 1
description: 'Export schema and data'
execute: out_command
description: 'Export schema and data'
execute: out_command
}

out_cmd.add_flag(cli.Flag{
flag: .bool
name: 'create-public-schema'
flag: .bool
name: 'create-public-schema'
description: 'Include "CREATE SCHEMA PUBLIC"'
})

Expand Down
22 changes: 11 additions & 11 deletions cmd/vsql/server.v
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import vsql

fn register_server_command(mut cmd cli.Command) {
mut server_cmd := cli.Command{
name: 'server'
description: 'Run as a server for PostgreSQL clients'
usage: '<file>'
name: 'server'
description: 'Run as a server for PostgreSQL clients'
usage: '<file>'
required_args: 1
execute: server_command
execute: server_command
}
server_cmd.add_flag(cli.Flag{
flag: .bool
name: 'verbose'
abbrev: 'v'
flag: .bool
name: 'verbose'
abbrev: 'v'
description: 'Verbose (show all messages in and out of the server)'
})
server_cmd.add_flag(cli.Flag{
flag: .int
name: 'port'
abbrev: 'p'
flag: .int
name: 'port'
abbrev: 'p'
description: 'Port number (default 3210)'
})
cmd.add_command(server_cmd)
Expand All @@ -36,7 +36,7 @@ fn server_command(cmd cli.Command) ! {

mut server := vsql.new_server(vsql.ServerOptions{
db_file: cmd.args[0]
port: port
port: port
verbose: cmd.flags.get_bool('verbose') or { false }
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/vsql/version.v
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import cli

fn register_version_command(mut cmd cli.Command) {
mut version_cmd := cli.Command{
name: 'version'
name: 'version'
description: 'Show version'
execute: version_command
execute: version_command
}
cmd.add_command(version_cmd)
}
Expand Down
61 changes: 61 additions & 0 deletions examples/orm.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
import vsql
import time

// CHECK ./vsql/orm_test.v for more advanced usage

fn main() {
os.rm('test.vsql') or {}

example() or { panic(err) }
}

// NOTE for some reason if we declare a @[primary] on a struct field, we can not do delete queries on the tables...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now fixed: #203

// https://github.com/elliotchance/vsql/issues/200
struct Product {
id int //@[primary]
product_name string @[sql_type: 'varchar(100)']
price f64
}

fn example() ! {
timer := time.new_stopwatch()
mut db := vsql.open('test.vsql')!

sql db {
create table Product
}!

products := [
Product{1, 'Ice Cream', 5.99},
Product{2, 'Ham Sandwhich', 3.47},
Product{3, 'Bagel', 1.25},
]

for product in products {
sql db {
insert product into Product
} or { panic(err) }
}
sql db {
update Product set product_name = 'Cereal' where id == 1
} or { panic(err) }

prod_one := sql db {
select from Product where id == 1
}!

assert prod_one.len == 1

sql db {
delete from Product where product_name == 'Cereal'
} or { panic(err) }

all := sql db {
select from Product
}!

assert all.len == 2

println(timer.elapsed())
}
8 changes: 4 additions & 4 deletions vsql/bench.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub mut:
pub fn new_benchmark(conn &Connection) Benchmark {
return Benchmark{
account_rows: 100000
teller_rows: 10
branch_rows: 1
run_for: time.minute
conn: conn
teller_rows: 10
branch_rows: 1
run_for: time.minute
conn: conn
}
}

Expand Down
6 changes: 3 additions & 3 deletions vsql/btree.v
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mut:

fn new_btree(pager Pager, page_size int) &Btree {
return &Btree{
pager: pager
pager: pager
page_size: page_size
}
}
Expand Down Expand Up @@ -383,8 +383,8 @@ fn (mut p Btree) split_page(path []int, page Page, obj PageObject, kind u8) ! {
fn (p Btree) new_range_iterator(min []u8, max []u8) PageIterator {
return PageIterator{
btree: p
min: min
max: max
min: min
max: max
}
}

Expand Down
2 changes: 1 addition & 1 deletion vsql/btree_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_btree_test() ! {
page_size := 256
file_name := 'btree.vsql'

for tt in 0 .. vsql.times {
for tt in 0 .. times {
for size := 1; size <= 1000; size *= 10 {
for blob_size in blob_sizes {
if os.exists(file_name) {
Expand Down
4 changes: 2 additions & 2 deletions vsql/compiler.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ struct CompileResult {

fn (c CompileResult) with_agg(contains_agg bool) CompileResult {
return CompileResult{
run: c.run
typ: c.typ
run: c.run
typ: c.typ
contains_agg: contains_agg
}
}
31 changes: 16 additions & 15 deletions vsql/connection.v
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ fn open_connection(path string, options ConnectionOptions) !&Connection {
catalog_name := catalog_name_from_path(path)

mut conn := &Connection{
query_cache: options.query_cache
query_cache: options.query_cache
current_catalog: catalog_name
current_schema: vsql.default_schema_name
now: default_now
current_schema: default_schema_name
now: default_now
}

register_builtin_funcs(mut conn)!
Expand All @@ -140,9 +140,9 @@ pub fn (mut conn Connection) add_catalog(catalog_name string, path string, optio

catalog := &CatalogConnection{
catalog_name: catalog_name
path: path
storage: new_storage(btree)
options: options
path: path
storage: new_storage(btree)
options: options
}

conn.catalogs[catalog_name] = catalog
Expand Down Expand Up @@ -350,9 +350,9 @@ pub fn (mut conn Connection) register_virtual_table(create_table string, data Vi
mut table_name := conn.resolve_schema_identifier(stmt.table_name)!

conn.catalogs[conn.current_catalog].virtual_tables[table_name.storage_id()] = VirtualTable{
create_table_sql: create_table
create_table_sql: create_table
create_table_stmt: stmt
data: data
data: data
}

return
Expand Down Expand Up @@ -416,19 +416,20 @@ pub fn (mut conn CatalogConnection) schema_tables(schema string) ![]Table {
// canonical (fully qualified) form.
fn (conn Connection) resolve_identifier(identifier Identifier) Identifier {
return Identifier{
custom_id: identifier.custom_id
custom_typ: identifier.custom_typ
catalog_name: if identifier.catalog_name == '' && !identifier.entity_name.starts_with('$') {
custom_id: identifier.custom_id
custom_typ: identifier.custom_typ
catalog_name: if identifier.catalog_name == ''
&& !identifier.entity_name.starts_with('$') {
conn.current_catalog
} else {
identifier.catalog_name
}
schema_name: if identifier.schema_name == '' && !identifier.entity_name.starts_with('$') {
schema_name: if identifier.schema_name == '' && !identifier.entity_name.starts_with('$') {
conn.current_schema
} else {
identifier.schema_name
}
entity_name: identifier.entity_name
entity_name: identifier.entity_name
sub_entity_name: identifier.sub_entity_name
}
}
Expand Down Expand Up @@ -518,8 +519,8 @@ pub mut:
pub fn default_connection_options() ConnectionOptions {
return ConnectionOptions{
query_cache: new_query_cache()
page_size: 4096
mutex: sync.new_rwmutex()
page_size: 4096
mutex: sync.new_rwmutex()
}
}

Expand Down
4 changes: 2 additions & 2 deletions vsql/connection_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ fn test_concurrent_writes() ! {
waits << spawn fn (file_name string, i int, options ConnectionOptions) {
mut db := open_database(file_name, options) or { panic(err) }
for j in 0 .. 100 {
if vsql.verbose {
if verbose {
println('${i}.${j}: INSERT start')
}
db.query('INSERT INTO foo (x) VALUES (1)') or { panic(err) }
if vsql.verbose {
if verbose {
println('${i}.${j}: INSERT done')
}
}
Expand Down
Loading
Loading