Click here to Skip to main content
15,860,859 members
Articles / Web Development / HTML

RaptorDB - The Document Store

Rate me:
Please Sign up or sign in to vote.
4.96/5 (278 votes)
24 Jul 2019CPOL86 min read 2.3M   16.3K   653  
NoSql, JSON based, Document store database with compiled .net map functions and automatic hybrid bitmap indexing and LINQ query filters (now with standalone Server mode, Backup and Active Restore, Transactions, Server side queries, MonoDroid support, HQ-Branch Replication, working in Linux, .net
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace RaptorDB
{
    public class LINQString : ExpressionVisitor
    {
        public LINQString()
        {
        }
        public StringBuilder sb = new StringBuilder();

        protected override Expression VisitBinary(BinaryExpression b)
        {
            sb.Append("(");
            this.Visit(b.Left);
            ExpressionType t = b.NodeType;

            switch (b.NodeType)
            {
                //case ExpressionType.Not:
                //    sb.Append(" NOT ");
                //    break;
                case ExpressionType.AndAlso:
                case ExpressionType.And:
                    sb.Append(" AND ");
                    break;
                case ExpressionType.OrElse:
                case ExpressionType.Or:
                    sb.Append(" OR ");
                    break;
                case ExpressionType.Equal:
                    sb.Append(" = ");
                    break;
                //case ExpressionType.NotEqual:
                //    sb.Append(" <> ");
                //    break;
                case ExpressionType.LessThan:
                    sb.Append(" < ");
                    break;
                case ExpressionType.LessThanOrEqual:
                    sb.Append(" <= ");
                    break;
                case ExpressionType.GreaterThan:
                    sb.Append(" > ");
                    break;
                case ExpressionType.GreaterThanOrEqual:
                    sb.Append(" >= ");
                    break;
            }

            this.Visit(b.Right);
            sb.Append(")");
            return b;
        }

        protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            string s = m.ToString();
            sb.Append(s.Substring(s.IndexOf('.') + 1));
            return m;
        }

        Stack<object> _stack = new Stack<object>();
        protected override Expression VisitMember(MemberExpression m)
        {
            var e = base.VisitMember(m);
            var c = m.Expression as ConstantExpression;
            if (c != null)
            {
                Type t = c.Value.GetType();
                var x = t.InvokeMember(m.Member.Name, BindingFlags.GetField |
                    BindingFlags.GetProperty |
                    BindingFlags.Public |
                    BindingFlags.NonPublic |
                    BindingFlags.Instance |
                    BindingFlags.Static, null, c.Value, null);

                if (x is string)
                    sb.Append("\"").Append(x).Append("\"");
                else if (!x.GetType().IsClass)
                {   // check for complex structs
                    if (x is DateTime || x is Guid)
                        sb.Append("\"").Append(x).Append("\"");
                    else // numbers
                        sb.Append(x);
                }
                else
                    _stack.Push(x);
            }

            if (m.Expression != null)
            {
                if (m.Expression.NodeType == ExpressionType.Parameter) // property
                    sb.Append(m.Member.Name);
                else if (m.Expression.NodeType == ExpressionType.MemberAccess) // obj.property
                {
                    Type t = m.Expression.Type;
                    var f = m.Expression as MemberExpression;
                    var val = t.InvokeMember(m.Member.Name, BindingFlags.GetField |
                        BindingFlags.GetProperty |
                        BindingFlags.Public |
                        BindingFlags.NonPublic |
                        BindingFlags.Instance |
                        BindingFlags.Static, null, _stack.Pop(), null);

                    if (val is string)
                        sb.Append("\"").Append(val).Append("\"");
                    else if (!val.GetType().IsClass)
                    {   // check for complex structs
                        if (val is DateTime || val is Guid)
                            sb.Append("\"").Append(val).Append("\"");
                        else // numbers
                            sb.Append(val);
                    }
                    else
                        _stack.Push(val);
                }
            }
            return e;
        }

        protected override Expression VisitConstant(ConstantExpression c)
        {
            IQueryable q = c.Value as IQueryable;
            if (q != null)
            {
                sb.Append(q.ElementType.Name);
            }
            else if (c.Value == null)
            {
                sb.Append("NULL");
            }
            else
            {
                //_stack.Push(c.Value);
                //if (Type.GetTypeCode(c.Value.GetType()) == TypeCode.Object)
                //    _stack.Pop();

                switch (Type.GetTypeCode(c.Value.GetType()))
                {
                    case TypeCode.Boolean:
                        //sb.Append(((bool)c.Value) ? 1 : 0);
                        sb.Append(((bool)c.Value) ? "True" : "False");
                        break;
                    case TypeCode.String:
                        sb.Append("\"");
                        sb.Append(c.Value);
                        sb.Append("\"");
                        break;
                    case TypeCode.Object:
                        break;
                    default:
                        sb.Append(c.Value);
                        break;
                }
            }
            return c;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect -
United Kingdom United Kingdom
Mehdi first started programming when he was 8 on BBC+128k machine in 6512 processor language, after various hardware and software changes he eventually came across .net and c# which he has been using since v1.0.
He is formally educated as a system analyst Industrial engineer, but his programming passion continues.

* Mehdi is the 5th person to get 6 out of 7 Platinum's on Code-Project (13th Jan'12)
* Mehdi is the 3rd person to get 7 out of 7 Platinum's on Code-Project (26th Aug'16)

Comments and Discussions