Exemplo n.º 1
0
        public static OracleDataType ResolveDataTypeFromJsonDataTypeNode(StatementGrammarNode jsonDataTypeNode)
        {
            if (jsonDataTypeNode == null)
            {
                return(OracleDataType.Empty);
            }

            if (!String.Equals(jsonDataTypeNode.Id, NonTerminals.JsonDataType))
            {
                throw new ArgumentException($"Node ID must be '{NonTerminals.JsonDataType}'. ", nameof(jsonDataTypeNode));
            }

            var dataTypeNode = jsonDataTypeNode[NonTerminals.DataType];

            if (dataTypeNode != null)
            {
                return(ResolveDataTypeFromNode(dataTypeNode));
            }

            var typeTerminal = jsonDataTypeNode[0];

            return
                (typeTerminal == null
                                        ? OracleDataType.Empty
                                        : new OracleDataType {
                FullyQualifiedName = OracleObjectIdentifier.Create(null, ((OracleToken)typeTerminal.Token).UpperInvariantValue)
            });
        }
Exemplo n.º 2
0
 internal OracleProgramParameterMetadata(string name, int position, int sequence, int dataLevel, ParameterDirection direction, string dataType, OracleObjectIdentifier customDataType, bool isOptional)
 {
     Name           = name;
     Position       = position;
     Sequence       = sequence;
     DataLevel      = dataLevel;
     DataType       = dataType;
     CustomDataType = customDataType;
     Direction      = direction;
     IsOptional     = isOptional;
 }
Exemplo n.º 3
0
        public static OracleSchemaObject CreateSchemaObjectMetadata(string objectType, string owner, string name, bool isValid, DateTime created, DateTime lastDdl, bool isTemporary)
        {
            var schemaObject = CreateObjectMetadata(objectType);

            schemaObject.FullyQualifiedName = OracleObjectIdentifier.Create(owner, name);
            schemaObject.IsValid            = isValid;
            schemaObject.Created            = created;
            schemaObject.LastDdl            = lastDdl;
            schemaObject.IsTemporary        = isTemporary;

            return(schemaObject);
        }
Exemplo n.º 4
0
        public static OracleConstraint CreateConstraint(string constraintType, string owner, string name, bool isEnabled, bool isValidated, bool isDeferrable, bool isRelied)
        {
            var constraint = CreateConstraint(constraintType);

            constraint.FullyQualifiedName = OracleObjectIdentifier.Create(owner, name);
            constraint.IsEnabled          = isEnabled;
            constraint.IsValidated        = isValidated;
            constraint.IsDeferrable       = isDeferrable;
            constraint.IsRelied           = isRelied;

            return(constraint);
        }
Exemplo n.º 5
0
 private static OracleColumn BuildSequencePseudoColumn(string normalizedName)
 {
     return
         (new OracleColumn
     {
         Name = normalizedName,
         DataType =
             new OracleDataType
         {
             FullyQualifiedName = OracleObjectIdentifier.Create(null, "INTEGER"),
             Scale = 0
         }
     });
 }
Exemplo n.º 6
0
 public bool Equals(OracleObjectIdentifier other)
 {
     return(string.Equals(NormalizedOwner, other.NormalizedOwner) && string.Equals(NormalizedName, other.NormalizedName));
 }
Exemplo n.º 7
0
        public static IReadOnlyList <OracleDataType> FromUnpivotColumnSelectorValues(IEnumerable <StatementGrammarNode> nodes)
        {
            var dataTypes = new List <OracleDataType>();

            var definitionIndex = 0;

            foreach (var node in nodes)
            {
                if (!String.Equals(node.Id, NonTerminals.NullOrStringOrNumberLiteralOrParenthesisEnclosedStringOrIntegerLiteralList))
                {
                    throw new ArgumentException($"All nodes must have ID of {nameof(NonTerminals.NullOrStringOrNumberLiteralOrParenthesisEnclosedStringOrIntegerLiteralList)}", nameof(nodes));
                }

                var literals = node.GetDescendants(Terminals.StringLiteral, Terminals.NumberLiteral);

                var typeIndex = 0;
                foreach (var literal in literals)
                {
                    var newDataType = String.Equals(literal.Id, Terminals.StringLiteral)
                                                ? new OracleDataType
                    {
                        FullyQualifiedName = OracleObjectIdentifier.Create(null, TerminalValues.Varchar2),
                        Length             = literal.Token.Value.ToPlainString().Length
                    }
                                                : NumberType;

                    if (definitionIndex == 0)
                    {
                        dataTypes.Add(newDataType);
                    }
                    else
                    {
                        if (dataTypes.Count <= typeIndex)
                        {
                            return(null);
                        }

                        var storedDataType = dataTypes[typeIndex];
                        if (storedDataType.FullyQualifiedName != newDataType.FullyQualifiedName)
                        {
                            return(null);
                        }

                        if (newDataType.Length > storedDataType.Length)
                        {
                            storedDataType.Length = newDataType.Length;
                        }
                    }

                    typeIndex++;
                }

                if (typeIndex != dataTypes.Count)
                {
                    return(null);
                }

                if (typeIndex > 0)
                {
                    definitionIndex++;
                }
            }

            return(dataTypes.AsReadOnly());
        }
