this repo has no description
0
fork

Configure Feed

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

working on handling unique constraints on album and artist

+76 -7
+1 -1
src/core/types.odin
··· 30 30 // Junctions 31 31 ArtistAlbum :: struct { 32 32 artist_id: string, 33 - album_id: string, 33 + album_id: Album_Id, 34 34 } 35 35 36 36
+61 -3
src/db/repo/album.odin
··· 13 13 album: types.Album, 14 14 allocator := context.allocator, 15 15 ) -> ( 16 - new_id: string, 16 + new_id: types.Album_Id, 17 17 err: db_pkg.DatabaseErrors, 18 18 ) { 19 19 ··· 21 21 ok: bool 22 22 23 23 id := db_pkg.gen_id("album", allocator) 24 - new_id = id 24 + new_id = types.Album_Id(id) 25 25 c_id := strings.clone_to_cstring(id, allocator) 26 26 c_title := strings.clone_to_cstring(album.title, allocator) 27 27 ··· 30 30 delete(c_title, allocator) 31 31 } 32 32 33 - query: cstring = "INSERT INTO album (id, title, mb_id, mb_rg_id) VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING" 33 + query: cstring = "INSERT INTO album (id, title, mb_id, mb_rg_id) VALUES (?, ?, ?, ?)" 34 34 35 35 stmt: ^sqlite.Statement 36 36 ··· 144 144 assert(rc == .Ok) 145 145 146 146 return rc 147 + } 148 + 149 + get_album_by_title :: proc( 150 + db: ^sqlite.Connection, 151 + title: string, 152 + allocator := context.allocator, 153 + ) -> ( 154 + res: types.Album, 155 + ok: bool, 156 + ) { 157 + 158 + query: cstring = "SELECT * FROM album WHERE title = ? LIMIT 1" 159 + 160 + stmt: ^sqlite.Statement 161 + 162 + if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 163 + fmt.eprintfln("failed to prepare statement. result code: {}", rc) 164 + return res, false 165 + } 166 + 167 + defer sqlite.finalize(stmt) 168 + 169 + c_title := strings.clone_to_cstring(title, allocator) 170 + defer delete(c_title) 171 + 172 + if rc := sqlite.bind_text( 173 + stmt, 174 + param_idx = 1, 175 + param_value = c_title, 176 + param_len = c.int(len(title)), 177 + free = {behaviour = .Static}, 178 + ); rc != .Ok { 179 + fmt.eprintfln("failed to bind value to ArtistId. result code: {}", rc) 180 + return res, false 181 + } 182 + 183 + fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 184 + 185 + albums := make([dynamic]types.Album, 0, 1) 186 + // defer { 187 + // for album in albums { 188 + // delete(string(album.id)) 189 + // delete(album.title) 190 + // } 191 + // delete_dynamic_array(albums) 192 + // } 193 + 194 + for sqlite.step(stmt) == .Row { 195 + 196 + album := types.Album { 197 + id = types.Album_Id(strings.clone_from(sqlite.column_text(stmt, 0))), 198 + title = strings.clone_from(sqlite.column_text(stmt, 1)), 199 + } 200 + 201 + append(&albums, album) 202 + } 203 + 204 + return albums[0], true 147 205 } 148 206 149 207 get_album_by_id :: proc(
+1 -1
src/db/repo/album.test.odin
··· 54 54 55 55 new_id, err := new_album(db, album) 56 56 fmt.printfln("Error: %v", err) 57 - defer delete(new_id) 57 + defer delete(string(new_id)) 58 58 testing.expect(t, err == .None) 59 59 60 60 }
+1 -1
src/db/repo/junctions.odin
··· 18 18 ok: bool 19 19 20 20 c_artist_id := strings.clone_to_cstring(a.artist_id, allocator) 21 - c_album_id := strings.clone_to_cstring(a.album_id, allocator) 21 + c_album_id := strings.clone_to_cstring(string(a.album_id), allocator) 22 22 23 23 defer { 24 24 delete(c_artist_id, allocator)
+12 -1
src/library/sync.odin
··· 99 99 fmt.printfln("Flac Comment %#v, artist %#v album %#v", flac, artist, album) 100 100 101 101 new_album_id, album_err := db.new_album(db_conn, album) 102 - defer delete(new_album_id) 102 + defer delete(string(new_album_id)) 103 103 assert(album_err == .None || album_err == .UniqueConstraint) 104 + fmt.printfln("\n\nAlbum Err %v", album_err) 105 + 106 + if (album_err == .UniqueConstraint) { 107 + fmt.printfln("Sync, album unique constraint") 108 + existing_album, existing_album_ok := db.get_album_by_title(db_conn, album.title) 109 + assert(existing_album_ok) 110 + fmt.printfln("Existing Album %v", existing_album) 111 + new_album_id = types.Album_Id(strings.clone(string(existing_album.id))) 112 + } 113 + 114 + 104 115 fmt.printfln("New album |%v| with id |%v|", album.title, new_album_id) 105 116 106 117 new_artist_id, artist_err := db.new_artist(db_conn, artist)