Exemple #1
0
        public bool IsEquivalent(IDataTypeInfo other)
        {
            switch (Type)
            {
            case DBDataType.INTEGER:
            case DBDataType.LONGINTEGER:
            case DBDataType.BOOLEAN:
            case DBDataType.BINARY_DATA:
                return(Type == other.Type);

            case DBDataType.DATE_TIME:
            case DBDataType.DATE:
            case DBDataType.TIME:
                return(other.Type.IsDateOrTimeOrDateTime());

            case DBDataType.DECIMAL:
                return(Type == other.Type && Length == other.Length && Decimals == other.Decimals);

            case DBDataType.TEXT:
                return(Type == other.Type &&
                       (Length == other.Length || Length > VARCHAR_MAXLENGTH && other.Length > VARCHAR_MAXLENGTH));

            default:
                return(false);
            }
        }
Exemple #2
0
        public UnionInfo(IDataTypeInfo dataTypeInfo, Func <UnionInfo, IEnumerable <IUnionCaseInfo> > enumerateCaseInfos)
        {
            this.dataTypeInfo = dataTypeInfo;

            var caseInfosB        = ImmutableList.CreateBuilder <IUnionCaseInfo>();
            var caseInfosByTypesB = ImmutableDictionary.CreateBuilder <Type, IUnionCaseInfo>();
            var caseInfosByNamesB = ImmutableDictionary.CreateBuilder <String, IUnionCaseInfo>();

            var invalidCaseErrorInfosB = ImmutableList.CreateBuilder <UnionCaseError>();

            foreach (var caseInfo in enumerateCaseInfos(this))
            {
                if (caseInfosByNamesB.TryGetValue(caseInfo.name, out var sameNameCaseInfo))
                {
                    invalidCaseErrorInfosB.Add(new UnionCaseError.HasDuplicateName(caseInfo, sameNameCaseInfo));
                }
                else
                {
                    caseInfosB.Add(caseInfo);
                    caseInfosByTypesB.Add(caseInfo.dataType, caseInfo);
                    caseInfosByNamesB.Add(caseInfo.name, caseInfo);
                }
            }

            caseInfos         = caseInfosB.ToImmutable();
            caseInfosByTypes  = caseInfosByTypesB.ToImmutable();
            caseInfosByNames  = caseInfosByNamesB.ToImmutable();
            invalidCaseErrors = invalidCaseErrorInfosB.ToImmutable();
        }
        private static IDataTypeInfo GetDataTypeInfo(Type propertyType, IContractResolver contractResolver)
        {
            bool IsNullableType(Type type) =>
            type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>);

            if (PrimitiveTypeMap.TryGetValue(propertyType, out DataType dataType))
            {
                return(DataTypeInfo.Simple(dataType));
            }
            else if (IsNullableType(propertyType))
            {
                return(GetDataTypeInfo(propertyType.GenericTypeArguments[0], contractResolver));
            }
            else if (TryGetEnumerableElementType(propertyType, out Type elementType))
            {
                IDataTypeInfo elementTypeInfo = GetDataTypeInfo(elementType, contractResolver);
                return(DataTypeInfo.AsCollection(elementTypeInfo));
            }
            else if (contractResolver.ResolveContract(propertyType) is JsonObjectContract jsonContract)
            {
                return(DataTypeInfo.Complex(DataType.Complex, propertyType, jsonContract));
            }
            else
            {
                return(DataTypeInfo.Unknown);
            }
        }
 public TableSourceColumnInfo(ITableSourceInfo tableSource, string name, IDataTypeInfo dataType, bool isMandatory, bool isPrimaryKey,
                              AutoNumberColumnInfo autoNumberInfo) : this(tableSource, name, dataType, isMandatory, isPrimaryKey)
 {
     this.autoNumberInfo = autoNumberInfo;
     // The auto-number is only valid if all fields are filled
     IsAutoGenerated = autoNumberInfo != null && !autoNumberInfo.SequenceName.IsNullOrEmpty() && !autoNumberInfo.TriggerName.IsNullOrEmpty();
 }
Exemple #5
0
 /// <summary>
 /// Deep copies content from src instance to this.
 /// <param name="source">Source for copy.</param>
 /// <returns>this.</returns>
 /// </summary>
 protected virtual IRecordInterface CopyFromImpl(IRecordInfo source)
 {
     if (typeof(IDataTypeInfo).IsInstanceOfType(source))
     {
         IDataTypeInfo typedSource = ((IDataTypeInfo)(source));
         if ((typedSource.Encoding != null))
         {
             if ((this.Encoding == null))
             {
                 this.Encoding = new com.epam.deltix.containers.MutableString(typedSource.Encoding);
             }
             else
             {
                 this.Encoding.Assign(typedSource.Encoding);
             }
         }
         else
         {
             this.Encoding = null;
         }
         if ((typedSource.HasIsNullable() == true))
         {
             this.IsNullable = typedSource.IsNullable;
         }
         else
         {
             this.NullifyIsNullable();
         }
     }
     return(this);
 }
