Fun 0.41.5
The programming language that makes You have fun
Loading...
Searching...
No Matches
query.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2025 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
17
22#ifdef FUN_WITH_SQLITE
23 Value vsql = pop_value(vm);
24 Value vh = pop_value(vm);
25 int hid = (int)vh.i;
26 char *sql = value_to_string_alloc(&vsql);
28 free_value(vsql);
29 SqlHandle *h = sql_reg_get(hid);
30 if (!h || !h->db || !sql) {
31 if (sql) free(sql);
33 break;
34 }
35 sqlite3_stmt *stmt = NULL;
36 if (sqlite3_prepare_v2(h->db, sql, -1, &stmt, NULL) != SQLITE_OK) {
37 free(sql);
39 break;
40 }
41 free(sql);
42 Value rows = make_array_from_values(NULL, 0);
43 int ncols = sqlite3_column_count(stmt);
44 while (sqlite3_step(stmt) == SQLITE_ROW) {
45 Value row = make_map_empty();
46 for (int i = 0; i < ncols; i++) {
47 const char *name = sqlite3_column_name(stmt, i);
48 int type = sqlite3_column_type(stmt, i);
49 Value kv;
50 switch (type) {
51 case SQLITE_INTEGER:
52 kv = make_int((int64_t)sqlite3_column_int64(stmt, i));
53 break;
54 case SQLITE_FLOAT:
55 kv = make_float(sqlite3_column_double(stmt, i));
56 break;
57 case SQLITE_TEXT:
58 kv = make_string((const char *)sqlite3_column_text(stmt, i));
59 break;
60 case SQLITE_NULL:
61 kv = make_nil();
62 break;
63 default:
64 kv = make_nil();
65 break; /* ignore blobs for now */
66 }
67 (void)map_set(&row, name ? name : "", kv);
68 }
69 (void)array_push(&rows, row);
70 /* Do NOT free 'row' here: rows array now owns it. Freeing would
71 destroy the map and leave a dangling pointer causing segfaults
72 when accessing fields like row["done"]. */
73 }
74 sqlite3_finalize(stmt);
75 push_value(vm, rows);
76#else
77 Value v1 = pop_value(vm);
79 Value v2 = pop_value(vm);
82#endif
83 break;
84}
@ OP_SQLITE_QUERY
Definition bytecode.h:181
const char * name
Definition env.c:29
Value v2
Definition exec.c:43
free(vals)
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Definition map.c:79
Value make_map_empty(void)
Construct a new empty map Value.
Definition map.c:35
free_value(v1)
push_value(vm, make_array_from_values(NULL, 0))
Tagged union representing a Fun value.
Definition value.h:68
Value vh
Definition stubs.c:44
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_float(double v)
Construct a Value representing a double-precision float.
Definition value.c:64
int array_push(Value *v, Value newElem)
Append a Value to an array.
Definition value.c:257
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.
Definition value.c:142