Ejemplo n.º 1
0
        internal bool Read(object[] primaryKeys)
        {
            if (primaryKeys.Length == 0)
            {
                throw new CSException("Read() requires parameters");
            }

            Initialize();

            CSStringCollection columnNames = new CSStringCollection();
            List <object>      keyValues   = new List <object>();

            if (primaryKeys.Length != _schema.KeyColumns.Count)
            {
                throw new CSException(GetType().Name + ".Read(..keys..) called with " + primaryKeys.Length + " parameters, but there are " + _schema.KeyColumns.Count + "  key fields defined");
            }

            for (int i = 0; i < primaryKeys.Length; i++)
            {
                columnNames.Add(_schema.KeyColumns[i].Name);
                keyValues.Add(primaryKeys[i]);
            }

            if (!ReadFields(_schema.ColumnsToRead, columnNames, keyValues))
            {
                return(false);
            }

            _dataState = CSObjectDataState.Loaded;

            Fire_ObjectRead();

            return(true);
        }
Ejemplo n.º 2
0
        private void Initialize()
        {
            if (_schema == null)
            {
                _schema = CSSchema.Get(GetType());
            }

            _dataState = CSObjectDataState.New;

            _fieldData = new CSFieldValueCollection(this);
        }
Ejemplo n.º 3
0
        internal bool Read()
        {
            bool ok = ReadFields(_schema.ColumnsToRead);

            if (ok)
            {
                _dataState = CSObjectDataState.Loaded;
            }

            return(ok);
        }
Ejemplo n.º 4
0
        internal void FromDataReader(ICSDbReader dataReader, Dictionary <string, string> aliasMap)
        {
            for (int i = 0; i < dataReader.FieldCount; i++)
            {
                string aliasName = dataReader.GetName(i);
                string fieldName;

                if (aliasMap == null)
                {
                    fieldName = aliasName;
                }
                else if (!aliasMap.TryGetValue(aliasName, out fieldName))
                {
                    continue;
                }

                CSSchemaColumn schemaColumn = _schema.Columns[fieldName];

                if (schemaColumn == null || schemaColumn.Hidden)
                {
                    continue;
                }

                object columnValue = dataReader[i];


                if (columnValue == _schema.DB.NullFieldValue())
                {
                    columnValue = null;
                }

                if (schemaColumn.MappedField.Trim && columnValue is string)
                {
                    columnValue = ((string)columnValue).Trim();
                }

                CSFieldValue fieldValue = _fieldData["#" + schemaColumn.Name];

                fieldValue.ValueDirect = columnValue;
                fieldValue.ValueState  = CSFieldValueState.Read;
            }

            _dataState = CSObjectDataState.Loaded;
        }
Ejemplo n.º 5
0
        protected void Deserialize(SerializationInfo info, StreamingContext context)
        {
            _dataState = (CSObjectDataState)info.GetValue("DataState", typeof(CSObjectDataState));

            foreach (CSFieldValue fieldValue in _fieldData)
            {
                fieldValue.ValueState  = (CSFieldValueState)info.GetValue("FieldState_" + fieldValue.SchemaField.Name, typeof(CSFieldValueState));
                fieldValue.ValueDirect = info.GetValue("FieldValue_" + fieldValue.SchemaField.Name, typeof(object));

                if (fieldValue.SchemaField.Relation != null && (fieldValue.SchemaField.Relation.RelationType == CSSchemaRelationType.OneToMany || fieldValue.SchemaField.Relation.RelationType == CSSchemaRelationType.ManyToMany))
                {
                    if (fieldValue.ValueDirect is CSList)
                    {
                        ((CSList)fieldValue.ValueDirect).Relation       = fieldValue.SchemaField.Relation;
                        ((CSList)fieldValue.ValueDirect).RelationObject = this;
                    }
                }
            }
        }
Ejemplo n.º 6
0
        protected bool ReadUsingUniqueKey(string uniqueColumnName, object value)
        {
            Initialize();

            Fire_ObjectReading();

            CSStringCollection keyList   = new CSStringCollection(uniqueColumnName);
            List <object>      valueList = new List <object>();

            valueList.Add(value);

            if (ReadFields(_schema.ColumnsToRead, keyList, valueList))
            {
                _dataState = CSObjectDataState.Loaded;

                Fire_ObjectRead();

                return(true);
            }

            return(false);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Mark the object for deletion. The object will be deleted next time it is saved
 /// </summary>
 public void MarkForDelete()
 {
     _dataState = CSObjectDataState.MarkedForDelete;
 }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        private bool Create()
        {
            CSParameterCollection parameters = new CSParameterCollection();

            List <string> fieldNames    = new List <string>();
            List <string> fieldValues   = new List <string>();
            List <string> sequenceNames = new List <string>();

            foreach (CSSchemaColumn schemaColumn in _schema.Columns)
            {
                CSFieldValue fieldValue = _fieldData["#" + schemaColumn.Name];

                if (fieldValue == null)
                {
                    continue;
                }

                CSSchemaField schemaField = fieldValue.SchemaField;

                if (schemaField.ClientGenerated && schemaColumn.IsKey)
                {
                    if (schemaField.FieldType == typeof(Guid))
                    {
                        fieldValue.Value = Guid.NewGuid();
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(schemaField.SequenceName) && (!fieldValue.IsDirty || schemaField.ReadOnly || schemaField.NoCreate || schemaColumn.Identity))
                    {
                        continue;
                    }
                }

                if (schemaField.ServerGenerated && schemaColumn.IsKey)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(schemaField.SequenceName))
                {
                    sequenceNames.Add(schemaField.SequenceName);
                    fieldValues.Add(null);
                }
                else
                {
                    CSParameter parameter = parameters.Add();

                    parameter.Value = fieldValue.ValueDirect;

                    fieldValues.Add("@" + parameter.Name.Substring(1));
                    sequenceNames.Add(null);
                }

                fieldNames.Add(schemaColumn.Name);
            }

            string[] primaryKeys = new string[_schema.KeyColumns.Count];

            for (int i = 0; i < _schema.KeyColumns.Count; i++)
            {
                primaryKeys[i] = _schema.KeyColumns[i].Name;
            }


            using (ICSDbReader reader = _schema.DB.ExecuteInsert(_schema.TableName, fieldNames.ToArray(), fieldValues.ToArray(), primaryKeys, sequenceNames.ToArray(), (_schema.IdentityColumn != null && _schema.IdentityColumn.MappedField != null) ? _schema.IdentityColumn.Name : null, parameters))
            {
                if (reader != null && !reader.IsClosed && reader.Read())
                {
                    FromDataReader(reader, null);
                }
            }

            _dataState = CSObjectDataState.Loaded;

            return(true);
        }
Ejemplo n.º 10
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);
            }
        }