Esempio n. 1
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update", filter);
            var _id = doc[Connector.Protocol._ID];

            doc.Delete(Connector.Protocol._ID);
            if (doc.Count == 0)
            {
                return(0);              // nothing to update
            }
            //20160212 spol
            if (filter != null)
            {
                var wrapDoc = new BSONDocument();
                wrapDoc.Set(new BSONDocumentElement(Connector.Protocol.SET, doc));
                doc = wrapDoc;
            }

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(_id);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            CheckCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
Esempio n. 2
0
        internal Collection(Database database, string name)
        {
          m_Database = database;
          m_Name = name;
          m_FullName = m_Database.Name + '.' + this.m_Name;

          //re-compute this in .ctor not to do this on every send
          m_FullNameCStringBuffer = BinUtils.WriteCStringToBuffer(m_FullName);
        }
Esempio n. 3
0
        protected Connector.Query MakeQuery(Connector.Database db, Query query, out Connector.Collection collection)
        {
            if (m_Source.ModifyTarget.IsNullOrWhiteSpace())
            {
                throw new MongoDBDataAccessException(StringConsts.QUERY_MODIFY_TARGET_MISSING_ERROR + "\n" + m_Source.OriginalSource);
            }

            collection = db[m_Source.ModifyTarget];

            return(MakeQuery(query));
        }
Esempio n. 4
0
        protected virtual int DoUpsert(Connector.Database db, Row row, FieldFilterFunc filter = null)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "upsert", filter);

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var result = collection.Save(doc);

            CheckCRUDResult(result, row.Schema.Name, "upsert");

            return(result.TotalDocumentsAffected);
        }
Esempio n. 5
0
        protected virtual int DoInsert(Connector.Database db, Row row)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "insert");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var result = collection.Insert(doc);

            checkCRUDResult(result, row.Schema.Name, "insert");

            return(result.TotalDocumentsAffected);
        }
Esempio n. 6
0
        protected virtual int DoDelete(Connector.Database db, Row row, IDataStoreKey key)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "delete");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);

            var result = collection.Delete(new Connector.DeleteEntry(qry, Connector.DeleteLimit.OnlyFirstMatch));

            CheckCRUDResult(result, row.Schema.Name, "delete");

            return(result.TotalDocumentsAffected);
        }
Esempio n. 7
0
        protected virtual int DoUpdate(Connector.Database db, Row row, IDataStoreKey key)
        {
            var doc = convertRowToBSONDocumentWith_ID(row, "update");

            var tname = GetCollectionName(row.Schema);

            var collection = db[tname];

            var qry = new Connector.Query();

            qry.Set(doc[Connector.Protocol._ID]);
            var upd = new Connector.UpdateEntry(qry, doc, false, false);

            var result = collection.Update(upd);

            checkCRUDResult(result, row.Schema.Name, "update");

            return(result.TotalDocumentsAffected);
        }
Esempio n. 8
0
        internal BSONDocument RunCommand(int requestID, Database database, BSONDocument command)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_RUN_COMMAND(m_BufferStream, 
                                           requestID, 
                                           database, 
                                           command);
          writeSocket(total);
       
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);

           if (Protocol.IsOKReplyDoc(reply))
          {
            return reply.Documents[0];
          }
          throw new MongoDBConnectorProtocolException(StringConsts.PROTO_RUN_COMMAND_REPLY_ERROR + command.ToString()); 
        }
Esempio n. 9
0
        internal string[] GetCollectionNames(int requestID, Database db)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_LIST_COLLECTIONS(m_BufferStream, requestID, db);
          writeSocket(total);
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);
          if (Protocol.IsOKReplyDoc(reply))
          {
            var cursor = reply.Documents[0]["cursor"] as BSONDocumentElement;
            if (cursor!=null && cursor.Value!=null)
            {
              var firstBatch = cursor.Value["firstBatch"] as BSONArrayElement;
              if (firstBatch!=null && firstBatch.Value!=null)
              {
                return firstBatch.Value.Where( e => e is BSONDocumentElement )
                                       .Cast<BSONDocumentElement>()
                                       .Where( de => de.Value!=null )
                                       .Select( de => de.Value["name"].AsString()).ToArray();
              }
            }
          }
          throw new MongoDBConnectorProtocolException(StringConsts.PROTO_LIST_COLLECTIONS_REPLY_ERROR); 
        }
Esempio n. 10
0
        internal void Ping(int requestID, Database db)
        {
          EnsureObjectNotDisposed();

          m_BufferStream.Position = 0;
          var total = Protocol.Write_PING(m_BufferStream, requestID, db);
          writeSocket(total);
          var got = readSocket();
          var reply = Protocol.Read_REPLY(got);
          Protocol.CheckReplyDataForErrors(reply);
          if (Protocol.IsOKReplyDoc(reply)) return;

          throw new MongoDBConnectorProtocolException(StringConsts.PROTO_PING_REPLY_ERROR); 
        }
 public MongoDBCRUDQueryExecutionContext(MongoDBDataStore  store, Database db)
 {
     DataStore = store;
     Database = db;
 }
Esempio n. 12
0
 public static Int32 Write_RUN_COMMAND(Stream stream, Int32 requestID, Database db, BSONDocument command)
 {
     return Write_QUERY(stream, requestID, db, null, QueryFlags.None, 0, -1, command, null);
 }
Esempio n. 13
0
        public static Int32 Write_QUERY(Stream stream,
            Int32 requestID,
            Database db,
            Collection collection, //may be null for $CMD
            QueryFlags flags,
            Int32 numberToSkip,
            Int32 numberToReturn,
            BSONDocument query,
            BSONDocument selector//may be null
            )
        {
            stream.Position = STD_HDR_LEN;//skip the header

                          BinUtils.WriteInt32(stream, (Int32)flags);

                          //if collection==null then query the $CMD collection
                          var fullNameBuffer = collection!=null ? collection.m_FullNameCStringBuffer : db.m_CMD_NameCStringBuffer;
                          stream.Write(fullNameBuffer, 0, fullNameBuffer.Length);

                          BinUtils.WriteInt32(stream, numberToSkip);
                          BinUtils.WriteInt32(stream, numberToReturn);

                          query.WriteAsBSON(stream);

                          if (selector!=null)
                           selector.WriteAsBSON(stream);

                          var total = (Int32)stream.Position;
                          stream.Position = 0;
                          writeStandardHeader(stream, total, requestID, 0, OP_QUERY);
                          return total;
        }
Esempio n. 14
0
        public static Int32 Write_PING(Stream stream, Int32 requestID, Database db)
        {
            var body = new BSONDocument();
                          body.Set( new BSONInt32Element("ping", 1) );

                          return Write_QUERY(stream, requestID, db, null, QueryFlags.None, 0, -1, body, null);
        }
Esempio n. 15
0
        public static Int32 Write_LIST_COLLECTIONS(Stream stream, Int32 requestID, Database db)
        {
            var body = new BSONDocument();
                          body.Set( new BSONInt32Element("listCollections", 1) );

                          return Write_QUERY(stream, requestID, db, null, QueryFlags.None, 0, -1, body, null);
        }