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 "nsIMutableArray.h"
26 #include "nsComponentManagerUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsEmbedString.h"
31 #include "unstable/mozIStorageConnection.h"
32 #include "unstable/mozIStorageStatement.h"
34 /* Project includes */
35 #include <abstract/base/aaIEntity.h>
36 #include <abstract/base/aaIResource.h>
37 #include <abstract/base/aaIFlow.h>
38 #include <abstract/base/aaIState.h>
39 #include <abstract/base/aaIBalance.h>
40 #include <abstract/storage/aaIFilter.h>
41 #include <abstract/storage/aaILoadQuery.h>
42 #include "aaLoadBalance.h"
44 #define AA_LOADBALANCE_SELECT_QUERY "SELECT balance.flow_id, flow.tag,\
45 flow.entity_id, entity.tag, term.resource_id, resource.tag, balance.amount,\
46 balance.value, balance.side\
48 LEFT JOIN flow ON balance.flow_id==flow.id\
49 LEFT JOIN term ON flow.id == term.flow_id AND balance.side == term.side\
50 LEFT JOIN entity ON flow.entity_id==entity.id\
51 LEFT JOIN resource ON term.resource_id==resource.id"
52 #define AA_LB_DEFAULT_FILTER \
53 " WHERE balance.paid IS NULL"
56 aaLoadBalance::aaLoadBalance(nsISupports
*aOuter
)
59 mFilter
= do_CreateInstance(AA_FILTER_CONTRACT_ID
);
60 nsCOMPtr
<aaIStringFilter
> strFilter(do_QueryInterface(mFilter
));
61 strFilter
->SetExpression(NS_LITERAL_CSTRING(AA_LB_DEFAULT_FILTER
));
65 aaLoadBalance::~aaLoadBalance()
69 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadBalance
,
74 aaLoadBalance::getSelectQuery() const
76 return AA_LOADBALANCE_SELECT_QUERY
;
80 aaLoadBalance::fetchRow()
83 PRInt64 flowId
, entityId
, resourceId
;
85 nsCOMPtr
<aaIFlow
> flow(do_CreateInstance("@aasii.org/base/flow;1"));
89 rv
= mStatement
->GetInt64(0, &flowId
);
95 rv
= mStatement
->GetUTF8String(1, str
);
98 flow
->SetTag(NS_ConvertUTF8toUTF16(str
));
100 nsCOMPtr
<aaIEntity
> entity(do_CreateInstance("@aasii.org/base/entity;1"));
101 rv
= mStatement
->GetInt64(2, &entityId
);
104 entity
->SetId(entityId
);
106 rv
= mStatement
->GetUTF8String(3, str
);
109 entity
->SetTag(NS_ConvertUTF8toUTF16(str
));
111 flow
->SetEntity(entity
);
113 nsCOMPtr
<aaIResource
> resource(do_CreateInstance("@aasii.org/base/resource;1"));
114 rv
= mStatement
->GetInt64(4, &resourceId
);
117 resource
->SetId(resourceId
);
119 rv
= mStatement
->GetUTF8String(5, str
);
122 resource
->SetTag(NS_ConvertUTF8toUTF16(str
));
125 rv
= mStatement
->GetDouble(6, &amount
);
130 rv
= mStatement
->GetDouble(7, &value
);
134 PRBool side
= PR_FALSE
;
135 rv
= mStatement
->GetInt32(8, &side
);
140 CallCreateInstance("@aasii.org/base/state;1", nsnull
, &balance
);
143 balance
->SetFlow(flow
);
144 balance
->SetResource(resource
);
145 balance
->SetAmount(amount
);
146 balance
->SetValue(value
);
147 balance
->SetSide(side
);