public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType)
 {
     this.npathEngine = npathEngine;
     this.query = query;
     propertyPathTraverser = new PropertyPathTraverser(this);
     this.propertyColumnMap = new Hashtable() ;
     this.npathQueryType = queryType;
 }
Example #2
0
		public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType, IClassMap rootClassMap, Hashtable propertyColumnMap)
		{
			this.npathEngine = npathEngine;
			this.query = query;
			this.rootClassMap = rootClassMap;
			propertyPathTraverser = new PropertyPathTraverser(this);
			this.propertyColumnMap = propertyColumnMap;
			this.npathQueryType = queryType;
		}
Example #3
0
		public SqlEmitter(INPathEngine npathEngine, NPathSelectQuery query,NPathQueryType queryType, IClassMap rootClassMap,SqlSelectStatement parentQuery,IPropertyMap backReference,int subQueryLevel)
		{
			this.npathEngine = npathEngine;
			this.query = query;
			this.rootClassMap = rootClassMap;
			propertyPathTraverser = new PropertyPathTraverser(this);
			this.propertyColumnMap = new Hashtable() ;
			this.npathQueryType = queryType;
			this.parentQuery = parentQuery;
			this.backReference = backReference;
			this.subQueryLevel = subQueryLevel;
		}
        public virtual string ToSql(string npath, NPathQueryType queryType, Type type, ref Hashtable propertyColumnMap, ref IList outParameters, IList inParameters)
        {
            NPathParser      parser = new NPathParser();
            NPathSelectQuery query  = parser.ParseSelectQuery(npath, inParameters);

            IClassMap rootClassMap = this.Context.DomainMap.MustGetClassMap(type);

            this.ResultParameters = new ArrayList();

            SqlEmitter sqlEmitter = new SqlEmitter(this, query, queryType, rootClassMap, propertyColumnMap);

            string sql = sqlEmitter.EmitSql();

            outParameters = sqlEmitter.ResultParameters;

            return(sql);
        }
        public virtual NPathQueryType GetNPathQueryType(string npath)
        {
            NPathQueryType npathQueryType = NPathQueryType.SelectObjects;

            try
            {
                NPathParser      parser = new NPathParser();
                NPathSelectQuery query  = parser.ParseSelectQuery(npath);

                npathQueryType = GetNPathQueryType(query);
            }
            catch
            {
                npathQueryType = NPathQueryType.SelectObjects;
            }

            return(npathQueryType);
        }
        public virtual string ToSql(string npath,NPathQueryType queryType, Type type, ref Hashtable propertyColumnMap, ref IList outParameters, IList inParameters)
        {
            NPathParser parser = new NPathParser() ;
            NPathSelectQuery query = parser.ParseSelectQuery(npath, inParameters) ;

            IClassMap rootClassMap = this.Context.DomainMap.MustGetClassMap(type);

            this.ResultParameters = new ArrayList();

            SqlEmitter sqlEmitter = new SqlEmitter(this, query,queryType, rootClassMap, propertyColumnMap);

            string sql = sqlEmitter.EmitSql();

            outParameters = sqlEmitter.ResultParameters;

            return sql;
        }
