[base] Switch to mozbuild (#114)
[abstract.git] / storage / aaSaveResource.cpp
blobe9c30e92f1b1bb89525fbe38b55a5beb1952d461
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <abstract/aacore.h>
24 #include "nsCOMPtr.h"
25 #include "nsStringAPI.h"
26 #include "nsEmbedString.h"
28 /* Unfrozen API */
29 #include "unstable/mozIStorageConnection.h"
30 #include "unstable/mozIStorageStatement.h"
32 /* Project includes */
33 #include <abstract/base/aaIResource.h>
34 #include <abstract/storage/aaISaveQuery.h>
35 #include "aaSaveResource.h"
37 aaSaveResource::aaSaveResource(nsISupports *aOuter)
39 nsresult rv;
40 mConnection = do_QueryInterface(aOuter);
41 PRBool isReady;
42 rv = mConnection->GetConnectionReady(&isReady);
43 if (NS_FAILED(rv))
44 mConnection = nsnull;
47 aaSaveResource::~aaSaveResource()
51 NS_IMPL_ISUPPORTS1(aaSaveResource,
52 aaISaveQuery)
53 /* aaISaveQuery */
54 NS_IMETHODIMP
55 aaSaveResource::Save(aaIDataNode *aNode, aaIDataNode *aOldNode)
57 nsresult rv;
59 nsCOMPtr<aaIResource> resource(do_QueryInterface(aNode, &rv));
60 NS_ENSURE_SUCCESS(rv, rv);
62 nsEmbedString wTag;
63 rv = resource->GetTag(wTag);
64 NS_ENSURE_SUCCESS(rv, rv);
65 nsEmbedCString newTag = NS_ConvertUTF16toUTF8(wTag);
67 if (! aOldNode) {
68 PRInt64 id = insert(newTag);
69 if (! id)
70 return NS_ERROR_FAILURE;
71 resource->SetId(id);
72 return NS_OK;
75 nsCOMPtr<aaIResource> old(do_QueryInterface(aOldNode, &rv));
76 NS_ENSURE_SUCCESS(rv, rv);
77 rv = old->GetTag(wTag);
78 NS_ENSURE_SUCCESS(rv, rv);
79 nsEmbedCString oldTag = NS_ConvertUTF16toUTF8(wTag);
81 return replace(newTag, oldTag);
84 /* Private methods */
85 PRInt64
86 aaSaveResource::insert(const nsACString &newTag)
88 nsresult rv;
89 NS_ENSURE_TRUE(mConnection, NS_ERROR_NOT_INITIALIZED);
91 nsCOMPtr<mozIStorageStatement> statement;
92 rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
93 "INSERT INTO resource (tag) VALUES (?)"), getter_AddRefs(statement));
94 NS_ENSURE_SUCCESS(rv, 0);
96 rv = statement->BindUTF8StringParameter(0, newTag);
97 NS_ENSURE_SUCCESS(rv, 0);
99 rv = statement->Execute();
100 NS_ENSURE_SUCCESS(rv, 0);
102 PRInt64 id;
103 rv = mConnection->GetLastInsertRowID(&id);
104 NS_ENSURE_SUCCESS(rv, 0);
106 return id;
109 nsresult
110 aaSaveResource::replace(const nsACString &newTag, const nsACString &oldTag)
112 nsresult rv;
113 NS_ENSURE_TRUE(mConnection, NS_ERROR_NOT_INITIALIZED);
115 nsCOMPtr<mozIStorageStatement> statement;
116 rv = mConnection->CreateStatement(NS_LITERAL_CSTRING(
117 "UPDATE resource SET tag=? WHERE tag=?"), getter_AddRefs(statement));
118 NS_ENSURE_SUCCESS(rv, rv);
120 rv = statement->BindUTF8StringParameter(0, newTag);
121 rv = statement->BindUTF8StringParameter(1, oldTag);
122 NS_ENSURE_SUCCESS(rv, rv);
123 return statement->Execute();