orm #

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
  • [skip] field will be skipped
  • [sql: type] sets the type which is used in sql (special type serial)
  • [sql: 'name'] sets a custom column name for the field

Usage

struct Foo {
    id   int    [primary; sql: serial]
    name string [nonull]
}

Create

sql db {
    create table Foo
}

Drop

sql db {
    drop table Foo
}

Insert

var := Foo{
    name: 'abc'
}

sql db {
    insert var into Foo
}

Update

sql db {
    update Foo set name = 'cde' 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 limit 5
}
result := sql db {
    select from Foo where id > 1 order by id
}

Constants #

const (
	num64    = [ast.i64_type_idx, ast.u64_type_idx]
	nums     = [
		ast.i8_type_idx,
		ast.i16_type_idx,
		ast.int_type_idx,
		ast.byte_type_idx,
		ast.u16_type_idx,
		ast.u32_type_idx,
		ast.bool_type_idx,
	]
	float    = [
		ast.f32_type_idx,
		ast.f64_type_idx,
	]
	string   = ast.string_type_idx
	time     = -2
	type_idx = {
		'i8':     ast.i8_type_idx
		'i16':    ast.i16_type_idx
		'int':    ast.int_type_idx
		'i64':    ast.i64_type_idx
		'byte':   ast.byte_type_idx
		'u16':    ast.u16_type_idx
		'u32':    ast.u32_type_idx
		'u64':    ast.u64_type_idx
		'f32':    ast.f32_type_idx
		'f64':    ast.f64_type_idx
		'bool':   ast.bool_type_idx
		'string': ast.string_type_idx
	}
	string_max_len = 2048
)

fn bool_to_primitive #

fn bool_to_primitive(b bool) Primitive

needed for backend functions

fn byte_to_primitive #

fn byte_to_primitive(b byte) Primitive

fn f32_to_primitive #

fn f32_to_primitive(b f32) Primitive

fn f64_to_primitive #

fn f64_to_primitive(b f64) Primitive

fn i16_to_primitive #

fn i16_to_primitive(b i16) Primitive

fn i64_to_primitive #

fn i64_to_primitive(b i64) Primitive

fn i8_to_primitive #

fn i8_to_primitive(b i8) Primitive

fn infix_to_primitive #

fn infix_to_primitive(b InfixType) Primitive

fn int_to_primitive #

fn int_to_primitive(b int) Primitive

fn orm_select_gen #

fn orm_select_gen(orm SelectConfig, para string, num bool, qm string, start_pos int, where QueryData) string

fn orm_stmt_gen #

fn orm_stmt_gen(table string, para string, kind StmtKind, num bool, qm string, start_pos int, data QueryData, where QueryData) string

fn orm_table_gen #

fn orm_table_gen(table string, para string, defaults bool, def_unique_len int, fields []TableField, sql_from_v fn (int) ?string, alternative bool) ?string

fn string_to_primitive #

fn string_to_primitive(b string) Primitive

fn time_to_primitive #

fn time_to_primitive(b time.Time) Primitive

fn u16_to_primitive #

fn u16_to_primitive(b u16) Primitive

fn u32_to_primitive #

fn u32_to_primitive(b u32) Primitive

fn u64_to_primitive #

fn u64_to_primitive(b u64) Primitive

interface Connection #

interface Connection {
	@select(config SelectConfig, data QueryData, where QueryData) ?[][]Primitive
	insert(table string, data QueryData) ?
	update(table string, data QueryData, where QueryData) ?
	delete(table string, where QueryData) ?
	create(table string, fields []TableField) ?
	drop(talbe string) ?
	last_id() Primitive
}

type Primitive #

type Primitive = InfixType
	| bool
	| byte
	| f32
	| f64
	| i16
	| i64
	| i8
	| int
	| string
	| time.Time
	| u16
	| u32
	| u64

enum MathOperationKind #

enum MathOperationKind {
	add
	sub
	mul
	div
}

enum OperationKind #

enum OperationKind {
	neq
	eq
	gt
	lt
	ge
	le
}

enum OrderType #

enum OrderType {
	asc
	desc
}

enum StmtKind #

enum StmtKind {
	insert
	update
	delete
}

struct InfixType #

struct InfixType {
pub:
	name     string
	operator MathOperationKind
	right    Primitive
}

struct QueryData #

struct QueryData {
pub:
	fields []string
	data   []Primitive
	types  []int
	kinds  []OperationKind
	is_and []bool
}

struct SelectConfig #

struct SelectConfig {
pub:
	table      string
	is_count   bool
	has_where  bool
	has_order  bool
	order      string
	order_type OrderType
	has_limit  bool
	primary    string = 'id'
	has_offset bool
	fields     []string
	types      []int
}

struct TableField #

struct TableField {
pub:
	name        string
	typ         int
	is_time     bool
	default_val string
	is_arr      bool
	attrs       []StructAttribute
}