private static void AppendPrimaryKey <TEntity>(TEntity entity, NodeReference node)
        {
            var typeDescription = EntityTypeDescriptor.GetTypeDescription(entity.GetType());
            var identityKeys    = typeDescription.IdentityKeys;

            if (identityKeys.Any())
            {
                //according to the validation rules
                var identityKey = identityKeys.Single();

                var newSeed = IncrementIdentity(node);

                var propertyType = identityKey.ColumnInfo.PropertyType;
                propertyType = !EntityTypeDescriptor.IsNullableType(propertyType)
                    ? propertyType
                    : Nullable.GetUnderlyingType(propertyType);

                var correctValue = Convert.ChangeType(newSeed, propertyType);
                identityKey.ColumnInfo.SetValue(entity, correctValue, null);
                node.AppendSubscript(newSeed);
            }
            else
            {
                var hash = PrimaryKeyCalculator.GetPrimaryKey(entity);

                if (IsKeyExists(hash, node))
                {
                    throw new GlobalsDbException("Violation of Primary Key constraint. Cannot insert duplicate key");
                }

                node.SetSubscriptCount(0);

                node.AppendSubscript(hash);
            }
        }
        private static object ReadComplexNode(NodeReference node, Type objectType)
        {
            var isNullable = EntityTypeDescriptor.IsNullableType(objectType);

            if (isNullable && IsNullableComplexColumnEmpty(node))
            {
                return(null);
            }

            var underlyingType = !isNullable
                ? objectType
                : Nullable.GetUnderlyingType(objectType);

            var typeDescription = EntityTypeDescriptor.GetTypeDescription(underlyingType);

            var instance = InstanceCreator.CreateInstance(underlyingType);

            foreach (var column in typeDescription.Columns)
            {
                var subscript = column.ColumnAttribute.Name ?? column.ColumnInfo.Name;

                if (column.IsSimpleColumn)
                {
                    node.AppendSubscript(subscript);
                    var nodeValue = node.GetObject();
                    column.ColumnInfo.SetValue(instance, ConvertValue(nodeValue, column.ColumnInfo.PropertyType), null);
                }
                else if (column.IsArrayColumn)
                {
                    node.AppendSubscript(subscript);
                    column.ColumnInfo.SetValue(instance, ReadArray(node, column.ColumnInfo.PropertyType), null);
                }
                else if (column.IsEnumerableColumn)
                {
                    node.AppendSubscript(subscript);
                    column.ColumnInfo.SetValue(instance, ReadEnumerable(node, column.ColumnInfo.PropertyType), null);
                }
                else if (column.IsComplexColumn)
                {
                    node.AppendSubscript(subscript);
                    column.ColumnInfo.SetValue(instance,
                                               ReadComplexNode(node, column.ColumnInfo.PropertyType), null);
                }

                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            }

            return(instance);
        }
 static void ReadData(NodeReference node)
 {
     try
     {
         node.AppendSubscript("");
         string subscr = node.NextSubscript();
         while (!subscr.Equals(""))
         {
             node.SetSubscript(node.GetSubscriptCount(), subscr);
             if (node.HasData())
             {
                 Console.Write(" ".PadLeft(node.GetSubscriptCount() * 4, '-') + subscr + "  ->  ");
                 GetData(node);
             }
             if (node.HasSubnodes())
             {
                 ReadData(node);
             }
             subscr = node.NextSubscript();
         }
     }
     catch (GlobalsException ex)
     {
         Console.WriteLine(ex.Message);
     }
     finally
     {
         node.SetSubscriptCount(node.GetSubscriptCount() - 1);
     }
 }
