private QvxTable GetData(ScriptCode script, ConnectorParameter parameter)
        {
            try
            {
                var qlikApp = AppInstance.GetQlikInstance(parameter, script.AppId);
                if (qlikApp == null)
                {
                    return(new QvxTable());
                }
                foreach (var filter in script.Filter)
                {
                    logger.Debug($"Filter: {filter}");
                    foreach (var value in filter.Values)
                    {
                        logger.Debug($"");
                        var result = qlikApp.FirstSession.Selections.SelectValue(filter.Name, value);
                        if (result == false)
                        {
                            logger.Error($"The Dimension \"{filter.Name}\" could not found.");
                            return(null);
                        }
                    }
                }

                var resultTable = tableFunctions.GetTableInfosFromApp($"Table_{script.AppId}_{script.ObjectId}", script, parameter, qlikApp);
                return(resultTable.QvxTable);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "The table script can not be executed.");
                return(new QvxTable());
            }
        }
        public override QvxDataTable ExtractQuery(string query, List <QvxTable> tables)
        {
            try
            {
                AppInstance.LoadMemory();
                logger.Debug($"Parse query {query}");
                var script = ScriptCode.Parse(query);
                if (script == null)
                {
                    throw new Exception("The sql script is not valid.");
                }

                var parameter = ConnectorParameter.Create(MParameters);
                var qvxTable  = GetData(script, parameter);
                var result    = new QvxDataTable(qvxTable);
                result.Select(qvxTable.Fields);
                AppInstance.SaveMemory();
                AppInstance.Dispose();
                logger.Debug($"Send result table {qvxTable.TableName}");
                return(result);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "The query could not be executed.");
                LogManager.Flush();
                return(new QvxDataTable(new QvxTable()
                {
                    TableName = "Error"
                }));
            }
        }
 private QvDataContractResponse GetFields(ConnectorParameter parameter, string appId, string objectId)
 {
     try
     {
         var oId = GetObjectId(objectId);
         if (String.IsNullOrEmpty(oId))
         {
             throw new Exception("no object id for field table found.");
         }
         var script      = ScriptCode.Create(appId, oId);
         var resultTable = tableFunctions.GetTableInfosFromApp("FieldTable", script, parameter);
         if (resultTable == null)
         {
             throw new Exception("no field table found.");
         }
         return(new QvDataContractFieldListResponse {
             qFields = resultTable.QvxTable.Fields
         });
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"fields from app {appId} and table {objectId} not loaded.");
         return(new QvDataContractFieldListResponse {
             qFields = new QvxField[0]
         });
     }
 }
Example #4
0
        private QvDataContractResponse GetPreview(UserParameter parameter, string appId, string objectId)
        {
            q2gconhypercubemain.Connection connection = null;

            try
            {
                var oId = GetObjectId(objectId);
                if (String.IsNullOrEmpty(oId))
                {
                    throw new Exception("no object id for preview table found.");
                }
                var config  = QlikApp.CreateConfig(parameter, appId);
                var qlikApp = new QlikApp(parameter);
                connection = qlikApp.CreateNewConnection(config);
                var script      = ScriptCode.Create(appId, oId);
                var resultTable = tableFunctions.GetTableInfosFromApp("PreviewTable", script, connection.CurrentApp);
                if (resultTable == null)
                {
                    throw new Exception("no preview table found.");
                }
                return(PreviewResponse.Create(resultTable.Preview));
            }
            catch (Exception ex)
            {
                logger.Error(ex, $"fields from app {appId} and table {objectId} not loaded.");
                return(new PreviewResponse());
            }
            finally
            {
                connection?.Close();
            }
        }
        private bool IsUsedField(string field, ScriptCode code)
        {
            if (code.Fields.Count == 0)
            {
                return(true);
            }

            return(code.Fields.IndexOf(field) > -1);
        }