Exemplo n.º 8
0
 public static OracleDataType CreateTimestampWithTimeZoneDataType(int scale)
 {
     return(new OracleDataType {
         FullyQualifiedName = OracleObjectIdentifier.Create(String.Empty, OracleDatabaseModelBase.BuiltInDataTypeTimestampWithTimeZone), Scale = scale
     });
 }
Exemplo n.º 9
0
 public static OracleDataType CreateTimestampDataType(int scale)
 {
     return(new OracleDataType {
         FullyQualifiedName = OracleObjectIdentifier.Create(String.Empty, TerminalValues.Timestamp), Scale = scale
     });
 }
Exemplo n.º 10
0
        public static bool TryResolveDataTypeFromExpression(StatementGrammarNode expressionNode, OracleColumn column)
        {
            if (expressionNode == null || expressionNode.TerminalCount == 0 || !String.Equals(expressionNode.Id, NonTerminals.Expression))
            {
                return(false);
            }

            expressionNode = expressionNode.UnwrapIfNonChainedExpressionWithinParentheses(out var isChainedExpression);
            if (isChainedExpression)
            {
                return(false);
            }

            var analyzedNode = expressionNode[NonTerminals.CastOrXmlCastFunction, NonTerminals.CastFunctionParameterClause, NonTerminals.AsDataType, NonTerminals.DataType];

            if (analyzedNode != null)
            {
                column.DataType = OracleReferenceBuilder.ResolveDataTypeFromNode(analyzedNode);
                column.Nullable = true;
                return(true);
            }

            if (String.Equals(expressionNode.FirstTerminalNode.Id, Terminals.Collect))
            {
                column.DataType = OracleDataType.DynamicCollectionType;
                column.Nullable = true;
                return(true);
            }

            var    tokenValue = expressionNode.FirstTerminalNode.Token.Value;
            string literalInferredDataTypeName = null;
            var    literalInferredDataType     = new OracleDataType();
            var    nullable = false;

            switch (expressionNode.FirstTerminalNode.Id)
            {
            case Terminals.StringLiteral:
                if (expressionNode.TerminalCount != 1)
                {
                    break;
                }

                if (tokenValue[0] == 'n' || tokenValue[0] == 'N')
                {
                    literalInferredDataTypeName = TerminalValues.NChar;
                }
                else
                {
                    literalInferredDataTypeName = TerminalValues.Char;
                }

                literalInferredDataType.Length = tokenValue.ToPlainString().Length;
                nullable = literalInferredDataType.Length == 0;

                break;

            case Terminals.NumberLiteral:
                if (expressionNode.TerminalCount != 1)
                {
                    break;
                }

                switch (tokenValue[tokenValue.Length - 1])
                {
                case 'f':
                case 'F':
                    literalInferredDataTypeName = TerminalValues.BinaryFloat;
                    break;

                case 'd':
                case 'D':
                    literalInferredDataTypeName = TerminalValues.BinaryDouble;
                    break;

                default:
                    literalInferredDataTypeName = TerminalValues.Number;
                    break;
                }

                /*if (includeLengthPrecisionAndScale)
                 * {
                 *      literalInferredDataType.Precision = GetNumberPrecision(tokenValue);
                 *      int? scale = null;
                 *      if (literalInferredDataType.Precision.HasValue)
                 *      {
                 *              var indexDecimalDigit = tokenValue.IndexOf('.');
                 *              if (indexDecimalDigit != -1)
                 *              {
                 *                      scale = tokenValue.Length - indexDecimalDigit - 1;
                 *              }
                 *      }
                 *
                 *      literalInferredDataType.Scale = scale;
                 * }*/

                break;

            case Terminals.Date:
                if (expressionNode.TerminalCount == 2)
                {
                    literalInferredDataTypeName = TerminalValues.Date;
                }

                break;

            case Terminals.Timestamp:
                if (expressionNode.TerminalCount == 2)
                {
                    var timestampStringValue = expressionNode.LastTerminalNode.Token.Value.ToPlainString();
                    var timeZoneElement      = OracleStatementValidator.TimestampValidator.Match(timestampStringValue).Groups["Timezone"];
                    literalInferredDataTypeName   = timeZoneElement.Success ? OracleDatabaseModelBase.BuiltInDataTypeTimestampWithTimeZone : TerminalValues.Timestamp;
                    literalInferredDataType.Scale = 9;
                }

                break;
            }

            if (literalInferredDataTypeName != null)
            {
                literalInferredDataType.FullyQualifiedName = OracleObjectIdentifier.Create(null, literalInferredDataTypeName);
                column.DataType = literalInferredDataType;
                column.Nullable = nullable;
                return(true);
            }

            return(false);
        }
