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"); }
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); } }
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); }
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 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); }
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 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); }
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); } }
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 CreateSecondBranch(NodeReference node, Connection myConn) { node.SetSubscriptCount(1); node.Set("Some bank 2", "DXO987"); node.Set(65241.24, "DXO987", 26032009100100); string slip = "Smith John||1965"; byte[] bytes = System.Text.Encoding.GetEncoding(1251).GetBytes(slip); node.Set(bytes, "DXO987", 26032009100100, 6541963285249512); ValueList myList = myConn.CreateList(); myList.Append(1, 29242664509184, "Jane Doe", 500.26, "Recurring payment to Amazon"); node.Set(myList, "DXO987", 26032009100100, 6541963285249512, 1); myList.Close(); myList = myConn.CreateList(); myList.Append(0, 26548962495545, "John Doe", 1015.10, "Payment for delivery"); node.Set(myList, "DXO987", 26032009100100, 6541963285249512, 2); myList.Close(); Console.WriteLine("Info about second bank is createed"); }
static void Main(string[] args) { try { Connection myConn1 = Connect(); NodeReference nodeRef = myConn1.CreateNodeReference("CardInfo"); CreateFirstBranch(nodeRef, myConn1); CreateSecondBranch(nodeRef, myConn1); CreateThirdBranch(nodeRef, myConn1); nodeRef.SetSubscriptCount(0); ReadData(nodeRef); Disconnect(myConn1); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }