0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/client/model.cpp

75 lines
1.9 KiB
C++
Raw Normal View History

2008-10-24 23:51:28 +02:00
// model.cpp
/**
* Copyright (C) 2008 10gen Inc.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2008-12-29 02:28:49 +01:00
*
2008-10-24 23:51:28 +02:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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-02-14 15:41:52 +01:00
_id = b["_id"].wrap();
return true;
}
2009-01-14 23:09:51 +01:00
2009-02-08 23:55:17 +01:00
void Model::save(){
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;
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-02-14 15:41:52 +01:00
_id = o["_id"].wrap();
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-09 19:34:40 +01:00
conn.done();
2009-02-08 23:55:17 +01:00
}
2009-01-14 23:09:51 +01:00
} // namespace mongo