Source

mongo-dart / lib / db.dart

Full commit
vadimtsushko ccd8fe4 



vadimtsushko a4f4058 

vadimtsushko ccd8fe4 

vadimtsushko a4f4058 
vadimtsushko ccd8fe4 







vadimtsushko 51713e5 

vadimtsushko ccd8fe4 
vadimtsushko a4f4058 
vadimtsushko ccd8fe4 

vadimtsushko 6a4d7de 
vadimtsushko e056253 
vadimtsushko 6a4d7de 
vadimtsushko 7057331 
vadimtsushko 2b81e76 




vadimtsushko b049363 
vadimtsushko ccd8fe4 
vadimtsushko 9f4d884 
vadimtsushko ebe5c2b 
Adam S f48ec3a 
vadimtsushko f35d109 
Adam S f48ec3a 




vadimtsushko b049363 
Adam S f48ec3a 




vadimtsushko 9f4d884 
vadimtsushko f35d109 
Adam S f48ec3a 
vadimtsushko f35d109 


vadimtsushko a4f4058 
vadimtsushko 9f4d884 
vadimtsushko f35d109 


vadimtsushko a4f4058 












vadimtsushko e056253 






vadimtsushko a4f4058 



vadimtsushko 7b5e3e2 
vadimtsushko f35d109 
vadimtsushko 9f4d884 
vadimtsushko 17f6764 



vadimtsushko 7b5e3e2 
vadimtsushko 9f4d884 

vadimtsushko a4f4058 
vadimtsushko 2b81e76 
vadimtsushko a4f4058 









vadimtsushko 51713e5 
vadimtsushko a4f4058 
vadimtsushko ccd8fe4 
class Db{
  String databaseName;
  ServerConfig serverConfig;
  Connection connection;
  validateDatabaseName(String dbName) {
    if(dbName.length === 0) throw "database name cannot be the empty string";  
    var invalidChars = [" ", ".", "\$", "/", "\\"];
    for(var i = 0; i < invalidChars.length; i++) {
      if(dbName.indexOf(invalidChars[i]) != -1) throw new Exception("database names cannot contain the character '" + invalidChars[i] + "'");
    }
  }    
  Db(this.databaseName, [this.serverConfig]){
     if (serverConfig === null) {
      serverConfig = new ServerConfig();
     }
    connection = new Connection(serverConfig);
  }      
  DbCollection collection(String collectionName){
      return new DbCollection(this,collectionName);
  }
  Future<MongoReplyMessage> executeQueryMessage(MongoMessage queryMessage){
    return connection.query(queryMessage);
  }  
  executeMessage(MongoMessage message){
    connection.execute(message);
  }    
  Future<bool> open(){
//    print("opening db");
    if (connection.connected){
      connection.close();
      connection = new Connection(serverConfig);
    }
    return connection.connect();
  }
  Future<Map> executeDbCommand(MongoMessage message){
      Completer<Map> result = new Completer();
      //print("executeDbCommand.message = ${message}");
      connection.query(message).then((replyMessage){
        //print("replyMessage = ${replyMessage}");
        //print("replyMessage.documents = ${replyMessage.documents}");
        
        String errMsg;
        if (replyMessage.documents.length == 0) {
          errMsg = "Error executing Db command, Document length 0 $replyMessage";
          print("Error: $errMsg");
          var m = new Map();
          m["errmsg"]=errMsg;
          result.complete(m);
        } else  if (replyMessage.documents[0]["ok"] == 1.0){
          result.complete(replyMessage.documents[0]);
        } else {
          errMsg = "Error executing Db command";
          if (replyMessage.documents[0].containsKey("errmsg")){
            errMsg = replyMessage.documents[0]["errmsg"];
          }
          print("Error: $errMsg");
          result.complete(replyMessage.documents[0]);
        }         
      });
    return result.future;        
  }
  Future<bool> dropCollection(String collectionName){
    Completer completer = new Completer();
    collectionsInfoCursor(collectionName).toList().then((v){
      if (v.length == 1){
        executeDbCommand(DbCommand.createDropCollectionCommand(this,collectionName))
          .then((res)=>completer.complete(res));
        } else{
          completer.complete(true);
        }  
    });    
    return completer.future;    
  }
  Future<Map> drop(){
    Completer completer = new Completer();
    executeDbCommand(DbCommand.createDropDatabaseCommand(this))
      .then((res)=>completer.complete(res));
    return completer.future;    
  }
  
  removeFromCollection(String collectionName, [Map selector = const {}]){
    connection.execute(new MongoRemoveMessage("$databaseName.$collectionName", selector));    
  }    
  
  Future<Map> getLastError(){    
    return executeDbCommand(DbCommand.createGetLastErrorCommand(this));
  }
  Future<Map> getNonce(){    
    return executeDbCommand(DbCommand.createGetNonceCommand(this));
  }

  Future<Map> wait(){
    return getLastError();
  }
  close(){
//    print("closing db");
    connection.close();
  }
  
  Cursor collectionsInfoCursor([String collectionName]) {
    Map selector = {};
    // If we are limiting the access to a specific collection name
    if(collectionName !== null){
      selector["name"] = this.databaseName + "." + collectionName;
    }  
    // Return Cursor
      return new Cursor(this, new DbCollection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector);      
  }    
}