1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm: allow inserting empty objects with db.sqlite (SQLite uses a slightly different SQL dialect) (#17334)

This commit is contained in:
walking devel 2023-02-16 09:34:16 +00:00 committed by GitHub
parent 289993ad7f
commit 580dbc3f0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 72 additions and 34 deletions

View File

@ -134,20 +134,20 @@ pub fn (db Connection) insert(table string, data orm.QueryData) ! {
is_and: [] is_and: []
} }
query, converted_data := orm.orm_stmt_gen(table, '`', .insert, false, '?', 1, converted_primitive_data, query, converted_data := orm.orm_stmt_gen(.default, table, '`', .insert, false, '?',
orm.QueryData{}) 1, converted_primitive_data, orm.QueryData{})
mysql_stmt_worker(db, query, converted_data, orm.QueryData{})! mysql_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
// update is used internally by V's ORM for processing `UPDATE ` queries // update is used internally by V's ORM for processing `UPDATE ` queries
pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .update, false, '?', 1, data, where) query, _ := orm.orm_stmt_gen(.default, table, '`', .update, false, '?', 1, data, where)
mysql_stmt_worker(db, query, data, where)! mysql_stmt_worker(db, query, data, where)!
} }
// delete is used internally by V's ORM for processing `DELETE ` queries // delete is used internally by V's ORM for processing `DELETE ` queries
pub fn (db Connection) delete(table string, where orm.QueryData) ! { pub fn (db Connection) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .delete, false, '?', 1, orm.QueryData{}, query, _ := orm.orm_stmt_gen(.default, table, '`', .delete, false, '?', 1, orm.QueryData{},
where) where)
mysql_stmt_worker(db, query, orm.QueryData{}, where)! mysql_stmt_worker(db, query, orm.QueryData{}, where)!
} }

View File

@ -33,20 +33,21 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
// insert is used internally by V's ORM for processing `INSERT ` queries // insert is used internally by V's ORM for processing `INSERT ` queries
pub fn (db DB) insert(table string, data orm.QueryData) ! { pub fn (db DB) insert(table string, data orm.QueryData) ! {
query, converted_data := orm.orm_stmt_gen(table, '"', .insert, true, '$', 1, data, query, converted_data := orm.orm_stmt_gen(.default, table, '"', .insert, true, '$',
orm.QueryData{}) 1, data, orm.QueryData{})
pg_stmt_worker(db, query, converted_data, orm.QueryData{})! pg_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
// update is used internally by V's ORM for processing `UPDATE ` queries // update is used internally by V's ORM for processing `UPDATE ` queries
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '"', .update, true, '$', 1, data, where) query, _ := orm.orm_stmt_gen(.default, table, '"', .update, true, '$', 1, data, where)
pg_stmt_worker(db, query, data, where)! pg_stmt_worker(db, query, data, where)!
} }
// delete is used internally by V's ORM for processing `DELETE ` queries // delete is used internally by V's ORM for processing `DELETE ` queries
pub fn (db DB) delete(table string, where orm.QueryData) ! { pub fn (db DB) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '"', .delete, true, '$', 1, orm.QueryData{}, where) query, _ := orm.orm_stmt_gen(.default, table, '"', .delete, true, '$', 1, orm.QueryData{},
where)
pg_stmt_worker(db, query, orm.QueryData{}, where)! pg_stmt_worker(db, query, orm.QueryData{}, where)!
} }

View File

@ -53,20 +53,21 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
// insert is used internally by V's ORM for processing `INSERT ` queries // insert is used internally by V's ORM for processing `INSERT ` queries
pub fn (db DB) insert(table string, data orm.QueryData) ! { pub fn (db DB) insert(table string, data orm.QueryData) ! {
query, converted_data := orm.orm_stmt_gen(table, '`', .insert, true, '?', 1, data, query, converted_data := orm.orm_stmt_gen(.sqlite, table, '`', .insert, true, '?',
orm.QueryData{}) 1, data, orm.QueryData{})
sqlite_stmt_worker(db, query, converted_data, orm.QueryData{})! sqlite_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
// update is used internally by V's ORM for processing `UPDATE ` queries // update is used internally by V's ORM for processing `UPDATE ` queries
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .update, true, '?', 1, data, where) query, _ := orm.orm_stmt_gen(.sqlite, table, '`', .update, true, '?', 1, data, where)
sqlite_stmt_worker(db, query, data, where)! sqlite_stmt_worker(db, query, data, where)!
} }
// delete is used internally by V's ORM for processing `DELETE ` queries // delete is used internally by V's ORM for processing `DELETE ` queries
pub fn (db DB) delete(table string, where orm.QueryData) ! { pub fn (db DB) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .delete, true, '?', 1, orm.QueryData{}, where) query, _ := orm.orm_stmt_gen(.sqlite, table, '`', .delete, true, '?', 1, orm.QueryData{},
where)
sqlite_stmt_worker(db, query, orm.QueryData{}, where)! sqlite_stmt_worker(db, query, orm.QueryData{}, where)!
} }