Exemple #6
0
        public bool IsEquivalent(IDataTypeInfo other)
        {
            switch (Type)
            {
            case DBDataType.INTEGER:
            case DBDataType.LONGINTEGER:
            case DBDataType.BOOLEAN:
            case DBDataType.BINARY_DATA:
                return(Type == other.Type);

            case DBDataType.DATE_TIME:
            case DBDataType.DATE:
            case DBDataType.TIME:
                return(other.Type.IsDateOrTimeOrDateTime());

            case DBDataType.DECIMAL:
                return(Type == other.Type && Length == other.Length && Decimals == other.Decimals);

            case DBDataType.TEXT:
                // Unicode setting is ignored here because we don't want to force
                return(Type == other.Type &&
                       (Length == other.Length || Length >= VARCHAR_MAXLENGTH && other.Length >= VARCHAR_MAXLENGTH));

            default:
                return(false);
            }
        }
Exemple #7
0
        private IList <ITableSourceColumnInfo> GetColumnsInfo(iDB2TableSourceInfo tableSource)
        {
            IList <ITableSourceColumnInfo> columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                string query = @"SELECT COLS.*, PKS.PK_NAME, CHK.CHECK_CLAUSE, UDTS.ORDERING_ROUT_NAME 
                                 FROM QSYS2.SYSCOLUMNS COLS 
                                 INNER JOIN QSYS2.SYSTABLES TAB ON TAB.TABLE_NAME = COLS.TABLE_NAME AND TAB.TABLE_SCHEMA = COLS.TABLE_SCHEMA 
                                 LEFT JOIN SYSIBM.SQLPRIMARYKEYS  PKS ON PKS.COLUMN_NAME = COLS.COLUMN_NAME AND PKS.TABLE_NAME = COLS.TABLE_NAME AND PKS.TABLE_SCHEM = COLS.TABLE_SCHEMA 
                                 LEFT JOIN QSYS2.SYSCHKCST CHK ON  CHK.CHECK_CLAUSE LIKE COLS.COLUMN_NAME ||' IN%'
                                 LEFT JOIN SYSIBM.UDT_S UDTS ON UDTS.UDT_NAME = COLS.USER_DEFINED_TYPE_NAME 
                                 WHERE COLS.TABLE_NAME = '" + tableSource.Name + @"'
                                    AND COLS.SYSTEM_TABLE_SCHEMA = '" + tableSource.Database.Identifier + @"'
                                    AND ( CHK.CONSTRAINT_NAME IS NULL
                                          OR (CHK.CONSTRAINT_NAME IN ( SELECT CONSTRAINT_NAME
                                                                       FROM QSYS2.SYSCST CST
                                                                       WHERE CST.SYSTEM_TABLE_NAME  =  TAB.SYSTEM_TABLE_NAME  
                                                                             AND CST.TABLE_SCHEMA = TAB.TABLE_SCHEMA)))";

                IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, query);
                cmd.CommandTimeout = QueryTimeout;

                using (IDataReader reader = cmd.ExecuteReader()) {
                    int primaryKeyColumnCount = 0;
                    while (reader.Read())
                    {
                        string columnName = (string)reader["COLUMN_NAME"];

                        IDataTypeInfo datatype = CreateDataTypeInfo(reader["DATA_TYPE"] == DBNull.Value ? null : (string)reader["DATA_TYPE"],
                                                                    reader["LENGTH"] == DBNull.Value ? 0 : Convert.ToInt32(reader["LENGTH"]),
                                                                    reader["NUMERIC_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_PRECISION"]),
                                                                    reader["NUMERIC_SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_SCALE"]),
                                                                    reader["CHECK_CLAUSE"] == DBNull.Value ? null : (string)reader["CHECK_CLAUSE"],
                                                                    reader["ORDERING_ROUT_NAME"] == DBNull.Value ? null : (string)reader["ORDERING_ROUT_NAME"],
                                                                    reader["CCSID"] == DBNull.Value ? 0 : Convert.ToInt32(reader["CCSID"]));

                        bool isMandatory     = "N".EqualsIgnoreCase((string)reader["IS_NULLABLE"]);
                        bool isPrimaryKey    = (reader["PK_NAME"] != DBNull.Value);
                        bool isAutoGenerated = "YES".EqualsIgnoreCase((string)reader["IS_IDENTITY"]);

                        iDB2TableSourceColumnInfo info = new iDB2TableSourceColumnInfo(tableSource, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated);
                        columnsInfo.Add(info);
                        primaryKeyColumnCount += isPrimaryKey ? 1 : 0;
                    }
                    if (primaryKeyColumnCount > 1)
                    {
                        //we don't support composite primary keys so setting all to non primary key
                        IList <ITableSourceColumnInfo> columnsInfoCopy = new List <ITableSourceColumnInfo>();

                        foreach (ITableSourceColumnInfo c in columnsInfo)
                        {
                            columnsInfoCopy.Add(new iDB2TableSourceColumnInfo(c.TableSource, c.Name, c.DataType, c.IsMandatory, false, c.IsAutoGenerated));
                        }
                        columnsInfo = columnsInfoCopy;
                    }
                }
            }
            return(columnsInfo);
        }
