Exemplo n.º 1
0
        private List <TObjectType> GetObjects(string sqlQuery, CSParameterCollection parameters, Dictionary <string, string> aliasMap, IEnumerable <PrefetchField> prefetchFields)
        {
            using (CSTransaction csTransaction = new CSTransaction(Schema))
            {
                List <TObjectType> objectList = new List <TObjectType>();

                using (ICSDbReader reader = DB.CreateReader(sqlQuery, parameters))
                {
                    while (reader.Read())
                    {
                        TObjectType csObject = CSObject <TObjectType> .New();

                        csObject.Fire_ObjectReading();

                        csObject.FromDataReader(reader, aliasMap);

                        foreach (PrefetchField prefetchField in prefetchFields)
                        {
                            csObject.ReadRelationToOne(prefetchField.SchemaField, reader, prefetchField.AliasMap);
                        }

                        if (FilterPredicate != null)
                        {
                            bool shouldAdd = true;

                            foreach (Predicate <TObjectType> predicate in FilterPredicate.GetInvocationList())
                            {
                                if (!predicate(csObject))
                                {
                                    shouldAdd = false;
                                    break;
                                }
                            }

                            if (!shouldAdd)
                            {
                                continue;
                            }
                        }

                        csObject.ObjectDeleted += OnObjectDeleted;

                        objectList.Add(csObject);

                        csObject.Fire_ObjectRead();
                    }
                }

                csTransaction.Commit();

                return(objectList);
            }
        }
