0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 17:10:48 +01:00
mongodb/client/model.cpp

139 lines
3.7 KiB
C++
Raw Normal View History

2008-10-24 23:51:28 +02:00
// model.cpp
/* 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"
#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();
if ( b.isEmpty() )
return false;
2009-02-08 23:55:17 +01:00
unserialize(b);
2009-03-26 20:14:24 +01:00
_id = b["_id"].wrap().getOwned();
return true;
}
2009-01-14 23:09:51 +01:00
2009-12-02 22:36:32 +01:00
void Model::remove( bool safe ){
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() )
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 );
2010-04-21 22:02:44 +02:00
BSONElement myId;
{
BSONObjIterator i = b.iterator();
while ( i.more() ){
BSONElement e = i.next();
if ( strcmp( e.fieldName() , "_id" ) == 0 ){
myId = e;
break;
}
}
}
if ( myId.type() ){
if ( _id.isEmpty() ){
_id = myId.wrap();
}
else if ( myId.woCompare( _id.firstElement() ) ){
stringstream ss;
ss << "_id from serialize and stored differ: ";
ss << "[" << myId << "] != ";
ss << "[" << _id.firstElement() << "]";
throw UserException( 13121 , ss.str() );
}
}
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 {
2010-04-22 16:36:17 +02:00
if ( myId.eoo() ){
myId = _id["_id"];
b.append( myId );
}
assert( ! myId.eoo() );
2009-02-14 15:41:52 +01:00
BSONObjBuilder qb;
2010-04-22 16:36:17 +02:00
qb.append( myId );
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();
2010-04-21 22:02:44 +02:00
log(4) << "updated model" << getNS() << " " << q << " " << o << endl;
2009-02-13 22:17:52 +01:00
2010-04-21 23:22:44 +02:00
conn->update( getNS() , q , o , true );
2009-02-13 22:17:52 +01:00
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() )
throw UserException( 9003 , (string)"error on Model::save: " + errmsg );
2009-02-08 23:55:17 +01:00
}
2010-04-20 23:07:07 +02:00
BSONObj Model::toObject(){
BSONObjBuilder b;
serialize( b );
return b.obj();
}
void Model::append( const char * name , BSONObjBuilder& b ){
BSONObjBuilder bb( b.subobjStart( name ) );
serialize( bb );
bb.done();
}
2009-01-14 23:09:51 +01:00
} // namespace mongo