Example #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;
            }
        }
Example #2
0
        private void ReadRelationToMany(CSSchemaField schemaField)
        {
//            if (_schema.KeyColumns.Count != 1)
//                throw new CSException("...ToMany only supported with single primary key");
//
            try
            {
                CSList relationCollection = (CSList)Activator.CreateInstance(schemaField.FieldType);

                relationCollection.Relation       = schemaField.Relation;
                relationCollection.RelationObject = this;

                _fieldData[schemaField.Name].ValueDirect = relationCollection;
                _fieldData[schemaField.Name].ValueState  = CSFieldValueState.Read;
            }
            catch (TargetInvocationException ex)
            {
                throw ex.InnerException;
            }
        }
Example #3
0
        internal bool ReadField(CSSchemaField schemaField)
        {
            if (schemaField.Relation != null)
            {
                if (schemaField.Relation.RelationType == CSSchemaRelationType.OneToMany || schemaField.Relation.RelationType == CSSchemaRelationType.ManyToMany)
                {
                    ReadRelationToMany(schemaField);
                }
                else
                {
                    ReadRelationToOne(schemaField, null, null);
                }

                return(true);
            }
            else
            {
                return(ReadFields(new CSStringCollection(schemaField.MappedColumn.Name)));
            }
        }
Example #4
0
        private void CreateFields()
        {
            PropertyInfo[] propInfoList = ClassType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            bool foundDefaultSort = false;

            foreach (PropertyInfo propInfo in propInfoList)
            {
                if (propInfo.DeclaringType == typeof(CSObject))
                    continue;

                MethodInfo getMethod = propInfo.GetGetMethod();
                MethodInfo setMethod = propInfo.GetSetMethod();

                if ((getMethod == null && setMethod == null) || (getMethod != null && !getMethod.IsAbstract && !_nonAbstract))
                    continue;

                CSSchemaField schemaField = new CSSchemaField(propInfo, this);

                if (schemaField.MappedColumn != null || schemaField.Relation != null)
                    _fieldList.Add(schemaField);

                if (propInfo.IsDefined(typeof(ToStringAttribute), true))
                    _toStringProperty = propInfo;

                if (propInfo.IsDefined(typeof(SoftDeleteAttribute), true))
                    _softDeleteField = schemaField;

                DefaultSortAttribute sortAttribute = (DefaultSortAttribute)Attribute.GetCustomAttribute(propInfo,typeof(DefaultSortAttribute), true);

                if (sortAttribute != null)
                {
                    if (foundDefaultSort)
                        throw new CSException(String.Format("Field [{0}.{1}] : only one field can have the DefaultSort attribute", ClassType.Name, propInfo.Name));

                    if (!string.IsNullOrEmpty(_defaultSortExpression))
                        throw new CSException(String.Format("Field [{0}.{1}] has DefaultSort attribute, but class already has a DefaultSortExpression attribute", ClassType.Name, propInfo.Name));

                    _defaultSortExpression = schemaField.Name;

                    if (sortAttribute.SortDirection == CSSort.Descending)
                        _defaultSortExpression += "-";

                    foundDefaultSort = true;
                }

            }

            foreach (CSSchemaColumn schemaColumn in _columnList)
            {
                if (schemaColumn.MappedField == null)
                    _fieldList.Add(new CSSchemaField(schemaColumn, this));
            }
        }
