Exemple #1
0
 public FilterContext(DataFilter owner, Resultset source, Object filterPredicate, QueryContext queryContext, Object[] parameters)
     : base(owner, source, queryContext, parameters)
 {
     _source = source;
     _compiledBody = new FunctionLink();
     List<ColumnBinding> bindings = new List<ColumnBinding>();
     ExpressionTransformer transformer = new ExpressionTransformer(bindings);
     foreach (RowType.TypeInfo ti in source.RowType.Fields)
         foreach (RowType.TypeInfo nested_ti in ti.NestedType.Fields)
             if (!nested_ti.IsHidden)
             {
                 ColumnBinding b = new ColumnBinding();
                 b.typecode = Type.GetTypeCode(nested_ti.DataType);
                 b.rnum = ti.Ordinal;
                 b.TableName = ti.Name;
                 b.Name = nested_ti.Name;
                 b.fieldType = nested_ti;
                 b.natural = nested_ti.IsNatural;
                 b.container = nested_ti.IsContainer;
                 b.caseSensitive = nested_ti.IsCaseSensitive;
                 b.data = new SymbolLink(nested_ti.DataType);
                 bindings.Add(b);
                 if (nested_ti.IsContainer)
                     transformer.NeedTransform = true;
             }
     LispExecutive.Enter(_resolver = new DataResolver(new Binder(bindings)));
     if (transformer.NeedTransform)
         _filterPredicate = transformer.Process(filterPredicate);
     else
         _filterPredicate = filterPredicate;
 }
Exemple #2
0
 public QueryContext(QueryContext owner, bool allowCorrelation)
 {
     this.owner = owner;
     this.allowCorrelation = allowCorrelation;
     tables = new List<string>();
     bindings = new List<Binding>();
 }
Exemple #3
0
 private void ProcessQueryExp(Notation notation, Symbol qexpr, QueryContext context)        
 {
     Notation.Record[] recs = notation.Select(qexpr,
         new Descriptor[] { Descriptor.Union, Descriptor.Except }, 2);
     if (recs.Length > 0)
     {
         ProcessQueryExp(notation, recs[0].Arg0, context);
         ProcessQueryTerm(notation, recs[0].Arg1, context);
     }
     else
         ProcessQueryTerm(notation, qexpr, context);
     ConfirmBindings(notation, qexpr, context);
 }
Exemple #4
0
 public SelectorContext(DataSelector owner, bool distinct, Column[] columns, DataResolver resolver,
     Resultset source, QueryContext queryContext, object[] parameters, FunctionLink[] compiledBody)
     : base(owner, source, queryContext, parameters)
 {
     _columns = columns;
     _source = source;
     _resolver = resolver;
     _topRows = owner.TopRows;
     _compiledBody = compiledBody;
     RowNum = 1;
     if (distinct)
         _distinctSet = new HashSet<Row>(new RowEqualityComparer());
     LispExecutive.Enter(resolver);
 }
Exemple #5
0
 public void OpenQuery(string queryText, string baseUri)
 {
     MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;
     DataSourceTreeController dsController = mainWindow.DatasourceController;
     DocumentController docController = mainWindow.Controller;
     context = null;
     if (reader != null)
         reader.Close();            
     command = new Command(dsController.Dictionary);
     string searchPath = docController.SearchPath;
     if (!String.IsNullOrEmpty(baseUri) && !String.IsNullOrEmpty(searchPath))
         searchPath = baseUri + ";" + searchPath;
     command.SearchPath = searchPath;
     command.CommandText = queryText;
     command.BeforeExecute += new QueryExecuteDelegate(command_BeforeExecute);
 }
Exemple #6
0
 public override Resultset Get(QueryContext queryContext, object[] parameters)
 {
     Resultset source = ChildNodes[0].Get(queryContext, parameters);
     RowType.Locator[] locator = new RowType.Locator[_columns.Length];
     int[] way = new int[_columns.Length];
     Binder binder = new Binder(source);
     for (int k = 0; k < _columns.Length; k++)
     {
         Column col = _columns[k];
         locator[k] = col.Name is String ? binder.GetLocator((string)col.Name) :
             binder.GetLocator(((int)col.Name) - 1);
         way[k] = col.Direction ==
            SortDirection.Descending ? -1 : 1;
     }
     return new Resultset(source.RowType, new SorterContext(source, 
         new ComplexComparer(locator, way)));
 }
