ci: fix compilation and formatting of sqlite example in docs.md

pull/9608/head
Delyan Angelov 2021-04-05 08:02:37 +03:00
parent 92fa9bbea9
commit 34aa67b1e8
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 11 additions and 11 deletions

View File

@ -3306,26 +3306,26 @@ struct C.sqlite3 {
struct C.sqlite3_stmt {
}
type FnSqlite3Callback = fn (voidptr, int, &charptr, &charptr) int
type FnSqlite3Callback = fn (voidptr, int, &&char, &&char) int
fn C.sqlite3_open(charptr, &&C.sqlite3) int
fn C.sqlite3_open(&char, &&C.sqlite3) int
fn C.sqlite3_close(&C.sqlite3) int
fn C.sqlite3_column_int(stmt &C.sqlite3_stmt, n int) int
// ... you can also just define the type of parameter and leave out the C. prefix
fn C.sqlite3_prepare_v2(&C.sqlite3, charptr, int, &&C.sqlite3_stmt, &charptr) int
fn C.sqlite3_prepare_v2(&C.sqlite3, &char, int, &&C.sqlite3_stmt, &&char) int
fn C.sqlite3_step(&C.sqlite3_stmt)
fn C.sqlite3_finalize(&C.sqlite3_stmt)
fn C.sqlite3_exec(db &C.sqlite3, sql charptr, cb FnSqlite3Callback, cb_arg voidptr, emsg &charptr) int
fn C.sqlite3_exec(db &C.sqlite3, sql &char, cb FnSqlite3Callback, cb_arg voidptr, emsg &&char) int
fn C.sqlite3_free(voidptr)
fn my_callback(arg voidptr, howmany int, cvalues &charptr, cnames &charptr) int {
fn my_callback(arg voidptr, howmany int, cvalues &&char, cnames &&char) int {
unsafe {
for i in 0 .. howmany {
print('| ${cstring_to_vstring(cnames[i])}: ${cstring_to_vstring(cvalues[i]):20} ')
@ -3344,17 +3344,17 @@ fn main() {
stmt := &C.sqlite3_stmt(0)
// NB: you can also use the `.str` field of a V string,
// to get its C style zero terminated representation
C.sqlite3_prepare_v2(db, query.str, -1, &stmt, 0)
C.sqlite3_prepare_v2(db, &char(query.str), -1, &stmt, 0)
C.sqlite3_step(stmt)
nr_users := C.sqlite3_column_int(stmt, 0)
C.sqlite3_finalize(stmt)
println('There are $nr_users users in the database.')
//
error_msg := charptr(0)
error_msg := &char(0)
query_all_users := 'select * from users'
rc := C.sqlite3_exec(db, query_all_users.str, my_callback, 7, &error_msg)
rc := C.sqlite3_exec(db, &char(query_all_users.str), my_callback, voidptr(7), &error_msg)
if rc != C.SQLITE_OK {
eprintln(cstring_to_vstring(error_msg))
eprintln(unsafe { cstring_to_vstring(error_msg) })
C.sqlite3_free(error_msg)
}
C.sqlite3_close(db)

View File

@ -270,8 +270,8 @@ pub fn (s string) cstr() byteptr {
*/
// cstring_to_vstring creates a copy of cstr and turns it into a v string.
[unsafe]
pub fn cstring_to_vstring(cstr &byte) string {
return unsafe { tos_clone(cstr) }
pub fn cstring_to_vstring(cstr &char) string {
return unsafe { tos_clone(&byte(cstr)) }
}
// replace_once replaces the first occurence of `rep` with the string passed in `with`.