Ejemplo n.º 1
0
        public void SqlScope_GetTest()
        {
            var sut = new SqlScope();

            sut.Add(SqlName.Parse("a", ObjectLevel.Unknown), "42");
            Assert.Equal("42", sut.ResolveObject(SqlName.Parse("a", ObjectLevel.Unknown), null));
        }
Ejemplo n.º 2
0
 public void SqlName_EqualsTest()
 {
     Assert.True(SqlName.Parse("a.b", ObjectLevel.Child).Equals(SqlName.Parse("a.b", ObjectLevel.Child)));
     Assert.False(SqlName.Parse("a", ObjectLevel.Child).Equals(SqlName.Parse("b", ObjectLevel.Child)));
     Assert.False(SqlName.Parse("a.b", ObjectLevel.Child).Equals(SqlName.Parse("b", ObjectLevel.Child)));
     Assert.False(SqlName.Parse("a", ObjectLevel.Child).Equals(null));
 }
Ejemplo n.º 3
0
        public void SqlScope_AddChildScopeTest()
        {
            var sut = new SqlScope();
            var a   = sut.CreateChildScope();
            var b   = a.CreateChildScope();
            var c   = b.CreateChildScope();
            var d   = b.CreateChildScope();

            a.Add(SqlName.Parse("a", ObjectLevel.Local), "1");
            b.Add(SqlName.Parse("b", ObjectLevel.Local), "2");
            c.Add(SqlName.Parse("c", ObjectLevel.Local), "3");
            d.Add(SqlName.Parse("d", ObjectLevel.Local), "4");
            Assert.Equal("1", a.ResolveObject(SqlName.Parse("a", ObjectLevel.Local), null));
            Assert.Null(a.ResolveObject(SqlName.Parse("b", ObjectLevel.Local), null));
            Assert.Null(a.ResolveObject(SqlName.Parse("c", ObjectLevel.Local), null));
            Assert.Null(a.ResolveObject(SqlName.Parse("d", ObjectLevel.Local), null));

            Assert.Equal("1", b.ResolveObject(SqlName.Parse("a", ObjectLevel.Local), null));
            Assert.Equal("2", b.ResolveObject(SqlName.Parse("b", ObjectLevel.Local), null));
            Assert.Null(b.ResolveObject(SqlName.Parse("c", ObjectLevel.Local), null));
            Assert.Null(b.ResolveObject(SqlName.Parse("d", ObjectLevel.Local), null));

            Assert.Equal("1", c.ResolveObject(SqlName.Parse("a", ObjectLevel.Local), null));
            Assert.Equal("2", c.ResolveObject(SqlName.Parse("b", ObjectLevel.Local), null));
            Assert.Equal("3", c.ResolveObject(SqlName.Parse("c", ObjectLevel.Local), null));
            Assert.Null(c.ResolveObject(SqlName.Parse("d", ObjectLevel.Local), null));

            Assert.Equal("1", d.ResolveObject(SqlName.Parse("a", ObjectLevel.Local), null));
            Assert.Equal("2", d.ResolveObject(SqlName.Parse("b", ObjectLevel.Local), null));
            Assert.Null(d.ResolveObject(SqlName.Parse("c", ObjectLevel.Local), null));
            Assert.Equal("4", d.ResolveObject(SqlName.Parse("d", ObjectLevel.Local), null));
        }