Exemple #7
0
 private void command_BeforeExecute(Command source, Notation notation, Optimizer optimizer, QueryContext context)
 {
     this.context = context;
     Notation.Record[] recs = notation.Select(Descriptor.Root, 1);
     if (recs.Length > 0)
     {
         Notation.Record[] recsd = notation.Select(recs[0].Arg0, Descriptor.Binding, 1);
         if (recsd.Length > 0)
             throw new ESQLException("Query parameters is not supported in XQueryConsole", null);
     }
     if (context.UseSampleData)
     {
         String path = Path.Combine(
             Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Data");
         if (Directory.Exists(path))
             context.DatabaseDictionary.SearchPath = path;                
     }
 }
Exemple #8
0
        private void ProcessJoinedTable(Notation notation, Symbol sym, QueryContext context)
        {
            Notation.Record[] recs = notation.Select(sym, new Descriptor[] {
                Descriptor.CrossJoin,
                Descriptor.UnionJoin,
                Descriptor.NaturalJoin,
                Descriptor.QualifiedJoin,
                Descriptor.Branch });

            switch (recs[0].descriptor)
            {
                case Descriptor.CrossJoin:
                    ProcessTableRefSimple(notation, recs[0].Arg0, context);
                    ProcessTableRef(notation, recs[0].Arg1, context);
                    break;

                case Descriptor.UnionJoin:
                    ProcessTableRefSimple(notation, recs[0].Arg0, context);
                    ProcessTableRef(notation, recs[0].Arg1, context);
                    break;

                case Descriptor.NaturalJoin:
                    ProcessTableRefSimple(notation, recs[0].Arg0, context);
                    ProcessTableRef(notation, recs[0].Arg1, context);
                    break;

                case Descriptor.QualifiedJoin:
                    ProcessTableRefSimple(notation, recs[0].Arg0, context);
                    ProcessTableRef(notation, recs[0].Arg1, context);
                    recs = notation.Select(sym, Descriptor.JoinSpec, 1);
                    if (recs.Length > 0)
                        ScanExpr(notation, recs[0].Arg0, context);                    
                    break;

                case Descriptor.Branch:
                    ProcessTableRef(notation, recs[0].Arg0, context);
                    break;
            }
        }
Exemple #9
0
 public abstract Resultset Get(QueryContext queryContext, object[] parameters);        
Exemple #10
0
 public QueryContext(QueryContext owner)
     : this(owner, true)
 {
 }
Exemple #11
0
 private void ProcessParameter(Notation notation, ref object arg, QueryContext context)
 {
     Parameter param = (Parameter)arg;
     Stack<Binding> bindings = new Stack<Binding>();
     object src = null;
     for (QueryContext curr = context; curr != null; curr = curr.owner)
     {
         src = null;
         for (int k = 0; k < curr.bindings.Count; k++)
             if (param.Equals(curr.bindings[k].target))
             {
                 src = curr.bindings[k].placeholder;
                 break;
             }
         Binding b;
         if (src == null)
         {
             b = new Binding();
             b.target = param;
             b.placeholder = curr.CreatePlaceholder();
             bindings.Push(b);
             curr.bindings.Add(b);
         }
         else
             break;
     }
     while (bindings.Count > 0)
     {
         Binding b = bindings.Pop();
         b.src = src;
         src = b.placeholder;
     }
     arg = src;
 }
Exemple #12
0
 private void ProcessQualifiedName(Notation notation, ref object arg, QueryContext context)
 {
     Qname qname = (Qname)arg;
     if (qname.Qualifier != null)
     {
         Stack<Binding> bindings = new Stack<Binding>();
         for (QueryContext curr = context; curr != null; curr = curr.owner)
         {                    
             object src = null;
             if (curr.tables.Contains(qname.Qualifier))
                 src = qname;
             else
                 for (int k = 0; k < curr.bindings.Count; k++)
                     if (qname.Equals(curr.bindings[k].target))
                     {
                         src = curr.bindings[k].placeholder;
                         break;
                     }
             Binding b;
             if (src == null)
             {
                 b = new Binding();
                 b.target = qname;
                 b.placeholder = curr.CreatePlaceholder();
                 bindings.Push(b);
                 curr.bindings.Add(b);
             }
             else
             {
                 while (bindings.Count > 0)
                 {
                     b = bindings.Pop();
                     b.src = src;
                     src = b.placeholder;
                 }
                 arg = src;
                 return;
             }
             if (!curr.allowCorrelation)
                 break;
         }
         throw new ESQLException(Properties.Resources.InvalidIdentifier, qname);
     }            
 }
Exemple #13
0
 public LispProcessingContext(QueryNode node, Resultset[] src, QueryContext queryContext, Object[] parameters)
     : base(src)
 {
     _node = node;
     _parameters = parameters;
     _queryContext = queryContext;
     _executive = queryContext.CreateExecutive(this);
 }
Exemple #14
0
 private void ProcessQueryTerm(Notation notation, Symbol qterm, QueryContext context)
 {
     Notation.Record[] recs = notation.Select(qterm, Descriptor.Intersect, 2);
     if (recs.Length > 0)
     {
         ProcessQueryTerm(notation, recs[0].Arg0, context);
         ProcessQueryTerm(notation, recs[0].Arg1, context);
     }
     else
         ProcessSimpleTable(notation, qterm, context);
 }
Exemple #15
0
 private void ProcessSimpleTable(Notation notation, Symbol sym, QueryContext context)
 {
     if (sym.Tag == Tag.SQuery)
         ProcessQuerySpec(notation, sym, context);
 }
Exemple #16
0
 private void ProcessTableRefSimple(Notation notation, Symbol sym, QueryContext context)
 {
     if (sym.Tag != Tag.Qname)
     {
         Notation.Record[] recs = notation.Select(sym, Descriptor.Dynatable, 1);
         if (recs.Length > 0)
             ScanExpr(notation, recs[0].Arg0, context);
         else
             ProcessQueryExp(notation, sym, new QueryContext(context, true));
     }
 }
Exemple #17
0
 private void ProcessTableRef(Notation notation, Symbol sym, QueryContext context)
 {
     if (sym.Tag == Tag.Join)
         ProcessJoinedTable(notation, sym, context);
     else
         ProcessTableRefSimple(notation, sym, context);
 }
Exemple #18
0
 private void ProcessFromClause(Notation notation, Symbol sym, QueryContext context)
 {
     Notation.Record[] recs = notation.Select(sym, Descriptor.From, 1);
     if (recs.Length > 0)
     {
         Symbol[] arr = Lisp.ToArray<Symbol>(recs[0].args[0]);
         for (int k = 0; k < arr.Length; k++)
             ProcessTableRef(notation, arr[k], context);
     }
 }
Exemple #19
0
 private void ProcessSelectList(Notation notation, Symbol sym, QueryContext context)
 {
     Notation.Record[] recs = notation.Select(sym, new Descriptor[] { Descriptor.Select });
     if (recs.Length > 0 && recs[0].args.Length != 0)
         ScanRecord(notation, recs[0], context);
 }
Exemple #20
0
 private void ProcessQuerySpec(Notation notation, Symbol sym, QueryContext context)
 {
     context.tables.Clear();
     TableAnalyzer analyzer = new TableAnalyzer(notation);
     analyzer.WalkFromClause(sym);
     foreach (String name in analyzer.Tables)
         context.tables.Add(name);
     ProcessSelectList(notation, sym, context);
     ProcessFromClause(notation, sym, context);
     ProcessWhereClause(notation, sym, context);
     ProcessHavingClause(notation, sym, context);            
 }
Exemple #21
0
 public override Resultset Get(QueryContext queryContext, object[] parameters)
 {
     // Prepare bindings
     Resultset source = ChildNodes[0].Get(queryContext, parameters);
     List<ColumnBinding> bindings = new List<ColumnBinding>();            
     ExpressionTransformer transformer = new ExpressionTransformer(bindings);
     foreach (RowType.TypeInfo ti in source.RowType.Fields)
         foreach (RowType.TypeInfo nested_ti in ti.NestedType.Fields)
             if (!nested_ti.IsHidden && nested_ti.IsNatural)
             {
                 ColumnBinding b = new ColumnBinding();
                 b.typecode = Type.GetTypeCode(nested_ti.DataType);
                 b.rnum = ti.Ordinal;
                 b.TableName = ti.Name;
                 b.Name = nested_ti.Name;
                 b.fieldType = nested_ti;
                 b.natural = nested_ti.IsNatural;
                 b.container = nested_ti.IsContainer;
                 b.caseSensitive = nested_ti.IsCaseSensitive;
                 b.data = new SymbolLink(nested_ti.DataType);
                 bindings.Add(b);
                 if (nested_ti.IsContainer)
                     transformer.NeedTransform = true;
             }
     foreach (RowType.TypeInfo ti in source.RowType.Fields)
         foreach (RowType.TypeInfo nested_ti in ti.NestedType.Fields)
             if (!nested_ti.IsHidden && !nested_ti.IsNatural)
             {
                 ColumnBinding b = new ColumnBinding();
                 b.typecode = Type.GetTypeCode(nested_ti.DataType);
                 b.rnum = ti.Ordinal;
                 b.TableName = ti.Name;
                 b.Name = nested_ti.Name;
                 b.fieldType = nested_ti;
                 b.natural = nested_ti.IsNatural;
                 b.container = nested_ti.IsContainer;
                 b.caseSensitive = nested_ti.IsCaseSensitive;
                 b.data = new SymbolLink(nested_ti.DataType);
                 bindings.Add(b);
                 if (nested_ti.IsContainer)
                     transformer.NeedTransform = true;
             }
     // Expand columns.
     // On this step we transform select table.* and select * to select field1,...
     List<Column> columns = new List<Column>();
     if (_targets == null) // Select fields from all selected tables
     {
         foreach (ColumnBinding b in bindings)
             columns.Add(new Column(ATOM.Create(null, new string[] { b.TableName, b.Name }, false)));
     }
     else
         foreach (Column col in _targets)
         {
             if (Lisp.IsFunctor(col.Expr, Table)) // Select fields from specified table
             {
                 bool found = false;
                 String name = (String)Lisp.Second(col.Expr);
                 var tableBindings =
                         from b in bindings
                         where b.TableName == name
                         select b;
                 foreach (ColumnBinding b in tableBindings)
                 {
                     columns.Add(new Column(ATOM.Create(null, new string[] { b.TableName, b.Name }, false)));
                     found = true;
                 }
                 if (!found)
                     throw new ESQLException(Properties.Resources.InvalidIdentifier, name);
             }
             else // Select expression specified
                 if (transformer.NeedTransform)
                 {
                     Column c = new Column();
                     c.Alias = col.Alias;
                     c.Expr = transformer.Process(col.Expr);
                     columns.Add(c);
                 }
                 else
                     columns.Add(col);
         }
     // Create demand context   
     FunctionLink[] compiledBody = new FunctionLink[columns.Count];
     SelectorResolver resolver = new SelectorResolver(new Binder(bindings));
     SelectorContext context = new SelectorContext(this, _distinct, columns.ToArray(),
         resolver, source, queryContext, parameters, compiledBody);
     // Create columns type info and generate unique column names by field name and alias
     RowType.TypeInfo[] fields = new RowType.TypeInfo[columns.Count];
     List<String> fieldNames = new List<string>();
     int p = 1;
     for (int k = 0; k < fields.Length; k++)
     {
         String name;
         RowType.TypeInfo fieldType = null;                
         object expr = columns[k].Expr;
         compiledBody[k] = new FunctionLink();
         Type resType = context.LispExecutive.Compile(null, expr, compiledBody[k]);                
         if (Lisp.IsAtom(expr))
         {
             ColumnBinding b = resolver.GetBinding(expr);
             if (b != null)
                 fieldType = b.fieldType;
         }
         else if (Lisp.IsNode(expr) && expr is String)
         {
             fieldType = new RowType.TypeInfo(null, typeof(String),
                 TypeConverter.GetValueSize(expr));
         }
         else if (Lisp.IsFunctor(expr, ID.ParamRef))
         {
             object value = parameters[(int)Lisp.Arg1(expr)];
             fieldType = new RowType.TypeInfo(null, value.GetType(),
                 TypeConverter.GetValueSize(value));
         }
         else if (Lisp.IsFunctor(expr, ID.ToString))
         {
             if (Lisp.Length(expr) == 3)
                 fieldType = new RowType.TypeInfo(null, typeof(String), (int)Lisp.Arg2(expr));
             else
                 fieldType = new RowType.TypeInfo(null, typeof(String), 0);
         }
         else if (Lisp.IsFunctor(expr, ID.ToInt16))
             fieldType = new RowType.TypeInfo(null, typeof(Int16), 0);
         else if (Lisp.IsFunctor(expr, ID.ToInt32))
             fieldType = new RowType.TypeInfo(null, typeof(Int32), 0);
         else if (Lisp.IsFunctor(expr, ID.ToInt64))
             fieldType = new RowType.TypeInfo(null, typeof(Int64), 0);
         else if (Lisp.IsFunctor(expr, ID.ToSingle))
             fieldType = new RowType.TypeInfo(null, typeof(Single), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDouble))
             fieldType = new RowType.TypeInfo(null, typeof(Double), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDecimal))
             fieldType = new RowType.TypeInfo(null, typeof(Decimal), 0);
         else if (Lisp.IsFunctor(expr, ID.ToDateTime))
             fieldType = new RowType.TypeInfo(null, typeof(DateTime), 0);
         else
             fieldType = new RowType.TypeInfo(null, resType, 0);
         if (!String.IsNullOrEmpty(columns[k].Alias))
             name = columns[k].Alias;
         else
             if (fieldType.Name == null)
                 name = String.Format("Expr{0}", p++);
             else
                 name = fieldType.Name;                
         fields[k] = new RowType.TypeInfo(k, 
             Util.CreateUniqueName(fieldNames, name), fieldType);                
     }
     ScanDynatableAccessors(ChildNodes[0], resolver);
     return new Resultset(new RowType(fields), context);            
 }        
