load sqlite from cli
This commit is contained in:
		
							parent
							
								
									f3911ead38
								
							
						
					
					
						commit
						133b5a9559
					
				
					 2 changed files with 34 additions and 16 deletions
				
			
		
							
								
								
									
										2
									
								
								justfile
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								justfile
									
										
									
									
									
								
							| 
						 | 
					@ -28,7 +28,7 @@ build:
 | 
				
			||||||
#     # zig build -Dtarget=wasm32-emscripten
 | 
					#     # zig build -Dtarget=wasm32-emscripten
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run:
 | 
					run:
 | 
				
			||||||
    nixGL zig build run
 | 
					    nixGL zig build run -- {{ justfile_directory() }}/life.sqlite3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test:
 | 
					test:
 | 
				
			||||||
    zig build test
 | 
					    zig build test
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										48
									
								
								src/main.zig
									
										
									
									
									
								
							
							
						
						
									
										48
									
								
								src/main.zig
									
										
									
									
									
								
							| 
						 | 
					@ -14,16 +14,45 @@ const State = union(StateTag) {
 | 
				
			||||||
    loaded: *c.sqlite3,
 | 
					    loaded: *c.sqlite3,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn loadSqlite(path: [:0]const u8) State {
 | 
				
			||||||
 | 
					    var db_opt: ?*c.sqlite3 = undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const result = c.sqlite3_open(path.ptr, &db_opt);
 | 
				
			||||||
 | 
					    if (result != c.SQLITE_OK) {
 | 
				
			||||||
 | 
					        std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) });
 | 
				
			||||||
 | 
					        _ = c.sqlite3_close(db_opt);
 | 
				
			||||||
 | 
					        return State{ .unloaded = {} };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (db_opt) |db| {
 | 
				
			||||||
 | 
					        std.debug.print("[SQLite] Success\n", .{});
 | 
				
			||||||
 | 
					        return State{ .loaded = db };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return State{ .unloaded = {} };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn main() !void {
 | 
					pub fn main() !void {
 | 
				
			||||||
    const alloc = std.heap.page_allocator;
 | 
					    var state = State{ .unloaded = {} };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
 | 
				
			||||||
 | 
					    defer _ = gpa.deinit();
 | 
				
			||||||
 | 
					    const alloc = gpa.allocator();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const args = try std.process.argsAlloc(alloc);
 | 
				
			||||||
 | 
					    defer std.process.argsFree(alloc, args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std.debug.print("Arguments: {s}\n", .{args});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (args.len > 1) {
 | 
				
			||||||
 | 
					        state = loadSqlite(args[1]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const screen_width = 800;
 | 
					    const screen_width = 800;
 | 
				
			||||||
    const screen_height = 450;
 | 
					    const screen_height = 450;
 | 
				
			||||||
    raylib.InitWindow(screen_width, screen_height, "FabApp");
 | 
					    raylib.InitWindow(screen_width, screen_height, "FabApp");
 | 
				
			||||||
    raylib.SetTargetFPS(60);
 | 
					    raylib.SetTargetFPS(60);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var state = State{ .unloaded = {} };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory());
 | 
					    var file_dialog_state = raylib.InitGuiWindowFileDialog(raylib.GetWorkingDirectory());
 | 
				
			||||||
    // const ext = ".sqlite3";
 | 
					    // const ext = ".sqlite3";
 | 
				
			||||||
    // @memcpy(file_dialog_state.filterExt[0..ext.len], ext);
 | 
					    // @memcpy(file_dialog_state.filterExt[0..ext.len], ext);
 | 
				
			||||||
| 
						 | 
					@ -41,18 +70,7 @@ pub fn main() !void {
 | 
				
			||||||
                // slices -> C-string (null-terminated)
 | 
					                // slices -> C-string (null-terminated)
 | 
				
			||||||
                const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file });
 | 
					                const db_path = try std.fs.path.joinZ(alloc, &[_][]const u8{ dir, file });
 | 
				
			||||||
                defer alloc.free(db_path);
 | 
					                defer alloc.free(db_path);
 | 
				
			||||||
 | 
					                state = loadSqlite(db_path);
 | 
				
			||||||
                var db_opt: ?*c.sqlite3 = undefined;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                const result = c.sqlite3_open(db_path.ptr, &db_opt);
 | 
					 | 
				
			||||||
                if (result != c.SQLITE_OK) {
 | 
					 | 
				
			||||||
                    std.debug.print("[SQLite] err {}: {s}\n", .{ result, c.sqlite3_errmsg(db_opt) });
 | 
					 | 
				
			||||||
                    _ = c.sqlite3_close(db_opt);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                if (db_opt) |db| {
 | 
					 | 
				
			||||||
                    std.debug.print("[SQLite] Success\n", .{});
 | 
					 | 
				
			||||||
                    state = State{ .loaded = db };
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            file_dialog_state.SelectFilePressed = false;
 | 
					            file_dialog_state.SelectFilePressed = false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue