Beispiel #1
0
        /// <summary>
        /// Load all records by query
        /// </summary>
        /// <param name="q">query</param>
        /// <returns>DataTable filled with data that matched specified query</returns>
        public DataTable LoadAll(Query q)
        {
            QTable  source = new QTable(q.Table);
            DataSet ds     = CreateDataSet(source.Name);

            if (ds == null)
            {
                ds = new DataSet();
            }
            var tbl = Dalc.Load(q, ds);

            return(tbl);
        }
Beispiel #2
0
        /// <summary>
        /// Load DataRow by query
        /// </summary>
        /// <param name="q">query</param>
        /// <returns>DataRow or null if no records matched</returns>
        public DataRow Load(Query q)
        {
            QTable  table = new QTable(q.Table);
            DataSet ds    = CreateDataSet(table.Name);

            if (ds == null)
            {
                ds = new DataSet();
            }
            var tbl = Dalc.Load(q, ds);

            return(tbl.Rows.Count > 0 ? tbl.Rows[0] : null);
        }
Beispiel #3
0
        /// <summary>
        /// Load DataRow from specifed data source by primary key
        /// </summary>
        /// <param name="tableName">data table name</param>
        /// <param name="pk">primary key values</param>
        /// <returns>DataRow or null</returns>
        public DataRow Load(string tableName, params object[] pk)
        {
            DataSet ds = CreateDataSet(tableName);

            if (ds == null)
            {
                throw new Exception("Unknown source name");
            }
            Query q = new Query(tableName, ComposePkCondition(ds.Tables[tableName], pk));

            Dalc.Load(q, ds);
            return(ds.Tables[q.Table].Rows.Count > 0 ? ds.Tables[q.Table].Rows[0] : null);
        }
Beispiel #4
0
        public object[] GetToKeys(object fromKey)
        {
            var q = new Query(RelationSourceName, ComposeFromCondition(fromKey));

            if (PositionFieldName != null)
            {
                q.Sort = new[] { PositionFieldName }
            }
            ;
            var ds = new DataSet();

            Dalc.Load(ds, q);
            return(ds.Tables[q.SourceName].Rows.Cast <DataRow>().Select(r => r[ToFieldName]).ToArray());
        }
Beispiel #5
0
        protected string[] Select(QueryNode condition, string fldName)
        {
            DataSet ds = new DataSet();
            var     q  = new Query(UserRoleSourceName, condition);

            q.SetFields(fldName);
            Dalc.Load(q, ds);
            var values = new string[ds.Tables[UserRoleSourceName].Rows.Count];

            for (int i = 0; i < values.Length; i++)
            {
                values[i] = (string)ds.Tables[UserRoleSourceName].Rows[i][fldName];
            }
            return(values);
        }
Beispiel #6
0
        /// <summary>
        /// Method gets children of folder by folder name
        /// </summary>
        /// <param name="name">Folder name</param>
        /// <returns>Array of children instances</returns>
        public DalcFileObject[] GetChildren(string name)
        {
            if (name.Length > 0)
            {
                name = FormatPath(name);
            }
            Query   q  = new Query(TableName, new QueryConditionNode((QField)ParentFieldName, Conditions.Equal, (QConst)name));
            DataSet ds = new DataSet();

            Dalc.Load(q, ds);
            ArrayList fileList = new ArrayList();

            foreach (DataRow row in ds.Tables[TableName].Rows)
            {
                fileList.Add(FileSystemHelper.SetFileProperties(new DalcFileObject(this),
                                                                new DataRowDictionary(row)));
            }
            return((DalcFileObject[])fileList.ToArray(typeof(DalcFileObject)));
        }
Beispiel #7
0
        public void Run()
        {
            var   ds     = new DataSet();
            Query batchQ = new Query(SourceName);

            batchQ.RecordCount = BatchSize;

            do
            {
                if (ds.Tables.Contains(SourceName))
                {
                    ds.Tables[SourceName].Clear();
                }
                Dalc.Load(ds, batchQ);

                if (Transaction != null)
                {
                    Transaction.Begin();
                }

                foreach (DataRow r in ds.Tables[SourceName].Rows)
                {
                    Indexer.Add(r);
                    if (Indexer.DelayedIndexing)
                    {
                        Indexer.RunDelayedIndexing();
                    }
                }

                if (Transaction != null)
                {
                    Transaction.Commit();
                }

                // next batch
                batchQ.StartRecord += BatchSize;
            } while (ds.Tables[SourceName].Rows.Count >= BatchSize);
        }
