Ejemplo n.º 1
0
        public ManyToManyRelation NewManyToManyRelation(Relation source, Relation target)
        {
            ManyToManyRelation relation = new ManyToManyRelation(source.PrimaryModelClass, target.PrimaryModelClass);

            relation.Table        = source.ForeignModelClass.Name;
            relation.Schema       = source.ForeignModelClass.Schema;
            relation.SourceColumn = source.ForeignColumn;
            relation.TargetColumn = target.ForeignColumn;

            return(relation);
        }
Ejemplo n.º 2
0
        public object[] Join(ManyToManyRelation manyToMany, object primaryKeyValue)
        {
            var alias = "t";

            var firstJoin = string.Format("JOIN {0} ON {1}.`{2}` = {3}.`{4}`",
                                          GetTableName(manyToMany.SourceTable),
                                          GetTableName(manyToMany.SourceTable),
                                          manyToMany.SourceID,
                                          GetTableName(manyToMany.JoinTable),
                                          manyToMany.JoinSourceId
                                          );

            var secondJoin = string.Format("JOIN {0} ON {1}.`{2}` = {3}.`{4}`",
                                           GetTableName(manyToMany.JoinTable),
                                           GetTableName(manyToMany.JoinTable),
                                           manyToMany.JoinTargetId,
                                           alias,
                                           manyToMany.TargetID
                                           );

            var where = string.Format("WHERE {0}.`{1}` = '{2}'",
                                      GetTableName(manyToMany.JoinTable),
                                      manyToMany.JoinSourceId,
                                      primaryKeyValue
                                      );

            var query = string.Format(@"SELECT {0}.{1} FROM {2} as {3} {4} {5} {6}",
                                      // SELECT
                                      alias,
                                      manyToMany.TargetID,
                                      //FROM
                                      GetTableName(manyToMany.TargetTable),
                                      // AS ALIAS
                                      alias,
                                      // FIRST JOIN
                                      firstJoin,
                                      // SECOND JOIN
                                      secondJoin,
                                      // WHERE
                                      where
                                      );

            return(ReadQuery(query));
        }
Ejemplo n.º 3
0
        public TransactionResult AddManyToManyRelation(ManyToManyRelation attribute, object sourceId, Model model)
        {
            TransactionResult result = new TransactionResult(SQLiteErrorCode.Ok, null);

            if (model != null)
            {
                var query = string.Format("INSERT INTO {0} ({1}, {2}) VALUES('{3}', '{4}')",
                                          attribute.JoinTable,
                                          attribute.JoinSourceId,
                                          attribute.JoinTargetId,
                                          sourceId,
                                          model.PrimaryKeyValue
                                          );

                result = this.WriteQuery(query);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public TransactionResult AddManyToManyRelations(ManyToManyRelation attribute, object sourceId, IDictionary models)
        {
            TransactionResult result = new TransactionResult(SQLiteErrorCode.Ok, null);

            if (models != null)
            {
                foreach (object targetId in models.Keys)
                {
                    var query = string.Format("INSERT INTO {0} ({1}, {2}) VALUES('{3}', '{4}')",
                                              attribute.JoinTable,
                                              attribute.JoinSourceId,
                                              attribute.JoinTargetId,
                                              sourceId,
                                              targetId
                                              );

                    result = this.WriteQuery(query);
                }
            }

            return(result);
        }
Ejemplo n.º 5
0
        private void GenerateHasAndBelongsToRelationFromTargets(CodeTypeDeclaration classDeclaration, CodeNamespace nameSpace,
                                               ManyToManyRelation relationship)
        {
            if (String.IsNullOrEmpty(relationship.Table))
                throw new ArgumentNullException(
                    String.Format("Class {0} does not have a table name on it's many to many relation to class {1}",
                                  relationship.Source.Name, relationship.Target.Name));
            if (String.IsNullOrEmpty(relationship.SourceColumn))
                throw new ArgumentNullException(
                    String.Format("Class {0} does not have a source column name on it's many to many relation to class {1}",
                                  relationship.Source.Name, relationship.Target.Name));
            if (String.IsNullOrEmpty(relationship.TargetColumn))
                throw new ArgumentNullException(
                    String.Format("Class {0} does not have a target column name on it's many to many relation to class {1}",
                                  relationship.Source.Name, relationship.Target.Name));

            CodeTypeDeclaration targetClass = GenerateClass(relationship.Target, nameSpace);

            string propertyName = String.IsNullOrEmpty(relationship.SourcePropertyName)
                                      ? NamingHelper.GetPlural(relationship.Source.Name)
                                      : NamingHelper.GetPlural(relationship.SourcePropertyName);

            string propertyType = String.IsNullOrEmpty(relationship.TargetPropertyType)
                                      ? GenericListInterface
                                      : relationship.TargetPropertyType;

            CodeMemberField memberField;
            if (!relationship.Source.AreRelationsGeneric())
                memberField = GetMemberField(propertyName, propertyType, Accessor.Private, relationship.TargetAccess);
            else
                memberField = GetGenericMemberField(targetClass.Name, propertyName, propertyType, Accessor.Private, relationship.TargetAccess);

            classDeclaration.Members.Add(memberField);

            CodeMemberProperty memberProperty;
            if (String.IsNullOrEmpty(relationship.SourceDescription))
                memberProperty = GetMemberProperty(memberField, NamingHelper.GetPlural(propertyName), true, true, relationship.Target.DoesImplementINotifyPropertyChanged(), null);
            else
                memberProperty =
                    GetMemberProperty(memberField, NamingHelper.GetPlural(propertyName), true, true, relationship.Target.DoesImplementINotifyPropertyChanged(),
                                      relationship.SourceDescription);
            classDeclaration.Members.Add(memberProperty);

            memberProperty.CustomAttributes.Add(relationship.GetHasAndBelongsToAttributeFromSource());
        }
Ejemplo n.º 6
0
        public ManyToManyRelation NewManyToManyRelation(Relation source, Relation target)
        {
            ManyToManyRelation relation = new ManyToManyRelation(source.PrimaryModelClass, target.PrimaryModelClass);
            relation.Table = source.ForeignModelClass.Name;
            relation.Schema = source.ForeignModelClass.Schema;
            relation.SourceColumn = source.ForeignColumn;
            relation.TargetColumn = target.ForeignColumn;

            return relation;
        }
Ejemplo n.º 7
0
 private void GenerateHasAndBelongsToRelationFromSources(CodeTypeDeclaration classDeclaration, CodeNamespace nameSpace,
                                        ManyToManyRelation relationship)
 {
     if (relationship.TargetPropertyGenerated)
         GenerateHasMany(
             classDeclaration,
             relationship.Target.Name,
             relationship.EffectiveTargetPropertyName,
             relationship.TargetPropertyType,
             relationship.EffectiveTargetAccess,
             relationship.TargetDescription,
             relationship.GetHasAndBelongsToAttributeFromTarget(Context),
             relationship.Source.AreRelationsGeneric(),
             relationship.Target.DoesImplementINotifyPropertyChanged(),
             relationship.Target.DoesImplementINotifyPropertyChanging(),
             relationship.Source.Name,
             relationship.EffectiveSourcePropertyName,
             relationship.EffectiveAutomaticAssociations,
             true,
             relationship.GetCollectionIdAttribute(relationship.EffectiveTargetRelationType));
 }