2009-02-10 20:04:03 +01:00
|
|
|
/** @file model.h */
|
2008-10-20 00:46:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-10-20 00:46:53 +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-20 00:46:53 +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-20 00:46:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "dbclient.h"
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Model is a base class for defining objects which are serializable to the Mongo
|
2009-01-15 16:17:11 +01:00
|
|
|
database via the database driver.
|
|
|
|
|
2009-02-09 23:40:45 +01:00
|
|
|
Definition
|
2009-01-15 16:17:11 +01:00
|
|
|
Your serializable class should inherit from Model and implement the abstract methods
|
|
|
|
below.
|
|
|
|
|
2009-02-09 23:40:45 +01:00
|
|
|
Loading
|
2009-01-27 03:15:08 +01:00
|
|
|
To load, first construct an (empty) object. Then call load(). Do not load an object
|
2009-01-15 16:17:11 +01:00
|
|
|
more than once.
|
|
|
|
*/
|
|
|
|
class Model {
|
|
|
|
public:
|
|
|
|
Model() { }
|
|
|
|
virtual ~Model() { }
|
|
|
|
|
|
|
|
virtual const char * getNS() = 0;
|
|
|
|
virtual void serialize(BSONObjBuilder& to) = 0;
|
2009-02-27 16:37:13 +01:00
|
|
|
virtual void unserialize(const BSONObj& from) = 0;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Define this as you see fit if you are using the default conn() implementation. */
|
2009-02-08 20:35:34 +01:00
|
|
|
static string defaultServer;
|
2009-01-15 16:17:11 +01:00
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Override this if you need to do fancier connection management than simply using globalConn. */
|
2009-02-08 20:35:34 +01:00
|
|
|
virtual string modelServer(){
|
|
|
|
uassert( "defaultServer not set" , defaultServer.size() );
|
|
|
|
return defaultServer;
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 03:15:08 +01:00
|
|
|
/** Load a single object.
|
|
|
|
@return true if successful.
|
|
|
|
*/
|
2009-01-15 16:17:11 +01:00
|
|
|
bool load(BSONObj& query);
|
2009-02-20 21:07:52 +01:00
|
|
|
void save( bool check=false );
|
2009-02-08 23:55:17 +01:00
|
|
|
|
|
|
|
private:
|
2009-02-14 15:41:52 +01:00
|
|
|
BSONObj _id;
|
2009-01-15 16:17:11 +01:00
|
|
|
};
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|