public override SqlExpression VisitTrimFunction(PlSqlParser.TrimFunctionContext context)
        {
            var arg1 = Expression.Build(context.concatenationWrapper());
            var part = "both";

            if (context.LEADING() != null)
            {
                part = "leading";
            }
            else if (context.TRAILING() != null)
            {
                part = "trailing";
            }
            else if (context.BOTH() != null)
            {
                part = "both";
            }

            var toTrim = " ";

            if (context.quoted_string() != null)
            {
                toTrim = InputString.AsNotQuoted(context.quoted_string());
            }

            var arg2 = SqlExpression.Constant(part);
            var arg3 = SqlExpression.Constant(toTrim);

            return(SqlExpression.FunctionCall("SQL_TRIM", new SqlExpression[] { arg1, arg2, arg3 }));
        }
예제 #2
0
        public static SqlStatement Create(PlSqlParser.CreateUserStatementContext context)
        {
            var                   userName = context.userName().GetText();
            SqlExpression         arg;
            SqlIdentificationType type;

            if (context.byPassword() != null)
            {
                arg  = SqlExpression.Constant(InputString.AsNotQuoted(context.byPassword().CHAR_STRING().GetText()));
                type = SqlIdentificationType.Password;
            }
            else if (context.externalId() != null)
            {
                arg  = SqlExpression.Constant(InputString.AsNotQuoted(context.externalId().CHAR_STRING()));
                type = SqlIdentificationType.External;
            }
            else if (context.globalId() != null)
            {
                arg  = SqlExpression.Constant(InputString.AsNotQuoted(context.globalId().CHAR_STRING()));
                type = SqlIdentificationType.Global;
            }
            else
            {
                throw new ParseCanceledException();
            }

            var id = new SqlUserIdentifier(type, arg);

            return(new CreateUserStatement(userName, id));
        }
        public override SqlExpression VisitTimeStampFunction(PlSqlParser.TimeStampFunctionContext context)
        {
            SqlExpression arg;

            if (context.bind_variable() != null)
            {
                arg = SqlExpression.VariableReference(Name.Variable(context.bind_variable()));
            }
            else if (context.argString != null)
            {
                arg = SqlExpression.Constant(InputString.AsNotQuoted(context.argString));
            }
            else
            {
                throw new ParseCanceledException("Invalid argument in a TIMESTAMP implicit function");
            }

            SqlExpression tzArg = null;

            if (context.tzString != null)
            {
                tzArg = SqlExpression.Constant(InputString.AsNotQuoted(context.tzString));
            }

            var args = tzArg != null
                                ? new SqlExpression[] { arg, tzArg }
                                : new SqlExpression[] { arg };

            return(SqlExpression.FunctionCall("TOTIMESTAMP", args));
        }
        public override SqlExpression VisitDateImplicitConvert(PlSqlParser.DateImplicitConvertContext context)
        {
            var s = InputString.AsNotQuoted(context.quoted_string());

            return(SqlExpression.FunctionCall("CAST", new SqlExpression[] {
                SqlExpression.Constant(s),
                SqlExpression.Constant("DATE")
            }));
        }
예제 #5
0
        public static string Simple(PlSqlParser.IdContext context)
        {
            if (context == null)
            {
                return(null);
            }

            return(InputString.AsNotQuoted(context.GetText()));
        }
        public override SqlStatement VisitCreateProcedureStatement(PlSqlParser.CreateProcedureStatementContext context)
        {
            var  procedureName = Name.Object(context.objectName());
            bool orReplace     = context.OR() != null && context.REPLACE() != null;

            var body = context.body();
            var call = context.callSpec();

            RoutineParameter[] parameters = null;
            if (context.parameter() != null &&
                context.parameter().Length > 0)
            {
                parameters = context.parameter().Select(Parameter.Routine).ToArray();
            }

            if (body != null)
            {
                var plsqlBody = Visit(body);

                if (!(plsqlBody is PlSqlBody))
                {
                    throw new ParseCanceledException("Invalid procedure body.");
                }

                var block = ((PlSqlBody)plsqlBody).AsPlSqlStatement();

                var declarationArray = context.declaration();
                if (declarationArray != null && declarationArray.Length > 0)
                {
                    foreach (var declContext in declarationArray)
                    {
                        var declaration = Visit(declContext);
                        block.Declarations.Add(declaration);
                    }
                }

                return(new CreateProcedureStatement(procedureName, parameters, block)
                {
                    ReplaceIfExists = orReplace
                });
            }

            var typeString    = InputString.AsNotQuoted(call.dotnetSpec().typeString.Text);
            var assemblyToken = InputString.AsNotQuoted(call.dotnetSpec().assemblyString);

            if (assemblyToken != null)
            {
                typeString = String.Format("{0}, {1}", typeString, assemblyToken);
            }

            return(new CreateExternalProcedureStatement(procedureName, parameters, typeString));
        }