Ejemplo n.º 4
0
        public void SqlName_ParseTest()
        {
            Assert.Equal("a", SqlName.Parse("a", ObjectLevel.Child).GetQFullName(null, 0));
            Assert.Equal("a.b", SqlName.Parse("a.b", ObjectLevel.Child).GetQFullName(null, 0));
            Assert.Equal("a.b.c", SqlName.Parse("a.b.c", ObjectLevel.Child).GetQFullName(null, 0));
            Assert.Equal("a.b.c.d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 0));
            Assert.Equal("a.b.c.d.e", SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).GetQFullName(null, 0));

            Assert.Equal(ObjectLevel.Server, SqlName.Parse("a", ObjectLevel.Server).ObjectLevel);

            Assert.Equal(ObjectLevel.Database, SqlName.Parse("a.b", ObjectLevel.Database).ObjectLevel);
            Assert.Equal(ObjectLevel.Server, SqlName.Parse("a.b", ObjectLevel.Database).Parent.ObjectLevel);

            Assert.Equal(ObjectLevel.Schema, SqlName.Parse("a.b.c", ObjectLevel.Schema).ObjectLevel);
            Assert.Equal(ObjectLevel.Database, SqlName.Parse("a.b.c", ObjectLevel.Schema).Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Server, SqlName.Parse("a.b.c", ObjectLevel.Schema).Parent.Parent.ObjectLevel);

            Assert.Equal(ObjectLevel.Object, SqlName.Parse("a.b.c.d", ObjectLevel.Object).ObjectLevel);
            Assert.Equal(ObjectLevel.Schema, SqlName.Parse("a.b.c.d", ObjectLevel.Object).Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Database, SqlName.Parse("a.b.c.d", ObjectLevel.Object).Parent.Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Server, SqlName.Parse("a.b.c.d", ObjectLevel.Object).Parent.Parent.Parent.ObjectLevel);

            Assert.Equal(ObjectLevel.Child, SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).ObjectLevel);
            Assert.Equal(ObjectLevel.Object, SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Schema, SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).Parent.Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Database, SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).Parent.Parent.Parent.ObjectLevel);
            Assert.Equal(ObjectLevel.Server, SqlName.Parse("a.b.c.d.e", ObjectLevel.Child).Parent.Parent.Parent.Parent.ObjectLevel);
        }
Ejemplo n.º 5
0
        public void SqlScope_ResolveTest()
        {
            var sut = new SqlScope(null);
            var a   = sut.CreateChildScope();

            sut.Add(SqlName.Parse("a", ObjectLevel.Unknown), "42");
            Assert.Equal("42", a.ResolveObject(SqlName.Parse("a", ObjectLevel.Local), null));
        }
