this repo has no description
0
fork

Configure Feed

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

use addon instead of custom big insert queries

+107 -256
+2 -2
TODO.md
··· 1 1 2 - - [ ] On unique constraint for album and artist, add select query and return existing id instead of generated id 2 + - [x] On unique constraint for album and artist, add select query and return existing id instead of generated id 3 3 - [ ] File table 4 - - [ ] Track table 4 + - [x] Track table 5 5 - [ ] Track <-> Artist 6 6 - [ ] Track - File 7 7
+1
main.odin
··· 1 +
+22 -79
src/db/repo/album.odin
··· 18 18 ) { 19 19 20 20 fmt.printfln("New Album: %#v", album) 21 - ok: bool 22 21 23 22 id := db_pkg.gen_id("album", allocator) 24 23 new_id = types.Album_Id(id) 25 - c_id := strings.clone_to_cstring(id, allocator) 26 - c_title := strings.clone_to_cstring(album.title, allocator) 27 24 28 - defer { 29 - delete(c_id, allocator) 30 - delete(c_title, allocator) 31 - } 32 25 33 - query: cstring = "INSERT INTO album (id, title, mb_id, mb_rg_id) VALUES (?, ?, ?, ?)" 34 - 35 - stmt: ^sqlite.Statement 36 - 37 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 38 - return new_id, .UnknownError 39 - } 40 - defer sqlite.finalize(stmt) 41 - 42 - if rc := sqlite.bind_text( 43 - stmt, 44 - param_idx = 1, 45 - param_value = c_id, 46 - param_len = c.int(len(id)), 47 - free = {behaviour = .Static}, 48 - ); rc != .Ok { 49 - fmt.eprintfln("failed to bind value to ArtistId. result code: {}", rc) 50 - return new_id, .UnknownError 51 - } 52 - 53 - 54 - if rc := sqlite.bind_text( 55 - stmt, 56 - param_idx = 2, 57 - param_value = c_title, 58 - param_len = c.int(len(album.title)), 59 - free = {behaviour = .Static}, 60 - ); rc != .Ok { 61 - fmt.eprintfln("failed to bind value to artist title. result code: {}", rc) 62 - return new_id, .UnknownError 63 - } 64 - 65 - 66 - c_mb_id, c_mb_rg_id: cstring 67 - defer delete(c_mb_id, allocator) 68 - defer delete(c_mb_rg_id, allocator) 69 - 26 + mb_id_value: sa.Query_Param_Value 70 27 if mb_id, ok := album.mb_id.?; ok { 71 - c_mb_id = strings.clone_to_cstring(mb_id, allocator) 72 - fmt.printfln("%s %s", mb_id, c_mb_id) 73 - 74 - if rc := sqlite.bind_text( 75 - stmt, 76 - param_idx = 3, 77 - param_value = c_mb_id, 78 - param_len = c.int(len(mb_id)), 79 - free = {behaviour = .Static}, 80 - ); rc != .Ok { 81 - fmt.eprintfln("failed to bind value to mb_id. result code: {}", rc) 82 - return new_id, .UnknownError 83 - } 84 - 28 + mb_id_value = mb_id 85 29 } 86 30 87 - 31 + mb_rg_id_value: sa.Query_Param_Value 88 32 if mb_rg_id, ok := album.mb_rg_id.?; ok { 89 - c_mb_rg_id = strings.clone_to_cstring(mb_rg_id, allocator) 90 - 91 - if rc := sqlite.bind_text( 92 - stmt, 93 - param_idx = 4, 94 - param_value = c_mb_rg_id, 95 - param_len = c.int(len(mb_rg_id)), 96 - free = {behaviour = .Static}, 97 - ); rc != .Ok { 98 - fmt.eprintfln("failed to bind value to mb_rg_id. result code: {}", rc) 99 - return new_id, .UnknownError 100 - } 101 - 33 + mb_rg_id_value = mb_rg_id 102 34 } 103 35 36 + query := "INSERT INTO album (id, title, mb_id, mb_rg_id) VALUES (?, ?, ?, ?)" 104 37 105 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 38 + rc := sa.execute( 39 + db, 40 + query, 41 + { 42 + {index = 1, value = id}, 43 + {index = 2, value = album.title}, 44 + {index = 3, value = mb_id_value}, 45 + {index = 4, value = mb_rg_id_value}, 46 + }, 47 + ) 106 48 49 + fmt.printfln("\n\n\n\nRC:%v \n\n\n\n", rc) 107 50 108 - rc := sqlite.step(stmt) 109 - fmt.printfln("Step RC: %v", rc) 110 - if (rc == .Constraint) { 51 + #partial switch rc { 52 + case .Constraint: 111 53 return new_id, .UniqueConstraint 112 - } 113 - if (rc != .Done) { 54 + case .Done, .Ok: 55 + return new_id, .None 56 + case: 57 + fmt.eprintfln("new_album failed with result code: %v", rc) 114 58 return new_id, .UnknownError 115 59 } 116 60 117 - return new_id, .None 118 61 } 119 62 120 63
+25 -80
src/db/repo/artist.odin
··· 19 19 err: db_pkg.DatabaseErrors, 20 20 ) { 21 21 22 + 22 23 fmt.printfln("New artist: %#v", artist) 23 - ok: bool 24 24 25 25 id := db_pkg.gen_id("artist", allocator) 26 26 new_id = types.Artist_Id(id) 27 27 28 - c_id := strings.clone_to_cstring(id, allocator) 29 - c_name := strings.clone_to_cstring(artist.name, allocator) 30 28 31 - defer { 32 - delete(c_id, allocator) 33 - delete(c_name, allocator) 29 + mb_id_value: sa.Query_Param_Value 30 + if mb_id, ok := artist.mb_id.?; ok { 31 + mb_id_value = mb_id 34 32 } 35 33 36 - query: cstring = "INSERT INTO artist (id, name, mb_id, acoust_id) VALUES (?, ?, ?, ?)" 37 - 38 - stmt: ^sqlite.Statement 39 - 40 - 41 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 42 - return new_id, .UnknownError 34 + acoust_id_value: sa.Query_Param_Value 35 + if acoust_id, ok := artist.acoust_id.?; ok { 36 + acoust_id_value = acoust_id 43 37 } 44 - defer sqlite.finalize(stmt) 45 38 46 39 47 - if rc := sqlite.bind_text( 48 - stmt, 49 - param_idx = 1, 50 - param_value = c_id, 51 - param_len = c.int(len(id)), 52 - free = {behaviour = .Static}, 53 - ); rc != .Ok { 54 - fmt.eprintfln("failed to bind value to ArtistId. result code: {}", rc) 55 - return new_id, .UnknownError 56 - } 40 + query := "INSERT INTO artist (id, name, mb_id, acoust_id) VALUES (?, ?, ?, ?)" 57 41 58 - if rc := sqlite.bind_text( 59 - stmt, 60 - param_idx = 2, 61 - param_value = c_name, 62 - param_len = c.int(len(artist.name)), 63 - free = {behaviour = .Static}, 64 - ); rc != .Ok { 65 - fmt.eprintfln("failed to bind value to artist name. result code: {}", rc) 66 - return new_id, .UnknownError 67 - } 42 + rc := sa.execute( 43 + db, 44 + query, 45 + { 46 + {index = 1, value = id}, 47 + {index = 2, value = artist.name}, 48 + {index = 3, value = mb_id_value}, 49 + {index = 4, value = acoust_id_value}, 50 + }, 51 + ) 68 52 69 53 70 - c_mb_id, c_acoust_id: cstring 71 - defer delete(c_mb_id, allocator) 72 - defer delete(c_acoust_id, allocator) 73 - 74 - if mb_id, ok := artist.mb_id.?; ok { 75 - c_mb_id = strings.clone_to_cstring(mb_id, allocator) 76 - fmt.printfln("%s %s", mb_id, c_mb_id) 77 - 78 - if rc := sqlite.bind_text( 79 - stmt, 80 - param_idx = 3, 81 - param_value = c_mb_id, 82 - param_len = c.int(len(mb_id)), 83 - free = {behaviour = .Static}, 84 - ); rc != .Ok { 85 - fmt.eprintfln("failed to bind value to mb_id. result code: {}", rc) 86 - return new_id, .UnknownError 87 - } 88 - } 89 - 90 - 91 - if acoust_id, ok := artist.acoust_id.?; ok { 92 - c_acoust_id = strings.clone_to_cstring(acoust_id, allocator) 93 - fmt.printfln("%s %s", acoust_id, c_acoust_id) 94 - 95 - if rc := sqlite.bind_text( 96 - stmt, 97 - param_idx = 4, 98 - param_value = c_acoust_id, 99 - param_len = c.int(len(acoust_id)), 100 - free = {behaviour = .Static}, 101 - ); rc != .Ok { 102 - fmt.eprintfln("failed to bind value to acoust_id. result code: {}", rc) 103 - return new_id, .UnknownError 104 - } 105 - } 106 - 107 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 108 - 109 - rc := sqlite.step(stmt) 110 - fmt.printfln("Step RC: %v", rc) 111 - if (rc == .Constraint) { 54 + #partial switch rc { 55 + case .Constraint: 112 56 return new_id, .UniqueConstraint 113 - } 114 - if (rc != .Done) { 57 + case .Done, .Ok: 58 + return new_id, .None 59 + case: 60 + fmt.eprintfln("new_album failed with result code: %v", rc) 115 61 return new_id, .UnknownError 116 62 } 117 63 118 - return new_id, .None 119 - 120 64 121 65 } 66 + 122 67 123 68 new_artist_batch :: proc( 124 69 db: ^sqlite.Connection,
+31 -1
src/db/repo/junctions.odin
··· 8 8 import "core:fmt" 9 9 import "core:strings" 10 10 11 - new_artist_album :: proc( 11 + new_artist_album1 :: proc( 12 12 db: ^sqlite.Connection, 13 13 a: types.ArtistAlbum, 14 14 allocator := context.allocator, ··· 73 73 return .None 74 74 75 75 } 76 + 77 + 78 + new_artist_album :: proc( 79 + db: ^sqlite.Connection, 80 + a: types.ArtistAlbum, 81 + allocator := context.allocator, 82 + ) -> db_pkg.DatabaseErrors { 83 + 84 + fmt.printfln("New Artist<->Album $#v", a) 85 + 86 + query := "INSERT INTO artist_album (artist_id, album_id) VALUES (?, ?) ON CONFLICT DO NOTHING" 87 + 88 + rc := sa.execute( 89 + db, 90 + query, 91 + {{index = 1, value = string(a.artist_id)}, {index = 2, value = string(a.album_id)}}, 92 + ) 93 + 94 + 95 + fmt.printfln("Step RC: %v", rc) 96 + if (rc == .Constraint) { 97 + return .UniqueConstraint 98 + } 99 + if (rc != .Done && rc != .Ok) { 100 + return .UnknownError 101 + } 102 + 103 + return .None 104 + 105 + }
+20 -91
src/db/repo/track.odin
··· 19 19 ) { 20 20 21 21 fmt.printfln("New Track: %#v", track) 22 - ok: bool 23 22 24 23 id := db_pkg.gen_id("track", allocator) 25 24 new_id = types.Track_Id(id) 26 25 27 - c_id := strings.clone_to_cstring(id, allocator) 28 - c_title := strings.clone_to_cstring(track.title, allocator) 29 - c_album_id := strings.clone_to_cstring(string(track.album_id), allocator) 30 - 31 - defer { 32 - delete(c_id, allocator) 33 - delete(c_title, allocator) 34 - delete(c_album_id, allocator) 35 - } 36 - 37 - query: cstring = "INSERT INTO track (id, title, album_id, track_number, mb_id) VALUES (?, ?, ?, ?, ?)" 38 - 39 - stmt: ^sqlite.Statement 40 - 41 - fmt.println("before prepare") 42 - 43 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 44 - fmt.eprintfln("prepare error %v", rc) 45 - return new_id, .UnknownError 46 - } 47 - defer sqlite.finalize(stmt) 48 - 49 - fmt.println("after prepare") 50 - 51 - if rc := sqlite.bind_text( 52 - stmt, 53 - param_idx = 1, 54 - param_value = c_id, 55 - param_len = c.int(len(id)), 56 - free = {behaviour = .Static}, 57 - ); rc != .Ok { 58 - fmt.eprintfln("failed to bind value to TrackId. result code: {}", rc) 59 - return new_id, .UnknownError 60 - } 61 - 62 - if rc := sqlite.bind_text( 63 - stmt, 64 - param_idx = 2, 65 - param_value = c_title, 66 - param_len = c.int(len(track.title)), 67 - free = {behaviour = .Static}, 68 - ); rc != .Ok { 69 - fmt.eprintfln("failed to bind value to track title. result code: {}", rc) 70 - return new_id, .UnknownError 71 - } 72 - 73 - if rc := sqlite.bind_text( 74 - stmt, 75 - param_idx = 3, 76 - param_value = c_album_id, 77 - param_len = c.int(len(track.album_id)), 78 - free = {behaviour = .Static}, 79 - ); rc != .Ok { 80 - fmt.eprintfln("failed to bind value to track album id. result code: {}", rc) 81 - return new_id, .UnknownError 82 - } 83 - 84 - 85 - if rc := sqlite.bind_int(stmt, param_idx = 4, param_value = i32(track.track_number)); 86 - rc != .Ok { 87 - fmt.eprintfln("failed to bind value to track album id. result code: {}", rc) 88 - return new_id, .UnknownError 89 - } 90 - 91 - 92 - c_mb_id: cstring 93 - defer delete(c_mb_id, allocator) 94 - 26 + mb_id_value: sa.Query_Param_Value 95 27 if mb_id, ok := track.mb_id.?; ok { 96 - c_mb_id = strings.clone_to_cstring(mb_id, allocator) 97 - fmt.printfln("%s %s", mb_id, c_mb_id) 98 - 99 - if rc := sqlite.bind_text( 100 - stmt, 101 - param_idx = 5, 102 - param_value = c_mb_id, 103 - param_len = c.int(len(mb_id)), 104 - free = {behaviour = .Static}, 105 - ); rc != .Ok { 106 - fmt.eprintfln("failed to bind value to mb_id. result code: {}", rc) 107 - return new_id, .UnknownError 108 - } 28 + mb_id_value = mb_id 109 29 } 110 30 31 + rc := sa.execute( 32 + db, 33 + "INSERT INTO track (id, title, album_id, track_number, mb_id) VALUES (?, ?, ?, ?, ?)", 34 + { 35 + {index = 1, value = id}, 36 + {index = 2, value = track.title}, 37 + {index = 3, value = string(track.album_id)}, 38 + {index = 4, value = i32(track.track_number)}, 39 + {index = 5, value = mb_id_value}, 40 + }, 41 + ) 111 42 112 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 113 - 114 - rc := sqlite.step(stmt) 115 - fmt.printfln("Step RC: %v", rc) 116 - if (rc == .Constraint) { 43 + #partial switch rc { 44 + case .Constraint: 117 45 return new_id, .UniqueConstraint 118 - } 119 - if (rc != .Done) { 46 + case .Done, .Ok: 47 + return new_id, .None 48 + case: 49 + fmt.eprintfln("new_track2 failed with result code: %v", rc) 120 50 return new_id, .UnknownError 121 51 } 52 + } 122 53 123 - return new_id, .None 124 - } 125 54 126 55 get_track_by_title :: proc( 127 56 db: ^sqlite.Connection,
+6 -3
vendor/sqlite/addons/addons.odin
··· 103 103 stmt: ^sqlite3.Statement 104 104 prepare(db, &stmt, sql, params, location) or_return 105 105 defer sqlite3.finalize(stmt) 106 - for sqlite3.step(stmt) == .Row { 107 - // consume all rows 106 + result := sqlite3.Result_Code.Done 107 + for { 108 + result = sqlite3.step(stmt) 109 + if result != .Row do break 108 110 } 109 111 110 - return .Ok 112 + if result == .Done do return .Ok 113 + return result 111 114 } 112 115 113 116 @(require_results)