Esempio n. 1
0
        public ODocument ToSingle()
        {
            ODocument document = null;

            switch (_document.GetField <PayloadStatus>("PayloadStatus"))
            {
            case PayloadStatus.SingleRecord:
                document = _document.GetField <ODocument>("Content");
                break;

            case PayloadStatus.RecordCollection:
                document = _document.GetField <List <ODocument> >("Content").FirstOrDefault();
                break;

            case PayloadStatus.SerializedResult:
                document = new ODocument();
                document.SetField <object>("value", _document.GetField <object>("Content"));
                break;

            default:
                break;
            }

            return(document);
        }
Esempio n. 2
0
        public Dictionary <string, string> ConfigList()
        {
            ConfigList operation = new ConfigList();
            ODocument  document  = _connection.ExecuteOperation(operation);

            return(document.GetField <Dictionary <string, string> >("config"));
        }
Esempio n. 3
0
        public string ConfigGet(string key)
        {
            ConfigGet operation = new ConfigGet();

            operation.ConfigKey = key;
            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <string>(key));
        }
Esempio n. 4
0
        public bool ConfigSet(string configKey, string configValue)
        {
            ConfigSet operation = new ConfigSet();

            operation.Key   = configKey;
            operation.Value = configValue;
            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <bool>("IsCreated"));
        }
Esempio n. 5
0
        public bool DatabaseExist(string databaseName)
        {
            DbExist operation = new DbExist();

            operation.DatabaseName = databaseName;

            ODocument document = _connection.ExecuteOperation <DbExist>(operation);

            return(document.GetField <bool>("Exists"));
        }
Esempio n. 6
0
        public bool DatabaseExist(string databaseName, OStorageType storageType)
        {
            DbExist operation = new DbExist(null);

            operation.DatabaseName = databaseName;
            operation.StorageType  = storageType;

            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <bool>("Exists"));
        }
Esempio n. 7
0
        public bool CreateDatabase(string databaseName, ODatabaseType databaseType, OStorageType storageType)
        {
            DbCreate operation = new DbCreate();

            operation.DatabaseName = databaseName;
            operation.DatabaseType = databaseType;
            operation.StorageType  = storageType;

            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <bool>("IsCreated"));
        }
Esempio n. 8
0
        public Dictionary <string, string> Databases()
        {
            Dictionary <string, string> returnValue = new Dictionary <string, string>();
            DBList    operation = new DBList();
            ODocument document  = _connection.ExecuteOperation(operation);

            string[] databases = document.GetField <string>("databases").Split(',');
            foreach (var item in databases)
            {
                string[] keyValue = item.Split(':');
                returnValue.Add(keyValue[0], keyValue[1] + ":" + keyValue[2]);
            }
            return(returnValue);
        }
        public int GetModifiedCount()
        {
            switch (_document.GetField <PayloadStatus>("PayloadStatus"))
            {
            case PayloadStatus.SingleRecord:
                return(1);

            case PayloadStatus.RecordCollection:
                return(_document.GetField <List <ODocument> >("Content").Count);

            case PayloadStatus.SerializedResult:
                return(Convert.ToInt32(_document.GetField <object>("Content")));
            }

            return(0);
        }
Esempio n. 10
0
        public List <ODocument> Query(string sql, string fetchPlan)
        {
            CommandPayloadQuery payload = new CommandPayloadQuery();

            payload.Text         = sql;
            payload.NonTextLimit = -1;
            payload.FetchPlan    = fetchPlan;

            Command operation = new Command(GetConnection().Database);

            operation.OperationMode  = OperationMode.Asynchronous;
            operation.CommandPayload = payload;

            ODocument document = GetConnection().ExecuteOperation(operation);

            return(document.GetField <List <ODocument> >("Content"));
        }
Esempio n. 11
0
        public List <ODocument> Query(string sql, string fetchPlan)
        {
            CommandPayload payload = new CommandPayload();

            payload.Type             = CommandPayloadType.Sql;
            payload.Text             = sql;
            payload.NonTextLimit     = -1;
            payload.FetchPlan        = fetchPlan;
            payload.SerializedParams = new byte[] { 0 };

            Command operation = new Command();

            operation.OperationMode  = OperationMode.Asynchronous;
            operation.ClassType      = CommandClassType.Idempotent;
            operation.CommandPayload = payload;

            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <List <ODocument> >("Content"));
        }