Exemplo n.º 11
0
        private static void ResolveTypeMetadata(OracleDataTypeReference dataTypeReference)
        {
            var dataTypeNode = dataTypeReference.RootNode;

            var isSqlDataType          = String.Equals(dataTypeNode.Id, NonTerminals.DataType);
            var isAssociativeArrayType = String.Equals(dataTypeNode.Id, NonTerminals.AssociativeArrayIndexType);

            if (!isSqlDataType &&
                !String.Equals(dataTypeNode.Id, NonTerminals.PlSqlDataType) &&
                !String.Equals(dataTypeNode.Id, NonTerminals.PlSqlDataTypeWithoutConstraint) &&
                !isAssociativeArrayType)
            {
                throw new ArgumentException($"RootNode ID must be '{NonTerminals.DataType}' or '{NonTerminals.PlSqlDataType}' but is {dataTypeNode.Id}. ", nameof(dataTypeReference));
            }

            var owner = String.Equals(dataTypeNode.FirstTerminalNode.Id, Terminals.SchemaIdentifier)
                                ? dataTypeNode.FirstTerminalNode.Token.Value
                                : String.Empty;

            var dataType = dataTypeReference.ResolvedDataType = new OracleDataType();

            var builtInDataTypeNode = isAssociativeArrayType
                                ? dataTypeNode
                                : dataTypeNode[NonTerminals.BuiltInDataType];

            string name;

            if (builtInDataTypeNode != null)
            {
                var isVarying = builtInDataTypeNode[Terminals.Varying] != null;

                switch (builtInDataTypeNode.FirstTerminalNode.Id)
                {
                case Terminals.Double:
                    name = TerminalValues.BinaryDouble;
                    break;

                case Terminals.Long:
                    name = builtInDataTypeNode.ChildNodes.Count > 1 && String.Equals(builtInDataTypeNode.ChildNodes[1].Id, Terminals.Raw)
                                                        ? "LONG RAW"
                                                        : TerminalValues.Long;
                    break;

                case Terminals.Interval:
                    var yearToMonthNode = builtInDataTypeNode[NonTerminals.YearToMonthOrDayToSecond, NonTerminals.IntervalYearToMonth];
                    if (yearToMonthNode == null)
                    {
                        var dayToSecondNode = builtInDataTypeNode[NonTerminals.YearToMonthOrDayToSecond, NonTerminals.IntervalDayToSecond];
                        name = dayToSecondNode == null ? String.Empty : OracleDatabaseModelBase.BuiltInDataTypeIntervalDayToSecond;
                    }
                    else
                    {
                        name = OracleDatabaseModelBase.BuiltInDataTypeIntervalYearToMonth;
                    }

                    break;

                case Terminals.National:
                    name = isVarying ? TerminalValues.NVarchar2 : TerminalValues.NChar;
                    break;

                case Terminals.Character:
                    name = isVarying ? TerminalValues.Varchar2 : TerminalValues.Char;
                    break;

                default:
                    name = ((OracleToken)builtInDataTypeNode.FirstTerminalNode.Token).UpperInvariantValue;
                    break;
                }

                StatementGrammarNode precisionNode;
                if (String.Equals(name, OracleDatabaseModelBase.BuiltInDataTypeIntervalDayToSecond) ||
                    String.Equals(name, OracleDatabaseModelBase.BuiltInDataTypeIntervalYearToMonth))
                {
                    var intervalPrecisions = builtInDataTypeNode.GetDescendants(NonTerminals.DataTypeSimplePrecision).ToArray();
                    if (intervalPrecisions.Length > 0)
                    {
                        dataType.Precision = GetSimplePrecisionValue(intervalPrecisions[0], out precisionNode);
                        dataTypeReference.PrecisionNode = precisionNode;

                        if (intervalPrecisions.Length == 2)
                        {
                            dataType.Scale = GetSimplePrecisionValue(intervalPrecisions[1], out precisionNode);
                            dataTypeReference.ScaleNode = precisionNode;
                        }
                    }
                }
                else
                {
                    var simplePrecisionNode = builtInDataTypeNode.GetSingleDescendant(NonTerminals.DataTypeSimplePrecision);
                    var precisionValue      = GetSimplePrecisionValue(simplePrecisionNode, out precisionNode);

                    switch (name)
                    {
                    case TerminalValues.Float:
                    case TerminalValues.Timestamp:
                        dataType.Precision = precisionValue;
                        dataTypeReference.PrecisionNode = precisionNode;
                        break;

                    default:
                        dataType.Length = precisionValue;
                        dataTypeReference.LengthNode = precisionNode;
                        break;
                    }

                    TryResolveVarcharDetails(dataTypeReference, builtInDataTypeNode);

                    TryResolveNumericPrecisionAndScale(dataTypeReference, builtInDataTypeNode);
                }
            }
            else if (!isSqlDataType)
            {
                name = ((OracleToken)dataTypeNode.LastTerminalNode.Token).UpperInvariantValue;
            }
            else
            {
                var identifier = dataTypeNode[NonTerminals.SchemaDatatype, Terminals.DataTypeIdentifier];
                name = identifier == null ? String.Empty : ((OracleToken)identifier.Token).UpperInvariantValue;
            }

            dataType.FullyQualifiedName = OracleObjectIdentifier.Create(owner, name);

            dataTypeReference.ResolvedDataType = dataType;
        }