Example #1
0
        protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParameter)
        {
            TypeExtension        typeExt = TypeExtension.GetTypeExtension(type, Extensions);
            ObjectMapper         om      = db.MappingSchema.GetObjectMapper(type);
            List <MemberMapper>  list    = new List <MemberMapper>();
            StringBuilder        sb      = new StringBuilder();
            SqlQueryInfo         query   = new SqlQueryInfo(om);
            MetadataProviderBase mp      = MappingSchema.MetadataProvider;

            sb.Append("INSERT INTO ");
            AppendTableName(sb, db, type);
            sb.Append(" (\n");

            foreach (MemberMapper mm in GetFieldList(om))
            {
                // IT: This works incorrectly for complex mappers.
                //
                // [2009-03-24] ili: use mm.MemberAccessor instead of mm.ComplexMemberAccessor
                // as in CreateUpdateSqlText
                //

                bool isSet;

                if (mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet) == null || !isSet)
                {
                    sb.AppendFormat("\t{0},\n",
                                    db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));
                    list.Add(mm);
                }
            }

            sb.Remove(sb.Length - 2, 1);

            sb.Append(") VALUES (\n");

            foreach (MemberMapper mm in list)
            {
                SqlQueryParameterInfo p = query.AddParameter(
                    db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
                    mm.Name);

                if (nParameter < 0)
                {
                    sb.AppendFormat("\t{0},\n", p.ParameterName);
                }
                //sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(p.ParameterName, ConvertType.NameToQueryParameter));
                else
                {
                    sb.AppendFormat("\t{{{0}}},\n", nParameter++);
                }
            }

            sb.Remove(sb.Length - 2, 1);

            sb.Append(")");

            query.QueryText = sb.ToString();

            return(query);
        }
Example #2
0
        protected SqlQueryInfo CreateUpdateSqlText(DbManager db, Type type, int nParameter)
        {
            TypeExtension        typeExt = TypeExtension.GetTypeExtension(type, Extensions);
            ObjectMapper         om      = db.MappingSchema.GetObjectMapper(type);
            StringBuilder        sb      = new StringBuilder();
            SqlQueryInfo         query   = new SqlQueryInfo(om);
            MetadataProviderBase mp      = MappingSchema.MetadataProvider;

            sb.Append("UPDATE\n\t");
            AppendTableName(sb, db, type);
            sb.Append("\nSET\n");

            MemberMapper[] fields    = GetFieldList(om);
            bool           hasFields = false;

            foreach (MemberMapper mm in fields)
            {
                bool isSet;

                if (mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet) != null && isSet)
                {
                    continue;
                }

                mp.GetPrimaryKeyOrder(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);

                if (isSet)
                {
                    continue;
                }

                hasFields = true;

                SqlQueryParameterInfo p = query.AddParameter(
                    db.DataProvider.Convert(mm.Name + "_P", ConvertType.NameToQueryParameter).ToString(),
                    mm.Name);

                sb.AppendFormat("\t{0} = ", db.DataProvider.Convert(p.FieldName, ConvertType.NameToQueryField));

                if (nParameter < 0)
                {
                    sb.AppendFormat("{0},\n", p.ParameterName);
                }
                else
                {
                    sb.AppendFormat("\t{{{0}}},\n", nParameter++);
                }
            }

            if (!hasFields)
            {
                throw new DataAccessException(
                          string.Format("There are no fields to update in the type '{0}'.", query.ObjectType.FullName));
            }

            sb.Remove(sb.Length - 2, 1);

            AddWherePK(db, query, sb, nParameter);

            query.QueryText = sb.ToString();

            return(query);
        }