Example #5
0
		internal CSFieldValue(CSObject objectData , CSSchemaField schemaField)
		{
			_csObject = objectData;
			_schemaField = schemaField;
		}
        private static object EvalVariable(string var, QueryExpression parentQuery)
        {
            Match match = _regexVar.Match(var);

            if (!match.Success)
            {
                throw new CSExpressionException("Object [" + var + "] is unknown");
            }

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

            foreach (Capture capture in match.Groups["Object"].Captures)
            {
                fieldNames.Add(capture.Value);
            }

            QueryExpression subExpression = null;

            CSTable currentTable = parentQuery.Table;

            string fieldName = null;

            for (int i = 0; i < fieldNames.Count; i++)
            {
                bool isLast = (i == fieldNames.Count - 1);

                CSJoin        currentJoin = null;
                CSSchemaField schemaField = currentTable.Schema.Fields[fieldNames[i]];

                if (schemaField == null)
                {
                    throw new CSException("Error in expression. [" + fieldNames[i] + "] is not a valid field name");
                }

                if (schemaField.Relation != null)
                {
                    switch (schemaField.Relation.RelationType)
                    {
                    case CSSchemaRelationType.OneToMany:
                    {
                        if (subExpression != null)
                        {
                            throw new CSExpressionException("Error in expression [" + var + "]: Multiple *ToMany relations");
                        }

                        subExpression       = new QueryExpression(schemaField.Relation.ForeignSchema);
                        subExpression.Joins = new CSJoinList();

                        subExpression.Expression = currentTable.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalKey) + "=" + subExpression.Table.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.ForeignKey);
                    }
                    break;

                    case CSSchemaRelationType.ManyToMany:
                    {
                        if (subExpression != null)
                        {
                            throw new CSExpressionException("Error in expression [" + var + "]: Multiple *ToMany relations");
                        }

                        subExpression       = new QueryExpression(schemaField.Relation.ForeignSchema);
                        subExpression.Joins = new CSJoinList();

                        currentJoin = new CSJoin();

                        currentJoin.LeftSchema  = schemaField.Relation.ForeignSchema;
                        currentJoin.LeftTable   = subExpression.Table.TableName;
                        currentJoin.LeftAlias   = subExpression.Table.TableAlias;
                        currentJoin.RightTable  = schemaField.Relation.LinkTable;
                        currentJoin.RightAlias  = CSNameGenerator.NextTableAlias;
                        currentJoin.LeftColumn  = schemaField.Relation.ForeignKey;
                        currentJoin.RightColumn = schemaField.Relation.ForeignLinkKey;

                        currentJoin.Type = CSJoinType.Inner;

                        subExpression.Joins.Add(currentJoin);

                        subExpression.Expression = currentJoin.RightAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalLinkKey) + "=" + currentTable.TableAlias + "." + currentTable.Schema.DB.QuoteField(schemaField.Relation.LocalKey);

                        currentJoin = null;
                    }
                    break;

                    case CSSchemaRelationType.ManyToOne:
                    {
                        if (!isLast)
                        {
                            if (subExpression != null)                                             // meaning we already encountered a *toMany relation
                            {
                                currentJoin = new CSJoin(schemaField.Relation, subExpression.Table.TableAlias);

                                if (subExpression.Joins.Contains(currentJoin))
                                {
                                    currentJoin = subExpression.Joins.GetExistingJoin(currentJoin);
                                }
                                else
                                {
                                    subExpression.Joins.Add(currentJoin);
                                }
                            }
                            else
                            {
                                currentJoin = new CSJoin(schemaField.Relation, currentTable.TableAlias);

                                if (parentQuery.Joins.Contains(currentJoin))
                                {
                                    currentJoin = parentQuery.Joins.GetExistingJoin(currentJoin);
                                }
                                else
                                {
                                    parentQuery.Joins.Add(currentJoin);
                                }
                            }
                        }
                    }
                    break;
                    }

                    if (!isLast)
                    {
                        if (currentJoin != null)
                        {
                            currentTable = new CSTable(schemaField.Relation.ForeignSchema, currentJoin.RightAlias);
                        }
                        else
                        {
                            if (subExpression != null)
                            {
                                currentTable = new CSTable(schemaField.Relation.ForeignSchema, subExpression.Table.TableAlias);
                            }
                            else
                            {
                                currentTable = new CSTable(schemaField.Relation.ForeignSchema);
                            }
                        }
                    }
                    else
                    {
                        fieldName = currentTable.Schema.DB.QuoteField(currentTable.TableAlias + "." + schemaField.Relation.LocalKey);
                    }
                }
                else
                {
                    fieldName = currentTable.Schema.DB.QuoteField(currentTable.TableAlias + "." + schemaField.MappedColumn.Name);
                }
            }

            if (subExpression != null)
            {
                subExpression.FieldName = fieldName;

                return(subExpression);
            }
            else
            {
                //if (fieldName != null)
                return(fieldName);
            }
        }
Example #7
0
        private void CreateFields()
        {
            PropertyInfo[] propInfoList = ClassType.GetRuntimeProperties().ToArray();//.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            bool foundDefaultSort = false;

            foreach (PropertyInfo propInfo in propInfoList)
            {
                if (propInfo.DeclaringType == typeof(CSObject))
                {
                    continue;
                }

                MethodInfo getMethod = propInfo.GetMethod;
                MethodInfo setMethod = propInfo.SetMethod;

                if ((getMethod == null && setMethod == null) || (getMethod != null && !getMethod.IsAbstract && !_nonAbstract))
                {
                    continue;
                }

                CSSchemaField schemaField = new CSSchemaField(propInfo, this);

                if (schemaField.MappedColumn != null || schemaField.Relation != null)
                {
                    _fieldList.Add(schemaField);
                }

                if (propInfo.IsDefined(typeof(ToStringAttribute), true))
                {
                    _toStringProperty = propInfo;
                }

                if (propInfo.IsDefined(typeof(SoftDeleteAttribute), true))
                {
                    _softDeleteField = schemaField;
                }

                var sortAttribute = propInfo.GetCustomAttribute <DefaultSortAttribute>(true);

                if (sortAttribute != null)
                {
                    if (foundDefaultSort)
                    {
                        throw new CSException(String.Format("Field [{0}.{1}] : only one field can have the DefaultSort attribute", ClassType.Name, propInfo.Name));
                    }

                    if (!string.IsNullOrEmpty(_defaultSortExpression))
                    {
                        throw new CSException(String.Format("Field [{0}.{1}] has DefaultSort attribute, but class already has a DefaultSortExpression attribute", ClassType.Name, propInfo.Name));
                    }

                    _defaultSortExpression = schemaField.Name;

                    if (sortAttribute.SortDirection == CSSort.Descending)
                    {
                        _defaultSortExpression += "-";
                    }

                    foundDefaultSort = true;
                }
            }

            foreach (CSSchemaColumn schemaColumn in _columnList)
            {
                if (schemaColumn.MappedField == null)
                {
                    _fieldList.Add(new CSSchemaField(schemaColumn, this));
                }
            }
        }
Example #8
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);
        }
Example #9
0
 internal CSFieldValue(CSObject objectData, CSSchemaField schemaField)
 {
     _csObject    = objectData;
     _schemaField = schemaField;
 }