Skip to content

Commit

Permalink
fmt example and test code
Browse files Browse the repository at this point in the history
  • Loading branch information
lydiandy committed Mar 19, 2021
1 parent 65ec98e commit a42e005
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 98 deletions.
8 changes: 8 additions & 0 deletions example/create_and_init_db.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,39 @@ fn main() {
password: '' // chagne to your password
database: 'test_db' // change to your database
}

// connect to database with config
mut db := vsql.connect(config) or { panic('connect error:$err') }

// create table person
db.exec('drop table if exists person')
db.exec("create table person (id integer primary key, name text default '',age integer default 0,income integer default 0);")

// insert data
db.exec("insert into person (id,name,age,income) values (1,'tom',29,1000)")
db.exec("insert into person (id,name,age,income) values (2,'jack',33,500)")
db.exec("insert into person (id,name,age,income) values (3,'mary',25,2000)")
db.exec("insert into person (id,name,age,income) values (4,'lisa',25,1000)")
db.exec("insert into person (id,name,age,income) values (5,'andy',18,0)")

// create table cat
db.exec('drop table if exists cat')
db.exec("create table cat (id integer primary key,name text default '',owner_id integer)")

// insert data
db.exec("insert into cat (id,name,owner_id) values (1,'cat1',1)")
db.exec("insert into cat (id,name,owner_id) values (2,'cat2',3)")
db.exec("insert into cat (id,name,owner_id) values (3,'cat3',5)")

// create table food
db.exec('drop table if exists food')
db.exec("create table food (id integer primary key,name text default '',cat_id integer)")

// insert data
db.exec("insert into food (id,name,cat_id) values (1,'food1',1)")
db.exec("insert into food (id,name,cat_id) values (2,'food2',3)")
db.exec("insert into food (id,name,cat_id) values (3,'food3',0)")

// for test create table,drop person2
db.exec('drop table if exists person2')
db.exec('drop table if exists new_person')
Expand Down
2 changes: 2 additions & 0 deletions example/hello.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ fn main() {
password: ''
database: 'test_db'
}

// connect to database with config
mut db := vsql.connect(config) or { panic('connect error:$err') }

// start to use db
res := db.table('person').column('*').end()
println(res)
Expand Down
40 changes: 24 additions & 16 deletions example/schema.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,47 @@ fn main() {
password: ''
database: 'test_db'
}

// connect to database with config
mut db := vsql.connect(config) or { panic('connect error:$err') }
mut res := []pg.Row{}

// create database
res = db.create_database('mydb')
println(res)

// create table
db.create_table('person2', fn (mut table vsql.Table) {
// table.increment('id').primary()
table.increment('id')
table.boolean('is_ok')
table.string_('open_id', 255).size(100).unique()
table.datetime('attend_time')
table.string_('form_id', 255).not_null()
table.integer('is_send').default_to('1')
table.decimal('amount', 10, 2).not_null().check('amount>0')
// table constraint
table.primary(['id'])
table.unique(['id'])
table.check('amount>30')
table.check('amount<60')
})
mut table := db.create_table('person2')
// table.increment('id').primary()
table.increment('id')
table.boolean('is_ok')
table.string_('open_id', 255).size(100).unique()
table.datetime('attend_time')
table.string_('form_id', 255).not_null()
table.integer('is_send').default_to('1')
table.decimal('amount', 10, 2).not_null().check('amount>0')
// table constraint
table.primary(['id'])
table.unique(['id'])
table.check('amount>30')
table.check('amount<60')

table.exec()

// alter table
//

// rename table
res = db.rename_table('person', 'new_person')
println(res)

// truncate table
res = db.truncate('new_person')
println(res)

// drop table
res = db.drop_table('food')
println(res)

// drop table if exist
res = db.drop_table_if_exist('cat')
println(res)
Expand Down
57 changes: 51 additions & 6 deletions example/select.v
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//before you run the example,run create_and_init_db.v first
// before you run the example,run create_and_init_db.v first
module main

import vsql
Expand All @@ -14,103 +14,140 @@ fn main() {
database: 'test_db'
}
// connect to database with config
mut db := vsql.connect(config) or {
panic('connect error:$err')
}
mut db := vsql.connect(config) or { panic('connect error:$err') }
mut res := []pg.Row{}

