2008-10-24 23:51:28 +02:00
|
|
|
// model.cpp
|
|
|
|
|
2009-10-27 20:58:27 +01:00
|
|
|
/* Copyright 2009 10gen
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2008-10-24 23:51:28 +02:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "model.h"
|
2009-02-08 20:35:34 +01:00
|
|
|
#include "connpool.h"
|
2008-10-24 23:51:28 +02:00
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-02-08 23:55:17 +01:00
|
|
|
bool Model::load(BSONObj& query){
|
2009-02-09 19:34:40 +01:00
|
|
|
ScopedDbConnection conn( modelServer() );
|
|
|
|
|
|
|
|
BSONObj b = conn->findOne(getNS(), query);
|
|
|
|
conn.done();
|
2009-02-08 20:35:34 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( b.isEmpty() )
|
|
|
|
return false;
|
2009-02-08 23:55:17 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
unserialize(b);
|
2009-03-26 20:14:24 +01:00
|
|
|
_id = b["_id"].wrap().getOwned();
|
2009-01-15 16:17:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2009-01-14 23:09:51 +01:00
|
|
|
|
2009-12-02 22:36:32 +01:00
|
|
|
void Model::remove( bool safe ){
|
2009-12-28 22:43:43 +01:00
|
|
|
uassert( 10016 , "_id isn't set - needed for remove()" , _id["_id"].type() );
|
2009-12-02 22:36:32 +01:00
|
|
|
|
|
|
|
ScopedDbConnection conn( modelServer() );
|
|
|
|
conn->remove( getNS() , _id );
|
|
|
|
|
|
|
|
string errmsg = "";
|
|
|
|
if ( safe )
|
|
|
|
errmsg = conn->getLastError();
|
|
|
|
|
|
|
|
conn.done();
|
|
|
|
|
|
|
|
if ( safe && errmsg.size() )
|
2009-12-28 23:06:07 +01:00
|
|
|
throw UserException( 9002 , (string)"error on Model::remove: " + errmsg );
|
2009-12-02 22:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Model::save( bool safe ){
|
2009-02-09 19:34:40 +01:00
|
|
|
ScopedDbConnection conn( modelServer() );
|
2009-02-08 23:55:17 +01:00
|
|
|
|
|
|
|
BSONObjBuilder b;
|
|
|
|
serialize( b );
|
|
|
|
|
2009-02-14 15:41:52 +01:00
|
|
|
if ( _id.isEmpty() ){
|
2009-02-08 23:55:17 +01:00
|
|
|
OID oid;
|
2009-03-26 22:28:16 +01:00
|
|
|
oid.init();
|
2009-02-08 23:55:17 +01:00
|
|
|
b.appendOID( "_id" , &oid );
|
|
|
|
|
2009-02-09 19:04:32 +01:00
|
|
|
BSONObj o = b.obj();
|
2009-02-09 19:34:40 +01:00
|
|
|
conn->insert( getNS() , o );
|
2009-03-26 20:14:24 +01:00
|
|
|
_id = o["_id"].wrap().getOwned();
|
2009-02-08 23:55:17 +01:00
|
|
|
|
2009-02-13 22:17:52 +01:00
|
|
|
log(4) << "inserted new model " << getNS() << " " << o << endl;
|
2009-02-08 23:55:17 +01:00
|
|
|
}
|
|
|
|
else {
|
2009-02-14 15:41:52 +01:00
|
|
|
BSONElement id = _id["_id"];
|
|
|
|
b.append( id );
|
|
|
|
|
|
|
|
BSONObjBuilder qb;
|
|
|
|
qb.append( id );
|
2009-02-08 23:55:17 +01:00
|
|
|
|
2009-02-14 15:41:52 +01:00
|
|
|
BSONObj q = qb.obj();
|
2009-02-13 22:17:52 +01:00
|
|
|
BSONObj o = b.obj();
|
|
|
|
|
|
|
|
log(4) << "updated old model" << getNS() << " " << q << " " << o << endl;
|
|
|
|
|
|
|
|
conn->update( getNS() , q , o );
|
|
|
|
|
2009-02-08 23:55:17 +01:00
|
|
|
}
|
2009-02-20 21:07:52 +01:00
|
|
|
|
|
|
|
string errmsg = "";
|
2009-12-02 22:36:32 +01:00
|
|
|
if ( safe )
|
2009-02-20 21:07:52 +01:00
|
|
|
errmsg = conn->getLastError();
|
2009-02-08 23:55:17 +01:00
|
|
|
|
2009-02-09 19:34:40 +01:00
|
|
|
conn.done();
|
2009-02-20 21:07:52 +01:00
|
|
|
|
2009-12-02 22:36:32 +01:00
|
|
|
if ( safe && errmsg.size() )
|
2009-12-28 23:06:07 +01:00
|
|
|
throw UserException( 9003 , (string)"error on Model::save: " + errmsg );
|
2009-02-08 23:55:17 +01:00
|
|
|
}
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
} // namespace mongo
|