Esempio n. 12
0
        public List <ODocument> Gremlin(string query)
        {
            CommandPayload payload = new CommandPayload();

            payload.Language         = "gremlin";
            payload.Type             = CommandPayloadType.Sql;
            payload.Text             = query;
            payload.NonTextLimit     = -1;
            payload.FetchPlan        = "";
            payload.SerializedParams = new byte[] { 0 };

            Command operation = new Command();

            operation.OperationMode  = OperationMode.Asynchronous;
            operation.ClassType      = CommandClassType.NonIdempotent;
            operation.CommandPayload = payload;

            ODocument document = _connection.ExecuteOperation(operation);

            return(document.GetField <List <ODocument> >("Content"));
        }
Esempio n. 13
0
        public Dictionary <string, ODatabaseInfo> Databases()
        {
            Dictionary <string, ODatabaseInfo> returnValue = new Dictionary <string, ODatabaseInfo>();
            DBList    operation = new DBList(null);
            ODocument document  = _connection.ExecuteOperation(operation);
            ODocument databases = document.GetField <ODocument>("databases");

            if (OClient.Serializer == ORecordFormat.ORecordDocument2csv)
            {
                foreach (var item in databases)
                {
                    string       databaseName = item.Key;
                    string[]     pathValue    = item.Value.ToString().Split(':');
                    var          info         = new ODatabaseInfo();
                    OStorageType storageType;

                    Enum.TryParse <OStorageType>(pathValue[0], true, out storageType);
                    info.DataBaseName = databaseName.Replace("\"", "");
                    info.StorageType  = storageType;
                    info.Path         = pathValue[1].Replace("\"", "");;
                    returnValue.Add(info.DataBaseName, info);
                }
            }
            else
            {
                foreach (var item in databases)
                {
                    var          info      = new ODatabaseInfo();
                    string[]     pathValue = item.Value.ToString().Split(':');
                    OStorageType storageType;

                    Enum.TryParse <OStorageType>(pathValue[0], true, out storageType);
                    info.DataBaseName = item.Key;
                    info.StorageType  = storageType;
                    info.Path         = item.Value.ToString();
                    returnValue.Add(info.DataBaseName, info);
                }
            }
            return(returnValue);
        }
Esempio n. 14
0
        public T GetField <T>(string fieldPath)
        {
            var type = typeof(T);

            object fieldValue;

            if (TryGetValue(fieldPath, out fieldValue))
            {
                if (fieldValue == null || fieldValue.GetType() == typeof(T))
                {
                    return((T)fieldValue);
                }

                if (fieldValue is ICollection && (fieldValue as ICollection).Count == 1)
                {
                    var enumerable = (fieldValue as IEnumerable).GetEnumerator();
                    enumerable.MoveNext();
                    if (enumerable.Current != null && enumerable.Current is T)
                    {
                        return((T)enumerable.Current);
                    }
                }

                // if value is list or set type, get element type and enumerate over its elements
                if (!type.GetTypeInfo().IsPrimitive&& ImplementsIList(type) && !type.IsArray)
                {
                    var         value       = (T)Activator.CreateInstance(type);
                    Type        elementType = type.GetGenericArguments()[0];
                    IEnumerator enumerator  = EnumerableFromField <T>(fieldValue).GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        // if current element is ODocument type which is Dictionary<string, object>
                        // map its dictionary data to element instance
                        if (enumerator.Current is ODocument)
                        {
                            var instance = Activator.CreateInstance(elementType);
                            ((ODocument)enumerator.Current).Map(ref instance);

                            ((IList)value).Add(instance);
                        }
                        else
                        {
                            try
                            {
                                ((IList)value).Add(enumerator.Current);
                            }
                            catch
                            {
                                ((IList)value).Add(Convert.ChangeType(enumerator.Current, elementType));
                            }
                        }
                    }

                    return(value);
                }

                if (type.Name == "HashSet`1")
                {
                    var value = (T)Activator.CreateInstance(type);

                    Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                    IEnumerator enumerator  = ((IEnumerable)fieldValue).GetEnumerator();

                    var addMethod = type.GetMethod("Add");

                    while (enumerator.MoveNext())
                    {
                        // if current element is ODocument type which is Dictionary<string, object>
                        // map its dictionary data to element instance
                        if (enumerator.Current is ODocument)
                        {
                            var instance = Activator.CreateInstance(elementType);
                            ((ODocument)enumerator.Current).Map(ref instance);

                            addMethod.Invoke(value, new object[] { instance });
                        }
                        else
                        {
                            addMethod.Invoke(value, new object[] { enumerator.Current });
                        }
                    }
                    return(value);
                }
                else if (type == typeof(DateTime))
                {
                    DateTime parsedValue;
                    if (DateTime.TryParse((string)fieldValue, out parsedValue))
                    {
                        return((T)(object)parsedValue);
                    }
                }
                else if (type == typeof(TimeSpan) || type == typeof(Nullable <TimeSpan>))
                {
                    if (fieldValue != null && (fieldValue.GetType() == typeof(TimeSpan) || fieldValue.GetType() == typeof(Nullable <TimeSpan>)))
                    {
                        return((T)fieldValue);
                    }
                    TimeSpan parsedValue;
                    if (TimeSpan.TryParse((string)fieldValue, out parsedValue))
                    {
                        return((T)(object)parsedValue);
                    }
                }
                else if (type == typeof(Guid))
                {
                    Guid parsedValue;
                    if (Guid.TryParse((string)fieldValue, out parsedValue))
                    {
                        return((T)(object)parsedValue);
                    }
                }
                else if (type == typeof(Decimal))
                {
                    if (fieldValue != null)
                    {
                        return((T)(object)Convert.ChangeType(fieldValue, typeof(T)));
                    }
                    else
                    {
                        return((T)(object)null);
                    }
                }

                return((T)fieldValue);
            }
            if (fieldPath.Contains("."))
            {
                ODocument target = this;
                var       fields = fieldPath.Split('.');
                for (int i = 0; i < fields.Length - 1; i++)
                {
                    target = target.GetField <ODocument>(fields[i]);
                }
                return(target.GetField <T>(fields.Last()));
            }

            var result = type.GetTypeInfo().IsPrimitive || type == typeof(string) || type.IsArray ? default(T) : (T)Activator.CreateInstance(type);

            SetField(fieldPath, result);
            return(result);
        }
