Exemple #1
0
        private void AppendType(EdmProperty column)
        {
            TypeUsage typeUsage = column.TypeUsage;
            bool      flag      = false;
            Facet     facet;

            if (typeUsage.EdmType.Name == "binary" && 8 == typeUsage.GetMaxLength() && (column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out facet) && facet.Value != null) && StoreGeneratedPattern.Computed == (StoreGeneratedPattern)facet.Value)
            {
                flag = true;
                this.AppendIdentifier("rowversion");
            }
            else
            {
                string name = typeUsage.EdmType.Name;
                if (typeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType && name.EndsWith("(max)", StringComparison.Ordinal))
                {
                    this.AppendIdentifier(name.Substring(0, name.Length - "(max)".Length));
                    this.AppendSql("(max)");
                }
                else
                {
                    this.AppendIdentifier(name);
                }
                switch (typeUsage.EdmType.Name)
                {
                case "decimal":
                case "numeric":
                    this.AppendSqlInvariantFormat("({0}, {1})", (object)typeUsage.GetPrecision(), (object)typeUsage.GetScale());
                    break;

                case "datetime2":
                case "datetimeoffset":
                case "time":
                    this.AppendSqlInvariantFormat("({0})", (object)typeUsage.GetPrecision());
                    break;

                case "binary":
                case "varbinary":
                case "nvarchar":
                case "varchar":
                case "char":
                case "nchar":
                    this.AppendSqlInvariantFormat("({0})", (object)typeUsage.GetMaxLength());
                    break;
                }
            }
            this.AppendSql(column.Nullable ? " null" : " not null");
            if (flag || !column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out facet) || (facet.Value == null || (StoreGeneratedPattern)facet.Value != StoreGeneratedPattern.Identity))
            {
                return;
            }
            if (typeUsage.EdmType.Name == "uniqueidentifier")
            {
                this.AppendSql(" default newid()");
            }
            else
            {
                this.AppendSql(" identity");
            }
        }
Exemple #2
0
        public void AppendType(TypeUsage typeUsage, bool isNullable, bool isIdentity)
        {
            bool isTimestamp = false;

            JetDataTypeAlias alias;

            JetDataTypeAliasCollection.Instance.TryGetValue(typeUsage.EdmType.Name, out alias);

            if (alias == null)
            {
                throw new NotSupportedException(string.Format("Type {0} unsupported", typeUsage.EdmType.Name));
            }

            string jetTypeName = alias.Alias;
            string jetLength   = "";


            switch (jetTypeName)
            {
            case "decimal":
            case "numeric":
                jetLength = string.Format(System.Globalization.CultureInfo.InvariantCulture, "({0}, {1})", typeUsage.GetPrecision(), typeUsage.GetScale());
                break;

            case "binary":
            case "varbinary":
            case "varchar":
            case "char":
                jetLength = string.Format("({0})", typeUsage.GetMaxLength());
                break;

            default:
                break;
            }

            AppendSql(jetTypeName);
            AppendSql(jetLength);
            AppendSql(isNullable ? " null" : " not null");

            if (isTimestamp)
            {
                ;// nothing to generate for identity
            }
            else if (isIdentity && jetTypeName == "guid")
            {
                AppendSql(" default GenGUID()");
            }
            //AppendSql(" counter");
            //AppendSql(" autoincrement");
            else if (isIdentity)
            {
                AppendSql(" identity(1,1)");
            }
        }
Exemple #3
0
        public void AppendType(TypeUsage typeUsage, bool isNullable, bool isIdentity)
        {
            bool isTimestamp = false;

            string sqliteTypeName = typeUsage.EdmType.Name;
            string sqliteLength   = "";


            switch (sqliteTypeName)
            {
            case "decimal":
            case "numeric":
                sqliteLength = string.Format(System.Globalization.CultureInfo.InvariantCulture, "({0}, {1})", typeUsage.GetPrecision(), typeUsage.GetScale());
                break;

            case "binary":
            case "varbinary":
            case "varchar":
            case "nvarchar":
            case "char":
            case "nchar":
                sqliteLength = string.Format("({0})", typeUsage.GetMaxLength());
                break;

            default:
                break;
            }

            AppendSql(sqliteTypeName);
            AppendSql(sqliteLength);
            AppendSql(isNullable ? " null" : " not null");

            if (isTimestamp)
            {
                ;// nothing to generate for identity
            }
            else if (isIdentity && sqliteTypeName == "guid")
            {
                AppendSql(" default GenGUID()");
            }
            else if (sqliteTypeName == "bit")
            {
                AppendSql($" DEFAULT(0)");
            }
            else if (isIdentity)
            {
                AppendSql(" constraint primary key autoincrement");
            }
        }
        public void AppendType(TypeUsage typeUsage, bool isNullable, bool isIdentity)
        {
            bool isTimestamp = false;

            string db2TypeName = typeUsage.EdmType.Name;
            string db2Length   = "";


            switch (db2TypeName)
            {
            case "decimal":
            case "numeric":
                db2Length = string.Format(System.Globalization.CultureInfo.InvariantCulture, "({0}, {1})", typeUsage.GetPrecision(), typeUsage.GetScale());
                break;

            case "binary":
            case "varbinary":
            case "varchar":
            case "char":
                db2Length = string.Format("({0})", typeUsage.GetMaxLength());
                break;

            default:
                break;
            }

            AppendSql(db2TypeName);
            AppendSql(db2Length);
            AppendSql(isNullable ? " null" : " not null");

            if (isTimestamp)
#pragma warning disable 642
            {
                ;// nothing to generate for identity
            }
#pragma warning restore 642
            else if (isIdentity && db2TypeName == "guid")
            {
                AppendSql(" default GenGUID()");
            }
            else if (isIdentity)
            {
                AppendSql(" GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1)");
            }
        }
