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 "nsComponentManagerUtils.h"
26 #include "nsServiceManagerUtils.h"
27 #include "nsDirectoryServiceDefs.h"
28 #include "nsIProperties.h"
30 #include "nsStringAPI.h"
34 #include "unstable/nsAppDirectoryServiceDefs.h"
35 #include "unstable/mozStorageCID.h"
36 #include "unstable/mozIStorageService.h"
37 #include "unstable/mozIStorageConnection.h"
39 /* Project includes */
40 #include <abstract/storage/aaISession.h>
41 #include <abstract/storage/aaILoadQuery.h>
42 #include "aaSession.h"
44 aaSession::aaSession()
49 aaSession::~aaSession()
53 NS_IMPL_ISUPPORTS2(aaSession
,
59 aaSession::CreateQuery(const char *aContractID
, nsISupports
* *_retval
)
61 NS_ENSURE_ARG_POINTER(_retval
);
63 nsCOMPtr
<nsISupports
> query (do_CreateInstance(aContractID
, mConnection
,
65 NS_ENSURE_SUCCESS(rv
, rv
);
66 NS_IF_ADDREF(*_retval
= query
);
71 aaSession::GetConnection(mozIStorageConnection
* * aConnection
)
73 NS_ENSURE_TRUE(mConnection
, NS_ERROR_NOT_INITIALIZED
);
74 NS_IF_ADDREF(*aConnection
= mConnection
);
79 aaSession::GetChoosing(PRBool
*aChoosing
)
81 NS_ENSURE_ARG_POINTER(aChoosing
);
82 *aChoosing
= mChoosing
;
89 aaSession::BeginQuery()
92 return NS_ERROR_ALREADY_INITIALIZED
;
97 NS_IMETHODIMP
aaSession::GetQueryBuffer(nsISupports
* *aQueryBuffer
)
99 NS_ENSURE_ARG_POINTER(aQueryBuffer
);
100 NS_IF_ADDREF(*aQueryBuffer
= mQueryBuffer
);
103 NS_IMETHODIMP
aaSession::SetQueryBuffer(nsISupports
* aQueryBuffer
)
105 mQueryBuffer
= aQueryBuffer
;
111 aaSession::Save(aaIDataNode
*aNode
, aaIDataNode
*aOldNode
)
115 mDispatcher
= do_CreateInstance("@aasii.org/storage/save-dispatcher;1",
117 NS_ENSURE_SUCCESS(rv
, rv
);
119 return mDispatcher
->Save(aNode
, aOldNode
);
124 /* Private methods */
125 #define AA_TABLE_ENTITY "entity"
126 #define AA_TABLE_ENTITY_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
127 tag CHAR (47) UNIQUE"
129 #define AA_TABLE_RESOURCE "resource"
130 #define AA_TABLE_RESOURCE_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
131 tag CHAR (47) UNIQUE"
133 #define AA_TABLE_FLOW "flow"
134 #define AA_TABLE_FLOW_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
135 tag CHAR (47) UNIQUE, entity_id INTEGER NOT NULL, rate DOUBLE NOT NULL,\
138 #define AA_TABLE_TERM "term"
139 #define AA_TABLE_TERM_CREATE "flow_id INTEGER NOT NULL, side BOOLEAN NOT\
140 NULL, resource_id INTEGER NOT NULL, PRIMARY KEY (flow_id, side)"
142 #define AA_TABLE_FACT "fact"
143 #define AA_TABLE_FACT_CREATE "day INTEGER NOT NULL, event_id INTEGER NOT NULL,\
144 transfer_id INTEGER NOT NULL, side BOOLEAN NOT NULL, flow_id INTEGER\
145 NOT NULL, PRIMARY KEY (day, event_id, transfer_id, side)"
147 #define AA_TABLE_STATE "state"
148 #define AA_TABLE_STATE_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
149 flow_id INTEGER NOT NULL, side INTEGER NOT NULL,\
150 amount DOUBLE NOT NULL, start INTEGER NOT NULL, paid INTEGER"
153 #define AA_TABLE_JOB "job"
154 #define AA_TABLE_JOB_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
155 flow_id INTEGER NOT NULL, resource_id INTEGER NOT NULL"
158 #define AA_TABLE_EVENT "event"
159 #define AA_TABLE_EVENT_CREATE "day INTEGER NOT NULL, id INTEGER NOT NULL,\
160 PRIMARY KEY (day, id)"
162 #define AA_TABLE_TRANSFER "transfer"
163 #define AA_TABLE_TRANSFER_CREATE "day INTEGER NOT NULL, event_id INTEGER\
164 NOT NULL, id INTEGER NOT NULL, amount DOUBLE NOT NULL,\
165 PRIMARY KEY (day, event_id, id)"
167 #define AA_TABLE_TRANSACTION "txn"
168 #define AA_TABLE_TRANSACTION_CREATE "day INTEGER NOT NULL, event_id INTEGER\
169 NOT NULL, transfer_id INTEGER NOT NULL, value DOUBLE NOT NULL,\
170 PRIMARY KEY (day, event_id, transfer_id)"
172 /* XXX _acc This belongs to a separate module */
173 #define AA_TABLE_QUOTE "quote"
174 #define AA_TABLE_QUOTE_CREATE "resource_id INTEGER NOT NULL,\
175 day INTEGER NOT NULL, rate DOUBLE NOT NULL, PRIMARY KEY (resource_id, day)"
177 #define AA_TABLE_BALANCE "balance"
178 #define AA_TABLE_BALANCE_CREATE "id INTEGER PRIMARY KEY AUTOINCREMENT,\
179 flow_id INTEGER NOT NULL, side INTEGER NOT NULL,\
180 amount DOUBLE NOT NULL, value DOUBLE NOT NULL, start INTEGER NOT NULL,\
181 paid INTEGER, UNIQUE (flow_id, start)"
184 aaSession::getDatabase()
188 nsCOMPtr
<nsIProperties
> dsp
;
189 dsp
= do_GetService("@mozilla.org/file/directory_service;1", &rv
);
190 NS_ENSURE_SUCCESS(rv
, rv
);
192 nsCOMPtr
<nsIFile
> file
;
193 rv
= dsp
->Get(NS_APP_USER_PROFILE_50_DIR
, NS_GET_IID(nsIFile
)
194 , getter_AddRefs( file
));
195 NS_ENSURE_SUCCESS(rv
, rv
);
197 rv
= file
->AppendNative(nsDependentCString("default.sqlite"));
198 NS_ENSURE_SUCCESS(rv
, rv
);
200 nsCOMPtr
<mozIStorageService
> storageSvc
;
201 storageSvc
= do_GetService(MOZ_STORAGE_SERVICE_CONTRACTID
, &rv
);
202 NS_ENSURE_SUCCESS(rv
, rv
);
204 nsCOMPtr
<mozIStorageConnection
> connection
;
205 rv
= storageSvc
->OpenDatabase(file
, getter_AddRefs( connection
));
206 NS_ENSURE_SUCCESS(rv
, rv
);
208 rv
= checkTable(connection
, AA_TABLE_ENTITY
, AA_TABLE_ENTITY_CREATE
);
209 NS_ENSURE_SUCCESS(rv
, rv
);
210 rv
= checkTable(connection
, AA_TABLE_RESOURCE
, AA_TABLE_RESOURCE_CREATE
);
211 NS_ENSURE_SUCCESS(rv
, rv
);
212 rv
= checkTable(connection
, AA_TABLE_FLOW
, AA_TABLE_FLOW_CREATE
);
213 NS_ENSURE_SUCCESS(rv
, rv
);
214 rv
= checkTable(connection
, AA_TABLE_TERM
, AA_TABLE_TERM_CREATE
);
215 NS_ENSURE_SUCCESS(rv
, rv
);
216 rv
= checkTable(connection
, AA_TABLE_EVENT
, AA_TABLE_EVENT_CREATE
);
217 NS_ENSURE_SUCCESS(rv
, rv
);
218 rv
= checkTable(connection
, AA_TABLE_TRANSFER
, AA_TABLE_TRANSFER_CREATE
);
219 NS_ENSURE_SUCCESS(rv
, rv
);
220 rv
= checkTable(connection
, AA_TABLE_FACT
, AA_TABLE_FACT_CREATE
);
221 NS_ENSURE_SUCCESS(rv
, rv
);
222 rv
= checkTable(connection
, AA_TABLE_STATE
, AA_TABLE_STATE_CREATE
);
223 NS_ENSURE_SUCCESS(rv
, rv
);
224 rv
= checkTable(connection
, AA_TABLE_QUOTE
, AA_TABLE_QUOTE_CREATE
);
225 NS_ENSURE_SUCCESS(rv
, rv
);
226 rv
= checkTable(connection
, AA_TABLE_TRANSACTION
, AA_TABLE_TRANSACTION_CREATE
);
227 NS_ENSURE_SUCCESS(rv
, rv
);
228 rv
= checkTable(connection
, AA_TABLE_BALANCE
, AA_TABLE_BALANCE_CREATE
);
229 NS_ENSURE_SUCCESS(rv
, rv
);
230 mConnection
= connection
;
236 aaSession::checkTable(mozIStorageConnection
*aConnection
,
237 const char *tableName
, const char *createString
)
242 rv
= aConnection
->TableExists(nsDependentCString(tableName
), &hasTable
);
243 NS_ENSURE_SUCCESS(rv
, rv
);
245 rv
= aConnection
->CreateTable(tableName
, createString
);
246 NS_ENSURE_SUCCESS(rv
, rv
);