Exemple #8
0
        public UnionCaseInfo(IDataTypeInfo dataTypeInfo, IUnionInfo declaringUnionInfo)
        {
            var t = dataTypeInfo.dataType;

            this.dataTypeInfo       = dataTypeInfo;
            this.declaringUnionInfo = declaringUnionInfo;
            name = t.MayGetCompanion <IUnionCaseNameProvider>()?.GetUnionCaseName(t) ?? t.Name;
        }
 private TableSourceColumnInfo(ITableSourceInfo tableSource, string name, IDataTypeInfo dataType, bool isMandatory, bool isPrimaryKey)
 {
     TableSource  = tableSource;
     Name         = name;
     DataType     = dataType;
     IsMandatory  = isMandatory;
     IsPrimaryKey = isPrimaryKey;
 }
Exemple #10
0
        private IList <ITableSourceColumnInfo> GetColumnsInfo(DB2LUWTableSourceInfo tableSource)
        {
            IList <ITableSourceColumnInfo> columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                string query = @"SELECT COLS.*, PKS.PK_NAME, cast(CHK.TEXT as varchar(2000)) as CHECK_CLAUSE, UDTS.ORDERING_ROUT_NAME  
				FROM SYSCAT.COLUMNS COLS 
				INNER JOIN SYSCAT.TABLES TAB ON TAB.TABNAME = COLS.TABNAME AND TAB.TABSCHEMA = COLS.TABSCHEMA               
				LEFT JOIN SYSIBM.SQLPRIMARYKEYS  PKS ON PKS.COLUMN_NAME = COLS.COLNAME AND PKS.TABLE_NAME = COLS.TABNAME AND PKS.TABLE_SCHEM = COLS.TABSCHEMA 
				LEFT JOIN SYSCAT.TABDEP DEP ON DEP.TABSCHEMA = TAB.TABSCHEMA and DEP.TABNAME = TAB.TABNAME 
				LEFT JOIN SYSCAT.COLCHECKS COL_CHK 
					ON (TAB.TYPE = 'T' and COL_CHK.TABNAME = TAB.TABNAME and COL_CHK.TABSCHEMA = TAB.TABSCHEMA and COLS.COLNAME = COL_CHK.COLNAME)
					OR (TAB.TYPE in ('V', 'L') and COL_CHK.TABNAME = DEP.BNAME and COL_CHK.TABSCHEMA = TAB.TABSCHEMA and COLS.COLNAME = COL_CHK.COLNAME )
                LEFT JOIN SYSCAT.CHECKS CHK on COL_CHK.constname = CHK.constname and COL_CHK.tabschema = CHK.tabschema and COL_CHK.tabname = CHK.tabname
				LEFT JOIN SYSIBM.UDT_S UDTS ON UDTS.UDT_NAME = COLS.TYPENAME 
				WHERE COLS.TABNAME = '"                 + tableSource.Name + "' and TAB.TABSCHEMA = '" + tableSource.Database.Identifier + "'";


                IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, query);
                cmd.CommandTimeout = QueryTimeout;

                using (IDataReader reader = cmd.ExecuteReader()) {
                    int primaryKeyColumnCount = 0;
                    while (reader.Read())
                    {
                        string columnName = (string)reader["COLNAME"];

                        IDataTypeInfo datatype = CreateDataTypeInfo(reader["TYPENAME"] == DBNull.Value ? null : (string)reader["TYPENAME"],
                                                                    reader["LENGTH"] == DBNull.Value ? 0 : Convert.ToInt32(reader["LENGTH"]),
                                                                    reader["LENGTH"] == DBNull.Value ? 0 : Convert.ToInt32(reader["LENGTH"]),
                                                                    reader["SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["SCALE"]),
                                                                    reader["CHECK_CLAUSE"] == DBNull.Value ? null : (string)reader["CHECK_CLAUSE"],
                                                                    reader["ORDERING_ROUT_NAME"] == DBNull.Value ? null : (string)reader["ORDERING_ROUT_NAME"],
                                                                    reader["CODEPAGE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["CODEPAGE"]));

                        bool isMandatory     = "N".EqualsIgnoreCase((string)reader["NULLS"]);
                        bool isPrimaryKey    = (reader["PK_NAME"] != DBNull.Value);
                        bool isAutoGenerated = "Y".EqualsIgnoreCase((string)reader["IDENTITY"]);

                        DB2LUWTableSourceColumnInfo info = new DB2LUWTableSourceColumnInfo(tableSource, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated);
                        columnsInfo.Add(info);
                        primaryKeyColumnCount += isPrimaryKey ? 1 : 0;
                    }
                    if (primaryKeyColumnCount > 1)
                    {
                        //we don't support composite primary keys so setting all to non primary key
                        IList <ITableSourceColumnInfo> columnsInfoCopy = new List <ITableSourceColumnInfo>();

                        foreach (ITableSourceColumnInfo c in columnsInfo)
                        {
                            columnsInfoCopy.Add(new DB2LUWTableSourceColumnInfo(c.TableSource, c.Name, c.DataType, c.IsMandatory, false, c.IsAutoGenerated));
                        }
                        columnsInfo = columnsInfoCopy;
                    }
                }
            }
            return(columnsInfo);
        }
 public DB2LUWTableSourceColumnInfo(ITableSourceInfo tableSource, string name, IDataTypeInfo dataType, bool isMandatory, bool isPrimaryKey, bool isAutoGenerated)
 {
     TableSource     = tableSource;
     Name            = name;
     DataType        = dataType;
     IsMandatory     = isMandatory;
     IsPrimaryKey    = isPrimaryKey;
     IsAutoGenerated = isAutoGenerated;
 }
 public PropertyMetadata(IMetadataCache cache, string name, EntityMetadata owner, PropertyGeneralUsageCategoryStruct generalBehavior
                         , DataType dataType, bool isNullable, bool isExpression, string title, string expressionDefinitionIdentifier)
 {
     _cache         = cache;
     Name           = name;
     Title          = title;
     GeneralBahvior = generalBehavior;
     _dataTypeInfo  = dataType;
     DataType       = (DataTypes)dataType.Id;
     IsNullable     = isNullable;
     IsExpression   = isExpression;
     _expressionDefinitionIdentifier = expressionDefinitionIdentifier;
     owner.AddProperty(this);
     Owner = owner;
 }
Exemple #13
0
        private IEnumerable <ITableSourceColumnInfo> GetColumns(TableSourceInfo tableSource)
        {
            HashSet <string> primaryKeyColumns    = GetPrimaryKeyColumns(tableSource);
            HashSet <string> autoGeneratedColumns = GetAutoGeneratedColumns(tableSource);

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                using (IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn,
                                                                                        tableSource.Database.IsLinkedServer ? "sp_columns_ex" : string.Format("{0}.dbo.sp_columns", DMLIdentifiers.EscapeIdentifierInner(tableSource.Database.Catalog)))) {
                    cmd.CommandType    = CommandType.StoredProcedure;
                    cmd.CommandTimeout = QueryTimeout;
                    foreach (var param in tableSource.Database.IsLinkedServer ? spLinkedServerParams : spParams)
                    {
                        DatabaseServices.ExecutionService.CreateParameter(cmd,
                                                                          DatabaseServices.ExecutionService.ParameterPrefix + param.Key, DbType.String, param.Value(tableSource));
                    }
                    using (IDataReader reader = DatabaseServices.ExecutionService.ExecuteReader(cmd)) {
                        string colDecimals     = tableSource.Database.IsLinkedServer ? "DECIMAL_DIGITS" : "SCALE";
                        string colBufferLength = tableSource.Database.IsLinkedServer ? "BUFFER_LENGTH" : "LENGTH";
                        string colSize         = tableSource.Database.IsLinkedServer ? "COLUMN_SIZE" : "PRECISION";
                        while (reader.Read())
                        {
                            string columnName      = Convert.ToString(reader["COLUMN_NAME"]);
                            int    dataTypeCode    = Convert.ToInt32(reader["DATA_TYPE"]);
                            string dataTypeName    = Convert.ToString(reader["TYPE_NAME"]);
                            int    sqlDataTypeCode = Convert.ToInt32(reader["SQL_DATA_TYPE"]);
                            int    decimalDigits   = reader[colDecimals] == DBNull.Value ? 255 : Convert.ToInt32(reader[colDecimals]);
                            int    bufferLength    = reader[colBufferLength] == DBNull.Value ? -1 : Convert.ToInt32(reader[colBufferLength]);
                            int    size            = Convert.ToInt32(reader[colSize]);

                            IDataTypeInfo type = CreateDataTypeInfo(tableSource.Database.IsLinkedServer, dataTypeName, dataTypeCode,
                                                                    sqlDataTypeCode, decimalDigits, bufferLength, size);

                            bool isMandatory     = !Convert.ToString(reader["IS_NULLABLE"]).EqualsIgnoreCase("YES");
                            bool isPrimaryKey    = primaryKeyColumns.Contains(columnName);
                            bool isAutoGenerated = autoGeneratedColumns.Contains(columnName);

                            yield return(new TableSourceColumnInfo(tableSource, columnName, type, isMandatory, isPrimaryKey, isAutoGenerated));
                        }
                    }
                }
            }
        }