Example #6
0
        public static ScriptCode Parse(string script)
        {
            try
            {
                var resultScript = new ScriptCode(script);
                if (resultScript.Read())
                {
                    return(resultScript);
                }

                return(null);
            }
            catch (Exception ex)
            {
                throw new Exception("The script is not valid.", ex);
            }
        }
        private ResultTable GetData(ScriptCode script, UserParameter parameter)
        {
            q2gconhypercubemain.Connection connection = null;

            try
            {
                var config  = QlikApp.CreateConfig(parameter, script.AppId);
                var qlikApp = new QlikApp(parameter);
                connection = qlikApp.CreateNewConnection(config);
                if (!connection.Connect())
                {
                    return(null);
                }

                foreach (var filter in script.Filter)
                {
                    logger.Debug($"Filter: {filter}");
                    foreach (var value in filter.Values)
                    {
                        var selection = new QlikSelections(connection.CurrentApp);
                        var result    = selection.SelectValue(filter.Name, value);
                        if (result == false)
                        {
                            logger.Error($"The Dimension \"{filter.Name}\" could not found.");
                            return(null);
                        }
                    }
                }

                var resultTable = tableFunctions.GetTableInfosFromApp($"Table_{script.AppId}_{script.ObjectId}", script, connection.CurrentApp);
                return(resultTable.QvxTable);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "The table script can not be executed.");
                return(null);
            }
            finally
            {
                connection?.Close();
            }
        }
