[xpunit] Rename from 'cxxunit' and switch to mozbuild (bug #114)
[abstract.git] / storage / aaLoadBalance.cpp
blobf6fe2d7d8d20e960304b41b09cce05dc7dea66dc
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/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\
47 FROM balance\
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)
57 :aaLoadQuery(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));
62 mNewFilter = PR_TRUE;
65 aaLoadBalance::~aaLoadBalance()
69 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadBalance,
70 aaLoadQuery)
72 /* Virtual methods */
73 const char*
74 aaLoadBalance::getSelectQuery() const
76 return AA_LOADBALANCE_SELECT_QUERY;
79 aaIDataNode*
80 aaLoadBalance::fetchRow()
82 nsresult rv;
83 PRInt64 flowId, entityId, resourceId;
85 nsCOMPtr<aaIFlow> flow(do_CreateInstance("@aasii.org/base/flow;1"));
86 if ( !flow)
87 return nsnull;
89 rv = mStatement->GetInt64(0, &flowId);
90 if (NS_FAILED( rv ))
91 return nsnull;
92 flow->SetId(flowId);
94 nsEmbedCString str;
95 rv = mStatement->GetUTF8String(1, str);
96 if (NS_FAILED( rv ))
97 return nsnull;
98 flow->SetTag(NS_ConvertUTF8toUTF16(str));
100 nsCOMPtr<aaIEntity> entity(do_CreateInstance("@aasii.org/base/entity;1"));
101 rv = mStatement->GetInt64(2, &entityId);
102 if (NS_FAILED( rv ))
103 return nsnull;
104 entity->SetId(entityId);
106 rv = mStatement->GetUTF8String(3, str);
107 if (NS_FAILED( rv ))
108 return nsnull;
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);
115 if (NS_FAILED( rv ))
116 return nsnull;
117 resource->SetId(resourceId);
119 rv = mStatement->GetUTF8String(5, str);
120 if (NS_FAILED( rv ))
121 return nsnull;
122 resource->SetTag(NS_ConvertUTF8toUTF16(str));
124 double amount = 0.0;
125 rv = mStatement->GetDouble(6, &amount);
126 if (NS_FAILED( rv ))
127 return nsnull;
129 double value = 0.0;
130 rv = mStatement->GetDouble(7, &value);
131 if (NS_FAILED( rv ))
132 return nsnull;
134 PRBool side = PR_FALSE;
135 rv = mStatement->GetInt32(8, &side);
136 if (NS_FAILED( rv ))
137 return nsnull;
139 aaIBalance *balance;
140 CallCreateInstance("@aasii.org/base/state;1", nsnull, &balance);
141 if (! balance)
142 return nsnull;
143 balance->SetFlow(flow);
144 balance->SetResource(resource);
145 balance->SetAmount(amount);
146 balance->SetValue(value);
147 balance->SetSide(side);
148 return balance;