View File

@ -132,18 +132,18 @@ pub fn (db Connection) insert(table string, data orm.QueryData) ! {
is_and: [] is_and: []
} }
query, converted_data := orm.orm_stmt_gen(table, '`', .insert, false, '?', 1, converted_primitive_data, query, converted_data := orm.orm_stmt_gen(.default, table, '`', .insert, false, '?',
orm.QueryData{}) 1, converted_primitive_data, orm.QueryData{})
mysql_stmt_worker(db, query, converted_data, orm.QueryData{})! mysql_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db Connection) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .update, false, '?', 1, data, where) query, _ := orm.orm_stmt_gen(.default, table, '`', .update, false, '?', 1, data, where)
mysql_stmt_worker(db, query, data, where)! mysql_stmt_worker(db, query, data, where)!
} }
pub fn (db Connection) delete(table string, where orm.QueryData) ! { pub fn (db Connection) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .delete, false, '?', 1, orm.QueryData{}, query, _ := orm.orm_stmt_gen(.default, table, '`', .delete, false, '?', 1, orm.QueryData{},
where) where)
mysql_stmt_worker(db, query, orm.QueryData{}, where)! mysql_stmt_worker(db, query, orm.QueryData{}, where)!
} }

View File

@ -79,6 +79,11 @@ pub enum OrderType {
desc desc
} }
pub enum SQLDialect {
default
sqlite
}
fn (kind OperationKind) to_str() string { fn (kind OperationKind) to_str() string {
str := match kind { str := match kind {
.neq { '!=' } .neq { '!=' }
@ -181,9 +186,9 @@ pub interface Connection {
// Generates an sql stmt, from universal parameter // Generates an sql stmt, from universal parameter
// q - The quotes character, which can be different in every type, so it's variable // q - The quotes character, which can be different in every type, so it's variable
// num - Stmt uses nums at prepared statements (? or ?1) // num - Stmt uses nums at prepared statements (? or ?1)
// qm - Character for prepared statment, qm because of quotation mark like in sqlite // qm - Character for prepared statement, qm because of quotation mark like in sqlite
// start_pos - When num is true, it's the start position of the counter // start_pos - When num is true, it's the start position of the counter
pub fn orm_stmt_gen(table string, q string, kind StmtKind, num bool, qm string, start_pos int, data QueryData, where QueryData) (string, QueryData) { pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKind, num bool, qm string, start_pos int, data QueryData, where QueryData) (string, QueryData) {
mut str := '' mut str := ''
mut c := start_pos mut c := start_pos
mut data_fields := []string{} mut data_fields := []string{}
@ -217,11 +222,19 @@ pub fn orm_stmt_gen(table string, q string, kind StmtKind, num bool, qm string,
c++ c++
} }
str += 'INSERT INTO ${q}${table}${q} (' str += 'INSERT INTO ${q}${table}${q} '
str += select_fields.join(', ')
str += ') VALUES (' are_values_empty := values.len == 0
str += values.join(', ')
str += ')' if sql_dialect == .sqlite && are_values_empty {
str += 'DEFAULT VALUES'
} else {
str += '('
str += select_fields.join(', ')
str += ') VALUES ('
str += values.join(', ')
str += ')'
}
} }
.update { .update {
str += 'UPDATE ${q}${table}${q} SET ' str += 'UPDATE ${q}${table}${q} SET '

View File

@ -1,7 +1,7 @@
import orm import orm
fn test_orm_stmt_gen_update() { fn test_orm_stmt_gen_update() {
query, _ := orm.orm_stmt_gen('Test', "'", .update, true, '?', 0, orm.QueryData{ query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .update, true, '?', 0, orm.QueryData{
fields: ['test', 'a'] fields: ['test', 'a']
data: [] data: []
types: [] types: []
@ -16,7 +16,7 @@ fn test_orm_stmt_gen_update() {
} }
fn test_orm_stmt_gen_insert() { fn test_orm_stmt_gen_insert() {
query, _ := orm.orm_stmt_gen('Test', "'", .insert, true, '?', 0, orm.QueryData{ query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .insert, true, '?', 0, orm.QueryData{
fields: ['test', 'a'] fields: ['test', 'a']
data: [] data: []
types: [] types: []
@ -26,7 +26,7 @@ fn test_orm_stmt_gen_insert() {
} }
fn test_orm_stmt_gen_delete() { fn test_orm_stmt_gen_delete() {
query, _ := orm.orm_stmt_gen('Test', "'", .delete, true, '?', 0, orm.QueryData{ query, _ := orm.orm_stmt_gen(.default, 'Test', "'", .delete, true, '?', 0, orm.QueryData{
fields: ['test', 'a'] fields: ['test', 'a']
data: [] data: []
types: [] types: []

View File

@ -21,12 +21,33 @@ mut:
text string text string
} }
struct Account {
id int [primary; sql: serial]
}
pub fn insert_parent(db sqlite.DB, mut parent Parent) { pub fn insert_parent(db sqlite.DB, mut parent Parent) {
sql db { sql db {
insert parent into Parent insert parent into Parent
} }
} }
fn test_insert_empty_object() {
db := sqlite.connect(':memory:') or { panic(err) }
account := Account{}
sql db {
create table Account
insert account into Account
}
accounts := sql db {
select from Account
}
assert accounts.len == 1
}
fn test_orm_insert_mut_object() { fn test_orm_insert_mut_object() {
db := sqlite.connect(':memory:') or { panic(err) } db := sqlite.connect(':memory:') or { panic(err) }

View File

@ -31,18 +31,19 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
// sql stmt // sql stmt
pub fn (db DB) insert(table string, data orm.QueryData) ! { pub fn (db DB) insert(table string, data orm.QueryData) ! {
query, converted_data := orm.orm_stmt_gen(table, '"', .insert, true, '$', 1, data, query, converted_data := orm.orm_stmt_gen(.default, table, '"', .insert, true, '$',
orm.QueryData{}) 1, data, orm.QueryData{})
pg_stmt_worker(db, query, converted_data, orm.QueryData{})! pg_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '"', .update, true, '$', 1, data, where) query, _ := orm.orm_stmt_gen(.default, table, '"', .update, true, '$', 1, data, where)
pg_stmt_worker(db, query, data, where)! pg_stmt_worker(db, query, data, where)!
} }
pub fn (db DB) delete(table string, where orm.QueryData) ! { pub fn (db DB) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '"', .delete, true, '$', 1, orm.QueryData{}, where) query, _ := orm.orm_stmt_gen(.default, table, '"', .delete, true, '$', 1, orm.QueryData{},
where)
pg_stmt_worker(db, query, orm.QueryData{}, where)! pg_stmt_worker(db, query, orm.QueryData{}, where)!
} }

View File

@ -53,18 +53,19 @@ pub fn (db DB) @select(config orm.SelectConfig, data orm.QueryData, where orm.Qu
// sql stmt // sql stmt
pub fn (db DB) insert(table string, data orm.QueryData) ! { pub fn (db DB) insert(table string, data orm.QueryData) ! {
query, converted_data := orm.orm_stmt_gen(table, '`', .insert, true, '?', 1, data, query, converted_data := orm.orm_stmt_gen(.sqlite, table, '`', .insert, true, '?',
orm.QueryData{}) 1, data, orm.QueryData{})
sqlite_stmt_worker(db, query, converted_data, orm.QueryData{})! sqlite_stmt_worker(db, query, converted_data, orm.QueryData{})!
} }
pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! { pub fn (db DB) update(table string, data orm.QueryData, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .update, true, '?', 1, data, where) query, _ := orm.orm_stmt_gen(.sqlite, table, '`', .update, true, '?', 1, data, where)
sqlite_stmt_worker(db, query, data, where)! sqlite_stmt_worker(db, query, data, where)!
} }
pub fn (db DB) delete(table string, where orm.QueryData) ! { pub fn (db DB) delete(table string, where orm.QueryData) ! {
query, _ := orm.orm_stmt_gen(table, '`', .delete, true, '?', 1, orm.QueryData{}, where) query, _ := orm.orm_stmt_gen(.sqlite, table, '`', .delete, true, '?', 1, orm.QueryData{},
where)
sqlite_stmt_worker(db, query, orm.QueryData{}, where)! sqlite_stmt_worker(db, query, orm.QueryData{}, where)!
} }