Ejemplo n.º 6
0
        public void SqlName_ChildWellkownTest()
        {
            var a = new SqlName(null, "a", ObjectLevel.Database);

            Assert.Equal("a.b.c", a.ChildWellkown("b").ChildWellkown("c").GetQFullName(null, 0));
            Assert.True(ReferenceEquals(a.ChildWellkown("b").ChildWellkown("c"), a.ChildWellkown("b").ChildWellkown("c")));
            Assert.False(ReferenceEquals(a.ChildWellkown("b").Child("c"), a.ChildWellkown("b").ChildWellkown("c")));
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Resolve the name.
 /// </summary>
 /// <param name="name">the name to find the item thats called name</param>
 /// <param name="context">the resolver context.</param>
 /// <returns>the named object or null.</returns>
 public virtual object ResolveObject(SqlName name, IScopeNameResolverContext context)
 {
     for (int idx = 0; idx < this.Columns.Count; idx++)
     {
         var column = this.Columns[idx];
         if (column.Name.Equals(name))
         {
             return(column);
         }
     }
     return(null);
 }
Ejemplo n.º 8
0
        public void SqlName_GetQNameTest()
        {
            Assert.Equal("d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQName(null));
            Assert.Equal("c", SqlName.Parse("a.b.c", ObjectLevel.Child).GetQName(null));
            Assert.Equal("b", SqlName.Parse("a.b", ObjectLevel.Child).GetQName(null));
            Assert.Equal("a", SqlName.Parse("a", ObjectLevel.Child).GetQName(null));

            Assert.Equal("[d]", SqlName.Parse("[d]", ObjectLevel.Child).GetQName("["));
            Assert.Equal("[c]", SqlName.Parse("[c]", ObjectLevel.Child).GetQName("["));
            Assert.Equal("[b]", SqlName.Parse("[b]", ObjectLevel.Child).GetQName("["));
            Assert.Equal("[a]", SqlName.Parse("[a]", ObjectLevel.Child).GetQName("["));
        }
Ejemplo n.º 9
0
        public void SqlName_GetQFullNameTest()
        {
            Assert.Equal("d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 1));
            Assert.Equal("c", SqlName.Parse("a.b.c", ObjectLevel.Child).GetQFullName(null, 1));
            Assert.Equal("b", SqlName.Parse("a.b", ObjectLevel.Child).GetQFullName(null, 1));
            Assert.Equal("a", SqlName.Parse("a", ObjectLevel.Child).GetQFullName(null, 1));

            Assert.Equal("d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 1));
            Assert.Equal("c.d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 2));
            Assert.Equal("b.c.d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 3));
            Assert.Equal("a.b.c.d", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName(null, 4));

            Assert.Equal("[d]", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName("[", 1));
            Assert.Equal("[c].[d]", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName("[", 2));
            Assert.Equal("[b].[c].[d]", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName("[", 3));
            Assert.Equal("[a].[b].[c].[d]", SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetQFullName("[", 4));
        }
Ejemplo n.º 10
0
 public static ModelSystemDataType ConvertFromSqlName(SqlName name)
 {
     if (_ConvertFromSqlName is null)
     {
         var d = new Dictionary <string, ModelSystemDataType>(StringComparer.OrdinalIgnoreCase);
         d.Add("BigInt", ModelSystemDataType.BigInt);
         d.Add("Binary", ModelSystemDataType.Binary);
         d.Add("Bit", ModelSystemDataType.Bit);
         d.Add("Char", ModelSystemDataType.Char);
         d.Add("Cursor", ModelSystemDataType.Cursor);
         d.Add("Date", ModelSystemDataType.Date);
         d.Add("DateTime", ModelSystemDataType.DateTime);
         d.Add("DateTime2", ModelSystemDataType.DateTime2);
         d.Add("DateTimeOffset", ModelSystemDataType.DateTimeOffset);
         d.Add("Decimal", ModelSystemDataType.Decimal);
         d.Add("Float", ModelSystemDataType.Float);
         d.Add("Image", ModelSystemDataType.Image);
         d.Add("Int", ModelSystemDataType.Int);
         d.Add("Money", ModelSystemDataType.Money);
         d.Add("NChar", ModelSystemDataType.NChar);
         d.Add("NText", ModelSystemDataType.NText);
         d.Add("Numeric", ModelSystemDataType.Numeric);
         d.Add("NVarChar", ModelSystemDataType.NVarChar);
         d.Add("Real", ModelSystemDataType.Real);
         d.Add("Rowversion", ModelSystemDataType.Rowversion);
         d.Add("SmallDateTime", ModelSystemDataType.SmallDateTime);
         d.Add("SmallInt", ModelSystemDataType.SmallInt);
         d.Add("SmallMoney", ModelSystemDataType.SmallMoney);
         d.Add("Sql_Variant", ModelSystemDataType.Sql_Variant);
         d.Add("Table", ModelSystemDataType.Table);
         d.Add("Text", ModelSystemDataType.Text);
         d.Add("Time", ModelSystemDataType.Time);
         d.Add("Timestamp", ModelSystemDataType.Timestamp);
         d.Add("TinyInt", ModelSystemDataType.TinyInt);
         d.Add("UniqueIdentifier", ModelSystemDataType.UniqueIdentifier);
         d.Add("VarBinary", ModelSystemDataType.VarBinary);
         d.Add("VarChar", ModelSystemDataType.VarChar);
         _ConvertFromSqlName = d;
     }
     if (string.Equals(name.Parent.Name, "sys", StringComparison.OrdinalIgnoreCase))
     {
         return(_ConvertFromSqlName.GetValueOrDefault(name.Name, ModelSystemDataType.None));
     }
     return(ModelSystemDataType.None);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Resolve the name.
 /// </summary>
 /// <param name="name">the name to find the item thats called name</param>
 /// <param name="context">the resolver context.</param>
 /// <returns>the named object or null.</returns>
 public object ResolveObject(SqlName name, IScopeNameResolverContext context)
 {
     for (var that = this; (that != null); that = that.Parent)
     {
         {
             var result = that.ChildElements.GetValueOrDefault(name);
             if ((object)result != null)
             {
                 return(result);
             }
         }
         if ((object)that._NameResolver != null)
         {
             var result = that._NameResolver.ResolveObject(name, context);
             if ((object)result != null)
             {
                 return(result);
             }
         }
     }
     return(null);
 }
Ejemplo n.º 12
0
        public void ModelSqlDatabase_ResolveTest()
        {
            var sut = GetTestModelSqlDatabase();
            {
                var act = sut.ResolveObject(SqlName.Schema("dbo"), null);
                Assert.NotNull(act);
                Assert.IsType <ModelSqlSchema>(act);
                Assert.Equal("dbo", (act as ModelSqlSchema).Name.Name);
            }
            {
                var act = sut.ResolveObject(SqlName.Object("dbo", "name"), null);
                Assert.NotNull(act);
                Assert.IsType <ModelSqlTable>(act);
                Assert.Equal("dbo.name", (act as ModelSqlTable).Name.GetQFullName(null, 2));
            }

            /*
             * {
             * var act = sut.GetObject(SqlName.Root.Child("OfaSchlupfer"));
             * Assert.NotNull(act);
             * }
             */
        }
Ejemplo n.º 13
0
        public static string GetCondensed(SqlName name, ModelSystemDataType systemDataType, short?maxLength, byte?scale, byte?precision)
        {
            // TODO: this.Scale
            switch (systemDataType)
            {
            case ModelSystemDataType.None:
                break;

            case ModelSystemDataType.BigInt:
                return("bigint");

            case ModelSystemDataType.Int:
                return("int");

            case ModelSystemDataType.SmallInt:
                return("smallInt");

            case ModelSystemDataType.TinyInt:
                return("tinyInt");

            case ModelSystemDataType.Bit:
                return("bit");

            case ModelSystemDataType.Decimal:
                return("decimal");

            case ModelSystemDataType.Numeric:
                return("numeric");

            case ModelSystemDataType.Money:
                return("money");

            case ModelSystemDataType.SmallMoney:
                return("smallmoney");

            case ModelSystemDataType.Float:
                return("float");

            case ModelSystemDataType.Real:
                return("real");

            case ModelSystemDataType.DateTime:
                return("datetime");

            case ModelSystemDataType.SmallDateTime:
                return("smalldatetime");

            case ModelSystemDataType.Char:
                return("char(" + (maxLength.GetValueOrDefault().ToString()) + ")");

            case ModelSystemDataType.VarChar:
                return("varchar(" + (((maxLength.GetValueOrDefault(-1) <= 0) || (maxLength.GetValueOrDefault(-1) >= 8000)) ? "max" : maxLength.Value.ToString()) + ")");

            case ModelSystemDataType.Text:
                return("text");

            case ModelSystemDataType.NChar:
                return("nchar(" + (maxLength.GetValueOrDefault().ToString()) + ")");

            case ModelSystemDataType.NVarChar:
                return("nvarchar(" + (((maxLength.GetValueOrDefault(-1) <= 0) || (maxLength.GetValueOrDefault(-1) >= 8000)) ? "max" : maxLength.Value.ToString()) + ")");

            case ModelSystemDataType.NText:
                return("ntext");

            case ModelSystemDataType.Binary:
                return("binary");

            case ModelSystemDataType.VarBinary:
                return("varbinary");

            case ModelSystemDataType.Image:
                return("image");

            case ModelSystemDataType.Cursor:
                return("cursor");

            case ModelSystemDataType.Sql_Variant:
                return("sql_variant");

            case ModelSystemDataType.Table:
                return("table");    // ??

            case ModelSystemDataType.Timestamp:
                return("timestamp");

            case ModelSystemDataType.UniqueIdentifier:
                return("uniqueidentifier");

            case ModelSystemDataType.Date:
                return("date");

            case ModelSystemDataType.Time:
                return("time");

            case ModelSystemDataType.DateTime2:
                return("datetime2");

            case ModelSystemDataType.DateTimeOffset:
                return("datetimeoffset");

            case ModelSystemDataType.Rowversion:
                return("rowversion");

            default:
                break;
            }
            return(name.GetQFullName("[", 2));
        }
Ejemplo n.º 14
0
        public static ModelSqlTable Ensure(ModelSqlDatabase database, SqlName name)
        {
            var schema = ModelSqlSchema.Ensure(database, name.Parent?.Name ?? "dbo");

            return(Ensure(schema, name.Name));
        }
 // default schema?
 // handle use?
 public object Resolve(SqlName name)
 {
     // TODO: more magic needed here...
     return(this.ModelDatabase.ResolveObject(name, this));
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Add the item
 /// </summary>
 /// <param name="name">the name</param>
 /// <param name="namedElement">the item</param>
 public void Add(SqlName name, object namedElement)
 {
     this.ChildElements[name] = namedElement;
 }
Ejemplo n.º 17
0
 public void SqlName_EqualsTest1()
 {
     Assert.True(SqlName.Parse("a.b.c.d", ObjectLevel.Unknown).Equals(SqlName.Parse("a.b.c.d", ObjectLevel.Unknown)));
 }
Ejemplo n.º 18
0
 public void SqlName_IsRootTest()
 {
     Assert.True(SqlName.Parse("a", ObjectLevel.Child).Parent.IsRoot());
     Assert.True(SqlName.Parse("a", ObjectLevel.Child).IsRoot());
     Assert.False(SqlName.Parse("a.b", ObjectLevel.Child).IsRoot());
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ModelSqlSchemaChild"/> class.
 /// </summary>
 /// <param name="src">the copy source.</param>
 public ModelSqlSchemaChild(ModelSqlSchemaChild src)
 {
     this._Name = src._Name;
 }
Ejemplo n.º 20
0
 public void SqlName_GetHashCodeTest()
 {
     Assert.Equal(SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetHashCode(), SqlName.Parse("A.B.C.D", ObjectLevel.Child).GetHashCode());
     Assert.NotEqual(SqlName.Parse("a.b.c.d", ObjectLevel.Child).GetHashCode(), SqlName.Parse("A.B.C.DE", ObjectLevel.Child).GetHashCode());
 }
Ejemplo n.º 21
0
 public object ResolveColumn(SqlName name)
 {
     return(null);
 }
Ejemplo n.º 22
0
 public object ResolveObject(SqlName name)
 {
     return(null);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ModelSqlSynonym"/> class.
 /// Copy constructor
 /// </summary>
 /// <param name="src">instance to copy.</param>
 public ModelSqlSynonym(ModelSqlSynonym src)
 {
     this._Name = src._Name;
     this._For  = src._For;
 }
Ejemplo n.º 24
0
        /// <summary>
        /// Resolve the name.
        /// </summary>
        /// <param name="name">the name to find the item thats called name</param>
        /// <param name="context">the resolver context.</param>
        /// <returns>the named object or null.</returns>
        public object ResolveObject(SqlName name, IScopeNameResolverContext context)
        {
            ModelSqlDatabase database = null;

            {
                var nameA = name.GetAncestorAtLevel(ObjectLevel.Database);
                if ((object)nameA != null)
                {
                    if ((nameA.LevelCount == 1) && (nameA.ObjectLevel == ObjectLevel.Database))
                    {
                        database = this._Database.GetValueOrDefault(nameA);
                    }
                }
                else if ((nameA.LevelCount == 2) && (nameA.ObjectLevel == ObjectLevel.Database))
                {
                    if (SqlNameEqualityComparer.Level1.Equals(this.Name, nameA.Parent /* Server */))
                    {
                        database = this._Database.GetValueOrDefault(nameA);
                    }
                    else
                    {
                        return(null);
                    }
                }
                if (ReferenceEquals(nameA, name))
                {
                    return(database);
                }
                if ((object)database != null)
                {
                    return(database.ResolveObject(name, context));
                }
            }
#if false
            {
                if ((name.Level == 1) && (name.ObjectLevel == ObjectLevel.Database))
                {
                    return(this._Database.GetValueOrDefault(name));
                }
                if ((name.Level == 1) && (name.ObjectLevel == ObjectLevel.Unknown))
                {
                    var result = this._Database.GetValueOrDefault(name);
                    if ((object)result != null)
                    {
                        return(result);
                    }
                }
                if ((name.Level == 2) && (name.ObjectLevel == ObjectLevel.Database))
                {
                    if (SqlNameEqualityComparer.Level1.Equals(this.Name, name.Parent))
                    {
                        var result = this._Database.GetValueOrDefault(name);
                        if ((object)result != null)
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                if ((name.Level == 2) && (name.ObjectLevel == ObjectLevel.Unknown))
                {
                    if (SqlNameEqualityComparer.Level1.Equals(this.Name, name.Parent))
                    {
                        var result = this._Database.GetValueOrDefault(name);
                        if ((object)result != null)
                        {
                            return(result);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
#warning TODO check if name has 3 parts - than use the 3rd and go ahead with 2
            {
                var schemaName = name.Parent;
                var dbName     = schemaName?.Parent;
                if (!ReferenceEquals(dbName, null) && ReferenceEquals(dbName?.Parent, SqlName.Root))
                {
                    var result = this._Database.GetValueOrDefault(dbName);
                    if ((object)result != null)
                    {
                        return(result);
                    }
                }

                var serverName = dbName?.Parent;
                if (!ReferenceEquals(serverName, null) && ReferenceEquals(serverName?.Parent, SqlName.Root))
                {
                    if (serverName.Name == this.Name.Name)
                    {
                        var result = this._Database.GetValueOrDefault(serverName);
                        if ((object)result != null)
                        {
                            return(result);
                        }
                    }
                }
            }
#endif
            return(null);
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Gets the type by its's name
 /// </summary>
 /// <param name="name">the schema name to search for</param>
 /// <returns>the found schema or null.</returns>
 public ModelSqlDatabase GetTypeByName(SqlName name) => this._Database.GetValueOrDefault(name);