transport: pass correct channel
[abstract.git] / storage / base / aaLoadQuote.cpp
blob78b391763ab58806ef0d987c998ca315eeedfe2d
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,2008 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 "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsIInterfaceRequestorUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsXPCOMCID.h"
29 #include "nsIMutableArray.h"
30 #include "nsISupportsPrimitives.h"
32 /* Unfrozen API */
33 #include "nsIInterfaceRequestorUtils.h"
35 /* Project includes */
36 #include "aaIMoney.h"
37 #include "aaIHandler.h"
39 #include "aaLoadQuote.h"
41 #include "aaISqlFilter.h"
42 #include "aaIColumnNameConverter.h"
43 #include "aaOrderCondition.h"
45 /**********************************************************/
46 class CQuoteNameConverter : public aaIColumnNameConverter
48 public:
49 CQuoteNameConverter() {}
50 ~CQuoteNameConverter() {}
51 public:
52 NS_IMETHOD_(nsCAutoString) convertName(const nsACString& name) const {
53 if (name.Equals(NS_LITERAL_CSTRING("quote.tag"))) {
54 return NS_LITERAL_CSTRING("money.alpha_code");
55 } else if (name.Equals(NS_LITERAL_CSTRING("quote.date"))) {
56 return NS_LITERAL_CSTRING("quote.day");
57 } else if (name.Equals(NS_LITERAL_CSTRING("quote.rate"))) {
58 return NS_LITERAL_CSTRING("quote.rate");
60 return nsCAutoString();
63 /**********************************************************/
65 #ifndef AA_LOADQUOTE_SELECT_QUERY
66 #define AA_LOADQUOTE_SELECT_QUERY "SELECT\
67 money.id,\
68 money.alpha_code,\
69 strftime('%s',quote.day),\
70 quote.rate,\
71 quote.diff\
72 FROM (SELECT\
73 MAX(quote.day) AS latest_day,\
74 quote.resource_id AS q_resource_id\
75 FROM\
76 quote\
77 WHERE\
78 IFNULL(quote.resource_id = ?, 1)\
79 GROUP BY\
80 quote.resource_id)\
81 LEFT JOIN quote ON\
82 q_resource_id=quote.resource_id\
83 AND quote.day=latest_day\
84 LEFT JOIN money ON\
85 money.id=quote.resource_id"
86 #endif
88 #define AA_LOADQUOTE_SELECT_QUERY_FILTERED \
89 AA_LOADQUOTE_SELECT_QUERY AA_SQL_WHERE_FILTER_MARK AA_SQL_ORDER_MARK
91 static struct aaLoadRequest::ColumnMap map[] = {
92 { "money.id", 0}
93 ,{"money.alpha_code", 1}
94 ,{"time", 2}
95 ,{"rate", 3}
96 ,{"diff", 4}
97 ,{0, 0}
100 aaLoadQuote::aaLoadQuote()
102 mSql = AA_LOADQUOTE_SELECT_QUERY;
103 mSqlFiltered = AA_LOADQUOTE_SELECT_QUERY_FILTERED;
104 mMap = &map[0];
107 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadQuote,
108 aaLoadRequest)
110 /* aaISqlRequest */
111 NS_IMETHODIMP
112 aaLoadQuote::GetParams(nsIArray * * aParams)
114 nsresult rv;
115 nsCOMPtr<nsIMutableArray> result
116 = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
117 NS_ENSURE_SUCCESS(rv, rv);
119 nsCOMPtr<nsISupportsPRInt64> valInt64;
121 if (mFilter) {
122 nsCOMPtr<aaIMoney> money = do_QueryInterface(mFilter, &rv);
123 if (!money) {
124 money = do_GetInterface(mFilter, &rv);
125 if (NS_NOINTERFACE == rv) {
126 rv = result->InsertElementAt(nsnull, 0, PR_FALSE);
127 NS_ENSURE_SUCCESS(rv, rv);
128 *aParams = result;
129 result.forget();
131 return NS_OK;
134 NS_ENSURE_SUCCESS(rv, rv);
136 valInt64 = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID, &rv);
137 NS_ENSURE_SUCCESS(rv, rv);
138 rv = valInt64->SetData(money->PickId());
139 NS_ENSURE_SUCCESS(rv, rv);
140 rv = result->InsertElementAt(valInt64, 0, PR_FALSE);
141 NS_ENSURE_SUCCESS(rv, rv);
143 else {
144 rv = result->InsertElementAt(nsnull, 0, PR_FALSE);
145 NS_ENSURE_SUCCESS(rv, rv);
148 *aParams = result;
149 result.forget();
151 return NS_OK;
154 /* aaIDataNode */
155 NS_IMETHODIMP
156 aaLoadQuote::Accept(aaIHandler *aQuery)
158 return aQuery->HandleQuote(nsnull);
161 NS_IMETHODIMP
162 aaLoadQuote::GetCondition(aaICondition* pCondition, nsACString& aSql)
164 nsresult rc = NS_OK;
165 CQuoteNameConverter cnvrt;
166 NS_ENSURE_ARG_POINTER(pCondition);
168 aSql.Truncate();
170 pCondition->ToString(&cnvrt, aSql);
172 return rc;