this repo has no description
0
fork

Configure Feed

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

feat: update queries based on addons

+107 -157
+44 -68
src/db/repo/album.odin
··· 4 4 import sqlite "../../../vendor/sqlite" 5 5 import sa "../../../vendor/sqlite/addons" 6 6 import types "../../core" 7 - import "core:c" 8 7 import "core:fmt" 9 8 import "core:strings" 10 9 ··· 97 96 res: types.Album, 98 97 ok: bool, 99 98 ) { 100 - 101 - query: cstring = "SELECT * FROM album WHERE title = ? LIMIT 1" 102 - 103 - stmt: ^sqlite.Statement 99 + Album_Row :: struct { 100 + id: string `sqlite:"id"`, 101 + title: string `sqlite:"title"`, 102 + } 104 103 105 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 106 - fmt.eprintfln("failed to prepare statement. result code: {}", rc) 107 - return res, false 104 + rows := make([dynamic]Album_Row, 0, 1, allocator) 105 + defer { 106 + for row in rows { 107 + delete(row.id) 108 + delete(row.title) 109 + } 110 + delete_dynamic_array(rows) 108 111 } 109 112 110 - defer sqlite.finalize(stmt) 113 + rc := sa.query( 114 + db, 115 + &rows, 116 + "SELECT id, title FROM album WHERE title = ? LIMIT 1", 117 + {{index = 1, value = title}}, 118 + ) 111 119 112 - c_title := strings.clone_to_cstring(title, allocator) 113 - defer delete(c_title) 114 - 115 - if rc := sqlite.bind_text( 116 - stmt, 117 - param_idx = 1, 118 - param_value = c_title, 119 - param_len = c.int(len(title)), 120 - free = {behaviour = .Static}, 121 - ); rc != .Ok { 122 - fmt.eprintfln("failed to bind value to ArtistId. result code: {}", rc) 120 + if rc != .Ok || len(rows) == 0 { 123 121 return res, false 124 122 } 125 123 126 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 127 - 128 - album: types.Album 129 - 130 - for sqlite.step(stmt) == .Row { 131 - 132 - album = types.Album { 133 - id = types.Album_Id(strings.clone_from(sqlite.column_text(stmt, 0))), 134 - title = strings.clone_from(sqlite.column_text(stmt, 1)), 135 - } 136 - 124 + row := rows[0] 125 + album := types.Album { 126 + id = types.Album_Id(strings.clone(row.id, allocator)), 127 + title = strings.clone(row.title, allocator), 137 128 } 138 129 139 130 return album, true ··· 147 138 res: types.Album, 148 139 ok: bool, 149 140 ) { 150 - 151 - query: cstring = "SELECT * FROM album WHERE id = ? LIMIT 1" 152 - 153 - stmt: ^sqlite.Statement 154 - 155 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 156 - fmt.eprintfln("failed to prepare statement. result code: {}", rc) 157 - return res, false 158 - } 159 - 160 - defer sqlite.finalize(stmt) 161 - 162 - c_id := strings.clone_to_cstring(id, allocator) 163 - defer delete(c_id) 164 - 165 - if rc := sqlite.bind_text( 166 - stmt, 167 - param_idx = 1, 168 - param_value = c_id, 169 - param_len = c.int(len(id)), 170 - free = {behaviour = .Static}, 171 - ); rc != .Ok { 172 - fmt.eprintfln("failed to bind value to ArtistId. result code: {}", rc) 173 - return res, false 141 + Album_Row :: struct { 142 + id: string `sqlite:"id"`, 143 + title: string `sqlite:"title"`, 174 144 } 175 145 176 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 177 - 178 - albums := make([dynamic]types.Album, 0, 1) 146 + rows := make([dynamic]Album_Row, 0, 1, allocator) 179 147 defer { 180 - for album in albums { 181 - types.delete_album(album) 148 + for row in rows { 149 + delete(row.id) 150 + delete(row.title) 182 151 } 183 - delete_dynamic_array(albums) 152 + delete_dynamic_array(rows) 184 153 } 185 154 186 - for sqlite.step(stmt) == .Row { 155 + rc := sa.query( 156 + db, 157 + &rows, 158 + "SELECT id, title FROM album WHERE id = ? LIMIT 1", 159 + {{index = 1, value = id}}, 160 + ) 187 161 188 - album := types.Album { 189 - id = types.Album_Id(strings.clone_from(sqlite.column_text(stmt, 0))), 190 - title = strings.clone_from(sqlite.column_text(stmt, 1)), 191 - } 162 + if rc != .Ok || len(rows) == 0 { 163 + return res, false 164 + } 192 165 193 - append(&albums, album) 166 + row := rows[0] 167 + album := types.Album { 168 + id = types.Album_Id(strings.clone(row.id, allocator)), 169 + title = strings.clone(row.title, allocator), 194 170 } 195 171 196 - return albums[0], true 172 + return album, true 197 173 } 198 174 199 175
+22 -33
src/db/repo/artist.odin
··· 4 4 import sqlite "../../../vendor/sqlite" 5 5 import sa "../../../vendor/sqlite/addons" 6 6 import types "../../core" 7 - import "core:c" 8 7 import "core:fmt" 9 - import "core:mem" 10 8 import "core:strings" 11 - import "core:testing" 12 9 13 10 new_artist :: proc( 14 11 db: ^sqlite.Connection, ··· 102 99 res: types.Artist, 103 100 ok: bool, 104 101 ) { 105 - 106 - query: cstring = "SELECT * FROM artist WHERE name = ? LIMIT 1" 107 - 108 - stmt: ^sqlite.Statement 109 - 110 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 111 - fmt.eprintfln("failed to prepare statement. result code: {}", rc) 112 - return res, false 102 + Artist_Row :: struct { 103 + id: string `sqlite:"id"`, 104 + name: string `sqlite:"name"`, 113 105 } 114 106 115 - defer sqlite.finalize(stmt) 107 + rows := make([dynamic]Artist_Row, 0, 1, allocator) 108 + defer { 109 + for row in rows { 110 + delete(row.id) 111 + delete(row.name) 112 + } 113 + delete_dynamic_array(rows) 114 + } 116 115 117 - c_name := strings.clone_to_cstring(name, allocator) 118 - defer delete(c_name) 116 + rc := sa.query( 117 + db, 118 + &rows, 119 + "SELECT id, name FROM artist WHERE name = ? LIMIT 1", 120 + {{index = 1, value = name}}, 121 + ) 119 122 120 - if rc := sqlite.bind_text( 121 - stmt, 122 - param_idx = 1, 123 - param_value = c_name, 124 - param_len = c.int(len(name)), 125 - free = {behaviour = .Static}, 126 - ); rc != .Ok { 127 - fmt.eprintfln("failed to bind value to artist name. result code: {}", rc) 123 + if rc != .Ok || len(rows) == 0 { 128 124 return res, false 129 125 } 130 126 131 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 132 - 133 - artist: types.Artist 134 - 135 - for sqlite.step(stmt) == .Row { 136 - 137 - artist = types.Artist { 138 - id = types.Artist_Id(strings.clone_from(sqlite.column_text(stmt, 0))), 139 - name = strings.clone_from(sqlite.column_text(stmt, 1)), 140 - } 141 - 127 + row := rows[0] 128 + artist := types.Artist { 129 + id = types.Artist_Id(strings.clone(row.id, allocator)), 130 + name = strings.clone(row.name, allocator), 142 131 } 143 132 144 133 return artist, true
+41 -56
src/db/repo/track.odin
··· 4 4 import sqlite "../../../vendor/sqlite" 5 5 import sa "../../../vendor/sqlite/addons" 6 6 import types "../../core" 7 - import "core:c" 8 7 import "core:fmt" 9 8 import "core:strings" 10 9 ··· 61 60 res: types.Track, 62 61 ok: bool, 63 62 ) { 64 - query_with_album: cstring = "SELECT * FROM track WHERE title = ? AND album_id = ? LIMIT 1" 65 - query_without_album: cstring = "SELECT * FROM track WHERE title = ? LIMIT 1" 66 - query := query_without_album 67 - use_album_filter := false 68 - album_id_value := "" 69 - 70 - if v, has_album := album_id.?; has_album { 71 - query = query_with_album 72 - use_album_filter = true 73 - album_id_value = string(v) 63 + Track_Row :: struct { 64 + id: string `sqlite:"id"`, 65 + title: string `sqlite:"title"`, 66 + track_number: i32 `sqlite:"track_number"`, 67 + mb_id: string `sqlite:"mb_id"`, 68 + album_id: string `sqlite:"album_id"`, 74 69 } 75 70 76 - stmt: ^sqlite.Statement 77 - 78 - if rc := sqlite.prepare_v2(db, query, c.int(len(query)), &stmt, nil); rc != .Ok { 79 - fmt.eprintfln("failed to prepare statement. result code: {}", rc) 80 - return res, false 71 + rows := make([dynamic]Track_Row, 0, 1, allocator) 72 + defer { 73 + for row in rows { 74 + delete(row.id) 75 + delete(row.title) 76 + delete(row.mb_id) 77 + delete(row.album_id) 78 + } 79 + delete_dynamic_array(rows) 81 80 } 82 81 83 - defer sqlite.finalize(stmt) 82 + rc := sqlite.Result_Code.Ok 84 83 85 - c_title := strings.clone_to_cstring(title, allocator) 86 - defer delete(c_title) 87 - 88 - if rc := sqlite.bind_text( 89 - stmt, 90 - param_idx = 1, 91 - param_value = c_title, 92 - param_len = c.int(len(title)), 93 - free = {behaviour = .Static}, 94 - ); rc != .Ok { 95 - fmt.eprintfln("failed to bind value to track title. result code: {}", rc) 96 - return res, false 97 - } 98 - 99 - c_album_id: cstring 100 - defer delete(c_album_id) 101 - 102 - if use_album_filter { 103 - c_album_id = strings.clone_to_cstring(album_id_value, allocator) 104 - 105 - if rc := sqlite.bind_text( 106 - stmt, 107 - param_idx = 2, 108 - param_value = c_album_id, 109 - param_len = c.int(len(album_id_value)), 110 - free = {behaviour = .Static}, 111 - ); rc != .Ok { 112 - fmt.eprintfln("failed to bind value to album_id. result code: {}", rc) 113 - return res, false 114 - } 84 + if v, has_album := album_id.?; has_album { 85 + rc = sa.query( 86 + db, 87 + &rows, 88 + "SELECT id, title, track_number, COALESCE(mb_id, '') AS mb_id, album_id FROM track WHERE title = ? AND album_id = ? LIMIT 1", 89 + { 90 + {index = 1, value = title}, 91 + {index = 2, value = string(v)}, 92 + }, 93 + ) 94 + } else { 95 + rc = sa.query( 96 + db, 97 + &rows, 98 + "SELECT id, title, track_number, COALESCE(mb_id, '') AS mb_id, album_id FROM track WHERE title = ? LIMIT 1", 99 + {{index = 1, value = title}}, 100 + ) 115 101 } 116 102 117 - fmt.printfln("prepared sql: {}\n", sqlite.expanded_sql(stmt)) 118 - 119 - if sqlite.step(stmt) != .Row { 103 + if rc != .Ok || len(rows) == 0 { 120 104 return res, false 121 105 } 122 106 107 + row := rows[0] 123 108 track := types.Track { 124 - id = types.Track_Id(strings.clone_from(sqlite.column_text(stmt, 0))), 125 - title = strings.clone_from(sqlite.column_text(stmt, 1)), 126 - track_number = u8(sqlite.column_int(stmt, 2)), 127 - album_id = types.Album_Id(strings.clone_from(sqlite.column_text(stmt, 4))), 109 + id = types.Track_Id(strings.clone(row.id, allocator)), 110 + title = strings.clone(row.title, allocator), 111 + track_number = u8(row.track_number), 112 + album_id = types.Album_Id(strings.clone(row.album_id, allocator)), 128 113 } 129 114 130 - if mb_id_ptr := sqlite.column_text(stmt, 3); mb_id_ptr != nil { 131 - track.mb_id = strings.clone_from(mb_id_ptr) 115 + if len(row.mb_id) > 0 { 116 + track.mb_id = strings.clone(row.mb_id, allocator) 132 117 } 133 118 134 119 return track, true