// select+from
res = db.select_('*').from('person').end()
res = db.select_('id,name,age,income').from('person').end()

// table+column
res = db.table('person').column('*').end()
res = db.table('person').column('id,name,age').end()

// table+select is also ok
res = db.table('person').select_('*').end()

// from+column is also ok
res = db.from('person').column('*').end()

// first
res = db.table('person').column('*').first().end()

// limit
res = db.table('person').column('*').limit(3).end()

// offset
res = db.table('person').column('*').offset(1).end()

// offset+limit
res = db.table('person').column('*').offset(2).limit(2).end()

// distinct
res = db.table('person').column('id,name,age').distinct().end()
res = db.select_('id,name,age').distinct().from('person').end()

// order by
res = db.table('person').column('*').order_by('id desc').end()
res = db.table('person').column('*').order_by('name desc').order_by('age asc').end()
res = db.table('person').column('*').order_by('name desc').order_by('age').end()
res = db.table('person').column('').order_by_raw('name desc,age asc').end()

// group by
res = db.table('person').column('age,count(age)').group_by('age').end()
res = db.table('person').column('age,count(age)').group_by('age').group_by('name').end()
res = db.table('person').column('age,count(age)').group_by_raw('age,income').end()
res = db.table('person').column('age,count(age),avg(income)').group_by('age').end()

// having
res = db.table('person').column('age,count(age),avg(income)').group_by('age').having('count(*)=2').end()

// where raw
res = db.table('person').where_raw('id=?', '1').end()

// where
res = db.table('person').column('id,name,age').where('id=1').end()

// or where
res = db.table('person').column('id,name,age').where('id=1').or_where('id=2').end()

// and where
res = db.table('person').column('id,name,age').where('id=1').and_where('age=29').end()

// where not
res = db.table('person').column('id,name,age').where('id=1').where_not('age=0').end()

// or where not
res = db.table('person').column('id,name,age').where('id=1').or_where_not('age=0').end()

// where in
res = db.table('person').column('id,name,age').where_in('id', ['1', '2', '3']).end()

// or where in
res = db.table('person').column('id,name,age').where('id=1').or_where_in('id', ['1', '2', '3']).end()

// and where in
res = db.table('person').column('id,name,age').where('id=1').and_where_in('id', ['1', '2',
'3'
'3',
]).end()

// where not in
res = db.table('person').column('id,name,age').where('id=1').where_not_in('id', ['2', '3']).end()

// or where not in
res = db.table('person').column('id,name,age').where('id=1').or_where_not_in('id',
['2', '3']).end()

// where null
res = db.table('person').column('id,name,age').where('id>1').where_null('income').end()

// or where null
res = db.table('person').column('id,name,age').where('id>1').or_where_null('income').end()

// and where null
res = db.table('person').column('id,name,age').where('id>1').and_where_null('income').end()

// where not null
res = db.table('person').column('id,name,age').where('id>1').where_not_null('income').end()

// or where not null
res = db.table('person').column('id,name,age').where('id>1').or_where_not_null('income').end()

// where between
res = db.table('person').column('id,name,age,income').where('id>1').where_between('income',
['100', '1000']).end()

// or where between
res = db.table('person').column('id,name,age,income').where('id>1').or_where_between('income',
['100', '1000']).end()

// and where between
res = db.table('person').column('id,name,age,income').where('id>1').and_where_between('income',
['100', '1000']).end()

// where not between
res = db.table('person').column('id,name,age,income').where('id>1').where_not_between('income',
['100', '1000']).end()

// or where not between
res = db.table('person').column('id,name,age,income').where('id>1').or_where_not_between('income',
['100', '1000']).end()

// where exists
res = db.table('person').column('id,name,age,income').where('id>1').where_exists('select income from person where income>1000').end()

// or where exists
res = db.table('person').column('id,name,age,income').where('id>1').or_where_exists('select income from person where income>1000').end()

// and where exists
res = db.table('person').column('id,name,age,income').where('id>1').and_where_exists('select income from person where income>1000').end()

