[xpunit] Rename from 'cxxunit' and switch to mozbuild (bug #114)
[abstract.git] / storage / aaLoadFactList.cpp
blobafcf51f7f3cc40f9cb406d8f3423216e5da69546
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 "nsIMutableArray.h"
26 #include "nsComponentManagerUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsEmbedString.h"
30 /* Unfrozen API */
31 #include "unstable/mozIStorageConnection.h"
32 #include "unstable/mozIStorageStatement.h"
34 /* Project includes */
35 #include <abstract/base/aaIFlow.h>
36 #include <abstract/base/aaIEvent.h>
37 #include <abstract/base/aaIFact.h>
38 #include <abstract/base/aaITransaction.h>
39 #include <abstract/storage/aaILoadQuery.h>
40 #include "aaLoadFactList.h"
42 #define AA_LOADFACTLIST_SELECT_QUERY "SELECT strftime('%s',transfer.day),\
43 transfer.event_id, transfer.id, fromF.flow_id, toF.flow_id, transfer.amount,\
44 txn.value, fromFl.tag, toFl.tag, txn.earnings\
45 FROM txn\
46 LEFT JOIN transfer ON transfer.day == txn.day AND transfer.event_id == txn.event_id\
47 AND transfer.id == txn.transfer_id\
48 LEFT JOIN fact AS fromF ON transfer.day == fromF.day AND transfer.event_id\
49 == fromF.event_id AND transfer.id == fromF.transfer_id AND\
50 fromF.side == 1\
51 LEFT JOIN fact AS toF ON transfer.day == toF.day AND transfer.event_id ==\
52 toF.event_id AND transfer.id == toF.transfer_id AND toF.side == 0\
53 LEFT JOIN flow AS fromFl ON fromF.flow_id=fromFl.id\
54 LEFT JOIN flow AS toFl ON toF.flow_id=toFl.id"
56 aaLoadFactList::aaLoadFactList(nsISupports *aOuter)
57 :aaLoadQuery(aOuter)
61 aaLoadFactList::~aaLoadFactList()
65 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadFactList,
66 aaLoadQuery)
68 /* Virtual methods */
69 const char*
70 aaLoadFactList::getSelectQuery() const
72 return AA_LOADFACTLIST_SELECT_QUERY;
75 aaIDataNode*
76 aaLoadFactList::fetchRow()
78 nsresult rv;
79 PRInt64 id, eventId, fromId = 0, toId = 0;
80 PRTime time;
81 PRBool isNull;
82 double sum, value, earnings;
83 nsEmbedCString tag;
85 rv = mStatement->GetInt64(0, &time);
86 if (NS_FAILED(rv))
87 return nsnull;
88 time *= 1000000;
90 rv = mStatement->GetInt64(1, &eventId);
91 if (NS_FAILED(rv))
92 return nsnull;
94 nsCOMPtr<aaIEvent> event = do_CreateInstance("@aasii.org/base/event;1");
95 if (NS_UNLIKELY( ! event ))
96 return nsnull;
97 event->SetTime( time );
98 event->SetId( eventId );
100 rv = mStatement->GetInt64(2, &id);
101 NS_ENSURE_SUCCESS(rv, nsnull);
103 nsCOMPtr<aaIFlow> fromFlow(do_CreateInstance("@aasii.org/base/flow;1"));
104 nsCOMPtr<aaIFlow> toFlow(do_CreateInstance("@aasii.org/base/flow;1"));
105 nsCOMPtr<aaIFlow> finres(do_CreateInstance("@aasii.org/base/income-flow;1"));
106 NS_ENSURE_TRUE(fromFlow && toFlow, nsnull);
108 rv = mStatement->GetIsNull(3, &isNull);
109 NS_ENSURE_SUCCESS(rv, nsnull);
111 if (!isNull) {
112 rv = mStatement->GetInt64(3, &fromId);
113 NS_ENSURE_SUCCESS(rv, nsnull);
114 fromFlow->SetId(fromId);
116 rv = mStatement->GetUTF8String(7, tag);
117 NS_ENSURE_SUCCESS(rv, nsnull);
118 rv = fromFlow->SetTag(NS_ConvertUTF8toUTF16(tag));
119 NS_ENSURE_SUCCESS(rv, nsnull);
120 } else {
121 fromFlow = finres;
124 rv = mStatement->GetIsNull(4, &isNull);
125 NS_ENSURE_SUCCESS(rv, nsnull);
127 if (!isNull) {
128 rv = mStatement->GetInt64(4, &toId);
129 NS_ENSURE_SUCCESS(rv, nsnull);
130 toFlow->SetId(toId);
132 rv = mStatement->GetUTF8String(8, tag);
133 NS_ENSURE_SUCCESS(rv, nsnull);
134 rv = toFlow->SetTag(NS_ConvertUTF8toUTF16(tag));
135 NS_ENSURE_SUCCESS(rv, nsnull);
136 } else {
137 toFlow = finres;
140 NS_ENSURE_TRUE(toId || fromId, nsnull);
142 rv = mStatement->GetDouble(5, &sum);
143 NS_ENSURE_SUCCESS(rv, nsnull);
145 rv = mStatement->GetDouble(6, &value);
146 NS_ENSURE_SUCCESS(rv, nsnull);
148 rv = mStatement->GetIsNull(9, &isNull);
149 NS_ENSURE_SUCCESS(rv, nsnull);
151 if (!isNull) {
152 rv = mStatement->GetDouble(9, &earnings);
153 NS_ENSURE_SUCCESS(rv, nsnull);
156 nsCOMPtr<aaIFact> fact(do_CreateInstance("@aasii.org/base/fact;1"));
157 if (! fact)
158 return nsnull;
160 fact->SetId(id);
161 fact->SetEvent(event);
162 fact->SetTakeFrom(fromFlow);
163 fact->SetGiveTo(toFlow);
164 fact->SetAmount(sum);
166 aaITransaction *txn;
167 CallCreateInstance("@aasii.org/base/transaction;1", nsnull, &txn);
169 txn->SetFact(fact);
170 txn->SetValue(value);
171 if (!isNull)
172 txn->SetEarnings(earnings);
173 return txn;