Esempio n. 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;
 }
Esempio n. 2
0
 public LambdaExpr(object id, Executive.Parameter[] parameters, Type returnType, object body)
 {
     Name = new FuncName(id, parameters);
     _parameters = parameters;
     _retType = returnType;
     _body = body;
     _compiledBody = new FunctionLink();
     _compiled = false;
     Isolate = true;
 }        
Esempio n. 3
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);
 }
Esempio n. 4
0
 public override void Bind(Executive.Parameter[] parameters, MemoryPool pool)
 {
     m_context = new SymbolLink(typeof(IContextProvider));            
     pool.Bind(m_context);
     object data = QueryContext.Resolver.GetCurrentStack();
     QueryContext.Resolver.SetValue(ID.Context, m_context);
     m_compiledBody = new FunctionLink[m_expr.Length];
     for (int k = 0; k < m_expr.Length; k++)
     {
         m_compiledBody[k] = new FunctionLink();
         QueryContext.Engine.Compile(parameters, m_expr[k], m_compiledBody[k]);
     }
     if (Annotation != null)
     {
         m_compiledAnnotation = new FunctionLink[Annotation.Length];
         for (int k = 0; k < Annotation.Length; k++)
         {
             m_compiledAnnotation[k] = new FunctionLink();
             QueryContext.Engine.Compile(parameters, Annotation[k], m_compiledAnnotation[k]);
         }
     }
     QueryContext.Resolver.RevertToStack(data);
 }
Esempio n. 5
0
 public override void Bind(Executive.Parameter[] parameters, MemoryPool pool)
 {
     m_valueExpr = new FunctionLink();
     QueryContext.Engine.Compile(parameters, m_expr, m_valueExpr);
     m_value.IsStatic = true;
     SymbolLink[] depends = QueryContext.Engine.GetValueDependences(null, parameters, m_expr, m_valueExpr, false);
     foreach (SymbolLink s in depends)
         if (!s.IsStatic)
         {
             m_value.IsStatic = false;
             break;
         }
     object data = QueryContext.Resolver.GetCurrentStack();
     pool.Bind(m_value);
     QueryContext.Resolver.SetValue(m_var, m_value);
     if (ConditionExpr != null)
     {
         m_conditionExpr = new FunctionLink();
         QueryContext.Engine.Compile(parameters, ConditionExpr, m_conditionExpr);
     }
     m_bodyExpr.Bind(parameters, pool);
     QueryContext.Resolver.RevertToStack(data);
 }
Esempio n. 6
0
 public object Apply(object id, Parameter[] parameters, object lval, object[] args, FunctionLink dynamicFunc, MemoryPool pool)
 {
     object res = Undefined.Value;            
     if (dynamicFunc != null && dynamicFunc.Value != null)
     {
         CompiledLambda lambda = dynamicFunc.Value;
         res = lambda.Invoke(pool, args);
         return res;
     }
     else
     {
         CompiledLambda lambda = Compile(parameters, lval);
         if (dynamicFunc != null)
             dynamicFunc.Value = lambda;
         res = lambda.Invoke(pool, args);
     }            
     return res;
 }
Esempio n. 7
0
 public IBindableObject[] GetBindableObjects(Parameter[] parameters, object expr, FunctionLink dynamicFunc)
 {
     List<IBindableObject> res = new List<IBindableObject>();
     if (dynamicFunc.Value == null)
         Compile(parameters, expr, dynamicFunc);
     foreach (object cn in dynamicFunc.Value.Consts)
     {
         IBindableObject bo = cn as IBindableObject;
         if (bo != null)
             res.Add(bo);
     }
     return res.ToArray();
 }
Esempio n. 8
0
 public SymbolLink[] GetValueDependences(HashSet<Object> hs, Parameter[] parameters, object expr, 
     FunctionLink dynamicFunc, bool reviewLambdaExpr)
 {
     if (dynamicFunc.Value == null)
         Compile(parameters, expr, dynamicFunc);
     List<SymbolLink> res = new List<SymbolLink>();
     GetValueDependences(hs, parameters, dynamicFunc.Value, res, reviewLambdaExpr);
     return res.ToArray();
 }
Esempio n. 9
0
 public Type Compile(Parameter[] parameters, object expr, FunctionLink dynamicFunc)
 {
     try
     {
         CompiledLambda lambda = Compile(parameters, expr);
         if (dynamicFunc != null)
             dynamicFunc.Value = lambda;
         return lambda.ReturnType;
     }
     catch (Exception ex)
     {
         HandleRuntimeException(ex);
         throw;
     }
 }
Esempio n. 10
0
 public override void Bind(Executive.Parameter[] parameters, MemoryPool pool)
 {
     m_compiledExpr = new FunctionLink();
     QueryContext.Engine.Compile(parameters, m_expr, m_compiledExpr);
     m_bodyExpr.Bind(parameters, pool);
 }