Example #8
0
        private QvDataContractResponse GetFields(UserParameter parameter, string appId, string objectId)
        {
            q2gconhypercubemain.Connection connection = null;

            using (MappedDiagnosticsLogicalContext.SetScoped("connectionId", connection?.ConnId))
            {
                try
                {
                    var oId = GetObjectId(objectId);
                    if (String.IsNullOrEmpty(oId))
                    {
                        throw new Exception("no object id for field table found.");
                    }
                    var script  = ScriptCode.Create(appId, oId);
                    var config  = QlikApp.CreateConfig(parameter, appId);
                    var qlikApp = new QlikApp(parameter);
                    connection = qlikApp.CreateNewConnection(config);
                    var resultTable = tableFunctions.GetTableInfosFromApp("FieldTable", script, connection.CurrentApp);
                    if (resultTable == null)
                    {
                        throw new Exception("no field table found.");
                    }
                    var qvxTable = TableUtilities.ConvertTable(resultTable.QvxTable);
                    return(new QvDataContractFieldListResponse {
                        qFields = qvxTable.Fields
                    });
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"fields from app {appId} and table {objectId} not loaded.");
                    return(new QvDataContractFieldListResponse {
                        qFields = new QvxField[0]
                    });
                }
                finally
                {
                    connection?.Close();
                }
            }
        }
 private QvDataContractResponse GetPreview(ConnectorParameter parameter, string appId, string objectId)
 {
     try
     {
         var oId = GetObjectId(objectId);
         if (String.IsNullOrEmpty(oId))
         {
             throw new Exception("no object id for preview table found.");
         }
         var script      = ScriptCode.Create(appId, oId);
         var resultTable = tableFunctions.GetTableInfosFromApp("PreviewTable", script, parameter);
         if (resultTable == null)
         {
             throw new Exception("no preview table found.");
         }
         return(resultTable.Preview);
     }
     catch (Exception ex)
     {
         logger.Error(ex, $"fields from app {appId} and table {objectId} not loaded.");
         return(new TableFunc.PreviewResponse());
     }
 }
        public TableHelper GetTableInfosFromApp(string tableName, ScriptCode script, ConnectorParameter parameter, QlikApp qlikApp = null)
        {
            try
            {
                var resultTable = new QvxTable()
                {
                    TableName = tableName,
                };

                HyperCubePager    pager       = null;
                IEnumerable <int> columnOrder = null;
                var fields = new List <QvxField>();
                var rows   = new List <QvxDataRow>();
                var size   = new Size();

                if (qlikApp == null)
                {
                    qlikApp = AppInstance.GetQlikInstance(parameter, script.AppId);
                }

                if (qlikApp == null)
                {
                    return(null);
                }

                var masterObject = qlikApp.FirstSession.CurrentApp.GetMasterObjectAsync(script.ObjectId).Result;
                if (masterObject != null)
                {
                    logger.Debug($"find master object: {script.ObjectId}");
                    var genericObject = qlikApp.FirstSession.CurrentApp.CreateGenericSessionObjectAsync(masterObject.Properties).Result;
                    pager = genericObject.GetAllHyperCubePagers().FirstOrDefault() ?? null;
                    var tableLayout = genericObject.GetLayout().As <TableLayout>();
                    var hyperCube   = tableLayout.HyperCube;
                    columnOrder = hyperCube.ColumnOrder;
                    size        = hyperCube.Size;
                    fields.AddRange(GetHyperCubeFields(hyperCube.DimensionInfo, hyperCube.MeasureInfo, script));
                }
                var table = qlikApp.FirstSession.CurrentApp.GetObjectAsync <Table>(script.ObjectId).Result;
                if (table != null)
                {
                    logger.Debug($"table object: {script.ObjectId}");
                    pager       = table.HyperCubePager;
                    columnOrder = table.ColumnOrder;
                    size        = table.Size;
                    fields.AddRange(GetHyperCubeFields(table.DimensionInfo, table.MeasureInfo, script));
                }

                var preview = new PreviewResponse()
                {
                    MaxCount = 15,
                };

                if (script != null)
                {
                    var initalPage = new NxPage {
                        Top = 0, Left = 0, Width = size.cx, Height = preview.MaxCount
                    };
                    var allPages = new List <IEnumerable <NxDataPage> >();
                    allPages.Add(pager.GetData(new List <NxPage>()
                    {
                        initalPage
                    }));
                    if (script.Full)
                    {
                        var pageHeight = Math.Min(size.cy * size.cx, 5000) / size.cx;
                        logger.Debug($"read data - column count: {size.cx}");
                        initalPage = new NxPage {
                            Top = 0, Left = 0, Width = size.cx, Height = pageHeight
                        };
                        allPages         = pager.IteratePages(new[] { initalPage }, Pager.Next).ToList();
                        preview.MaxCount = 0;
                    }
                    if (allPages == null)
                    {
                        throw new Exception($"no dimension in table {script.ObjectId} exits.");
                    }
                    logger.Debug($"read pages - count {allPages.Count}");
                    foreach (var page in allPages)
                    {
                        var allMatrix = page?.SelectMany(p => p.Matrix);
                        foreach (var matrix in allMatrix)
                        {
                            var row  = new QvxDataRow();
                            var hrow = new PreviewRow();
                            var drow = new PreviewRow();
                            foreach (var order in columnOrder)
                            {
                                if (order < fields.Count)
                                {
                                    var field = fields[order];
                                    row[field] = matrix[order].Text;
                                    if (!preview.qPreview.Any(s => s.qValues.Contains(field.FieldName)))
                                    {
                                        hrow.qValues.Add(field.FieldName);
                                    }
                                    if (preview.qPreview.Count <= preview.MaxCount)
                                    {
                                        drow.qValues.Add(matrix[order].Text);
                                    }
                                }
                            }
                            rows.Add(row);
                            if (hrow.qValues.Count > 0)
                            {
                                preview.qPreview.Add(hrow);
                            }
                            if (drow.qValues.Count > 0)
                            {
                                preview.qPreview.Add(drow);
                            }
                        }
                    }
                }

                resultTable.Fields  = fields.ToArray();
                resultTable.GetRows = () => { return(rows); };
                logger.Debug($"return table {resultTable.TableName}");
                return(new TableHelper(resultTable, preview));
            }
            catch (Exception ex)
            {
                logger.Error(ex, "can´t read table infos.");
                return(null);
            }
        }
        private List <QvxField> GetHyperCubeFields(IEnumerable <TableHyperCubeDimensionq> dimensions, IEnumerable <TableHyperCubeMeasureq> measures, ScriptCode script = null)
        {
            var fields = new List <QvxField>();

            try
            {
                foreach (var dimInfo in dimensions)
                {
                    if (IsUsedField(dimInfo.FallbackTitle, script))
                    {
                        fields.Add(new QvxField(dimInfo.FallbackTitle, QvxFieldType.QVX_TEXT,
                                                QvxNullRepresentation.QVX_NULL_FLAG_SUPPRESS_DATA,
                                                QlikView.Qvx.QvxLibrary.FieldAttrType.ASCII));
                    }
                }

                foreach (var measureInfo in measures)
                {
                    if (IsUsedField(measureInfo.FallbackTitle, script))
                    {
                        fields.Add(new QvxField(measureInfo.FallbackTitle, QvxFieldType.QVX_TEXT,
                                                QvxNullRepresentation.QVX_NULL_FLAG_SUPPRESS_DATA,
                                                QlikView.Qvx.QvxLibrary.FieldAttrType.ASCII));
                    }
                }
                return(fields);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "can´t read hypercube fields.");
                return(fields);
            }
        }