Exemple #5
0
        private void AppendType(EdmProperty column)
        {
            TypeUsage type = column.TypeUsage;

            // check for rowversion-like configurations
            Facet storeGenFacet;
            bool  isTimestamp = false;

            if (type.EdmType.Name == "binary" &&
                8 == type.GetMaxLength() &&
                column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out storeGenFacet) &&
                storeGenFacet.Value != null &&
                StoreGeneratedPattern.Computed == (StoreGeneratedPattern)storeGenFacet.Value)
            {
                isTimestamp = true;
                AppendIdentifier("rowversion");
            }
            else
            {
                string typeName = type.EdmType.Name;
                // Special case: the EDM treats 'nvarchar(max)' as a type name, but SQL Server treats
                // it as a type 'nvarchar' and a type qualifier. As such, we can't escape the entire
                // type name as the EDM sees it.
                const string maxSuffix = "(max)";
                if (type.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType && typeName.EndsWith(maxSuffix, StringComparison.Ordinal))
                {
                    //Debug.Assert(new[] { "nvarchar(max)", "varchar(max)", "varbinary(max)" }.Contains(typeName),
                    //    "no other known SQL Server primitive types types accept (max)");
                    AppendIdentifier(typeName.Substring(0, typeName.Length - maxSuffix.Length));
                    AppendSql("(max)");
                }
                else
                {
                    AppendIdentifier(typeName);
                }
                switch (type.EdmType.Name)
                {
                case "decimal":
                case "numeric":
                    AppendSqlInvariantFormat("({0}, {1})", type.GetPrecision(), type.GetScale());
                    break;

                case "datetime2":
                case "datetimeoffset":
                case "time":
                    AppendSqlInvariantFormat("({0})", type.GetPrecision());
                    break;

                case "binary":
                case "varbinary":
                case "nvarchar":
                case "varchar":
                case "char":
                case "nchar":
                    AppendSqlInvariantFormat("({0})", type.GetMaxLength());
                    break;

                default:
                    break;
                }
            }
            AppendSql(column.Nullable ? " null" : " not null");

            if (!isTimestamp && column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out storeGenFacet) &&
                storeGenFacet.Value != null)
            {
                StoreGeneratedPattern storeGenPattern = (StoreGeneratedPattern)storeGenFacet.Value;
                if (storeGenPattern == StoreGeneratedPattern.Identity)
                {
                    if (type.EdmType.Name == "uniqueidentifier")
                    {
                        AppendSql(" default newid()");
                    }
                    else
                    {
                        AppendSql(" identity");
                    }
                }
            }
        }
        private void AppendType(EdmProperty column, string providerManifestToken)
        {
            TypeUsage typeUsage = column.TypeUsage;
            bool      flag      = false;
            Facet     facet;

            if (typeUsage.EdmType.Name == "binary" && 8 == typeUsage.GetMaxLength() && (column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out facet) && facet.Value != null) && StoreGeneratedPattern.Computed == (StoreGeneratedPattern)facet.Value)
            {
                flag = true;
                this.AppendIdentifier("rowversion");
            }
            else
            {
                string name = typeUsage.EdmType.Name;
                if (name.ToLowerInvariant() == "guid")
                {
                    this.AppendSql("raw(16)");
                }
                else if (name.ToLowerInvariant() == "pl/sql boolean")
                {
                    this.AppendSql("number(1, 0)");
                }
                else
                {
                    this.AppendSql(name);
                }
                switch (typeUsage.EdmType.Name)
                {
                case "number":
                    this.AppendSqlInvariantFormat("({0}, {1})", (object)typeUsage.GetPrecision(), (object)typeUsage.GetScale());
                    break;

                case "float":
                    this.AppendSqlInvariantFormat("({0})", (object)typeUsage.GetPrecision());
                    break;

                case "char":
                case "nchar":
                case "varchar2":
                case "nvarchar2":
                case "raw":
                    this.AppendSqlInvariantFormat("({0})", (object)typeUsage.GetMaxLength());
                    break;
                }
            }
            if (!flag && column.TypeUsage.Facets.TryGetValue("StoreGeneratedPattern", false, out facet) && facet.Value != null)
            {
                switch ((StoreGeneratedPattern)facet.Value)
                {
                case StoreGeneratedPattern.Identity:
                    if (EFOracleVersionUtils.GetStorageVersion(providerManifestToken) >= EFOracleVersion.Oracle12cR1)
                    {
                        this.AppendSql(" generated by default as identity");
                        break;
                    }
                    EFOracleDdlBuilder.m_bCreateSequenceAndTrigger = true;
                    break;

                case StoreGeneratedPattern.Computed:
                    EFOracleDdlBuilder.m_bCreateSequenceAndTrigger      = true;
                    EFOracleDdlBuilder.m_bStoreGeneratedPatternComputed = true;
                    break;
                }
            }
            this.AppendSql(column.Nullable ? " null" : " not null");
        }
        /// <summary>
        /// This method takes a type and a set of facets and returns the best mapped equivalent type
        /// in Jet
        /// </summary>
        /// <param name="storeType">A TypeUsage encapsulating an EDM type and a set of facets</param>
        /// <returns>A TypeUsage encapsulating a store type and a set of facets</returns>
        public override TypeUsage GetStoreType(TypeUsage edmType)
        {
            if (edmType == null)
            {
                throw new ArgumentNullException("edmType");
            }

            System.Diagnostics.Debug.Assert(edmType.EdmType.BuiltInTypeKind == BuiltInTypeKind.PrimitiveType);

            PrimitiveType primitiveType = edmType.EdmType as PrimitiveType;

            if (primitiveType == null)
            {
                throw new ArgumentException(String.Format("The underlying provider does not support the type '{0}'.", edmType));
            }

            ReadOnlyMetadataCollection <Facet> facets = edmType.Facets;

            switch (primitiveType.PrimitiveTypeKind)
            {
            case PrimitiveTypeKind.Boolean:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["bit"]));

            case PrimitiveTypeKind.Byte:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["tinyint"]));

            case PrimitiveTypeKind.Int16:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["smallint"]));

            case PrimitiveTypeKind.Int32:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Int64:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["int"]));

            case PrimitiveTypeKind.Guid:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["guid"]));

            case PrimitiveTypeKind.Double:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["float"]));

            case PrimitiveTypeKind.Single:
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["real"]));

            case PrimitiveTypeKind.Decimal:     // decimal, numeric, smallmoney, money
            {
                byte precision;
                if (!edmType.TryGetPrecision(out precision))
                {
                    precision = 18;
                }

                byte scale;
                if (!edmType.TryGetScale(out scale))
                {
                    scale = 0;
                }

                return(TypeUsage.CreateDecimalTypeUsage(StoreTypeNameToStorePrimitiveType["decimal"], precision, scale));
            }

            case PrimitiveTypeKind.Binary:     // binary, varbinary, image
            {
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > BINARY_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;
                if (isFixedLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["binary"], true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["image"], false);
                    System.Diagnostics.Debug.Assert(tu.Facets["MaxLength"].Description.IsConstant, "varbinary(max) is not constant!");
                }
                else
                {
                    tu = TypeUsage.CreateBinaryTypeUsage(StoreTypeNameToStorePrimitiveType["varbinary"], false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.String:
                // char, varchar, text
            {
                bool isUnicode     = edmType.GetIsUnicode();     // We do not handle unicode (everything's unicode in Jet)
                bool isFixedLength = edmType.GetIsFixedLength();
                bool isMaxLength   = edmType.GetMaxLength() > VARCHAR_MAXSIZE;
                int  maxLength     = edmType.GetMaxLength();

                TypeUsage tu;

                if (isFixedLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["char"], false, true, maxLength);
                }
                else if (isMaxLength)
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["text"], false, false);
                }
                else
                {
                    tu = TypeUsage.CreateStringTypeUsage(StoreTypeNameToStorePrimitiveType["varchar"], false, false, maxLength);
                }

                return(tu);
            }

            case PrimitiveTypeKind.DateTime:     // datetime
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["datetime"]));

            case PrimitiveTypeKind.Time:     // time
                return(TypeUsage.CreateDefaultTypeUsage(StoreTypeNameToStorePrimitiveType["time"]));

            default:
                throw new NotSupportedException(String.Format("There is no store type corresponding to the EDM type '{0}' of primitive type '{1}'.", edmType, primitiveType.PrimitiveTypeKind));
            }
        }