Example #7
0
        private void TraverseSingleSpan(ArrayList propertyMaps, Hashtable selectedColumns, ArrayList columnOrder, string special, string suggestion)
        {
            IPropertyMap parentMap = null;
            string       path      = "";
            string       prevPath  = "";

            foreach (IPropertyMap pathMap in propertyMaps)
            {
                IPropertyMap propertyMap = pathMap;
                if (propertyMaps.IndexOf(propertyMap) < propertyMaps.Count - 1)
                {
                    NPathQueryType queryType = this.sqlEmitter.npathQueryType;
                    if (queryType == NPathQueryType.SelectObjects || queryType == NPathQueryType.SelectMixed)
                    {
                        if (path == "")
                        {
                            GetPropertyColumnNamesAndAliases(propertyMap, propertyMap, selectedColumns, columnOrder, propertyMap.Name, propertyMap.Name, "");
                        }
                        else
                        {
                            GetPropertyColumnNamesAndAliases(propertyMap, path, selectedColumns, columnOrder, path, path + "." + propertyMap.Name, "");
                        }
                    }
                    prevPath = path;
                    if (path.Length > 0)
                    {
                        path += ".";
                    }
                    path += propertyMap.Name;
                    JoinType joinType;
                    if (HasNullableColumn(propertyMap) || propertyMap.IsCollection)
                    {
                        joinType = JoinType.OuterJoin;
                    }
                    else
                    {
                        joinType = JoinType.InnerJoin;
                    }
                    joinTree.SetupJoin(propertyMap, parentMap, path, joinType);
                }
                else
                {
                    if (special == "*")
                    {
                        IClassMap classMap = null;
                        if (propertyMaps.Count == 1)
                        {
                            classMap = this.sqlEmitter.RootClassMap;
                        }
                        else
                        {
                            //Should actually take refClassMap from previous prop in path!!
                            classMap = propertyMap.ClassMap;
                        }

                        foreach (IPropertyMap iPropertyMap in classMap.GetAllPropertyMaps())
                        {
                            if (!(iPropertyMap.ReferenceType != ReferenceType.None && iPropertyMap.AdditionalColumns.Count > 0))
                            {
                                if (iPropertyMap.IsCollection)
                                {
                                    if (this.SqlEmitter.NPathEngine.Context.PersistenceManager.GetListCountLoadBehavior(LoadBehavior.Default, iPropertyMap) == LoadBehavior.Eager)
                                    {
                                        if (iPropertyMap.GetIdColumnMap() != null)
                                        {
                                            if (path == "")
                                            {
                                                GetListPropertySubselectAndAlias(iPropertyMap, iPropertyMap.ClassMap, selectedColumns, columnOrder, iPropertyMap.Name, "");
                                            }
                                            else
                                            {
                                                if (prevPath != "")
                                                {
                                                    GetListPropertySubselectAndAlias(iPropertyMap, prevPath, selectedColumns, columnOrder, path + "." + iPropertyMap.Name, suggestion);
                                                }
                                                else
                                                {
                                                    GetListPropertySubselectAndAlias(iPropertyMap, path, selectedColumns, columnOrder, path + "." + iPropertyMap.Name, suggestion);
                                                }
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    //This if should be removed some day when the "JoinNonPrimary()" call a bit further down
                                    //has been refined to handle nullable OneToOne slaves...
                                    if (!(iPropertyMap.ReferenceType == ReferenceType.OneToOne && iPropertyMap.IsSlave && HasNullableColumn(iPropertyMap)))
                                    {
                                        if (iPropertyMap.Column.Length > 0)
                                        {
                                            //Exclude inverse property to property leading to this point in the path
                                            bool isInverse = false;
                                            if (parentMap != null)
                                            {
                                                if (iPropertyMap.Inverse.Length > 0)
                                                {
                                                    if (parentMap == iPropertyMap.GetInversePropertyMap())
                                                    {
                                                        isInverse = true;
                                                    }
                                                }
                                            }
                                            if (!isInverse)
                                            {
                                                if (path == "")
                                                {
                                                    GetPropertyColumnNamesAndAliases(iPropertyMap, iPropertyMap, selectedColumns, columnOrder, iPropertyMap.Name, iPropertyMap.Name, "");
                                                }
                                                else
                                                {
                                                    // here
                                                    if (parentMap != null && parentMap.ReferenceType == ReferenceType.ManyToOne && prevPath != "")
                                                    {
                                                        GetPropertyColumnNamesAndAliases(iPropertyMap, prevPath, selectedColumns, columnOrder, path + "." + iPropertyMap.Name, path + "." + iPropertyMap.Name, suggestion);
                                                    }
                                                    else
                                                    {
                                                        GetPropertyColumnNamesAndAliases(iPropertyMap, path, selectedColumns, columnOrder, path + "." + iPropertyMap.Name, path + "." + iPropertyMap.Name, suggestion);
                                                    }

                                                    //GetPropertyColumnNamesAndAliases(iPropertyMap, path, selectedColumns, columnOrder, path + "." + iPropertyMap.Name, path + "." + iPropertyMap.Name, suggestion);
                                                }
                                                if (iPropertyMap.MustGetTableMap() != iPropertyMap.ClassMap.MustGetTableMap())
                                                {
                                                    JoinNonPrimary(iPropertyMap);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (path == "")
                        {
                            GetPropertyColumnNamesAndAliases(propertyMap, propertyMap, selectedColumns, columnOrder, propertyMap.Name, propertyMap.Name, suggestion);
                        }
                        else
                        {
                            //GetPropertyColumnNamesAndAliases(propertyMap, path, selectedColumns, columnOrder, path, path + "." + propertyMap.Name, suggestion);
                            GetPropertyColumnNamesAndAliases(propertyMap, path, selectedColumns, columnOrder, path + "." + propertyMap.Name, path + "." + propertyMap.Name, suggestion);
                        }
                        if (propertyMap.MustGetTableMap() != propertyMap.ClassMap.MustGetTableMap())
                        {
                            JoinNonPrimary(propertyMap);
                        }
                    }
                }
                parentMap = propertyMap;
            }
        }
Example #8
0
        private void RunQuery(bool eval)
        {
            string query = GetQuery();

            if (query == "")
            {
                MessageBox.Show("You must enter a query first!");
                return;
            }
            try
            {
                NPathQuery npath    = new NPathQuery(query);
                IContext   context  = GetContext();
                IClassMap  classMap = context.NPathEngine.GetRootClassMap(query, context.DomainMap);
                Type       type     = context.AssemblyManager.MustGetTypeFromClassMap(classMap);

                if (type == null)
                {
                    throw new Exception("Could not find type for classMap " + classMap.Name);
                }

                npath.Context     = context;
                npath.PrimaryType = type;

                NPathQueryType npathQueryType = context.NPathEngine.GetNPathQueryType(query);

                string sql = npath.ToSql();
                textSql.Text = sql;

                if (!(eval))
                {
                    if (npathQueryType == NPathQueryType.SelectObjects)
                    {
                        DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap()), npath.Parameters);
                        gridRows.DataSource = sqlResult;

                        IList     result = context.GetObjectsByQuery(npath);
                        DataTable table  = GetDataTable(result, context);
                        gridObjects.DataSource = table;
                    }

                    if (npathQueryType == NPathQueryType.SelectScalar)
                    {
                        DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap()));
                        gridRows.DataSource = sqlResult;

                        object    result = context.ExecuteScalar(npath);
                        DataTable table  = GetScalarDataTable(result);
                        gridObjects.DataSource = table;
                    }

                    if (npathQueryType == NPathQueryType.SelectTable)
                    {
                        DataTable sqlResult = context.SqlExecutor.ExecuteDataTable(sql, context.GetDataSource(classMap.GetSourceMap()));
                        gridRows.DataSource = sqlResult;

                        DataTable result = context.GetDataTable(npath);
                        gridObjects.DataSource = result;
                    }
                }
                context.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public virtual string ToSql(string npath, Type type, ref Hashtable propertyColumnMap, ref IList outParameters, IList inParameters)
        {
            NPathQueryType queryType = GetNPathQueryType(npath);

            return(ToSql(npath, queryType, type, ref propertyColumnMap, ref outParameters, inParameters));
        }