Exemple #14
0
 public PropertyMetadata(IMetadataCache cache, string name, EntityTypeMetadata owner, PropertyGeneralUsageCategoryStruct generalBehavior
                         , DataType dataType, bool isNullable, bool isExpression, string title, string expressionDefinitionIdentifier, IEnumerable <MetadataDbAccess.Entities.PropertyBehavior> behaviors)
 {
     _cache         = cache;
     Name           = name;
     Title          = title;
     GeneralBahvior = generalBehavior;
     _dataTypeInfo  = dataType;
     DataType       = (DataTypes)dataType.Id;
     IsNullable     = isNullable;
     IsExpression   = isExpression;
     _expressionDefinitionIdentifier = expressionDefinitionIdentifier;
     owner.AddProperty(this);
     Owner     = owner;
     Behaviors = behaviors?.Select(x => new PropertyBehaviorMetadata
     {
         AdditionalBehavior = new AdditionalBehaviorMetadata
         {
             Name       = x.AdditionalBehavior.Name,
             Definition = x.AdditionalBehavior.Definition
         },
         Parameters = x.Parameters
     }).ToList();
 }
Exemple #15
0
        protected IEnumerable <ITableSourceColumnInfo> GetColumns(
            IEnumerable <CacheTableSourceInfo> tableSources,
            CreateDataTypeInfo createDataTypeInfo,
            CreateColumnInfo createColumnInfo)
        {
            var columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                string paramPrefix = DatabaseServices.ExecutionService.ParameterPrefix;
                string tableNames  = "'" + tableSources.Select(t => t.Name).StrCat("','") + "'";

                string query = string.Format(@"SELECT 
                                              TABLE_NAME, COLUMN_NAME, odbctype, DATA_TYPE, 
                                              IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, 
                                              NUMERIC_SCALE, PRIMARY_KEY, AUTO_INCREMENT , COLUMN_DEFAULT, DATETIME_PRECISION
                                              FROM INFORMATION_SCHEMA.COLUMNS 
                                              WHERE TABLE_SCHEMA= {0} 
                                                AND TABLE_NAME in (" + tableNames + @")
                                              ORDER BY ORDINAL_POSITION", paramPrefix + "schema");

                IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, query);
                DatabaseServices.ExecutionService.CreateParameter(cmd, paramPrefix + "schema", DbType.String, tableSources.First().Database.Name);
                cmd.CommandTimeout = QueryTimeout;

                using (IDataReader reader = cmd.ExecuteReader()) {
                    while (reader.Read())
                    {
                        int    length;
                        string tableName         = (string)reader["TABLE_NAME"];
                        string columnName        = (string)reader["COLUMN_NAME"];
                        int    precision         = reader["NUMERIC_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_PRECISION"]);
                        int    scale             = reader["NUMERIC_SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_SCALE"]);
                        int    datetimePrecision = reader["DATETIME_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(reader["DATETIME_PRECISION"]);
                        // Could not find what types are given in the DATA_TYPE column,
                        // but could find a table which gives the integer value odbctype so using this.
                        int    odbcType      = reader["odbctype"] == DBNull.Value ? 0 : Convert.ToInt32(reader["odbctype"]);
                        string cacheDataType = (string)reader["DATA_TYPE"];

                        //longblobs and longtext might cause overflow so we need to protect the decoding
                        Int32.TryParse(Convert.ToString(reader["CHARACTER_MAXIMUM_LENGTH"]), out length);
                        if (length == 0)
                        {
                            length = int.MaxValue;
                        }

                        IDataTypeInfo datatype = createDataTypeInfo(odbcType, cacheDataType, length, precision, scale, datetimePrecision);

                        bool isAutoGenerated = "YES".EqualsIgnoreCase((string)reader["AUTO_INCREMENT"]);
                        bool isPrimaryKey    = false;
                        bool isMandatory     = isAutoGenerated || "NO".EqualsIgnoreCase((string)reader["IS_NULLABLE"]);

                        // Force all "ID" columns to be the primary key. Caché can use composite primary keys and this is not supported
                        // by OutSystems, Caché always creates the "ID" column, so we use this as the Primary Key.
                        if (columnName.CompareTo("ID") == 0)
                        {
                            isPrimaryKey = true;
                            isMandatory  = true;
                        }

                        ITableSourceInfo tableSource = tableSources.First(t => t.Name.EqualsIgnoreCase(tableName));

                        ITableSourceColumnInfo columnInfo = createColumnInfo(tableSource, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated);
                        columnsInfo.Add(columnInfo);
                    }
                }
            }
            return(columnsInfo);
        }
 public TableSourceColumnInfo(ITableSourceInfo tableSource, string name, IDataTypeInfo dataType, bool isMandatory, bool isPrimaryKey,
                              bool isAutoNumber) : this(tableSource, name, dataType, isMandatory, isPrimaryKey)
 {
     IsAutoGenerated = isAutoNumber;
 }
 public static IDataTypeInfo AsCollection(IDataTypeInfo dataTypeInfo) =>
 dataTypeInfo.Match(
     onUnknownDataType: () => Unknown,
     onSimpleDataType: dataType => Simple(DataType.Collection(dataType)),
     onComplexDataType: (dataType, underlyingClrType, jsonContract) =>
     Complex(DataType.Collection(dataType), underlyingClrType, jsonContract));
 public VariableDetails(string name, IDataTypeInfo type)
 {
     Name = name;
     Type = type;
 }
        private static IList <Field> BuildForTypeRecursive(
            Type modelType,
            JsonObjectContract contract,
            IContractResolver contractResolver,
            Stack <Type> processedTypes)
        {
            Field BuildField(JsonProperty prop)
            {
                bool ShouldIgnore(Attribute attribute) =>
                attribute is JsonIgnoreAttribute || attribute is FieldBuilderIgnoreAttribute;

                IList <Attribute> attributes = prop.AttributeProvider.GetAttributes(true);

                if (attributes.Any(ShouldIgnore))
                {
                    return(null);
                }

                Field CreateComplexField(DataType dataType, Type underlyingClrType, JsonObjectContract jsonObjectContract)
                {
                    try
                    {
                        if (processedTypes.Contains(underlyingClrType))
                        {
                            // Skip recursive types.
                            return(null);
                        }

                        processedTypes.Push(underlyingClrType);
                        IList <Field> subFields =
                            BuildForTypeRecursive(underlyingClrType, jsonObjectContract, contractResolver, processedTypes);
                        return(new Field(prop.PropertyName, dataType, subFields));
                    }
                    finally
                    {
                        processedTypes.Pop();
                    }
                }

                Field CreateSimpleField(DataType dataType)
                {
                    var field = new Field(prop.PropertyName, dataType);

                    foreach (Attribute attribute in attributes)
                    {
                        switch (attribute)
                        {
                        case IsSearchableAttribute _:
                            field.IsSearchable = true;
                            break;

                        case IsFilterableAttribute _:
                            field.IsFilterable = true;
                            break;

                        case IsSortableAttribute _:
                            field.IsSortable = true;
                            break;

                        case IsFacetableAttribute _:
                            field.IsFacetable = true;
                            break;

                        case IsRetrievableAttribute isRetrievableAttribute:
                            field.IsRetrievable = isRetrievableAttribute.IsRetrievable;
                            break;

                        case AnalyzerAttribute analyzerAttribute:
                            field.Analyzer = analyzerAttribute.Name;
                            break;

                        case SearchAnalyzerAttribute searchAnalyzerAttribute:
                            field.SearchAnalyzer = searchAnalyzerAttribute.Name;
                            break;

                        case IndexAnalyzerAttribute indexAnalyzerAttribute:
                            field.IndexAnalyzer = indexAnalyzerAttribute.Name;
                            break;

                        case SynonymMapsAttribute synonymMapsAttribute:
                            field.SynonymMaps = synonymMapsAttribute.SynonymMaps;
                            break;

                        default:
                            Type attributeType = attribute.GetType();

                            // Match on name to avoid dependency - don't want to force people not using
                            // this feature to bring in the annotations component.
                            //
                            // Also, ignore key attributes on sub-fields.
                            if (attributeType.FullName == "System.ComponentModel.DataAnnotations.KeyAttribute" &&
                                processedTypes.Count <= 1)
                            {
                                field.IsKey = true;
                            }
                            break;
                        }
                    }

                    return(field);
                }

                ArgumentException FailOnUnknownDataType()
                {
                    string errorMessage =
                        $"Property '{prop.PropertyName}' is of type '{prop.PropertyType}', which does not map to an " +
                        "Azure Search data type. Please use a supported data type or mark the property with [JsonIgnore] or " +
                        "[FieldBuilderIgnore] and define the field by creating a Field object.";

                    return(new ArgumentException(errorMessage, nameof(modelType)));
                }

                IDataTypeInfo dataTypeInfo = GetDataTypeInfo(prop.PropertyType, contractResolver);

                return(dataTypeInfo.Match(
                           onUnknownDataType: () => throw FailOnUnknownDataType(),
                           onSimpleDataType: CreateSimpleField,
                           onComplexDataType: CreateComplexField));
            }

            return(contract.Properties.Select(BuildField).Where(field => field != null).ToArray());
        }