Beispiel #8
0
        /// <summary>
        /// Update record in data source by primary key
        /// </summary>
        /// <param name="tableName">data source identifier</param>
        /// <param name="pk">primary key values</param>
        /// <param name="changeset">column name -> value</param>
        public void Update(string tableName, object[] pk, IDictionary <string, object> changeset)
        {
            DataSet ds = CreateDataSet(tableName);

            if (ds == null)
            {
                throw new Exception("Unknown source name");
            }
            Query q = new Query(tableName, ComposePkCondition(ds.Tables[tableName], pk));
            var   t = Dalc.Load(q, ds);

            if (t.Rows.Count == 0)
            {
                throw new Exception("Record does not exist");
            }
            foreach (DataRow r in t.Rows)
            {
                foreach (KeyValuePair <string, object> entry in changeset)
                {
                    r[entry.Key] = PrepareValue(entry.Value);
                }
            }
            Dalc.Update(t);
        }
Beispiel #9
0
        public DataTable LoadDataTable(IDictionary <string, object> context)
        {
            var ds = new DataSet();

            return(Dalc.Load(PrepareQuery(context), ds));
        }
Beispiel #10
0
        protected void LoadToSink(SourceDescriptor sourceDescr, IList <object> ids, IList <FieldDescriptor> predFlds, Resource[] vals, StatementSink sink, SelectFilter flt)
        {
            // todo: more effective impl using IDbDalc datareader
            var ds       = new DataSet();
            var q        = new Query(sourceDescr.SourceName);
            var flds     = predFlds ?? sourceDescr.Fields;
            var loadType = flt.Predicates == null || flt.Predicates.Contains(NS.Rdf.typeEntity);

            q.Fields    = new string[flds.Count + 1];
            q.Fields[0] = sourceDescr.IdFieldName;
            for (int i = 0; i < flds.Count; i++)
            {
                q.Fields[i + 1] = flds[i].FieldName;
            }
            // compose query condition
            var condition = new QueryGroupNode(GroupType.And);

            if (ids != null)
            {
                condition.Nodes.Add(ComposeCondition(sourceDescr.IdFieldName, ids.ToArray()));
            }
            if (vals != null && !loadType)
            {
                var orGrp = new QueryGroupNode(GroupType.Or);
                for (int i = 0; i < flds.Count; i++)
                {
                    var valCnd = ComposeCondition(flds[i], vals);
                    if (valCnd != null)
                    {
                        orGrp.Nodes.Add(valCnd);
                    }
                }
                if (orGrp.Nodes.Count == 0)
                {
                    return;                     //values are not for this source
                }
                condition.Nodes.Add(orGrp);
            }
            if (flt.LiteralFilters != null)
            {
                var literalFltCondition = ComposeLiteralFilter(flds, flt.LiteralFilters);
                if (literalFltCondition != null)
                {
                    condition.Nodes.Add(literalFltCondition);
                }
            }
            q.Root = condition;
            if (flt.Limit > 0)
            {
                q.RecordCount = flt.Limit;
            }
            // log
            log.Write(LogEvent.Debug, q);
            Console.WriteLine(q.ToString());
            // query result handler
            Action <IDataReader> loadToSinkAction = delegate(IDataReader dataReader) {
                int recIndex = 0;
                while (dataReader.Read() && (recIndex < q.RecordCount))
                {
                    recIndex++;
                    var itemEntity = GetSourceItemEntity(sourceDescr, dataReader[sourceDescr.IdFieldName]);
                    for (int j = 0; j < flds.Count; j++)
                    {
                        var f   = flds[j];
                        var obj = PrepareResource(f, dataReader[f.FieldName]);
                        if (vals == null || vals.Contains(obj))
                        {
                            // literals post-filter
                            if (flt.LiteralFilters != null && !LiteralFilter.MatchesFilters(obj, flt.LiteralFilters, this))
                            {
                                continue;
                            }
                            if (!sink.Add(new Statement(itemEntity, EntityFieldHash[f], obj)))
                            {
                                return;
                            }
                        }
                    }
                    // type predicate
                    if (loadType && flt.LiteralFilters == null)
                    {
                        if (vals == null || vals.Contains(EntitySourceHash[sourceDescr]))
                        {
                            if (!sink.Add(
                                    new Statement(itemEntity, NS.Rdf.typeEntity, EntitySourceHash[sourceDescr])))
                            {
                                return;
                            }
                        }
                    }
                }
            };

            // DB DALC datareader optimization
            if (Dalc is IDbDalc)
            {
                var  dbDalc    = (IDbDalc)Dalc;
                bool closeConn = false;
                try {
                    if (dbDalc.Connection.State != ConnectionState.Open)
                    {
                        dbDalc.Connection.Open();
                        closeConn = true;
                    }
                    IDataReader rdr = dbDalc.LoadReader(q);
                    try {
                        loadToSinkAction(rdr);
                    } finally {
                        rdr.Close();
                    }
                } finally {
                    if (closeConn)
                    {
                        dbDalc.Connection.Close();
                    }
                }
            }
            else
            {
                Dalc.Load(ds, q);
                var tblRdr = ds.Tables[q.SourceName].CreateDataReader();
                try {
                    loadToSinkAction(tblRdr);
                } finally {
                    tblRdr.Close();
                }
            }
        }