Files
mongo/client/model.cpp

139 lines
3.7 KiB
C++
Raw Normal View History

2008-10-24 17:51:28 -04: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 17:51:28 -04:00
2010-04-27 15:27:52 -04:00
#include "pch.h"
2008-10-24 17:51:28 -04:00
#include "model.h"
#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();
if ( b.isEmpty() )
return false;
2009-02-08 17:55:17 -05:00
unserialize(b);
2009-03-26 15:14:24 -04:00
_id = b["_id"].wrap().getOwned();
return true;
}
2009-01-14 17:09:51 -05:00
2009-12-02 16:36:32 -05:00
void Model::remove( bool safe ){
uassert( 10016 , "_id isn't set - needed for remove()" , _id["_id"].type() );
2009-12-02 16:36:32 -05: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 16:36:32 -05:00
}
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 );
2010-04-21 16:02:44 -04: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: ";
2010-07-20 14:58:51 -04:00
ss << '[' << myId << "] != ";
ss << '[' << _id.firstElement() << ']';
2010-04-21 16:02:44 -04:00
throw UserException( 13121 , ss.str() );
}
}
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 {
2010-04-22 10:36:17 -04:00
if ( myId.eoo() ){
myId = _id["_id"];
b.append( myId );
}
assert( ! myId.eoo() );
2009-02-14 09:41:52 -05:00
BSONObjBuilder qb;
2010-04-22 10:36:17 -04:00
qb.append( myId );
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();
2010-04-21 16:02:44 -04:00
log(4) << "updated model" << getNS() << " " << q << " " << o << endl;
2009-02-13 16:17:52 -05:00
2010-04-21 17:22:44 -04:00
conn->update( getNS() , q , o , true );
2009-02-13 16:17:52 -05:00
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() )
throw UserException( 9003 , (string)"error on Model::save: " + errmsg );
2009-02-08 17:55:17 -05:00
}
2010-04-20 17:07:07 -04: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 17:09:51 -05:00
} // namespace mongo