sbiTunesDatabaseServices.cpp
Go to the documentation of this file.
1 /*
2  //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
28 
29 // C/C++ includes
30 #include <time.h>
31 
32 // Mozilla includes
33 #include <nsComponentManagerUtils.h>
34 #include <nsThreadUtils.h>
35 
36 // Songbird includes
37 #include <sbIDatabaseQuery.h>
38 #include <sbIDatabaseResult.h>
39 #include <sbIDatabasePreparedStatement.h>
40 
41 #define SB_DBQUERY_CONTRACTID "@songbirdnest.com/Songbird/DatabaseQuery;1"
42 
44 }
45 
47  // nothing to do, move along
48 }
49 
50 nsresult
52  nsresult rv;
53  mDBQuery = do_CreateInstance(SB_DBQUERY_CONTRACTID, &rv);
54  NS_ENSURE_SUCCESS(rv, rv);
55 
56  rv = mDBQuery->SetAsyncQuery(PR_FALSE);
57  NS_ENSURE_SUCCESS(rv, rv);
58 
59  rv = mDBQuery->SetDatabaseGUID(NS_LITERAL_STRING("songbird"));
60  NS_ENSURE_SUCCESS(rv, rv);
61 
62  nsString sql;
63  sql.AppendLiteral("CREATE TABLE IF NOT EXISTS itunes_id_map "
64  "(itunes_id TEXT UNIQUE NOT NULL, "
65  "songbird_id TEXT UNIQUE NOT NULL)");
66  rv = mDBQuery->AddQuery(sql);
67  NS_ENSURE_SUCCESS(rv, rv);
68 
69  PRBool dbOK;
70  rv = mDBQuery->Execute(&dbOK);
71  NS_ENSURE_SUCCESS(rv, rv);
72  NS_ENSURE_TRUE(dbOK == 0, NS_ERROR_FAILURE);
73 
74  NS_NAMED_LITERAL_STRING(INSERT_SQL,
75  "INSERT OR REPLACE INTO itunes_id_map (itunes_id, songbird_id) VALUES (?, ?)");
76  rv = mDBQuery->PrepareQuery(INSERT_SQL, getter_AddRefs(mInsertMapID));
77  NS_ENSURE_SUCCESS(rv, rv);
78 
79  NS_NAMED_LITERAL_STRING(SELECT_SQL,
80  "SELECT songbird_id FROM itunes_id_map WHERE itunes_id = ?");
81  rv = mDBQuery->PrepareQuery(SELECT_SQL, getter_AddRefs(mSelectMapID));
82  NS_ENSURE_SUCCESS(rv, rv);
83 
84  NS_NAMED_LITERAL_STRING(DELETE_SQL,
85  "DELETE FROM itunes_id_map WHERE songbird_id = ?");
86 
87  rv = mDBQuery->PrepareQuery(SELECT_SQL, getter_AddRefs(mDeleteMapID));
88  NS_ENSURE_SUCCESS(rv, rv);
89 
90  return NS_OK;
91 }
92 
93 nsresult
94 sbiTunesDatabaseServices::MapID(nsAString const & aiTunesLibID,
95  nsAString const & aiTunesID,
96  nsAString const & aSongbirdID) {
97 
98  nsresult rv = mDBQuery->AddPreparedStatement(mInsertMapID);
99  NS_ENSURE_SUCCESS(rv, rv);
100 
101  nsString compositeID(aiTunesLibID);
102  compositeID.Append(aiTunesID);
103  rv = mDBQuery->BindStringParameter(0, compositeID);
104  NS_ENSURE_SUCCESS(rv, rv);
105 
106  rv = mDBQuery->BindStringParameter(1, aSongbirdID);
107  NS_ENSURE_SUCCESS(rv, rv);
108 
109  PRBool dbOK;
110  rv = mDBQuery->Execute(&dbOK);
111  NS_ENSURE_SUCCESS(rv, rv);
112  NS_ENSURE_TRUE(dbOK == 0, NS_ERROR_FAILURE);
113 
114  return NS_OK;
115 }
116 
117 nsresult
118 sbiTunesDatabaseServices::GetSBIDFromITID(nsAString const & aiTunesLibID,
119  nsAString const & aiTunesID,
120  nsAString & aSongbirdID) {
121 
122  nsresult rv = mDBQuery->AddPreparedStatement(mSelectMapID);
123  NS_ENSURE_SUCCESS(rv, rv);
124 
125  nsString iTunesID(aiTunesLibID);
126  iTunesID.Append(aiTunesID);
127 
128  rv = mDBQuery->BindStringParameter(0, iTunesID);
129  NS_ENSURE_SUCCESS(rv, rv);
130 
131  PRInt32 dbOK;
132  rv = mDBQuery->Execute(&dbOK);
133  NS_ENSURE_SUCCESS(rv, rv);
134  NS_ENSURE_TRUE(dbOK == 0, NS_ERROR_FAILURE);
135 
136  nsCOMPtr<sbIDatabaseResult> result;
137  rv = mDBQuery->GetResultObject(getter_AddRefs(result));
138  NS_ENSURE_TRUE(result, NS_ERROR_FAILURE);
139 
140  rv = result->GetRowCell(0, 0, aSongbirdID);
141  NS_ENSURE_SUCCESS(rv, rv);
142 
143  return NS_OK;
144 }
145 
146 nsresult
147 sbiTunesDatabaseServices::RemoveSBIDEntry(nsAString const & aSongbirdID) {
148 
149  nsresult rv = mDBQuery->AddPreparedStatement(mDeleteMapID);
150  NS_ENSURE_SUCCESS(rv, rv);
151 
152  rv = mDBQuery->BindStringParameter(0, aSongbirdID);
153  NS_ENSURE_SUCCESS(rv, rv);
154 
155  PRBool dbOK;
156  rv = mDBQuery->Execute(&dbOK);
157  NS_ENSURE_SUCCESS(rv, rv);
158  NS_ENSURE_TRUE(dbOK == 0, NS_ERROR_FAILURE);
159 
160  return NS_OK;
161 }
return NS_OK
nsresult RemoveSBIDEntry(nsAString const &aSongbirdID)
nsresult GetSBIDFromITID(nsAString const &aiTunesLibID, nsAString const &aiiTunesID, nsAString &aSongbirdID)
nsresult MapID(nsAString const &aiTunesLibID, nsAString const &aiTunesID, nsAString const &aSongbirdID)
#define SB_DBQUERY_CONTRACTID