예제 #7
0
        public static ObjectName Object(PlSqlParser.ObjectNameContext context)
        {
            if (context == null)
            {
                return(null);
            }

            var parts     = context.id().Select(x => x.GetText()).ToArray();
            var realParts = new List <string>();

            foreach (var part in parts)
            {
                if (!String.IsNullOrEmpty(part))
                {
                    var sp = part.Split('.');
                    foreach (var s in sp)
                    {
                        realParts.Add(s);
                    }
                }
            }

            parts = realParts.ToArray();

            ObjectName name = null;

            for (int i = 0; i < parts.Length; i++)
            {
                var part = InputString.AsNotQuoted(parts[i]);

                if (name == null)
                {
                    name = new ObjectName(part);
                }
                else
                {
                    name = new ObjectName(name, part);
                }
            }

            return(name);
        }
예제 #8
0
        private static SetPasswordAction SetPassword(PlSqlParser.AlterUserIdActionContext context)
        {
            string password = null;

            if (context.byPassword() != null)
            {
                password = InputString.AsNotQuoted(context.byPassword().CHAR_STRING().GetText());
            }
            else if (context.externalId() != null)
            {
                throw new NotImplementedException();
            }
            else if (context.globalId() != null)
            {
                throw new NotImplementedException();
            }
            else
            {
                throw new ParseCanceledException();
            }

            return(new SetPasswordAction(SqlExpression.Constant(password)));
        }
        public override SqlExpression VisitConstantString(PlSqlParser.ConstantStringContext context)
        {
            var value = InputString.AsNotQuoted(context.quoted_string());

            return(SqlExpression.Constant(Field.String(value)));
        }
        public override SqlStatement VisitCreateFunctionStatement(PlSqlParser.CreateFunctionStatementContext context)
        {
            var  functionName = Name.Object(context.objectName());
            bool orReplace    = context.OR() != null && context.REPLACE() != null;

            var body = context.body();
            var call = context.callSpec();

            SqlType returnType;

            var returnTypeSpec = context.functionReturnType();

            if (returnTypeSpec.TABLE() != null)
            {
                returnType = new TabularType();
            }
            else if (returnTypeSpec.DETERMINISTIC() != null)
            {
                returnType = Function.DynamicType;
            }
            else
            {
                var typeInfo = new DataTypeVisitor().Visit(returnTypeSpec.primitive_type());
                if (!typeInfo.IsPrimitive)
                {
                    throw new NotSupportedException(String.Format("The return type of function '{0}' ('{1}') is not primitive.",
                                                                  functionName, typeInfo));
                }

                returnType = PrimitiveTypes.Resolve(typeInfo.TypeName, typeInfo.Metadata);
            }

            RoutineParameter[] parameters = null;
            if (context.parameter() != null &&
                context.parameter().Length > 0)
            {
                parameters = context.parameter().Select(Parameter.Routine).ToArray();
            }

            if (body != null)
            {
                var functionBody = Visit(body);

                if (!(functionBody is PlSqlBody))
                {
                    throw new ParseCanceledException("Invalid function body.");
                }

                var plsqlBody = ((PlSqlBody)functionBody).AsPlSqlStatement();

                var declarationArray = context.declaration();
                if (declarationArray != null && declarationArray.Length > 0)
                {
                    foreach (var declContext in declarationArray)
                    {
                        var declaration = Visit(declContext);
                        plsqlBody.Declarations.Add(declaration);
                    }
                }

                return(new CreateFunctionStatement(functionName, returnType, parameters, plsqlBody)
                {
                    ReplaceIfExists = orReplace
                });
            }

            var typeString    = InputString.AsNotQuoted(call.dotnetSpec().typeString.Text);
            var assemblyToken = InputString.AsNotQuoted(call.dotnetSpec().assemblyString);

            if (assemblyToken != null)
            {
                typeString = String.Format("{0}, {1}", typeString, assemblyToken);
            }

            return(new CreateExternalFunctionStatement(functionName, returnType, parameters, typeString));
        }