Example #4
0
 // create a new doc object around an existing doc in the db
 internal GlDoc(GlDocSet parent_docset, Guid doc_guid)
 {
     _DocUID       = doc_guid;
     _ParentDocSet = parent_docset;
     _DocNodeRef   = GlobalsDocDB.ActiveConnection().CreateNodeReference(_ParentDocSet.Name);
     _DocNodeRef.AppendSubscript(_DocUID.ToString());
 }
        internal static void DeleteEntity <TEntity>(TEntity entity, NodeReference node, Connection connection)
        {
            try
            {
                connection.StartTransaction();

                node.AcquireLock(NodeReference.EXCLUSIVE_LOCK,
                                 NodeReference.LOCK_INCREMENTALLY,
                                 NodeReference.RELEASE_AT_TRANSACTION_END);

                var key = PrimaryKeyCalculator.GetPrimaryKey(entity);

                if (!IsKeyExists(key, node))
                {
                    throw new GlobalsDbException("Unable to delete entity. Entity with this primary key does not exist");
                }

                node.SetSubscriptCount(0);

                node.AppendSubscript(key);

                node.Kill();

                connection.Commit();
            }
            catch
            {
                connection.Rollback();
                throw;
            }
        }
        private static object ReadArray(NodeReference node, Type columnType)
        {
            var arrayLengthsString = node.GetString(EnumerableLengthsSubscriptName);

            if (string.IsNullOrEmpty(arrayLengthsString))
            {
                return(null);
            }

            var elementType  = columnType.GetElementType();
            var arrayLengths = ToArrayIndexes(arrayLengthsString);
            var instance     = Array.CreateInstance(elementType, arrayLengths);

            var subscript = node.NextSubscript(EnumerableLengthsSubscriptName);

            while (!subscript.Equals(""))
            {
                node.AppendSubscript(subscript);

                var item    = ReadNode(node, elementType);
                var indexes = ToArrayIndexes(subscript);

                instance.SetValue(item, indexes);

                subscript = node.NextSubscript();
                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            }

            return(instance);
        }
        private static object ReadEnumerable(NodeReference node, Type columnType)
        {
            var instance    = (IList)Activator.CreateInstance(columnType);
            var elementType = columnType.IsGenericType ? columnType.GetGenericArguments().Single() : typeof(object);

            if (string.IsNullOrEmpty(node.NextSubscript("")))
            {
                return(null);
            }

            var subscript = node.NextSubscript(EnumerableLengthsSubscriptName);

            while (!subscript.Equals(""))
            {
                node.AppendSubscript(subscript);

                var item = ReadNode(node, elementType);
                instance.Add(item);

                subscript = node.NextSubscript();
                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            }

            return(instance);
        }
