// Boston, MA 02111-1307 USA.
// ============================================================================================
using System;
+using System.IO;
namespace Ejdb.BSON {
}
}
+ public BSONArray() {
+ }
+
+ public BSONArray(BSONUndefined[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetUndefined(i);
+ }
+ }
+
+ public BSONArray(BSONull[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNull(i);
+ }
+ }
+
+ public BSONArray(ushort[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, (int) arr[i]);
+ }
+ }
+
+ public BSONArray(uint[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, (long) arr[i]);
+ }
+ }
+
+ public BSONArray(ulong[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, (long) arr[i]);
+ }
+ }
+
+ public BSONArray(short[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, (int) arr[i]);
+ }
+ }
+
+ public BSONArray(string[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetString(i, arr[i]);
+ }
+ }
+
+ public BSONArray(int[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, arr[i]);
+ }
+ }
+
+ public BSONArray(long[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, arr[i]);
+ }
+ }
+
+ public BSONArray(float[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, arr[i]);
+ }
+ }
+
+ public BSONArray(double[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetNumber(i, arr[i]);
+ }
+ }
+
+ public BSONArray(bool[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetBool(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONOid[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetOID(i, arr[i]);
+ }
+ }
+
+ public BSONArray(DateTime[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetDate(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONDocument[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetObject(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONArray[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetArray(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONRegexp[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetRegexp(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONTimestamp[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetTimestamp(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONCodeWScope[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetCodeWScope(i, arr[i]);
+ }
+ }
+
+ public BSONArray(BSONBinData[] arr) {
+ for (var i = 0; i < arr.Length; ++i) {
+ SetBinData(i, arr[i]);
+ }
+ }
+
public BSONDocument SetNull(int idx) {
return base.SetNull(idx.ToString());
}
static Dictionary<Type, Action<BSONDocument, string, object>> TYPE_SETTERS =
new Dictionary<Type, Action<BSONDocument, string, object>> {
{typeof(bool), (d, k, v) => d.SetBool(k, (bool) v)},
+ {typeof(bool[]), (d, k, v) => d.SetArray(k, new BSONArray((bool[]) v))},
{typeof(byte), (d, k, v) => d.SetNumber(k, (int) v)},
{typeof(sbyte), (d, k, v) => d.SetNumber(k, (int) v)},
{typeof(ushort), (d, k, v) => d.SetNumber(k, (int) v)},
+ {typeof(ushort[]), (d, k, v) => d.SetArray(k, new BSONArray((ushort[]) v))},
{typeof(short), (d, k, v) => d.SetNumber(k, (int) v)},
+ {typeof(short[]), (d, k, v) => d.SetArray(k, new BSONArray((short[]) v))},
{typeof(uint), (d, k, v) => d.SetNumber(k, (int) v)},
+ {typeof(uint[]), (d, k, v) => d.SetArray(k, new BSONArray((uint[]) v))},
{typeof(int), (d, k, v) => d.SetNumber(k, (int) v)},
+ {typeof(int[]), (d, k, v) => d.SetArray(k, new BSONArray((int[]) v))},
{typeof(ulong), (d, k, v) => d.SetNumber(k, (long) v)},
+ {typeof(ulong[]), (d, k, v) => d.SetArray(k, new BSONArray((ulong[]) v))},
{typeof(long), (d, k, v) => d.SetNumber(k, (long) v)},
+ {typeof(long[]), (d, k, v) => d.SetArray(k, new BSONArray((long[]) v))},
{typeof(float), (d, k, v) => d.SetNumber(k, (float) v)},
+ {typeof(float[]), (d, k, v) => d.SetArray(k, new BSONArray((float[]) v))},
{typeof(double), (d, k, v) => d.SetNumber(k, (double) v)},
+ {typeof(double[]), (d, k, v) => d.SetArray(k, new BSONArray((double[]) v))},
{typeof(char), (d, k, v) => d.SetString(k, v.ToString())},
{typeof(string), (d, k, v) => d.SetString(k, (string) v)},
+ {typeof(string[]), (d, k, v) => d.SetArray(k, new BSONArray((string[]) v))},
{typeof(BSONOid), (d, k, v) => d.SetOID(k, (BSONOid) v)},
+ {typeof(BSONOid[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONOid[]) v))},
{typeof(BSONRegexp), (d, k, v) => d.SetRegexp(k, (BSONRegexp) v)},
+ {typeof(BSONRegexp[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONRegexp[]) v))},
{typeof(BSONValue), (d, k, v) => d.SetBSONValue((BSONValue) v)},
{typeof(BSONTimestamp), (d, k, v) => d.SetTimestamp(k, (BSONTimestamp) v)},
+ {typeof(BSONTimestamp[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONTimestamp[]) v))},
{typeof(BSONCodeWScope), (d, k, v) => d.SetCodeWScope(k, (BSONCodeWScope) v)},
+ {typeof(BSONCodeWScope[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONCodeWScope[]) v))},
{typeof(BSONBinData), (d, k, v) => d.SetBinData(k, (BSONBinData) v)},
+ {typeof(BSONBinData[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONBinData[]) v))},
{typeof(BSONDocument), (d, k, v) => d.SetDocument(k, (BSONDocument) v)},
+ {typeof(BSONDocument[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONDocument[]) v))},
{typeof(BSONArray), (d, k, v) => d.SetArray(k, (BSONArray) v)},
+ {typeof(BSONArray[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONArray[]) v))},
+ {typeof(DateTime), (d, k, v) => d.SetDate(k, (DateTime) v)},
+ {typeof(DateTime[]), (d, k, v) => d.SetArray(k, new BSONArray((DateTime[]) v))},
+ {typeof(BSONUndefined), (d, k, v) => d.SetUndefined(k)},
+ {typeof(BSONUndefined[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONUndefined[]) v))},
+ {typeof(BSONull), (d, k, v) => d.SetNull(k)},
+ {typeof(BSONull[]), (d, k, v) => d.SetArray(k, new BSONArray((BSONull[]) v))}
};
readonly List<BSONValue> _fieldslist;
/// </remarks>
/// <value>The type of the BSON.</value>
public virtual BSONType BSONType {
- get {
+ get {
return BSONType.OBJECT;
}
}
case BSONType.DATE:
{
DateTime dt = (DateTime) bv.Value;
- var diff = dt.ToUniversalTime() - BSONConstants.Epoch;
+ var diff = dt.ToLocalTime() - BSONConstants.Epoch;
long time = (long) Math.Floor(diff.TotalMilliseconds);
WriteTypeAndKey(bv, bw);
bw.Write(time);
public sealed class BSONRegexp : IBSONValue {
readonly string _re;
+
readonly string _opts;
public BSONType BSONType {
BSONRegexp() {
}
+ public BSONRegexp(string re) : this(re, "") {
+ }
+
public BSONRegexp(string re, string opts) {
this._re = re;
this._opts = opts;
--- /dev/null
+// ============================================================================================
+// .NET API for EJDB database library http://ejdb.org
+// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com>
+//
+// This file is part of EJDB.
+// EJDB is free software; you can redistribute it and/or modify it under the terms of
+// the GNU Lesser General Public License as published by the Free Software Foundation; either
+// version 2.1 of the License or any later version. EJDB 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 Lesser General Public
+// License for more details.
+// You should have received a copy of the GNU Lesser General Public License along with EJDB;
+// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+// Boston, MA 02111-1307 USA.
+// ============================================================================================
+using System;
+
+namespace Ejdb.BSON {
+
+ public sealed class BSONUndefined : IBSONValue {
+
+ public static BSONUndefined VALUE = new BSONUndefined();
+
+ public BSONType BSONType {
+ get {
+ return BSONType.UNDEFINED;
+ }
+ }
+
+ public override bool Equals(object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (ReferenceEquals(this, obj)) {
+ return true;
+ }
+ if (!(obj is BSONUndefined)) {
+ return false;
+ }
+ return true;
+ }
+
+ public override int GetHashCode() {
+ return 0;
+ }
+
+ public override string ToString() {
+ return "[BSONUndefined]";
+ }
+ }
+}
+
--- /dev/null
+// ============================================================================================
+// .NET API for EJDB database library http://ejdb.org
+// Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com>
+//
+// This file is part of EJDB.
+// EJDB is free software; you can redistribute it and/or modify it under the terms of
+// the GNU Lesser General Public License as published by the Free Software Foundation; either
+// version 2.1 of the License or any later version. EJDB 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 Lesser General Public
+// License for more details.
+// You should have received a copy of the GNU Lesser General Public License along with EJDB;
+// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+// Boston, MA 02111-1307 USA.
+// ============================================================================================
+using System;
+
+namespace Ejdb.BSON {
+
+ [Serializable]
+ public sealed class BSONull : IBSONValue {
+
+ public static BSONull VALUE = new BSONull();
+
+ public BSONType BSONType {
+ get {
+ return BSONType.NULL;
+ }
+ }
+
+ public override bool Equals(object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (ReferenceEquals(this, obj)) {
+ return true;
+ }
+ if (!(obj is BSONull)) {
+ return false;
+ }
+ return true;
+ }
+
+ public override int GetHashCode() {
+ return 0;
+ }
+
+ public override string ToString() {
+ return "[BSONull]";
+ }
+ }
+}
+
}
/// <summary>
- /// Save the BSON document doc into the collection cname.
+ /// Save the BSON document doc into the collection.
/// </summary>
/// <param name="cname">Name of collection.</param>
- /// <param name="doc">BSON document to save.</param>
- /// <param name="merge">If set to <c>true</c>
- /// If true the merge will be performend with old and new objects.
- /// Otherwise old object will be replaced.</param>
+ /// <param name="docs">BSON documents to save.</param>
/// <returns>True on success.</returns>
- public bool Save(string cname, BSONDocument doc, bool merge = false) {
+ public bool Save(string cname, params BSONDocument[] docs) {
+ CheckDisposed();
+ IntPtr cptr = _ejdbcreatecoll(_db, cname, null);
+ if (cptr == IntPtr.Zero) {
+ return false;
+ }
+ foreach (var doc in docs) {
+ if (!Save(cptr, doc, false)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /// <summary>
+ /// Save the BSON document doc into the collection.
+ /// And merge each doc object identified by <c>_id</c> with doc stored in DB.
+ /// </summary>
+ /// <param name="cname">Name of collection.</param>
+ /// <param name="docs">BSON documents to save.</param>
+ /// <returns>True on success.</returns>
+ public bool SaveMerge(string cname, params BSONDocument[] docs) {
CheckDisposed();
- bool rv;
IntPtr cptr = _ejdbcreatecoll(_db, cname, null);
if (cptr == IntPtr.Zero) {
return false;
}
+ foreach (var doc in docs) {
+ if (!Save(cptr, doc, true)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ bool Save(IntPtr cptr, BSONDocument doc, bool merge) {
+ bool rv;
BSONValue bv = doc.GetBSONValue("_id");
byte[] bsdata = doc.ToByteArray();
byte[] oiddata = new byte[12];
/// <returns>The query object.</returns>
/// <param name="qdoc">BSON query spec.</param>
/// <param name="defaultcollection">Name of the collection used by default.</param>
- public EJDBQuery CreateQuery(BSONDocument qdoc, string defaultcollection = null) {
+ public EJDBQuery CreateQuery(object qv = null, string defaultcollection = null) {
+ CheckDisposed();
+ return new EJDBQuery(this, BSONDocument.ValueOf(qv), defaultcollection);
+ }
+
+ public EJDBQuery CreateQueryFor(string defaultcollection) {
CheckDisposed();
- return new EJDBQuery(this, qdoc, defaultcollection);
+ return new EJDBQuery(this, new BSONDocument(), defaultcollection);
}
//.//////////////////////////////////////////////////////////////////
// Private staff //
_len = len;
}
+ public BSONIterator this[int idx] {
+ get {
+ if (_qresptr == IntPtr.Zero || idx >= _len || idx < 0) {
+ return null;
+ }
+ //static extern IntPtr _ejdbqresultbsondata([In] IntPtr qres, [In] int idx, out int size)
+ int size;
+ IntPtr bsdataptr = _ejdbqresultbsondata(_qresptr, idx, out size);
+ if (bsdataptr == IntPtr.Zero) {
+ return null;
+ }
+ byte[] bsdata = new byte[size];
+ Marshal.Copy(bsdataptr, bsdata, 0, bsdata.Length);
+ return new BSONIterator(bsdata);
+ }
+ }
+
public BSONIterator Next() {
if (_qresptr == IntPtr.Zero || _pos >= _len) {
return null;
}
- //static extern IntPtr _ejdbqresultbsondata([In] IntPtr qres, [In] int idx, out int size)
- int size;
- IntPtr bsdataptr = _ejdbqresultbsondata(_qresptr, _pos, out size);
- if (bsdataptr == IntPtr.Zero) {
- return null;
- }
- byte[] bsdata = new byte[size];
- Marshal.Copy(bsdataptr, bsdata, 0, bsdata.Length);
- _pos++;
- return new BSONIterator(bsdata);
+ return this[_pos++];
}
public IEnumerator<BSONIterator> GetEnumerator() {
get {
return _defaultcollection;
}
+ set {
+ _defaultcollection = value;
+ }
}
internal EJDBQuery(EJDB jb, BSONDocument qdoc, string defaultcollection = null) {
}
_hints = null;
}
+
+ public EJDBQuery SetDefaultCollection(string cname) {
+ _defaultcollection = cname;
+ return this;
+ }
//.//////////////////////////////////////////////////////////////////
// Privates //
//.//////////////////////////////////////////////////////////////////
}
q.Max(10);
using (EJDBQCursor cursor = q.Find(null, EJDBQuery.EXPLAIN_FLAG)) {
- Console.WriteLine(cursor.Log);
+ Assert.IsTrue(cursor.Log.IndexOf("MAX: 10") != -1);
}
q.Dispose();
jb.Dispose();
}
+
+ [Test]
+ public void Test4Q2() {
+ EJDB jb = new EJDB("testdb1", EJDB.DEFAULT_OPEN_MODE | EJDB.JBOTRUNC);
+ Assert.IsTrue(jb.IsOpen);
+
+ var parrot1 = BSONDocument.ValueOf(new{
+ name = "Grenny",
+ type = "African Grey",
+ male = true,
+ age = 1,
+ birthdate = DateTime.Now,
+ likes = new string[] { "green color", "night", "toys" },
+ extra1 = BSONull.VALUE
+ });
+
+ var parrot2 = BSONDocument.ValueOf(new{
+ name = "Bounty",
+ type = "Cockatoo",
+ male = false,
+ age = 15,
+ birthdate = DateTime.Now,
+ likes = new string[] { "sugar cane" },
+ extra1 = BSONull.VALUE
+ });
+ Assert.IsTrue(jb.Save("parrots", parrot1, parrot2));
+ Assert.AreEqual(2, jb.CreateQueryFor("parrots").Count());
+
+ var q = jb.CreateQuery(new{
+ name = new BSONRegexp("(grenny|bounty)", "i")
+ }).SetDefaultCollection("parrots").OrderBy("name");
+
+ using (var cur = q.Find()) {
+ Assert.AreEqual(2, cur.Length);
+
+ var doc = cur[0].ToBSONDocument();
+ Assert.AreEqual("Bounty", doc["name"]);
+ Assert.AreEqual(15, doc["age"]);
+
+ doc = cur[1].ToBSONDocument();
+ Assert.AreEqual("Grenny", doc["name"]);
+ Assert.AreEqual(1, doc["age"]);
+ }
+
+ q.Dispose();
+ jb.Dispose();
+ }
}
}
<Compile Include="Ejdb.DB\EJDBQuery.cs" />
<Compile Include="Ejdb.DB\EJDBQueryException.cs" />
<Compile Include="Ejdb.DB\EJDBQCursor.cs" />
+ <Compile Include="Ejdb.BSON\BSONull.cs" />
+ <Compile Include="Ejdb.BSON\BSONUndefined.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Ejdb.Tests/TestEJDB.cs">
<Files>
- <File FileName="Ejdb.DB/EJDB.cs" Line="275" Column="3" />
- <File FileName="Ejdb.BSON/BSONIterator.cs" Line="25" Column="2" />
- <File FileName="Ejdb.DB/EJDBQuery.cs" Line="196" Column="21" />
- <File FileName="Ejdb.DB/EJDBQCursor.cs" Line="26" Column="66" />
- <File FileName="Ejdb.BSON/BSONDocument.cs" Line="144" Column="3" />
- <File FileName="Ejdb.Tests/TestEJDB.cs" Line="24" Column="23" />
+ <File FileName="Ejdb.DB/EJDB.cs" Line="295" Column="16" />
+ <File FileName="Ejdb.DB/EJDBQuery.cs" Line="286" Column="16" />
+ <File FileName="Ejdb.DB/EJDBQCursor.cs" Line="71" Column="60" />
+ <File FileName="Ejdb.BSON/BSONDocument.cs" Line="224" Column="35" />
+ <File FileName="Ejdb.Tests/TestEJDB.cs" Line="112" Column="4" />
+ <File FileName="Ejdb.BSON/BSONRegexp.cs" Line="53" Column="3" />
+ <File FileName="Ejdb.BSON/BSONIterator.cs" Line="133" Column="20" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State selected="True" />
</Pad>
<Pad Id="MonoDevelop.NUnit.TestPad">
- <State expanded="True">
+ <State expanded="True" selected="True">
<Node name="nejdb" expanded="True">
<Node name="Ejdb" expanded="True">
<Node name="Tests" expanded="True">
- <Node name="TestEJDB" expanded="True">
- <Node name="Test4Q1" selected="True" />
- </Node>
+ <Node name="TestEJDB" expanded="True" />
</Node>
</Node>
</Node>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
- <BreakpointStore>
- <Breakpoint file="/home/adam/Projects/softmotions/ejdb/nejdb/Ejdb.Tests/TestEJDB.cs" line="101" column="8" />
- </BreakpointStore>
+ <BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
\ No newline at end of file
assert(from);
bson_type bt;
bson *bs = bson_create();
+ bson_init_as_query(bs);
while ((bt = bson_iterator_next(from)) != BSON_EOO) {
bson_append_field_from_iterator(from, bs);
}