Exemple #20
0
        private IList <ITableSourceColumnInfo> GetColumnsInfo(DB2ZOSTableSourceInfo tableSource)
        {
            IList <ITableSourceColumnInfo> columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                string query = @"SELECT COLS.*, 
                                (
                                SELECT PKS.NAME FROM SYSIBM.SYSINDEXES  PKS 
                                INNER JOIN SYSIBM.SYSKEYS KYS ON KYS.IXNAME = PKS.NAME
                                WHERE PKS.TBNAME = COLS.TBNAME AND PKS.CREATOR = COLS.TBCREATOR AND PKS.UNIQUERULE = 'P' AND KYS.COLNAME = COLS.NAME
                                ) as PK_NAME,
                                COL_CHK.CHECKCONDITION as CHECK_CLAUSE,
                                UDTS.ENCODING_SCHEME as ORDERING_ROUT_NAME  
                                FROM SYSIBM.SYSCOLUMNS COLS 
                                INNER JOIN SYSIBM.SYSTABLES TAB ON TAB.NAME = COLS.TBNAME AND TAB.CREATOR = COLS.TBCREATOR              
                                LEFT JOIN SYSIBM.SYSVIEWDEP VDEP ON TAB.TYPE = 'V' and TAB.CREATOR = VDEP.BCREATOR and TAB.NAME = VDEP.DNAME
                                LEFT JOIN SYSIBM.SYSCHECKDEP DEP ON 
                                  ((TAB.TYPE = 'T' or TAB.TYPE = 'P') and DEP.TBOWNER = TAB.CREATOR and DEP.TBNAME = TAB.NAME and DEP.COLNAME = COLS.NAME)
                                  OR (TAB.TYPE = 'V' and DEP.TBOWNER = VDEP.BCREATOR and DEP.TBNAME = VDEP.BNAME and DEP.COLNAME = COLS.NAME)
                                LEFT JOIN SYSIBM.SYSCHECKS COL_CHK ON 
                                  ((TAB.TYPE = 'T' or TAB.TYPE = 'P') and COL_CHK.TBOWNER = DEP.TBOWNER and COL_CHK.TBNAME = TAB.NAME and COL_CHK.CHECKNAME = DEP.CHECKNAME)
                                  OR (TAB.TYPE = 'V' and COL_CHK.TBOWNER = DEP.TBOWNER and COL_CHK.TBNAME = DEP.TBNAME and COL_CHK.CHECKNAME = DEP.CHECKNAME)
                                LEFT JOIN SYSIBM.SYSDATATYPES UDTS ON UDTS.NAME = COLS.COLTYPE 
                                WHERE (COLS.GENERATED_ATTR is null OR COLS.GENERATED_ATTR <> 'A') and TAB.CREATOR = '" + tableSource.Database.Identifier + "' and TAB.NAME = '" + tableSource.Name.ToUpper() + "'";


                IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, query);
                cmd.CommandTimeout = QueryTimeout;

                using (IDataReader reader = cmd.ExecuteReader()) {
                    int primaryKeyColumnCount = 0;
                    while (reader.Read())
                    {
                        string columnName = (string)reader["NAME"];

                        IDataTypeInfo datatype = CreateDataTypeInfo(reader["COLTYPE"] == DBNull.Value ? null : ((string)reader["COLTYPE"]).Trim(),
                                                                    reader["LENGTH"] == DBNull.Value ? 0 : Convert.ToInt32(reader["LENGTH"]),
                                                                    reader["LENGTH"] == DBNull.Value ? 0 : Convert.ToInt32(reader["LENGTH"]),
                                                                    reader["SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["SCALE"]),
                                                                    reader["CHECK_CLAUSE"] == DBNull.Value ? null : (string)reader["CHECK_CLAUSE"],
                                                                    reader["ORDERING_ROUT_NAME"] == DBNull.Value ? null : (string)reader["ORDERING_ROUT_NAME"],
                                                                    reader["CCSID"] == DBNull.Value ? 0 : Convert.ToInt32(reader["CCSID"]));

                        bool isMandatory     = "N".EqualsIgnoreCase((string)reader["NULLS"]);
                        bool isPrimaryKey    = (reader["PK_NAME"] != DBNull.Value);
                        var  defaultVal      = (string)reader["DEFAULT"];
                        bool isAutoGenerated = "I".EqualsIgnoreCase(defaultVal) || "J".EqualsIgnoreCase(defaultVal);

                        DB2ZOSTableSourceColumnInfo info = new DB2ZOSTableSourceColumnInfo(tableSource, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated);
                        columnsInfo.Add(info);
                        primaryKeyColumnCount += isPrimaryKey ? 1 : 0;
                    }
                    if (primaryKeyColumnCount > 1)
                    {
                        //we don't support composite primary keys so setting all to non primary key
                        IList <ITableSourceColumnInfo> columnsInfoCopy = new List <ITableSourceColumnInfo>();

                        foreach (ITableSourceColumnInfo c in columnsInfo)
                        {
                            columnsInfoCopy.Add(new DB2ZOSTableSourceColumnInfo(c.TableSource, c.Name, c.DataType, c.IsMandatory, false, c.IsAutoGenerated));
                        }
                        columnsInfo = columnsInfoCopy;
                    }
                }
            }
            return(columnsInfo);
        }
