Esempio n. 1
0
        internal void ReadRelationToOne(CSSchemaField schemaField, ICSDbReader dataReader, Dictionary <string, string> aliasMap)
        {
            try
            {
                CSFieldValue fieldLocal = _fieldData["#" + schemaField.Relation.LocalKey];

                if (fieldLocal.Value != null)
                {
                    CSObject relationObject = CSFactory.New(schemaField.FieldType);

                    if (dataReader != null)
                    {
                        relationObject.FromDataReader(dataReader, aliasMap);
                    }
                    else if (!relationObject.ReadUsingUniqueKey(schemaField.Relation.ForeignKey, fieldLocal.Value))
                    {
                        throw new CSException("Relation " + schemaField.Name + " could not be read");
                    }

                    _fieldData[schemaField.Name].ValueDirect = relationObject;
                    _fieldData[schemaField.Name].ValueState  = CSFieldValueState.Read;
                }
                else
                {
                    _fieldData[schemaField.Name].ValueDirect = null;
                    _fieldData[schemaField.Name].ValueState  = CSFieldValueState.Read;
                }
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Esempio n. 2
0
//        protected T GetField<T>(string fieldName)
//        {
//            return (T) GetField(fieldName);
//        }

        protected void SetField(string fieldName, object value)
        {
            CSFieldValue fieldValue = _fieldData[fieldName];

            if (fieldValue == null)
            {
                throw new CSException("Type [" + GetType().Name + "] does not contain property [" + fieldName + "]");
            }

            PropertyChangedEventArgs e = new PropertyChangedEventArgs(fieldName);

            fieldValue.Value = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, e);
            }
        }
Esempio n. 3
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;
        }
Esempio n. 4
0
        //        /// <summary>
        //        /// Gets or sets the specified field (by field name)
        //        /// </summary>
        //        /// <value>The field value</value>
        //		public object this[string fieldName]
        //		{
        //			get
        //			{
        //				int dotIndex = fieldName.IndexOf('.');
        //				string rootField = fieldName;
        //
        //				if (dotIndex > 0)
        //					rootField = fieldName.Substring(0,dotIndex);
        //
        //				object value = GetField(rootField);
        //
        //				if (dotIndex > 0 && value is CSObject)
        //					return (value as CSObject)[fieldName.Substring(dotIndex+1)];
        //				else
        //					return value;
        //			}
        //			set
        //			{
        //				SetField(fieldName,value);
        //			}
        //		}

        #region Protected Field Reading Methods

        protected object GetField(string fieldName)
        {
            CSFieldValue fieldValue = _fieldData[fieldName];

            if (fieldValue == null)
            {
                throw new CSException("Type [" + GetType().Name + "] does not contain property [" + fieldName + "]");
            }

            object value = fieldValue.Value;

            if (value == null)
            {
                return(fieldValue.SchemaField.NullValue);
            }
            else
            {
                return(value);
            }
        }
Esempio n. 5
0
        internal override void UpdateForeignKeys()
        {
            if (!Populated || Relation == null)
            {
                return;
            }

            foreach (TObjectType obj in _objectArray)
            {
                CSFieldValue parentValue = RelationObject.Data["#" + Relation.LocalKey];
                CSFieldValue thisValue   = obj.Data["#" + Relation.ForeignKey];

                if (obj.IsNew || thisValue.Value == null || !thisValue.Value.Equals(parentValue.Value))
                {
                    if (Relation.RelationType == CSSchemaRelationType.OneToMany)
                    {
                        thisValue.Value = parentValue.Value;
                    }
                }
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        private bool Write()
        {
            if (!_fieldData.IsDirty)
            {
                return(true);
            }

            if (_dataState == CSObjectDataState.Deleted)
            {
                return(false);
            }

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

            CSFilter whereClause = new CSFilter();

            CSParameterCollection parameters = new CSParameterCollection();

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

                if (!schemaColumn.IsKey && (fieldValue == null || !fieldValue.IsDirty || fieldValue.SchemaField.ReadOnly))
                {
                    continue;
                }

                CSParameter parameter = parameters.Add();

                parameter.Value = fieldValue.ValueDirect;

                if (schemaColumn.IsKey)
                {
                    whereClause = whereClause.And(_schema.DB.QuoteField(schemaColumn.Name) + "=@" + parameter.Name.Substring(1));
                }

                if (fieldValue.IsDirty && !fieldValue.SchemaField.ReadOnly && !schemaColumn.Identity && !schemaColumn.ReadOnly)
                {
                    fieldNames.Add(schemaColumn.Name);
                    fieldValues.Add("@" + parameter.Name.Substring(1));
                }

                fieldValue.ValueState = CSFieldValueState.Read;
            }

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

            if (fieldValues.Count > 0)
            {
                string sqlQuery = _schema.DB.BuildUpdateSQL(_schema.TableName, fieldNames.ToArray(), fieldValues.ToArray(), whereClause.Expression);

                if (_schema.DB.ExecuteNonQuery(sqlQuery, parameters) != 1)
                {
                    return(false);
                }
            }

            return(true);
        }