Merge lp:~tintou/noise/noise-create-db into lp:~elementary-apps/noise/trunk

Proposed by Corentin Noël
Status: Merged
Approved by: Cody Garver
Approved revision: 1830
Merged at revision: 1830
Proposed branch: lp:~tintou/noise/noise-create-db
Merge into: lp:~elementary-apps/noise/trunk
Diff against target: 234 lines (+139/-44)
3 files modified
src/DataBase.vala (+137/-27)
src/LocalBackend/LocalLibrary.vala (+1/-17)
vapi/libgda-5.0.vapi (+1/-0)
To merge this branch: bzr merge lp:~tintou/noise/noise-create-db
Reviewer Review Type Date Requested Status
elementary Apps team Pending
Review via email: mp+270345@code.launchpad.net

Commit message

Database: Create the database with the API instead of parsing a SQL query

Description of the change

Possible fix for the crash at launch.
Instead of parsing a SQL Query and executing it (as did with SQLHeavy), create the query with the proper API functions.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/DataBase.vala'
2--- src/DataBase.vala 2015-08-27 10:06:14 +0000
3+++ src/DataBase.vala 2015-09-07 23:34:57 +0000
4@@ -27,29 +27,6 @@
5 */
6
7 namespace Noise.Database {
8- namespace Tables {
9- public const string PLAYLISTS = """CREATE TABLE IF NOT EXISTS playlists (name TEXT, media TEXT,
10- sort_column_id INT, sort_direction TEXT, columns TEXT, rowid INTEGER PRIMARY KEY AUTOINCREMENT)""";
11-
12- public const string SMART_PLAYLISTS = """CREATE TABLE IF NOT EXISTS smart_playlists (name TEXT,
13- and_or INT, queries TEXT, limited INT, limit_amount INT, rowid INTEGER PRIMARY KEY AUTOINCREMENT)""";
14-
15- public const string COLUMNS = """CREATE TABLE IF NOT EXISTS columns (unique_id TEXT, sort_column_id INT,
16- sort_direction INT, columns TEXT)""";
17-
18- public const string MEDIA = """CREATE TABLE IF NOT EXISTS media (uri TEXT, file_size INT,
19- title TEXT, artist TEXT, composer TEXT, album_artist TEXT, album TEXT,
20- grouping TEXT, genre TEXT, comment TEXT, lyrics TEXT, has_embedded INT,
21- year INT, track INT, track_count INT, album_number INT,
22- album_count INT, bitrate INT, length INT, samplerate INT, rating INT,
23- playcount INT, skipcount INT, dateadded INT, lastplayed INT,
24- lastmodified INT, rowid INTEGER PRIMARY KEY AUTOINCREMENT)""";
25-
26- public const string DEVICES = """CREATE TABLE IF NOT EXISTS devices (unique_id TEXT,
27- sync_when_mounted INT, sync_music INT, sync_all_music INT, music_playlist STRING,
28- last_sync_time INT)""";
29- }
30-
31 /*
32 * NOTE:
33 * Update those constants when you change the order of columns.
34@@ -58,10 +35,7 @@
35 public static const string TABLE_NAME = "playlists";
36 public static const string NAME = "+0";
37 public static const string MEDIA = "+1";
38- public static const string SORT_COLUMN_ID = "+2";
39- public static const string SORT_DIRECTION = "+3";
40- public static const string COLUMNS = "+4";
41- public static const string ROWID = "+5";
42+ public static const string ROWID = "+2";
43 }
44
45 namespace SmartPlaylists {
46@@ -292,4 +266,140 @@
47 return builder.add_cond (sql_operator_type, id_field, id_value, 0);
48 }
49 }
50+
51+ public static void create_tables (Gda.Connection connection) {
52+ Error e = null;
53+
54+ /*
55+ * Creating the playlists table
56+ */
57+ var operation = Gda.ServerOperation.prepare_create_table (connection, "playlists", e,
58+ "name", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
59+ "media", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
60+ "rowid", typeof (int64), Gda.ServerOperationCreateTableFlag.PKEY_AUTOINC_FLAG);
61+ if (e != null) {
62+ critical (e.message);
63+ } else {
64+ try {
65+ operation.perform_create_table ();
66+ } catch (Error e) {
67+ // e.code == 1 is when the table already exists.
68+ if (e.code != 1) {
69+ critical (e.message);
70+ }
71+ }
72+ }
73+
74+ /*
75+ * Creating the smart_playlists table
76+ */
77+ operation = Gda.ServerOperation.prepare_create_table (connection, "smart_playlists", e,
78+ "name", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
79+ "and_or", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
80+ "queries", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
81+ "limited", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
82+ "limit_amount", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
83+ "rowid", typeof (int64), Gda.ServerOperationCreateTableFlag.PKEY_AUTOINC_FLAG);
84+ if (e != null) {
85+ critical (e.message);
86+ } else {
87+ try {
88+ operation.perform_create_table ();
89+ } catch (Error e) {
90+ // e.code == 1 is when the table already exists.
91+ if (e.code != 1) {
92+ critical (e.message);
93+ }
94+ }
95+ }
96+
97+ /*
98+ * Creating the columns table
99+ */
100+ operation = Gda.ServerOperation.prepare_create_table (connection, "columns", e,
101+ "unique_id", typeof (string), Gda.ServerOperationCreateTableFlag.UNIQUE_FLAG,
102+ "sort_column_id", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
103+ "sort_direction", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
104+ "columns", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
105+ "rowid", typeof (int64), Gda.ServerOperationCreateTableFlag.PKEY_AUTOINC_FLAG);
106+ if (e != null) {
107+ critical (e.message);
108+ } else {
109+ try {
110+ operation.perform_create_table ();
111+ } catch (Error e) {
112+ // e.code == 1 is when the table already exists.
113+ if (e.code != 1) {
114+ critical (e.message);
115+ }
116+ }
117+ }
118+
119+ /*
120+ * Creating the media table
121+ */
122+ operation = Gda.ServerOperation.prepare_create_table (connection, "media", e,
123+ "uri", typeof (string), Gda.ServerOperationCreateTableFlag.UNIQUE_FLAG,
124+ "file_size", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
125+ "title", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
126+ "artist", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
127+ "composer", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
128+ "album_artist", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
129+ "album", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
130+ "grouping", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
131+ "genre", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
132+ "comment", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
133+ "lyrics", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
134+ "has_embedded", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
135+ "year", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
136+ "track", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
137+ "track_count", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
138+ "album_number", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
139+ "album_count", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
140+ "bitrate", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
141+ "length", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
142+ "samplerate", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
143+ "rating", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
144+ "playcount", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
145+ "skipcount", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
146+ "dateadded", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
147+ "lastplayed", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
148+ "lastmodified", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
149+ "rowid", typeof (int64), Gda.ServerOperationCreateTableFlag.PKEY_AUTOINC_FLAG);
150+ if (e != null) {
151+ critical (e.message);
152+ } else {
153+ try {
154+ operation.perform_create_table ();
155+ } catch (Error e) {
156+ // e.code == 1 is when the table already exists.
157+ if (e.code != 1) {
158+ critical (e.message);
159+ }
160+ }
161+ }
162+
163+ /*
164+ * Creating the devices table
165+ */
166+ operation = Gda.ServerOperation.prepare_create_table (connection, "devices", e,
167+ "unique_id", typeof (string), Gda.ServerOperationCreateTableFlag.UNIQUE_FLAG,
168+ "sync_when_mounted", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
169+ "sync_music", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
170+ "sync_all_music", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
171+ "music_playlist", typeof (string), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG,
172+ "last_sync_time", typeof (int), Gda.ServerOperationCreateTableFlag.NOTHING_FLAG);
173+ if (e != null) {
174+ critical (e.message);
175+ } else {
176+ try {
177+ operation.perform_create_table ();
178+ } catch (Error e) {
179+ // e.code == 1 is when the table already exists.
180+ if (e.code != 1) {
181+ critical (e.message);
182+ }
183+ }
184+ }
185+ }
186 }
187
188=== modified file 'src/LocalBackend/LocalLibrary.vala'
189--- src/LocalBackend/LocalLibrary.vala 2015-09-03 16:49:54 +0000
190+++ src/LocalBackend/LocalLibrary.vala 2015-09-07 23:34:57 +0000
191@@ -54,7 +54,6 @@
192 private bool _doing_file_operations = false;
193
194 public Gda.Connection connection { public get; private set; }
195- private Gda.SqlParser parser;
196
197 private static const string DB_FILE = "database_0_3_1";
198
199@@ -135,22 +134,7 @@
200 error (e.message);
201 }
202
203- parser = connection.create_parser ();
204-
205- load_table (Database.Tables.PLAYLISTS);
206- load_table (Database.Tables.SMART_PLAYLISTS);
207- load_table (Database.Tables.COLUMNS);
208- load_table (Database.Tables.MEDIA);
209- load_table (Database.Tables.DEVICES);
210- }
211-
212- private void load_table (string table) {
213- try {
214- var statement = parser.parse_string (table, null);
215- connection.statement_execute_non_select (statement, null, null);
216- } catch (Error e) {
217- critical (e.message);
218- }
219+ Database.create_tables (connection);
220 }
221
222 /*
223
224=== modified file 'vapi/libgda-5.0.vapi'
225--- vapi/libgda-5.0.vapi 2015-08-26 16:43:54 +0000
226+++ vapi/libgda-5.0.vapi 2015-09-07 23:34:57 +0000
227@@ -755,6 +755,7 @@
228 public bool perform_drop_database (string? provider) throws GLib.Error;
229 public bool perform_drop_table () throws GLib.Error;
230 public static Gda.ServerOperation prepare_create_database (string provider, string? db_name) throws GLib.Error;
231+ public static Gda.ServerOperation prepare_create_table (Gda.Connection cnc, string table_name, GLib.Error error, ...);
232 public static Gda.ServerOperation prepare_drop_database (string provider, string? db_name) throws GLib.Error;
233 public static Gda.ServerOperation prepare_drop_table (Gda.Connection cnc, string table_name) throws GLib.Error;
234 [NoWrapper]

Subscribers

People subscribed via source and target branches