Esempio n. 11
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);            
 }        
Esempio n. 12
0
 public static bool ForAny([Implict] Executive engine, Resultset rs, object param, object body)        
 {
     FunctionLink compiledBody = new FunctionLink();
     Executive.Parameter[] parameters = new Executive.Parameter[1];
     parameters[0].ID = param;
     parameters[0].Type = typeof(System.Object);
     object[] args = new object[1];
     while (rs.Begin != null)
     {
         Row row = rs.Dequeue();
         if (!row.HasNullValues)
         {
             if (row.Length == 1)
                 args[0] = row.ItemArray[0];
             else
                 args[0] = Lisp.List(row.ItemArray);
             object res = engine.Apply(null, parameters, body, args, compiledBody, engine.DefaultPool);
             if (res != null && res != Undefined.Value)
             {
                 rs.Cancel();
                 return true;
             }
         }
     }
     return false;
 }
Esempio n. 13
0
 public override void Bind(Executive.Parameter[] parameters, MemoryPool pool)
 {
     m_valueExpr = new FunctionLink();
     QueryContext.Engine.Compile(parameters, m_expr, m_valueExpr);
     object data = QueryContext.Resolver.GetCurrentStack();
     pool.Bind(m_value);
     QueryContext.Resolver.SetValue(m_var, m_value);
     if (m_pos != null)
     {
         pool.Bind(m_posValue);
         QueryContext.Resolver.SetValue(m_pos, m_posValue);
     }
     if (ConditionExpr != null)
     {
         m_conditionExpr = new FunctionLink();
         QueryContext.Engine.Compile(parameters, ConditionExpr, m_conditionExpr);
     }
     m_bodyExpr.Bind(parameters, pool);
     QueryContext.Resolver.RevertToStack(data);
 }
Esempio n. 14
0
 public void Compile()
 {
     CheckDisposed();
     if (String.IsNullOrEmpty(CommandText))
         throw new XQueryException("CommandText is empty string");
     TokenizerBase tok = new Tokenizer(CommandText);
     Notation notation = new Notation();
     YYParser parser = new YYParser(notation);
     parser.yyparseSafe(tok);
     if (BaseUri != null)
         m_context.BaseUri = BaseUri;
     if (SearchPath != null)
         m_context.SearchPath = SearchPath;
     m_context.SchemaProcessing = SchemaProcessing;
     m_context.Engine.Prepare();
     SymbolLink contextLink = new SymbolLink(typeof(IContextProvider));
     m_context.Engine.DefaultPool.Bind(contextLink);
     m_context.Engine.DefaultPool.SetData(contextLink, this);
     m_context.Resolver.SetValue(ID.Context, contextLink);
     Translator translator = new Translator(m_context);
     translator.PreProcess(notation);
     if (OnPreProcess != null) // Chance to process custom declare option statement
         OnPreProcess(this, EventArgs.Empty);
     m_res = translator.Process(notation);
     if (m_res == null)
         throw new XQueryException("Can't run XQuery function module");
     m_res.Bind(null, m_context.Engine.DefaultPool);
     // Compile variables and check it for XQST0054
     m_vars = new FunctionLink[m_context.variables.Count];
     Dictionary<SymbolLink, SymbolLink[]> dependences = new Dictionary<SymbolLink, SymbolLink[]>();
     for (int k = 0; k < m_context.variables.Count; k++)
     {
         XQueryContext.VariableRecord rec = m_context.variables[k];
         m_vars[k] = new FunctionLink();
         HashSet<Object> hs = new HashSet<object>();
         dependences.Add(rec.link, m_context.Engine.GetValueDependences(hs, null, rec.expr, m_vars[k], true));
     }
     int i = 0;
     foreach (KeyValuePair<SymbolLink, SymbolLink[]> kvp in dependences)
     {
         List<SymbolLink> closure = new List<SymbolLink>();
         HashSet<SymbolLink> hs = new HashSet<SymbolLink>();
         hs.Add(kvp.Key);
         closure.AddRange(kvp.Value);
         bool expand;
         do
         {
             expand = false;
             for (int k = 0; k < closure.Count; k++)
             {
                 SymbolLink[] values;
                 if (dependences.TryGetValue(closure[k], out values))
                 {
                     if (hs.Contains(closure[k]))
                         throw new XQueryException(Properties.Resources.XQST0054, m_context.variables[i].id);
                     hs.Add(closure[k]);
                     closure.RemoveAt(k);
                     closure.AddRange(values);
                     expand = true;
                     break;
                 }
             }
         }
         while (expand);
         i++;
     }
     m_compiled = true;
 }