orm: use errcode comparisons, instead of strcmp(sqlite3_errmsg,msg)

pull/5783/head
Delyan Angelov 2020-07-10 10:14:30 +03:00
parent 581603f2bd
commit d3e676bdc1
4 changed files with 13 additions and 7 deletions

View File

@ -17,7 +17,6 @@ const (
stderr_value = 2 stderr_value = 2
) )
fn C.puts(charptr)
*/ */
fn builtin_init() { fn builtin_init() {

View File

@ -43,6 +43,7 @@ fn C.chmod(byteptr, int) int
fn C.printf(byteptr, ...byteptr) int fn C.printf(byteptr, ...byteptr) int
fn C.puts(byteptr) int
fn C.fputs(byteptr) int fn C.fputs(byteptr) int

View File

@ -37,6 +37,7 @@ fn C.sqlite3_step() int
fn C.sqlite3_prepare_v2() fn C.sqlite3_prepare_v2()
fn C.sqlite3_finalize() fn C.sqlite3_finalize()
fn C.sqlite3_column_count(voidptr) int fn C.sqlite3_column_count(voidptr) int
fn C.sqlite3_errstr(int) charptr
// Opens the connection with a database. // Opens the connection with a database.
pub fn connect(path string) ?DB { pub fn connect(path string) ?DB {
@ -58,7 +59,10 @@ fn (db DB) init_stmt(query string) &C.sqlite3_stmt {
// Only for V ORM // Only for V ORM
fn get_int_from_stmt(stmt &C.sqlite3_stmt) int { fn get_int_from_stmt(stmt &C.sqlite3_stmt) int {
C.sqlite3_step(stmt) x := C.sqlite3_step(stmt)
if x != C.SQLITE_OK && x != C.SQLITE_DONE {
C.puts( C.sqlite3_errstr(x) )
}
res := C.sqlite3_column_int(stmt, 0) res := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt) C.sqlite3_finalize(stmt)
return res return res

View File

@ -88,10 +88,10 @@ fn (mut g Gen) sql_stmt(node ast.SqlStmt) {
binds := g.sql_buf.str() binds := g.sql_buf.str()
g.sql_buf = strings.new_builder(100) g.sql_buf = strings.new_builder(100)
g.writeln(binds) g.writeln(binds)
g.writeln('sqlite3_step($g.sql_stmt_name);') step_res := g.new_tmp_var()
g.write('if (strcmp(sqlite3_errmsg(${db_name}.conn), "not an error") != 0)') g.writeln('\tint $step_res = sqlite3_step($g.sql_stmt_name);')
g.write('if (strcmp(sqlite3_errmsg(${db_name}.conn), "no more rows available") != 0) puts(sqlite3_errmsg(${db_name}.conn));') g.writeln('\tif( ($step_res != SQLITE_OK) && ($step_res != SQLITE_DONE)){ puts(sqlite3_errmsg(${db_name}.conn)); }')
g.writeln('sqlite3_finalize($g.sql_stmt_name);') g.writeln('\tsqlite3_finalize($g.sql_stmt_name);')
} }
fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
@ -163,7 +163,9 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) {
binds := g.sql_buf.str() binds := g.sql_buf.str()
g.sql_buf = strings.new_builder(100) g.sql_buf = strings.new_builder(100)
g.writeln(binds) g.writeln(binds)
g.writeln('if (strcmp(sqlite3_errmsg(${db_name}.conn), "not an error") != 0) puts(sqlite3_errmsg(${db_name}.conn)); ') binding_res := g.new_tmp_var()
g.writeln('int $binding_res = sqlite3_extended_errcode(${db_name}.conn);')
g.writeln('if ($binding_res != SQLITE_OK) { puts(sqlite3_errmsg(${db_name}.conn)); }')
// //
if node.is_count { if node.is_count {
g.writeln('$cur_line ${dbtype}__get_int_from_stmt($g.sql_stmt_name);') g.writeln('$cur_line ${dbtype}__get_int_from_stmt($g.sql_stmt_name);')