2008-10-24 17:51:28 -04:00
|
|
|
// model.cpp
|
|
|
|
|
|
2009-10-27 15:58:27 -04: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 17:51:28 -04:00
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "model.h"
|
2009-02-08 14:35:34 -05:00
|
|
|
#include "connpool.h"
|
2008-10-24 17:51:28 -04:00
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
namespace mongo {
|
|
|
|
|
|
2009-02-08 17:55:17 -05:00
|
|
|
bool Model::load(BSONObj& query){
|
2009-02-09 13:34:40 -05:00
|
|
|
ScopedDbConnection conn( modelServer() );
|
|
|
|
|
|
|
|
|
|
BSONObj b = conn->findOne(getNS(), query);
|
|
|
|
|
conn.done();
|
2009-02-08 14:35:34 -05:00
|
|
|
|
2009-01-15 10:17:11 -05:00
|
|
|
if ( b.isEmpty() )
|
|
|
|
|
return false;
|
2009-02-08 17:55:17 -05:00
|
|
|
|
2009-01-15 10:17:11 -05:00
|
|
|
unserialize(b);
|
2009-03-26 15:14:24 -04:00
|
|
|
_id = b["_id"].wrap().getOwned();
|
2009-01-15 10:17:11 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
2009-01-14 17:09:51 -05:00
|
|
|
|
2009-12-02 16:36:32 -05:00
|
|
|
void Model::remove( bool safe ){
|
|
|
|
|
uassert( "_id isn't set - needed for remove()" , _id["_id"].type() );
|
|
|
|
|
|
|
|
|
|
ScopedDbConnection conn( modelServer() );
|
|
|
|
|
conn->remove( getNS() , _id );
|
|
|
|
|
|
|
|
|
|
string errmsg = "";
|
|
|
|
|
if ( safe )
|
|
|
|
|
errmsg = conn->getLastError();
|
|
|
|
|
|
|
|
|
|
conn.done();
|
|
|
|
|
|
|
|
|
|
if ( safe && errmsg.size() )
|
|
|
|
|
throw UserException( (string)"error on Model::remove: " + errmsg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Model::save( bool safe ){
|
2009-02-09 13:34:40 -05:00
|
|
|
ScopedDbConnection conn( modelServer() );
|
2009-02-08 17:55:17 -05:00
|
|
|
|
|
|
|
|
BSONObjBuilder b;
|
|
|
|
|
serialize( b );
|
|
|
|
|
|
2009-02-14 09:41:52 -05:00
|
|
|
if ( _id.isEmpty() ){
|
2009-02-08 17:55:17 -05:00
|
|
|
OID oid;
|
2009-03-26 17:28:16 -04:00
|
|
|
oid.init();
|
2009-02-08 17:55:17 -05:00
|
|
|
b.appendOID( "_id" , &oid );
|
|
|
|
|
|
2009-02-09 13:04:32 -05:00
|
|
|
BSONObj o = b.obj();
|
2009-02-09 13:34:40 -05:00
|
|
|
conn->insert( getNS() , o );
|
2009-03-26 15:14:24 -04:00
|
|
|
_id = o["_id"].wrap().getOwned();
|
2009-02-08 17:55:17 -05:00
|
|
|
|
2009-02-13 16:17:52 -05:00
|
|
|
log(4) << "inserted new model " << getNS() << " " << o << endl;
|
2009-02-08 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
else {
|
2009-02-14 09:41:52 -05:00
|
|
|
BSONElement id = _id["_id"];
|
|
|
|
|
b.append( id );
|
|
|
|
|
|
|
|
|
|
BSONObjBuilder qb;
|
|
|
|
|
qb.append( id );
|
2009-02-08 17:55:17 -05:00
|
|
|
|
2009-02-14 09:41:52 -05:00
|
|
|
BSONObj q = qb.obj();
|
2009-02-13 16:17:52 -05:00
|
|
|
BSONObj o = b.obj();
|
|
|
|
|
|
|
|
|
|
log(4) << "updated old model" << getNS() << " " << q << " " << o << endl;
|
|
|
|
|
|
|
|
|
|
conn->update( getNS() , q , o );
|
|
|
|
|
|
2009-02-08 17:55:17 -05:00
|
|
|
}
|
2009-02-20 15:07:52 -05:00
|
|
|
|
|
|
|
|
string errmsg = "";
|
2009-12-02 16:36:32 -05:00
|
|
|
if ( safe )
|
2009-02-20 15:07:52 -05:00
|
|
|
errmsg = conn->getLastError();
|
2009-02-08 17:55:17 -05:00
|
|
|
|
2009-02-09 13:34:40 -05:00
|
|
|
conn.done();
|
2009-02-20 15:07:52 -05:00
|
|
|
|
2009-12-02 16:36:32 -05:00
|
|
|
if ( safe && errmsg.size() )
|
2009-02-20 15:07:52 -05:00
|
|
|
throw UserException( (string)"error on Model::save: " + errmsg );
|
2009-02-08 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
2009-01-14 17:09:51 -05:00
|
|
|
} // namespace mongo
|