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

mssql: support orm create and drop table (#10296)

This commit is contained in:
youyuanwu 2021-06-01 01:10:27 -07:00 committed by GitHub
parent 67518f946b
commit 98505207c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 3 deletions

View File

@ -6,8 +6,15 @@ pub:
server string
uid string
pwd string
// if dbname empty, conn str will not contain Database info,
// and it is up to the server to choose which db to connect to.
dbname string
}
pub fn (cfg Config) get_conn_str() string {
return 'Driver=$cfg.driver;Server=$cfg.server;UID=$cfg.uid;PWD=$cfg.pwd'
mut str := 'Driver=$cfg.driver;Server=$cfg.server;UID=$cfg.uid;PWD=$cfg.pwd'
if cfg.dbname != '' {
str += ';Database=$cfg.dbname'
}
return str
}

View File

@ -114,7 +114,10 @@ fn extract_error(fnName string, handle C.SQLHANDLE, tp C.SQLSMALLINT) string {
// add driver error string
if ret == C.SQLRETURN(C.SQL_SUCCESS) || ret == C.SQLRETURN(C.SQL_SUCCESS_WITH_INFO) {
unsafe {
err_str += ':odbc=$(&sql_state[0]).vstring():$i:${int(native_error)}:$(&message_text[0]).vstring()\n'
state_str := (&sql_state[0]).vstring()
native_error_code := int(native_error)
txt_str := (&message_text[0]).vstring()
err_str += '\n\todbc=$state_str:$i:$native_error_code:$txt_str'
}
}
}

View File

@ -21,6 +21,7 @@ enum SqlType {
sqlite3
mysql
psql
mssql
unknown
}
@ -68,6 +69,9 @@ fn (mut g Gen) sql_create_table(node ast.SqlStmtLine, expr ast.Expr) {
.psql {
g.psql_create_table(node, typ, expr)
}
.mssql {
g.mssql_create_table(node, typ, expr)
}
else {
verror('This database type `$typ` is not implemented yet in orm') // TODO add better error
}
@ -86,6 +90,9 @@ fn (mut g Gen) sql_drop_table(node ast.SqlStmtLine, expr ast.Expr) {
.psql {
g.psql_drop_table(node, typ, expr)
}
.mssql {
g.mssql_drop_table(node, typ, expr)
}
else {
verror('This database type `$typ` is not implemented yet in orm') // TODO add better error
}
@ -137,6 +144,9 @@ fn (mut g Gen) sql_type_from_v(typ SqlType, v_typ ast.Type) string {
.psql {
return g.psql_get_table_type(v_typ)
}
.mssql {
return g.mssql_get_table_type(v_typ)
}
else {
// add error
}
@ -1131,6 +1141,66 @@ fn (mut g Gen) psql_bind(val string, typ ast.Type) {
g.sql_buf.writeln(');')
}
// mssql
fn (mut g Gen) mssql_create_table(node ast.SqlStmtLine, typ SqlType, db_expr ast.Expr) {
g.writeln('// mssql table creator')
create_string := g.table_gen(node, typ, db_expr)
tmp := g.new_tmp_var()
g.write('Option_mssql__Result $tmp = mssql__Connection_query(&')
g.expr(db_expr)
g.writeln(', _SLIT("$create_string"));')
tmp_str := 'str_intp(1, _MOV((StrIntpData[]){{_SLIT("Something went wrong: "), $si_s_code ,{.d_s=IError_str(err)}}}))'
g.writeln('if (${tmp}.state != 0) { IError err = ${tmp}.err; eprintln($tmp_str); }')
}
fn (mut g Gen) mssql_drop_table(node ast.SqlStmtLine, typ SqlType, db_expr ast.Expr) {
table_name := g.get_table_name(node.table_expr)
g.writeln('// mssql table drop')
lit := '\\"'
drop_string := 'DROP TABLE $lit$table_name$lit;'
tmp := g.new_tmp_var()
g.write('Option_mssql__Result $tmp = mssql__Connection_query(&')
g.expr(db_expr)
g.writeln(', _SLIT("$drop_string"));')
tmp_str := 'str_intp(1, _MOV((StrIntpData[]){{_SLIT("Something went wrong: "), $si_s_code ,{.d_s=IError_str(err)}}}))'
g.writeln('if (${tmp}.state != 0) { IError err = ${tmp}.err; eprintln($tmp_str); }')
}
fn (mut g Gen) mssql_get_table_type(typ ast.Type) string {
mut table_typ := ''
match typ {
ast.i8_type, ast.byte_type, ast.bool_type {
table_typ = 'TINYINT'
}
ast.i16_type, ast.u16_type {
table_typ = 'SMALLINT'
}
ast.int_type, ast.u32_type {
table_typ = 'INT'
}
ast.i64_type, ast.u64_type {
table_typ = 'BIGINT'
}
ast.f32_type {
table_typ = 'FLOAT(24)'
}
ast.f64_type {
table_typ = 'FLOAT(53)'
}
ast.string_type {
table_typ = 'TEXT'
}
-1 {
table_typ = 'INT IDENTITY'
}
else {}
}
return table_typ
}
// utils
fn (mut g Gen) sql_select_arr(field ast.StructField, node ast.SqlExpr, primary string, tmp string) {
@ -1336,10 +1406,15 @@ fn (mut g Gen) table_gen(node ast.SqlStmtLine, typ SqlType, expr ast.Expr) strin
struct_data := typ_sym.struct_info()
table_name := g.get_table_name(node.table_expr)
mut lit := '`'
if typ == .psql {
if typ == .psql || typ == .mssql {
lit = '\\"'
}
mut create_string := 'CREATE TABLE IF NOT EXISTS $lit$table_name$lit ('
if typ == .mssql {
// mssql detecting create if not exist is awkward
create_string = 'IF NOT EXISTS (SELECT * FROM sysobjects WHERE name=\'$table_name\' and xtype=\'U\') CREATE TABLE $lit$table_name$lit ('
}
mut fields := []string{}
mut unique_fields := []string{}
@ -1641,6 +1716,9 @@ fn (mut g Gen) parse_db_from_type_string(name string) SqlType {
'pg.DB' {
return .psql
}
'mssql.Connection' {
return .mssql
}
else {
return .unknown
}