Exemple #22
0
 private void ProcessHavingClause(Notation notation, Symbol sym, QueryContext context)
 {
     Notation.Record[] recs = notation.Select(sym, Descriptor.Having, 1);
     if (recs.Length > 0)
         ScanExpr(notation, recs[0].Arg0, context);
 }
Exemple #23
0
 private void ScanExpr(Notation notation, Symbol sym, QueryContext context)
 {
     if (sym.Tag == Tag.SQuery)
         ProcessQueryExp(notation, sym, new QueryContext(context));
     else
         foreach (Notation.Record rec in notation.Select())
             if (rec.sym == sym)
                 ScanRecord(notation, rec, context);
 }
Exemple #24
0
 private void ScanRecord(Notation notation, Notation.Record rec, QueryContext context)
 {
     for (int k = 0; k < rec.args.Length; k++)
     {
         object arg = rec.args[k];
         if (Lisp.IsNode(arg))
         {
             if (arg is Parameter)
                 ProcessParameter(notation, ref rec.args[k], context);
             else if (arg is Qname)
             {
                 if (!IsServerQuery)
                     ProcessQualifiedName(notation, ref rec.args[k], context);
             }
             else if (arg is Symbol)
                 ScanExpr(notation, (Symbol)arg, context);
         }
         else
             for (object curr = arg; curr != null; curr = Lisp.Cdr(curr))
             {
                 object item = Lisp.Car(curr);
                 if (item is Parameter)
                 {
                     ProcessParameter(notation, ref item, context);
                     Lisp.Rplaca(curr, item);
                 }
                 else if (item is Qname)
                 {
                     if (!IsServerQuery)
                     {
                         ProcessQualifiedName(notation, ref item, context);
                         Lisp.Rplaca(curr, item);
                     }
                 }
                 else if (item is Symbol)
                     ScanExpr(notation, (Symbol)item, context);
             }
     }
 }
