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: */
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>
25 #include "nsStringAPI.h"
26 #include "nsEmbedString.h"
29 #include "unstable/mozIStorageConnection.h"
30 #include "unstable/mozIStorageStatement.h"
32 /* Project includes */
33 #include <abstract/base/aaIResource.h>
34 #include <abstract/base/aaIQuote.h>
35 #include <abstract/storage/aaISaveQuery.h>
36 #include "aaSaveQuote.h"
38 #define AA_QUOTE_SAVE "INSERT INTO quote (resource_id, day, rate) VALUES\
39 (?, (replace(round(julianday(?,'unixepoch')),'.0','')), ?)"
41 aaSaveQuote::aaSaveQuote(nsISupports
*aOuter
)
44 mConnection
= do_QueryInterface(aOuter
);
46 rv
= mConnection
->GetConnectionReady(&isReady
);
51 aaSaveQuote::~aaSaveQuote()
55 NS_IMPL_ISUPPORTS1(aaSaveQuote
,
59 aaSaveQuote::Save(aaIDataNode
*aNode
, aaIDataNode
*aOldNode
)
62 NS_ENSURE_TRUE( !aOldNode
, NS_ERROR_INVALID_ARG
);
64 nsCOMPtr
<aaIQuote
> quote(do_QueryInterface(aNode
, &rv
));
65 NS_ENSURE_SUCCESS(rv
, rv
);
67 nsCOMPtr
<aaIResource
> obj
;
68 rv
= quote
->GetResource(getter_AddRefs( obj
));
69 NS_ENSURE_SUCCESS(rv
, rv
);
70 NS_ENSURE_TRUE(obj
, NS_ERROR_NOT_INITIALIZED
);
73 rv
= obj
->GetId( &objId
);
74 NS_ENSURE_SUCCESS(rv
, rv
);
77 rv
= quote
->GetTime( &time
);
78 NS_ENSURE_SUCCESS(rv
, rv
);
82 rv
= quote
->GetRate( &rate
);
83 NS_ENSURE_SUCCESS(rv
, rv
);
85 nsCOMPtr
<mozIStorageStatement
> statement
;
86 rv
= mConnection
->CreateStatement(NS_LITERAL_CSTRING(AA_QUOTE_SAVE
),
87 getter_AddRefs(statement
));
88 NS_ENSURE_SUCCESS(rv
, rv
);
90 rv
= statement
->BindInt64Parameter(0, objId
);
91 NS_ENSURE_SUCCESS(rv
, rv
);
93 rv
= statement
->BindInt64Parameter(1, time
);
94 NS_ENSURE_SUCCESS(rv
, rv
);
96 rv
= statement
->BindDoubleParameter(2, rate
);
97 NS_ENSURE_SUCCESS(rv
, rv
);
99 return statement
->Execute();