// where not exists
res = db.table('person').column('id,name,age,income').where('id>1').where_not_exists('select income from person where income>1000').end()

// or where not exists
res = db.table('person').column('id,name,age,income').where('id>1').or_where_not_exists('select income from person where income>1000').end()

// aggregate function
res = db.table('person').count('*').end()
res = db.table('person').count('* as rows').end()
Expand All @@ -120,32 +157,40 @@ fn main() {
res = db.table('person').min('age as min_age').max('age as max_age').end()
res = db.table('person').sum('income').end()
res = db.table('person').avg('income').end()
//union statement

// union statement
stmt1 := db.table('person').column('id,name').where('id=1').to_sql()
stmt2 := db.table('person').column('id,name').where('id=2').to_sql()
stmt3 := db.table('person').column('id,name').where('id=3').to_sql()
res = db.table('person').column('id,name').where('id=4').union_(stmt1, stmt2, stmt3).end()
res = db.table('person').column('id,name').where('id=4').union_all(stmt1, stmt2, stmt3).end()
res = db.table('person').column('id,name').where('id=4').intersect(stmt1, stmt2, stmt3).end()
res = db.table('person').column('id,name').where('id=4').except(stmt1, stmt2, stmt3).end()

// join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').join('person as p',
'c.owner_id=p.id').end()

// inner join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').inner_join('person as p',
'c.owner_id=p.id').end()

// left join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').left_join('person as p',
'c.owner_id=p.id').end()

// right join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').right_join('person as p',
'c.owner_id=p.id').end()

// outer join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').outer_join('person as p',
'c.owner_id=p.id').end()

// cross join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').cross_join('person as p').end()
res = db.table('cat as c').column('c.id,c.name,p.name,p.age').join_raw('join person as p on c.owner_id=p.id').end()

// multi join
res = db.table('cat as c').column('c.id,c.name,p.name,p.age,f.name').left_join('person as p',
'c.owner_id=p.id').left_join('food as f', 'c.id=f.cat_id').end()
Expand Down
1 change: 1 addition & 0 deletions example/todo.v
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {
db.table('person')...
t.rollback()
})

//model migration
db.up()
db.down()
Expand Down
2 changes: 2 additions & 0 deletions example/transaction.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ fn main() {
password: ''
database: 'test_db'
}

// connect to database with config
mut db := vsql.connect(config) or { panic('connect error:$err') }

t := db.transaction()
// t := db.tx() //the shorter fn
t.exec("insert into person (id,name,age,income) values (33,'name33',33,0)")
Expand Down
33 changes: 21 additions & 12 deletions example/update.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,41 @@ fn main() {
password: ''
database: 'test_db'
}

// connect to database with config
mut db := vsql.connect(config) or { panic('connect error:$err') }
mut res := []pg.Row{}

// insert
res = db.table('person').insert({
'id': '6'
res = db.table('person').insert(map{
'id': '6'
'name': 'abc'
'age': '36'
'age': '36'
}).end()
res = db.table('person').insert({
'id': '7'
res = db.table('person').insert(map{
'id': '7'
'name': 'abc'
'age': '36'
'age': '36'
}).returning('id', 'name').end()
res = db.insert({
'id': '8'
res = db.insert(map{
'id': '8'
'name': 'tom'
}).into('person').returning('id').end()

// update
res = db.table('person').update({
res = db.table('person').update(map{
'name': 'paris'
}).where('id=1').returning('id').end()
res = db.table('person').update({
res = db.table('person').update(map{
'name': 'paris'
'age': '32'
'age': '32'
}).where('id=1').returning('id').end()

// delete
res = db.table('person').delete().where('id=3').end()

res = db.table('person').where('id=2').delete().end()
// -------------------

// res = db.create_database('mydb')
// create table
// db.create_table('person2', fn (mut table vsql.Table) {
Expand All @@ -62,12 +67,16 @@ fn main() {
// }) or {
// panic('create table failed:$err')
// }

// rename table
res = db.rename_table('person', 'new_person')

// truncate table
res = db.truncate('new_person')

// drop table
res = db.drop_table('food')

// drop table if exist
res = db.drop_table_if_exist('cat')
}
Loading

0 comments on commit a42e005

Please sign in to comment.