예제 #11
0
        public static SqlTableColumn Form(PlSqlParser.TableColumnContext context, List <ColumnConstraint> constraints)
        {
            var columnName = Name.Simple(context.columnName());
            var columnType = SqlTypeParser.Parse(context.datatype());

            if (columnType == null)
            {
                throw new ParseCanceledException("No type was found for table.");
            }

            SqlExpression defaultExpression = null;
            bool          identity          = false;
            bool          nullable          = true;
            string        indexType         = null;

            if (context.IDENTITY() != null)
            {
                if (!(columnType is NumericType))
                {
                    throw new InvalidOperationException("Cannot have an identity column that has not a numeric type");
                }

                identity = true;
            }
            else
            {
                var columnConstraints = context.columnConstraint();
                if (columnConstraints != null &&
                    columnConstraints.Length > 0)
                {
                    foreach (var constraintContext in columnConstraints)
                    {
                        if (constraintContext.PRIMARY() != null)
                        {
                            constraints.Add(new ColumnConstraint {
                                ColumnName = columnName,
                                Type       = ConstraintType.PrimaryKey
                            });
                        }
                        else if (constraintContext.UNIQUE() != null)
                        {
                            constraints.Add(new ColumnConstraint {
                                Type       = ConstraintType.Unique,
                                ColumnName = columnName
                            });
                        }
                        else if (constraintContext.NOT() != null &&
                                 constraintContext.NULL() != null)
                        {
                            nullable = false;
                        }
                    }
                }

                if (context.defaultValuePart() != null)
                {
                    defaultExpression = Expression.Build(context.defaultValuePart().expression());
                }

                if (context.columnIndex() != null)
                {
                    var columnIndex = context.columnIndex();
                    if (columnIndex.BLIST() != null)
                    {
                        indexType = DefaultIndexTypes.InsertSearch;
                    }
                    else if (columnIndex.NONE() != null)
                    {
                        indexType = DefaultIndexTypes.BlindSearch;
                    }
                    else if (columnIndex.id() != null)
                    {
                        indexType = Name.Simple(columnIndex.id());
                    }
                    else if (columnIndex.CHAR_STRING() != null)
                    {
                        indexType = InputString.AsNotQuoted(columnIndex.CHAR_STRING());
                    }
                }
            }

            return(new SqlTableColumn(columnName, columnType)
            {
                IsNotNull = !nullable,
                IsIdentity = identity,
                DefaultExpression = defaultExpression,
                IndexType = indexType
            });
        }
예제 #12
0
        public override DataTypeInfo VisitString_type(PlSqlParser.String_typeContext context)
        {
            string size = null;

            if (context.numeric() != null)
            {
                size = Number.PositiveInteger(context.numeric()).ToString();
            }
            else if (context.MAX() != null)
            {
                size = "MAX";
            }

            SqlTypeCode typeCode;

            if (context.CHAR() != null)
            {
                typeCode = SqlTypeCode.Char;
            }
            else if (context.VARCHAR() != null)
            {
                typeCode = SqlTypeCode.VarChar;
            }
            else if (context.STRING() != null)
            {
                typeCode = SqlTypeCode.String;
            }
            else if (context.CLOB() != null)
            {
                typeCode = SqlTypeCode.Clob;
            }
            else if (!context.long_varchar().IsEmpty)
            {
                typeCode = SqlTypeCode.LongVarChar;
            }
            else
            {
                throw new ParseCanceledException("Invalid string type");
            }

            string encoding = null;

            if (context.ENCODING() != null)
            {
                encoding = InputString.AsNotQuoted(context.encoding.Text);
            }

            string locale = null;

            if (context.LOCALE() != null)
            {
                locale = InputString.AsNotQuoted(context.locale.Text);
            }

            var meta = new List <DataTypeMeta>();

            if (size != null)
            {
                meta.Add(new DataTypeMeta("MaxSize", size));
            }
            if (locale != null)
            {
                meta.Add(new DataTypeMeta("Locale", locale));
            }
            if (encoding != null)
            {
                meta.Add(new DataTypeMeta("Encoding", encoding));
            }

            return(new DataTypeInfo(typeCode.ToString().ToUpperInvariant(), meta.ToArray()));
        }