Exemple #25
0
 public void CloseQuery()
 {
     if (reader != null)
     {
         reader.Close();
         reader = null;
         command = null;
         context = null;
     }
 }
Exemple #26
0
 private void ConfirmBindings(Notation notation, Symbol qexpr, QueryContext context)
 {
     if (context.bindings.Count > 0)
     {
         Symbol[] bindings = new Symbol[context.bindings.Count];
         for (int k = 0; k < bindings.Length; k++)
         {
             bindings[k] = new Symbol(Tag.Binding);
             if (context.bindings[k].src == null)
                 notation.Confirm(bindings[k], Descriptor.Link, context.bindings[k].target);
             else
                 notation.Confirm(bindings[k], Descriptor.Link, context.bindings[k].src);
         }
         notation.Confirm(qexpr, Descriptor.Binding, Lisp.List(bindings));
     }
 }
Exemple #27
0
        public override Resultset Get(QueryContext queryContext, object[] parameters)
        {
            if (ChildNodes.Count < 1)
                throw new InvalidOperationException();

            Resultset rs = ChildNodes[0].Get(queryContext, parameters);
            FilterContext context = new FilterContext(this, rs, FilterPredicate, queryContext, parameters);
            return new Resultset(rs.RowType, context);
        }
Exemple #28
0
 public LispProcessingContext(QueryNode node, Resultset src, QueryContext queryContext, Object[] parameters)
     : this(node, new Resultset[] { src }, queryContext, parameters)
 {
 }