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,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"
25 #include "nsISupportsPrimitives.h"
26 #include "nsXPCOMCID.h"
27 #include "nsComponentManagerUtils.h"
28 #include "nsStringAPI.h"
32 /* Project includes */
33 #include "aaIEntity.h"
34 #include "aaISqliteChannel.h"
35 #include "aaISqlRequest.h"
36 #include "aaBaseLoaders.h"
37 #include "aaSaveKeys.h"
38 #include "aaSaveEntity.h"
40 aaSaveEntity::aaSaveEntity()
44 NS_IMPL_ISUPPORTS1(aaSaveEntity
,
47 /* aaISqlTransaction */
49 aaSaveEntity::SetParam(nsISupports
*aParam
)
52 mEntity
= do_QueryInterface(aParam
, &rv
);
53 NS_ENSURE_SUCCESS(rv
, rv
);
59 aaSaveEntity::Execute(aaISqliteChannel
*aChannel
, nsISupports
* * _retval
)
61 NS_ENSURE_ARG_POINTER(aChannel
);
62 NS_ENSURE_TRUE(mEntity
, NS_ERROR_NOT_INITIALIZED
);
66 rv
= mEntity
->GetId(&id
);
67 NS_ENSURE_SUCCESS(rv
, rv
);
70 rv
= insert(aChannel
);
71 NS_ENSURE_SUCCESS(rv
, rv
);
74 rv
= update(aChannel
);
75 if (NS_FAILED(rv
)) return rv
;
86 aaSaveEntity::insert(aaISqliteChannel
*aChannel
)
90 mInserter
= do_CreateInstance(AA_INSERT_ENTITY_CONTRACT
, &rv
);
91 NS_ENSURE_SUCCESS(rv
, rv
);
94 rv
= mInserter
->SetFilter(mEntity
);
95 NS_ENSURE_SUCCESS(rv
, rv
);
97 rv
= aChannel
->Open(mInserter
, nsnull
);
98 NS_ENSURE_SUCCESS(rv
, rv
);
101 mIdLoader
= do_CreateInstance(AA_LOADLASTID_CONTRACT
, &rv
);
102 NS_ENSURE_SUCCESS(rv
, rv
);
104 nsCOMPtr
<nsISupportsCString
> filter
105 = do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
106 NS_ENSURE_SUCCESS(rv
, rv
);
108 rv
= filter
->SetData(NS_LITERAL_CSTRING("entity"));
109 NS_ENSURE_SUCCESS(rv
, rv
);
111 rv
= mIdLoader
->SetFilter(filter
);
112 NS_ENSURE_SUCCESS(rv
, rv
);
115 nsCOMPtr
<aaILoadObserver
> id
116 = do_CreateInstance(AA_SQLIDLISTENER_CONTRACT
, mEntity
, &rv
);
117 NS_ENSURE_SUCCESS(rv
, rv
);
119 rv
= aChannel
->Open(mIdLoader
, id
);
120 NS_ENSURE_SUCCESS(rv
, rv
);
126 aaSaveEntity::update(aaISqliteChannel
*aChannel
)
130 mUpdater
= do_CreateInstance(AA_UPDATE_ENTITY_CONTRACT
, &rv
);
131 NS_ENSURE_SUCCESS(rv
, rv
);
134 rv
= mUpdater
->SetFilter(mEntity
);
135 NS_ENSURE_SUCCESS(rv
, rv
);
137 rv
= aChannel
->Open(mUpdater
, nsnull
);
138 if (NS_FAILED(rv
)) return rv
;