1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/orm
Delyan Angelov db97630117
examples, orm: fix orm example; add -d trace_orm option to see all ORM generated queries (#17770)
* orm: add tracing of the generated queries by the orm module, when a program is compiled with `-d trace_orm`

* examples: fix examples/database/orm.v, add comments, and ensure that the example can be run several times with no errors

The example demonstrates connecting to all 3 DBs in the same program, and will be added to the CI very soon,
to serve both as a regression test, if it fails, and as an example to how to use the ORM in combination with
raw SQL queries to the DBs (which are driver/wrapper specific, unlike the ORM, but can be more convenient in some situations).
2023-03-25 21:46:17 +02:00
..
orm_create_and_drop_test.v cgen, checker: add panic in ORM for invalid queries, when there are no or {} blocks, add type checking for the fkey attribute, add tests (#16977) 2023-01-17 20:21:10 +02:00
orm_fn_calls_test.v orm: support fn calls in where (#17127) 2023-01-26 22:36:30 +02:00
orm_fn_test.v orm: allow inserting empty objects with db.sqlite (SQLite uses a slightly different SQL dialect) (#17334) 2023-02-16 11:34:16 +02:00
orm_insert_reserved_name_test.v sql: fix wrong field name on generated code (#17684) 2023-03-17 21:45:52 +01:00
orm_insert_test.v parser: breaking change, let V ORM queries return arrays for *all* non-count queries, including limit = 1 (#17719) 2023-03-22 09:48:01 +02:00
orm_interface_test.v orm: allow using connections, that were explicitly casted to orm.Connection too (#17427) 2023-02-27 23:54:03 +02:00
orm_last_id_test.v orm: make last_id() return int, instead of orm.Primitive (fix #12110) (#17151) 2023-01-29 16:00:23 +02:00
orm_mut_db_test.v orm: allow use of mut db orm.Connection (#17439) 2023-03-01 00:49:30 +02:00
orm_sql_or_blocks_test.v parser: breaking change, let V ORM queries return arrays for *all* non-count queries, including limit = 1 (#17719) 2023-03-22 09:48:01 +02:00
orm_string_interpolation_in_where_test.v cgen: support string interpolation in ORM queries (#17141) 2023-01-29 01:27:34 +02:00
orm_test.v parser: breaking change, let V ORM queries return arrays for *all* non-count queries, including limit = 1 (#17719) 2023-03-22 09:48:01 +02:00
orm.v examples, orm: fix orm example; add -d trace_orm option to see all ORM generated queries (#17770) 2023-03-25 21:46:17 +02:00
README.md vlib: move the mysql/sqlite/pg/mssql modules under vlib/db (#16820) 2023-01-13 17:02:32 +02:00

ORM

Attributes

Structs

  • [table: 'name'] sets a custom table name

Fields

  • [primary] sets the field as the primary key
  • [unique] sets the field as unique
  • [unique: 'foo'] adds the field to a unique group
  • [nonull] set the field as not null
  • [skip] field will be skipped
  • [sql: type] where type is a V type such as int or f64, or special type serial
  • [sql: 'name'] sets a custom column name for the field
  • [sql_type: 'SQL TYPE'] sets the sql type which is used in sql
  • [default: 'sql defaults'] sets the default value or function when create a new table
  • [fkey: 'parent_id'] sets foreign key for an field which holds an array

Usage

struct Foo {
    id          int         [primary; sql: serial]
    name        string      [nonull]
    created_at  time.Time   [sql_type: 'DATETIME']
    updated_at  string      [sql_type: 'DATETIME']
    deleted_at  time.Time
    children    []Child     [fkey: 'parent_id']
}

struct Child {
    id        int    [primary; sql: serial]
    parent_id int
    name      string
}

Create

sql db {
    create table Foo
}

Drop

sql db {
    drop table Foo
}

Insert

var := Foo{
    name:       'abc'
    created_at: time.now()
    updated_at: time.now().str()
    deleted_at: time.now()
    children: [
        Child{
            name: 'abc'
        },
        Child{
            name: 'def'
        },
    ]
}

sql db {
    insert var into Foo
}

Update

sql db {
    update Foo set name = 'cde', updated_at = time.now() where name == 'abc'
}

Delete

sql db {
    delete from Foo where id > 10
}

Select

result := sql db {
    select from Foo where id == 1
}
result := sql db {
    select from Foo where id > 1 && name != 'lasanha' limit 5
}
result := sql db {
    select from Foo where id > 1 order by id
}

Example

import db.pg

struct Member {
	id         string [default: 'gen_random_uuid()'; primary; sql_type: 'uuid']
	name       string
	created_at string [default: 'CURRENT_TIMESTAMP'; sql_type: 'TIMESTAMP']
}

fn main() {
	db := pg.connect(pg.Config{
		host: 'localhost'
		port: 5432
		user: 'user'
		password: 'password'
		dbname: 'dbname'
	}) or {
		println(err)
		return
	}

	defer {
		db.close()
	}

	sql db {
		create table Member
	}

	new_member := Member{
		name: 'John Doe'
	}

	sql db {
		insert new_member into Member
	}

	selected_member := sql db {
		select from Member where name == 'John Doe' limit 1
	}

	sql db {
		update Member set name = 'Hitalo' where id == selected_member.id
	}
}