Exemplo n.º 2
0
        public override bool Save()
        {
            if (!Populated)
            {
                return(true);
            }

            using (CSTransaction csTransaction = new CSTransaction(Schema, CSIsolationLevel.ReadUncommitted))
            {
                UpdateForeignKeys();

                foreach (TObjectType obj in _objectArray.ToArray())
                {
                    if (obj.IsDirty)
                    {
                        if (!obj.Save())
                        {
                            return(false);
                        }
                    }
                }

                if (Relation != null && Relation.PureManyToMany)
                {
                    if (_removedObjects != null)
                    {
                        foreach (TObjectType obj in _removedObjects)
                        {
                            CSParameterCollection parameters = new CSParameterCollection();

                            parameters.Add("@LocalKey").Value   = RelationObject.Data["#" + Relation.LocalKey].Value;
                            parameters.Add("@ForeignKey").Value = obj.Data["#" + Relation.ForeignKey].Value;

                            string deleteSql = DB.BuildDeleteSQL(Relation.LinkTable, null, DB.QuoteField(Relation.LocalLinkKey) + "=@LocalKey and " + DB.QuoteField(Relation.ForeignLinkKey) + "=@ForeignKey");

                            DB.ExecuteNonQuery(deleteSql, parameters);
                        }

                        _removedObjects = null;
                    }

                    if (_addedObjects != null)
                    {
                        foreach (TObjectType obj in _addedObjects)
                        {
                            CSParameterCollection parameters = new CSParameterCollection();

                            parameters.Add("@LocalKey").Value   = RelationObject.Data["#" + Relation.LocalKey].Value;
                            parameters.Add("@ForeignKey").Value = obj.Data["#" + Relation.ForeignKey].Value;

                            DB.ExecuteInsert(Relation.LinkTable,
                                             new[] { Relation.LocalLinkKey, Relation.ForeignLinkKey },
                                             new[] { "@LocalKey", "@ForeignKey" },
                                             null, null, null, parameters);

//                            string insertSql =
//                                DB.BuildInsertSQL(Relation.LinkTable,
//                                                new[] { Relation.LocalLinkKey, Relation.ForeignLinkKey },
//                                                new[] { "@LocalKey", "@ForeignKey" },
//                                                null, null, null);

                            //DB.ExecuteNonQuery(insertSql, parameters);
                        }
                    }
                }

                csTransaction.Commit();

                return(true);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves the object to the database
        /// </summary>
        /// <returns>true if the object was saved successfully, otherwise false</returns>
        /// <remarks>
        /// The object will be saved together with all child objects which have been changed. If the object was new, it will be created in the database.
        /// If required, any default data which has been filled in by the database server can be retrieved after the object is saved. This includes the primary key
        /// that has been generated by the database server (for autonumber/identity fields)
        /// </remarks>
        public bool Save()
        {
            bool cancelSave = false;

            Fire_ObjectSaving(ref cancelSave);

            if (cancelSave)
            {
                return(false);
            }

            if (_dataState == CSObjectDataState.New)
            {
                Fire_ObjectCreating(ref cancelSave);
            }
            else
            {
                Fire_ObjectUpdating(ref cancelSave);
            }

            CSObjectDataState oldState = _dataState;

            if (cancelSave)
            {
                return(false);
            }

            using (CSTransaction csTransaction = new CSTransaction(_schema, CSIsolationLevel.ReadUncommitted))
            {
                bool saveOk = SaveChildrenBefore();

                if (saveOk)
                {
                    if (_dataState == CSObjectDataState.MarkedForDelete)
                    {
                        saveOk = Delete();
                    }
                    else
                    {
                        if (_dataState == CSObjectDataState.New)
                        {
                            saveOk = Create();
                        }
                        else
                        {
                            saveOk = Write();
                        }
                    }
                }

                if (saveOk)
                {
                    saveOk = SaveChildrenAfter();
                }

                if (saveOk)
                {
                    csTransaction.Commit();
                }
                else
                {
                    csTransaction.Rollback();
                }

                if (saveOk)
                {
                    if (oldState == CSObjectDataState.New)
                    {
                        Fire_ObjectCreated();

                        //Reload();
                    }
                    else
                    {
                        Fire_ObjectUpdated();
                    }

                    Fire_ObjectSaved();
                }

                return(saveOk);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Physically deletes the object from the database
        /// </summary>
        /// <returns><c>true</c> if the object was deleted successfully. Otherwise <c>false</c></returns>
        public bool Delete()
        {
            if (_dataState == CSObjectDataState.Deleted || _dataState == CSObjectDataState.New)
            {
                return(false);
            }

            bool cancel = false;

            Fire_ObjectDeleting(ref cancel);

            if (cancel)
            {
                return(false);
            }

            StringBuilder whereClause = new StringBuilder();

            CSParameterCollection parameters = new CSParameterCollection();

            foreach (CSSchemaColumn schemaColumn in _schema.Columns)
            {
                if (!schemaColumn.IsKey)
                {
                    continue;
                }

                CSParameter parameter = parameters.Add();

                parameter.Value = _fieldData["#" + schemaColumn.Name].ValueDirect;

                if (whereClause.Length > 0)
                {
                    whereClause.Append(" and ");
                }

                whereClause.Append(_schema.DB.QuoteField(schemaColumn.Name) + "=@" + parameter.Name.Substring(1));
            }

            if (whereClause.Length == 0)
            {
                throw new CSException("No key fields");
            }

            using (CSTransaction csTransaction = new CSTransaction(_schema))
            {
                string deleteSql = _schema.DB.BuildDeleteSQL(_schema.TableName, null, whereClause.ToString());

                int numDeleted = _schema.DB.ExecuteNonQuery(deleteSql, parameters);

                if (numDeleted == 1)
                {
                    csTransaction.Commit();
                }
                else
                {
                    return(false);
                }
            }

            _dataState = CSObjectDataState.Deleted;

            Fire_ObjectDeleted();

            return(true);
        }
Exemplo n.º 5
0
        internal bool ReadFields(CSStringCollection columnList, CSStringCollection keyList, List <object> valueList)
        {
            List <string> fieldList = new List <string>();
            List <string> aliasList = new List <string>();
            Dictionary <string, string> fieldAliasMap = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            CSFilter whereClause = new CSFilter();
            CSTable  table       = new CSTable(_schema);

            CSParameterCollection parameters = new CSParameterCollection();

            foreach (CSSchemaColumn schemaColumn in _schema.Columns)
            {
                if (keyList.Contains(schemaColumn.Name))
                {
                    CSParameter parameter = parameters.Add();

                    parameter.Value = valueList[keyList.IndexOf(schemaColumn.Name)];

                    whereClause = whereClause.And(table.TableAlias + "." + _schema.DB.QuoteField(schemaColumn.Name) + "=" + parameter.Name);

                    _fieldData["#" + schemaColumn.Name].ValueDirect = parameter.Value;
                    _fieldData["#" + schemaColumn.Name].ValueState  = CSFieldValueState.Read;
                }
                else if (columnList.Contains(schemaColumn.Name))
                {
                    string alias = CSNameGenerator.NextFieldAlias;

                    fieldList.Add(table.TableAlias + "." + schemaColumn.Name);
                    aliasList.Add(alias);

                    fieldAliasMap[alias] = schemaColumn.Name;
                }
            }


            /** Build query for prefetch of relations **/

            CSJoinList joinList = new CSJoinList();

            List <PrefetchField> prefetchFields = GetPrefetchFieldsOne(table, fieldList, aliasList, joinList, null);

            if (whereClause.Expression.Length == 0)
            {
                return(false);
            }

            if (fieldList.Count == 0)
            {
                return(true);
            }

            string sqlQuery = _schema.DB.BuildSelectSQL(table.TableName, table.TableAlias, fieldList.ToArray(), aliasList.ToArray(), joinList.BuildJoinExpressions(), whereClause.Expression, null, 1, 1, true, false);

            using (CSTransaction csTransaction = new CSTransaction(_schema))
            {
                using (ICSDbReader dataReader = _schema.DB.CreateReader(sqlQuery, parameters))
                {
                    if (!dataReader.Read())
                    {
                        return(false);
                    }

                    FromDataReader(dataReader, fieldAliasMap);

                    foreach (PrefetchField prefetchField in prefetchFields)
                    {
                        ReadRelationToOne(prefetchField.SchemaField, dataReader, prefetchField.AliasMap);
                    }
                }

                csTransaction.Commit();
            }

            return(true);
        }