Esempio n. 15
0
        public T GetField <T>(string fieldPath)
        {
            var type = typeof(T);

            object fieldValue;

            if (TryGetValue(fieldPath, out fieldValue))
            {
                if (fieldValue == null || fieldValue.GetType() == typeof(T))
                {
                    return((T)fieldValue);
                }

                // if value is list or set type, get element type and enumerate over its elements
                if (!type.IsPrimitive && ImplementsIList(type) && !type.IsArray)
                {
                    var         value       = (T)Activator.CreateInstance(type);
                    Type        elementType = type.GetGenericArguments()[0];
                    IEnumerator enumerator  = EnumerableFromField <T>(fieldValue).GetEnumerator();

                    while (enumerator.MoveNext())
                    {
                        // if current element is ODocument type which is Dictionary<string, object>
                        // map its dictionary data to element instance
                        if (enumerator.Current is ODocument)
                        {
                            var instance = Activator.CreateInstance(elementType);
                            ((ODocument)enumerator.Current).Map(ref instance);

                            ((IList)value).Add(instance);
                        }
                        else
                        {
                            ((IList)value).Add(enumerator.Current);
                        }
                    }

                    return(value);
                }

                if (type.Name == "HashSet`1")
                {
                    var value = (T)Activator.CreateInstance(type);

                    Type        elementType = ((IEnumerable)value).GetType().GetGenericArguments()[0];
                    IEnumerator enumerator  = ((IEnumerable)fieldValue).GetEnumerator();

                    var addMethod = type.GetMethod("Add");

                    while (enumerator.MoveNext())
                    {
                        // if current element is ODocument type which is Dictionary<string, object>
                        // map its dictionary data to element instance
                        if (enumerator.Current is ODocument)
                        {
                            var instance = Activator.CreateInstance(elementType);
                            ((ODocument)enumerator.Current).Map(ref instance);

                            addMethod.Invoke(value, new object[] { instance });
                        }
                        else
                        {
                            addMethod.Invoke(value, new object[] { enumerator.Current });
                        }
                    }
                    return(value);
                }

                return((T)fieldValue);
            }
            if (fieldPath.Contains("."))
            {
                ODocument target = this;
                var       fields = fieldPath.Split('.');
                for (int i = 0; i < fields.Length - 1; i++)
                {
                    target = target.GetField <ODocument>(fields[i]);
                }
                return(target.GetField <T>(fields.Last()));
            }

            var result = type.IsPrimitive || type == typeof(string) || type.IsArray ? default(T) : (T)Activator.CreateInstance(type);

            SetField(fieldPath, result);
            return(result);
        }