Exemple #21
0
 public bool IsEquivalent(IDataTypeInfo other)
 {
     return(SqlDataType == other.SqlDataType);
 }
Exemple #22
0
 private static CacheTableSourceColumnInfo GetColumnInfo(ITableSourceInfo tableSource, string columnName, IDataTypeInfo dataType,
                                                         bool isMandatory, bool isPrimaryKey, bool isAutoGenerated)
 {
     return(new CacheTableSourceColumnInfo(tableSource, columnName, dataType, isMandatory, isPrimaryKey, isAutoGenerated));
 }
Exemple #23
0
        protected IEnumerable <ITableSourceColumnInfo> GetColumns(ITableSourceInfo tableSource, CreateDataTypeInfo createDataTypeInfo, CreateColumnInfo createColumnInfo)
        {
            string paramPrefix = DatabaseServices.ExecutionService.ParameterPrefix;
            var    columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection())
            {
                string database;
                string schema;
                string table;

                SnowflakeDatabaseObjectFactory.ParseQualifiedTableName(tableSource.QualifiedName, out database, out schema, out table);

                string queryInfoSchema = string.Format("SELECT DISTINCT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLUMN_DEFAULT, ORDINAL_POSITION " +
                                                       "FROM {0}.INFORMATION_SCHEMA.COLUMNS " +
                                                       "WHERE TABLE_NAME = '{1}' " +
                                                       "AND TABLE_SCHEMA = '{2}' " +
                                                       "ORDER BY ORDINAL_POSITION;", database, table, schema);

                string queryInfoTable = string.Format("DESC TABLE {0}", tableSource.QualifiedName);

                IDbCommand cmdInfoSchema = DatabaseServices.ExecutionService.CreateCommand(conn, queryInfoSchema);
                IDbCommand cmdInfoTable  = DatabaseServices.ExecutionService.CreateCommand(conn, queryInfoTable);
                cmdInfoSchema.CommandTimeout = QueryTimeout;
                cmdInfoTable.CommandTimeout  = QueryTimeout;

                string pkField = null;
                using (IDataReader readerInfoTable = cmdInfoTable.ExecuteReader())
                {
                    while (pkField == null && readerInfoTable.Read())
                    {
                        string fieldName = (string)readerInfoTable["name"];
                        string pk        = (string)readerInfoTable["primary key"];

                        if (pk == "Y")
                        {
                            pkField = fieldName;
                            break;
                        }
                    }
                }

                using (IDataReader readerInfoSchema = cmdInfoSchema.ExecuteReader())
                {
                    while (readerInfoSchema.Read())
                    {
                        string tableName  = (string)readerInfoSchema["TABLE_NAME"];
                        string columnName = (string)readerInfoSchema["COLUMN_NAME"];

                        string type = (string)readerInfoSchema["DATA_TYPE"];

                        int length;

                        //longblobs and longtext might cause overflow so we need to protect the decoding
                        Int32.TryParse(Convert.ToString(readerInfoSchema["CHARACTER_MAXIMUM_LENGTH"]), out length);

                        int           precision = readerInfoSchema["NUMERIC_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(readerInfoSchema["NUMERIC_PRECISION"]);
                        int           scale     = readerInfoSchema["NUMERIC_SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(readerInfoSchema["NUMERIC_SCALE"]);
                        IDataTypeInfo datatype  = createDataTypeInfo(type, length, precision, scale);

                        bool isAutoGenerated = false;
                        bool isPrimaryKey    = pkField != null && columnName.Equals(pkField);
                        bool isMandatory     = isAutoGenerated || "NO".EqualsIgnoreCase((string)readerInfoSchema["IS_NULLABLE"]);

                        ITableSourceColumnInfo columnInfo = createColumnInfo(tableSource, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated);
                        columnsInfo.Add(columnInfo);
                    }
                }
            }
            return(columnsInfo);
        }
 private static MySQLTableSourceColumnInfo GetPlatformColumnInfo(ITableSourceInfo tableSource, string columnName, IDataTypeInfo dataType,
                                                                 bool isMandatory, bool isPrimaryKey, bool isAutoGenerated, bool isUnsigned)
 {
     return(new MySQLPlatformTableSourceColumnInfo(tableSource, columnName, (MySQLPlatformDataTypeInfo)dataType, isMandatory, isPrimaryKey, isAutoGenerated));
 }
        protected IEnumerable <ITableSourceColumnInfo> GetColumns(IEnumerable <MySQLTableSourceInfo> tableSources, CreateDataTypeInfo createDataTypeInfo,
                                                                  CreateColumnInfo createColumnInfo)
        {
            string paramPrefix = DatabaseServices.ExecutionService.ParameterPrefix;
            var    columnsInfo = new List <ITableSourceColumnInfo>();

            using (IDbConnection conn = DatabaseServices.TransactionService.CreateConnection()) {
                string tableNames = "'" + tableSources.Select(t => t.Name).StrCat("','") + "'";
                string query      = string.Format(@"SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, DATA_TYPE, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, COLUMN_KEY, EXTRA , COLUMN_DEFAULT, DATETIME_PRECISION
                                               FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA= {0} AND TABLE_NAME in (" + tableNames + @")
                                               ORDER BY ORDINAL_POSITION;", paramPrefix + "schema");

                IDbCommand cmd = DatabaseServices.ExecutionService.CreateCommand(conn, query);
                DatabaseServices.ExecutionService.CreateParameter(cmd, paramPrefix + "schema", DbType.String, tableSources.First().Database.Name);
                cmd.CommandTimeout = QueryTimeout;

                using (IDataReader reader = cmd.ExecuteReader()) {
                    while (reader.Read())
                    {
                        string tableName  = (string)reader["TABLE_NAME"];
                        string columnName = (string)reader["COLUMN_NAME"];

                        string type = (string)reader["DATA_TYPE"];

                        int length;

                        //longblobs and longtext might cause overflow so we need to protect the decoding
                        Int32.TryParse(Convert.ToString(reader["CHARACTER_MAXIMUM_LENGTH"]), out length);
                        if (length == 0)
                        {
                            length = int.MaxValue;
                        }

                        //due to mysql bugs on metadata http://bugs.mysql.com/bug.php?id=69042
                        //will try to parse first from raw data type and fall back to numeric precision metadata
                        //numeric precision is reliable for floating point data types
                        string   column_type = ((string)reader["COLUMN_TYPE"]);
                        int      precision;
                        string[] pieces = column_type.Split(new[] { '(', ')' });
                        if (pieces.Length < 2 || int.TryParse(pieces[1], out precision) == false)
                        {
                            precision = reader["NUMERIC_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_PRECISION"]);
                        }

                        bool isUnsigned = column_type.Contains("unsigned");

                        int scale = reader["NUMERIC_SCALE"] == DBNull.Value ? 0 : Convert.ToInt32(reader["NUMERIC_SCALE"]);

                        int datetimePrecision = reader["DATETIME_PRECISION"] == DBNull.Value ? 0 : Convert.ToInt32(reader["DATETIME_PRECISION"]);

                        IDataTypeInfo datatype = createDataTypeInfo(type, column_type, length, precision, scale, datetimePrecision, isUnsigned);

                        bool isAutoGenerated = ((string)reader["EXTRA"]).ContainsIgnoreCase("auto_increment");
                        bool isPrimaryKey    = "PRI".EqualsIgnoreCase((string)reader["COLUMN_KEY"]);

                        bool isMandatory = isAutoGenerated || "NO".EqualsIgnoreCase((string)reader["IS_NULLABLE"]);

                        ITableSourceInfo tableInfo = tableSources.First(t => t.Name.EqualsIgnoreCase(tableName));

                        ITableSourceColumnInfo columnInfo = createColumnInfo(tableInfo, columnName, datatype, isMandatory, isPrimaryKey, isAutoGenerated, isUnsigned);
                        columnsInfo.Add(columnInfo);
                    }
                }
            }
            return(columnsInfo);
        }