Example #8
0
        // CONSTRUCTORS (internal). Note that GlDoc objects for existing documents
        // are created by the GlDocSet constructor. GlDoc objects for docs being
        // created by the user are created by GlDocSet.CreateNewDoc(). These two
        // GlDocSet methods call these internal constructors for GlDoc.

        // create a doc object for a brand-new doc (does not exist in DB yet)
        internal GlDoc(GlDocSet parent_docset)
        {
            _DocUID       = Guid.NewGuid();
            _ParentDocSet = parent_docset;
            _DocNodeRef   = GlobalsDocDB.ActiveConnection().CreateNodeReference(_ParentDocSet.Name);
            _DocNodeRef.AppendSubscript(_DocUID.ToString());
            _DocNodeRef.Set("GlDoc"); // dummy
        }
        private static bool IsNullableComplexColumnEmpty(NodeReference node)
        {
            node.AppendSubscript("");
            var isEmpty = string.IsNullOrEmpty(node.NextSubscript());

            node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            return(isEmpty);
        }
        private static void WriteComplexEntity <TEntity>(TEntity entity, NodeReference node)
        {
            if (entity == null)
            {
                return;
            }

            var entityType     = entity.GetType();
            var underlyingType = EntityTypeDescriptor.IsNullableType(entityType)
                ? Nullable.GetUnderlyingType(entityType)
                : entityType;

            var columns = EntityTypeDescriptor.GetTypeDescription(underlyingType).Columns;

            foreach (var column in columns)
            {
                node.AppendSubscript(column.ColumnAttribute.Name ?? column.ColumnInfo.Name);

                if (column.IsSimpleColumn)
                {
                    var value = column.ColumnInfo.GetValue(entity, null);

                    if (value != null)
                    {
                        if (column.IsNullableColumn)
                        {
                            node.Set(value != null ? value.ToString() : null);
                        }
                        else
                        {
                            node.Set(value.ToString());
                        }
                    }
                }
                else if (column.IsArrayColumn)
                {
                    var array = column.ColumnInfo.GetValue(entity, null) as Array;
                    WriteArray(node, array);
                }
                else if (column.IsEnumerableColumn)
                {
                    var items = column.ColumnInfo.GetValue(entity, null) as IEnumerable;

                    if (items != null)
                    {
                        WriteEnumerable(node, items);
                    }
                }
                else
                {
                    WriteComplexEntity(column.ColumnInfo.GetValue(entity, null), node);
                }

                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            }
        }
        private static void WriteEnumerable(NodeReference node, IEnumerable items)
        {
            var index = 0;

            node.Kill();

            foreach (var item in items)
            {
                node.AppendSubscript(ToSubscriptString(index));

                WriteItem(item, node);

                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
                index++;
            }

            node.AppendSubscript(EnumerableLengthsSubscriptName);
            node.Set(ToSubscriptString(index));
            node.SetSubscriptCount(node.GetSubscriptCount() - 1);
        }
        static void CreateFirstBranch(NodeReference node, Connection myConn)
        {
            node.AppendSubscript("111111111111");
            node.Set("Smith,John");
            node.AppendSubscript("DBO546");
            node.Set("Some bank 1");
            node.AppendSubscript(29244825509100);
            node.Set(28741.35);
            node.AppendSubscript(2145632596588547);
            string slip = "Smith,John/1965";

            byte[] bytes = System.Text.Encoding.GetEncoding(1251).GetBytes(slip);
            node.Set(bytes);
            node.AppendSubscript(1);
            ValueList myList = myConn.CreateList();

            myList.Append(0, 29244225564111, "John Doe", 500.26, "Payment for goods from ToysRUs");
            node.Set(myList);
            myList.Close();
            node.SetSubscriptCount(4);
            node.AppendSubscript(2);
            myList = myConn.CreateList();
            myList.Append(0, 26032009100100, "John Smith", 115.54, "Transfer to own account in different bank");
            node.Set(myList);
            myList.Close();
            Console.WriteLine("Info about first bank is createed");
        }
        private static void WriteArray(NodeReference node, Array array)
        {
            node.Kill();

            if (array == null)
            {
                return;
            }

            node.AppendSubscript(EnumerableLengthsSubscriptName);
            node.Set(ToSubscriptString(GetArrayLengths(array)));
            node.SetSubscriptCount(node.GetSubscriptCount() - 1);

            var indexedElements = GetIndexedElements(array, new List <int>());

            foreach (var indexedElement in indexedElements)
            {
                node.AppendSubscript(ToSubscriptString(indexedElement.Item2));
                WriteItem(indexedElement.Item1, node);
                node.SetSubscriptCount(node.GetSubscriptCount() - 1);
            }
        }
        internal static IEnumerable <NodeReference> GetEntitiesNodes(NodeReference node, DataContext context)
        {
            var result = new List <NodeReference>();

            node.AppendSubscript("");

            var subscript = node.NextSubscript();

            while (!subscript.Equals(""))
            {
                if (!string.Equals(subscript, IdentityValueSubscriptName))
                {
                    var entityNode = context.CreateNodeReference(node.GetName());
                    entityNode.AppendSubscript(subscript);
                    result.Add(entityNode);
                }
                node.SetSubscript(node.GetSubscriptCount(), subscript);
                subscript = node.NextSubscript();
            }

            return(result);
        }
        private static bool IsKeyExists(string key, NodeReference node)
        {
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }

            node.AppendSubscript(GetPreviousSubscriptKey(key));

            var subscript = node.NextSubscript();

            while (!subscript.Equals(""))
            {
                if (string.Equals(subscript, key))
                {
                    return(true);
                }
                node.SetSubscript(node.GetSubscriptCount(), subscript);
                subscript = node.NextSubscript();
            }

            return(false);
        }
 internal static void NavigateToIndex(NodeReference node, int index)
 {
     node.AppendSubscript(ToSubscriptString(index));
 }