this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

insert artists and albums

+235 -30
+74
src/db/album.odin
··· 1 + package db 2 + 3 + import sqlite "../../vendor/sqlite" 4 + import sa "../../vendor/sqlite/addons" 5 + import types "../core" 6 + import "core:fmt" 7 + import "core:mem" 8 + import "core:testing" 9 + 10 + new_album :: proc(db: ^sqlite.Connection, album: types.Album) -> sqlite.Result_Code { 11 + 12 + id := gen_id("album") 13 + defer delete(id) 14 + 15 + return sa.execute( 16 + db, 17 + "INSERT INTO album (id, title, mb_id, mb_rg_id) VALUES (?, ?, ?, ?)", 18 + {{1, id}, {2, album.title}, {3, album.mb_id.?}, {4, album.mb_rg_id.?}}, 19 + ) 20 + } 21 + 22 + 23 + // ================ 24 + // Tests 25 + // ================ 26 + 27 + 28 + @(test) 29 + should_create_new_album :: proc(t: ^testing.T) { 30 + 31 + 32 + track: mem.Tracking_Allocator 33 + mem.tracking_allocator_init(&track, context.allocator) 34 + context.allocator = mem.tracking_allocator(&track) 35 + defer { 36 + if len(track.allocation_map) > 0 { 37 + fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 38 + for _, entry in track.allocation_map { 39 + fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 40 + } 41 + } 42 + if len(track.bad_free_array) > 0 { 43 + fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 44 + for entry in track.bad_free_array { 45 + fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 46 + } 47 + } 48 + mem.tracking_allocator_destroy(&track) 49 + } 50 + 51 + 52 + db: ^sqlite.Connection 53 + 54 + if rc := sqlite.open(db_url, &db); rc != .Ok { 55 + fmt.panicf("failed to open database. result code {}", rc) 56 + } 57 + fmt.printfln("connected to database") 58 + 59 + defer { 60 + sqlite.close(db) 61 + fmt.printfln("\nconnection closed") 62 + } 63 + 64 + album := types.Album { 65 + id = "test", 66 + title = "Test title", 67 + mb_id = "asdf", 68 + mb_rg_id = "asdf", 69 + } 70 + 71 + rc := new_album(db, album) 72 + testing.expect(t, rc == .Ok) 73 + 74 + }
+150
src/db/artist.odin
··· 1 + #+vet explicit-allocators 2 + package db 3 + 4 + import sqlite "../../vendor/sqlite" 5 + import sa "../../vendor/sqlite/addons" 6 + import types "../core" 7 + import "core:fmt" 8 + import "core:mem" 9 + import "core:testing" 10 + 11 + new_artist :: proc( 12 + db: ^sqlite.Connection, 13 + artist: types.Artist, 14 + allocator := context.allocator, 15 + ) -> sqlite.Result_Code { 16 + 17 + id := gen_id("artist", allocator) 18 + defer delete(id, allocator) 19 + 20 + return sa.execute( 21 + db, 22 + "INSERT INTO artist (id, name, mb_id, acoust_id) VALUES (?, ?, ?, ?)", 23 + {{1, id}, {2, artist.name}, {3, artist.mb_id.?}, {4, artist.acoust_id.?}}, 24 + ) 25 + } 26 + 27 + new_artist_batch :: proc( 28 + db: ^sqlite.Connection, 29 + artists: []types.Artist, 30 + allocator := context.allocator, 31 + ) -> sqlite.Result_Code { 32 + 33 + rc: sqlite.Result_Code 34 + 35 + n := len(artists) 36 + 37 + 38 + fmt.printfln("Starting transaction") 39 + rc = sa.execute(db, "BEGIN TRANSACTION;") 40 + assert(rc == .Ok) 41 + 42 + for artist in artists { 43 + fmt.printfln("Inserting %v", artist) 44 + rc = new_artist(db, artist, allocator) 45 + assert(rc == .Ok) 46 + } 47 + 48 + fmt.printfln("Commiting transaction") 49 + rc = sa.execute(db, "COMMIT;") 50 + assert(rc == .Ok) 51 + 52 + return rc 53 + } 54 + 55 + 56 + // ================ 57 + // Tests 58 + // ================ 59 + 60 + // @(test) 61 + // should_create_new_artist :: proc(t: ^testing.T) { 62 + // 63 + // track: mem.Tracking_Allocator 64 + // mem.tracking_allocator_init(&track, context.allocator, context.allocator) 65 + // context.allocator = mem.tracking_allocator(&track) 66 + // defer { 67 + // if len(track.allocation_map) > 0 { 68 + // fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 69 + // for _, entry in track.allocation_map { 70 + // fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 71 + // } 72 + // } 73 + // if len(track.bad_free_array) > 0 { 74 + // fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 75 + // for entry in track.bad_free_array { 76 + // fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 77 + // } 78 + // } 79 + // mem.tracking_allocator_destroy(&track) 80 + // } 81 + // 82 + // 83 + // db: ^sqlite.Connection 84 + // 85 + // if rc := sqlite.open(db_url, &db); rc != .Ok { 86 + // fmt.panicf("failed to open database. result code {}", rc) 87 + // } 88 + // fmt.printfln("connected to database") 89 + // 90 + // defer { 91 + // sqlite.close(db) 92 + // fmt.printfln("\nconnection closed") 93 + // } 94 + // 95 + // artist := types.Artist { 96 + // id = "test", 97 + // name = "PinkPantheres", 98 + // mb_id = "asdf", 99 + // acoust_id = "123", 100 + // } 101 + // 102 + // rc := new_artist(db, artist, context.allocator) 103 + // testing.expect(t, rc == .Ok) 104 + // } 105 + 106 + @(test) 107 + should_create_new_artist_batch :: proc(t: ^testing.T) { 108 + 109 + 110 + track: mem.Tracking_Allocator 111 + mem.tracking_allocator_init(&track, context.allocator, context.allocator) 112 + context.allocator = mem.tracking_allocator(&track) 113 + defer { 114 + if len(track.allocation_map) > 0 { 115 + fmt.eprintf("=== %v allocations not freed: ===\n", len(track.allocation_map)) 116 + for _, entry in track.allocation_map { 117 + fmt.eprintf("- %v bytes @ %v\n", entry.size, entry.location) 118 + } 119 + } 120 + if len(track.bad_free_array) > 0 { 121 + fmt.eprintf("=== %v incorrect frees: ===\n", len(track.bad_free_array)) 122 + for entry in track.bad_free_array { 123 + fmt.eprintf("- %p @ %v\n", entry.memory, entry.location) 124 + } 125 + } 126 + mem.tracking_allocator_destroy(&track) 127 + } 128 + 129 + 130 + db: ^sqlite.Connection 131 + 132 + if rc := sqlite.open(db_url, &db); rc != .Ok { 133 + fmt.panicf("failed to open database. result code {}", rc) 134 + } 135 + fmt.printfln("connected to database") 136 + 137 + defer { 138 + sqlite.close(db) 139 + fmt.printfln("connection closed") 140 + } 141 + 142 + artists := []types.Artist { 143 + {id = "test1", name = "PinkPantheres1", mb_id = "asdf", acoust_id = "123"}, 144 + {id = "test2", name = "PinkPantheres2", mb_id = "asdf", acoust_id = "123"}, 145 + {id = "test3", name = "PinkPantheres3", mb_id = "asdf", acoust_id = "123"}, 146 + } 147 + 148 + rc := new_artist_batch(db, artists, context.allocator) 149 + testing.expect(t, rc == .Ok) 150 + }
+11 -30
src/db/main.odin
··· 1 1 package db 2 2 3 - import "core:fmt" 3 + import "core:crypto" 4 + import "core:encoding/uuid" 4 5 import "core:strings" 5 6 6 - import sqlite "../../vendor/sqlite" 7 - import sa "../../vendor/sqlite/addons" 8 - import t "../core" 9 - import "core:encoding/uuid" 7 + 8 + db_url: cstring = "oudio.db" 10 9 11 10 gen_id :: proc(prefix: Maybe(string), allocator := context.allocator) -> string { 12 11 id: string 12 + 13 + context.random_generator = crypto.random_generator() 13 14 14 15 id_uuid := uuid.generate_v7_basic() 15 16 17 + uuid_str := uuid.to_string(id_uuid, allocator) 18 + defer delete(uuid_str) 19 + 16 20 if (prefix != nil) { 17 - id = strings.concatenate([]string{prefix.?, "_", uuid.to_string(id_uuid, allocator)}) 21 + id = strings.concatenate([]string{prefix.?, "_", uuid_str}) 18 22 } else { 19 - id = uuid.to_string(id_uuid, allocator) 23 + id = uuid_str 20 24 } 21 25 22 26 return id 23 27 } 24 - 25 - new_album :: proc(db: ^sqlite.Connection, album: t.Album) -> sqlite.Result_Code { 26 - 27 - id := gen_id("album") 28 - 29 - return sa.execute( 30 - db, 31 - "INSERT INTO album (id,title,mb_id,mb_rg_id) VALUES (?, ?, ?, ?)", 32 - {{1, id}, {2, album.title}, {3, album.mb_id.?}, {4, album.mb_rg_id.?}}, 33 - ) 34 - } 35 - 36 - 37 - new_artist :: proc(db: ^sqlite.Connection, artist: t.Artist) -> sqlite.Result_Code { 38 - 39 - id := gen_id("artist") 40 - 41 - return sa.execute( 42 - db, 43 - "INSERT INTO artist (id, name, mb_id, acoust_id) VALUES (?, ?, ?, ?)", 44 - {{1, id}, {2, artist.name}, {3, artist.mb_id.?}, {4, artist.acoust_id.?}}, 45 - ) 46 - }