예제 #1
0
        public void CreateATable()
        {
            var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) };
            var table = new OracularTable ("foobar", null, null, justAnId);

            Assert.AreEqual ("foobar", table.Table);

            var fields = table.Fields.ToArray ();

            Assert.That (fields, Has.Length.EqualTo (1));
            Assert.AreEqual ("Id", fields[0].Name);
        }
예제 #2
0
        public void CreateParentRelationship()
        {
            var idAndOwner = new List<FieldConfig>
            {
                new FieldConfig("Id", null),
                new FieldConfig("OwnerId", null)
            };
            var ownerRelationship = new List<ParentConfig>
            {
                new ParentConfig("Owner", null, null)
            };
            var table = new OracularTable ("Account", null, ownerRelationship, idAndOwner);

            var parents = table.Parents.ToArray ();

            Assert.That (parents, Has.Length.EqualTo (1));
            Assert.AreEqual ("Owner", parents [0].Name);
        }
예제 #3
0
        public void DefaultParentFields()
        {
            var idAndAccountId = new List<FieldConfig>
            {
                new FieldConfig("Id", null),
                new FieldConfig("AccountId", null)
            };
            var ownerRelationship = new List<ParentConfig>
            {
                new ParentConfig("Account", null, null)
            };
            var table = new OracularTable ("Order", null, ownerRelationship, idAndAccountId);

            var parents = table.Parents.ToArray ();

            Assert.That (parents, Has.Length.EqualTo (1));

            var account = parents [0];
            Assert.AreEqual ("Account", account.Name);
            Assert.AreEqual ("AccountId", account.Id);
            Assert.AreEqual ("Account", account.Table);
        }
예제 #4
0
        public void DefaultTypeToString()
        {
            var idAndTestField = new List<FieldConfig> {
                new FieldConfig("Id", null),
                new FieldConfig("Test", null)
            };
            var table = new OracularTable ("foobar", null, null, idAndTestField);

            FieldConfig testField = null;
            foreach (var field in table.Fields)
            {
                if (field.Name == "Test")
                {
                    testField = field;
                    break;
                }
            }
            Assert.NotNull (testField);
            Assert.AreEqual (FieldType.String, testField.Type);
        }
예제 #5
0
 public abstract BuiltinExpansion ExpandMacro(OracularConfig config, OracularTable parent, OracularTable child, Reference reference, AstNode nestedSpec);
예제 #6
0
        public override BuiltinExpansion ExpandMacro(OracularConfig config, OracularTable parent, OracularTable child, Reference reference, AstNode nestedSpec)
        {
            var withTable = String.Format ("Annotated{0}{1}", parent.Table, nestedSpec.Id);

            var relationship = child.GetRelationshipTo (parent.Table);
            if (relationship == null)
            {
                var message = String.Format ("unable to find relationship from {0} to {1}", child.Table, parent.Table);
                throw new OracularException (message);
            }

            var mainJoin = String.Format ("LEFT JOIN [{0}] ON [{0}].[{1}] = [{2}].[{3}]",
                withTable,
                parent.Id,
                parent.Table,
                parent.Id
            );

            var macroField = String.Format ("{0}{1}{2}", Name, child.Table, nestedSpec.Id);

            var builder = new Sqlizer (child, config);

            var nestedWhere = nestedSpec == null ? "" : String.Format("\nWHERE {0}",
                (invertNested ? nestedSpec.Invert() : nestedSpec).Walk(builder)
            );

            var nestedJoins = builder.JoinTables.Count() == 0 ? "" : "\n" + String.Join("\n",
                builder.JoinTables
            );

            var nestedQuery = String.Format (@"[{0}] AS (
            SELECT DISTINCT [{1}].[{2}], 1 [{3}]
            FROM [{1}]
            LEFT JOIN [{4}] ON [{4}].[{5}] = [{1}].[{2}]{6}{7}
            )", withTable, parent.Table, parent.Id, macroField, child.Table, relationship.Id, nestedJoins, nestedWhere);

            var expansion = new BuiltinExpansion ();

            expansion.With.AddRange (builder.CommonTableExpressions);

            expansion.With.Add (nestedQuery);
            expansion.Join.Add (mainJoin);

            expansion.Where = String.Format ("[{0}].[{1}]{2}", withTable, macroField, suffix);

            return expansion;
        }