예제 #1
0
        protected SqlQueryInfo CreateUpdateSqlText(DbManager db, Type type)
        {
            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.AppendFormat("UPDATE\n\t{0}\nSET\n",
                            db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

            foreach (MemberMapper mm in GetFieldList(om))
            {
                bool isSet;

                if (!mp.GetNonUpdatableFlag(type, typeExt, mm.MemberAccessor, out isSet) || !isSet)
                {
                    SqlQueryParameterInfo p = query.AddParameter(
                        db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryParameter).ToString(),
                        mm.Name);

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

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

            AddWherePK(db, query, sb);

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #2
0
        private void AddWherePK(DbManager db, SqlQueryInfo query, StringBuilder sb, int nParameter, FullObjectMapper mapper)
        {
            sb.Append("WHERE\n");

            foreach (IMapper mm in mapper.PropertiesMapping)
            {
                if (mm is ValueMapper && mm.DataReaderIndex == mapper.DataReaderIndex)
                {
                    var valueMapper = (ValueMapper)mm;

                    string tableAlias = mapper.PropertyType.Name;

                    //mm.Name = ID_TRACK
                    SqlQueryParameterInfo p = query.AddParameter(
                        db.DataProvider.Convert(valueMapper.ColumnName + "_W", ConvertType.NameToQueryParameter).
                        ToString(),
                        valueMapper.ColumnName);

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

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

            sb.Remove(sb.Length - 5, 5);
        }
예제 #3
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);
        }
예제 #4
0
        protected virtual void AddWherePK(DbManager db, SqlQueryInfo query, StringBuilder sb, int nParameter)
        {
            sb.Append("WHERE\n");

            var memberMappers = GetKeyFieldList(db, query.ObjectType);

            if (memberMappers.Length == 0)
            {
                throw new DataAccessException(
                          string.Format("No primary key field(s) in the type '{0}'.", query.ObjectType.FullName));
            }

            foreach (var mm in memberMappers)
            {
                var p = query.AddParameter(
                    db.DataProvider.Convert(mm.Name + "_W", ConvertType.NameToQueryParameter).ToString(),
                    mm.Name);

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

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

            sb.Remove(sb.Length - 5, 5);
        }
예제 #5
0
        protected SqlQueryInfo CreateSelectByKeySqlText(DbManager db, Type type)
        {
            var om    = db.MappingSchema.GetObjectMapper(type);
            var sb    = new StringBuilder();
            var query = new SqlQueryInfo(om);

            sb.Append("SELECT\n");

            foreach (var mm in GetFieldList(om))
            {
                sb.AppendFormat("\t{0},\n",
                                db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));
            }

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

            sb.Append("FROM\n\t");

            AppendTableName(sb, db, type);

            AddWherePK(db, query, sb, -1);

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #6
0
        public virtual System.Collections.Generic.List <T> SelectAll <T>(DbManager db)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "SelectAll");

            return(db
                   .SetCommand(query.QueryText)
                   .ExecuteList <T>());
        }
예제 #7
0
        public virtual int DeleteByKey(DbManager db, params object[] key)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "Delete");

            return(db
                   .SetCommand(query.QueryText, query.GetParameters(db, key))
                   .ExecuteNonQuery());
        }
예제 #8
0
        public virtual int Delete(DbManager db, T obj)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, obj.GetType(), "Delete");

            return(db
                   .SetCommand(query.QueryText, query.GetParameters(db, obj))
                   .ExecuteNonQuery());
        }
예제 #9
0
        public virtual T SelectByKey(DbManager db, params object[] keys)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "SelectByKey");

            return(db
                   .SetCommand(query.QueryText, query.GetParameters(db, keys))
                   .ExecuteObject <T>());
        }
예제 #10
0
        public virtual IList SelectAll(DbManager db, IList list, Type type)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, type, "SelectAll");

            return(db
                   .SetCommand(query.QueryText)
                   .ExecuteList(list, type));
        }
예제 #11
0
        public virtual List <T> SelectAll(DbManager db)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "SelectAll");

            return(db
                   .SetCommand(query.QueryText)
                   .ExecuteList <T>());
        }
예제 #12
0
        public virtual L SelectAll <L>(DbManager db, L list)
            where L : IList <T>
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "SelectAll");

            return(db
                   .SetCommand(query.QueryText)
                   .ExecuteList <L, T>(list));
        }
예제 #13
0
        public virtual SqlQueryInfo GetSqlQueryInfo(DbManager db, Type type, string actionName)
        {
            string       key   = type.FullName + "$" + actionName + "$" + db.DataProvider.UniqueName + "$" + GetTableName(type);
            SqlQueryInfo query = (SqlQueryInfo)_actionSqlQueryInfo[key];

            if (query == null)
            {
                query = CreateSqlText(db, type, actionName);
                _actionSqlQueryInfo[key] = query;
            }

            return(query);
        }
예제 #14
0
        public virtual int Delete(DbManager db, int maxBatchSize, IEnumerable <T> list)
        {
            SqlQueryInfo query = GetSqlQueryInfo(db, typeof(T), "DeleteBatch");

            db.SetCommand(query.QueryText);

            return(ExecuteForEach(
                       db,
                       list,
                       query.GetMemberMappers(),
                       maxBatchSize,
                       delegate(T obj) { return query.GetParameters(db, obj); }));
        }
예제 #15
0
        protected SqlQueryInfo CreateDeleteSqlText(DbManager db, Type type)
        {
            ObjectMapper  om    = db.MappingSchema.GetObjectMapper(type);
            StringBuilder sb    = new StringBuilder();
            SqlQueryInfo  query = new SqlQueryInfo(om);

            sb.AppendFormat("DELETE FROM\n\t{0}\n",
                            db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

            AddWherePK(db, query, sb);

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #16
0
        protected SqlQueryInfo CreateDeleteSqlText(DbManager db, Type type, int nParameter)
        {
            var om    = db.MappingSchema.GetObjectMapper(type);
            var sb    = new StringBuilder();
            var query = new SqlQueryInfo(om);

            sb.Append("DELETE FROM\n\t");
            AppendTableName(sb, db, type);
            sb.AppendLine();

            AddWherePK(db, query, sb, nParameter);

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #17
0
        protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type)
        {
            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.AppendFormat("INSERT INTO {0} (\n",
                            db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

            foreach (MemberMapper mm in GetFieldList(om))
            {
                // IT: This works incorrectly for complex mappers.
                //
                bool isSet;

                if (!mp.GetNonUpdatableFlag(type, typeExt, mm.ComplexMemberAccessor, out isSet) || !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, ConvertType.NameToQueryParameter).ToString(),
                    mm.Name);

                sb.AppendFormat("\t{0},\n", p.ParameterName);
            }

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

            sb.Append(")");

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #18
0
        protected SqlQueryInfo CreateSelectAllSqlText(DbManager db, Type type)
        {
            ObjectMapper  om    = db.MappingSchema.GetObjectMapper(type);
            StringBuilder sb    = new StringBuilder();
            SqlQueryInfo  query = new SqlQueryInfo(om);

            sb.Append("SELECT\n");

            foreach (MemberMapper mm in GetFieldList(om))
            {
                sb.AppendFormat("\t{0},\n",
                                db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));
            }

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

            sb.AppendFormat("FROM\n\t{0}",
                            db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

            query.QueryText = sb.ToString();

            return(query);
        }
예제 #19
0
		protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParameter)
		{
			var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
			var om      = db.MappingSchema.GetObjectMapper(type);
			var list    = new List<MemberMapper>();
			var sb      = new StringBuilder();
			var query   = new SqlQueryInfo(om);
			var mp      = MappingSchema.MetadataProvider;

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

			foreach (var 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;
				var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);

				if (nonUpdatableAttribute == null || !isSet || nonUpdatableAttribute.OnInsert == false)
				{
					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 (var mm in list)
			{
				var 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;
		}
예제 #20
0
        protected SqlQueryInfo CreateUpdateSqlText(DbManager db, Type type, int nParameter)
        {
            var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
            var om      = db.MappingSchema.GetObjectMapper(type);
            var sb      = new StringBuilder();
            var query   = new SqlQueryInfo(om);
            var mp      = MappingSchema.MetadataProvider;

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

            var fields    = GetFieldList(om);
            var hasFields = false;

            foreach (var mm in fields)
            {
                bool isSet;

                var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);

                if (nonUpdatableAttribute != null && isSet && nonUpdatableAttribute.OnUpdate == true)
                {
                    continue;
                }

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

                if (isSet)
                {
                    continue;
                }

                hasFields = true;

                var 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);
        }
예제 #21
0
		protected SqlQueryInfo CreateSelectByKeySqlText(DbManager db, Type type)
		{
			ObjectMapper  om    = db.MappingSchema.GetObjectMapper(type);
			StringBuilder sb    = new StringBuilder();
			SqlQueryInfo  query = new SqlQueryInfo(om);

			sb.Append("SELECT\n");

			foreach (MemberMapper mm in GetFieldList(om))
				sb.AppendFormat("\t{0},\n",
					db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));

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

			sb.AppendFormat("FROM\n\t{0}\n",
				db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

			AddWherePK(db, query, sb);

			query.QueryText = sb.ToString();

			return query;
		}
예제 #22
0
		protected SqlQueryInfo CreateUpdateSqlText(DbManager db, Type type)
		{
			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.AppendFormat("UPDATE\n\t{0}\nSET\n",
				db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

			foreach (MemberMapper mm in GetFieldList(om))
			{
				bool isSet;

				if (!mp.GetNonUpdatableFlag(type, typeExt, mm.MemberAccessor, out isSet) || !isSet)
				{
					SqlQueryParameterInfo p = query.AddParameter(
						db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryParameter).ToString(),
						mm.Name);

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

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

			AddWherePK(db, query, sb);

			query.QueryText = sb.ToString();

			return query;
		}
예제 #23
0
		protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type)
		{
			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.AppendFormat("INSERT INTO {0} (\n",
				db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

			foreach (MemberMapper mm in GetFieldList(om))
			{
				// IT: This works incorrectly for complex mappers.
				//
				bool isSet;

				if (!mp.GetNonUpdatableFlag(type, typeExt, mm.ComplexMemberAccessor, out isSet) || !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, ConvertType.NameToQueryParameter).ToString(),
					mm.Name);

				sb.AppendFormat("\t{0},\n", p.ParameterName);
			}

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

			sb.Append(")");

			query.QueryText = sb.ToString();

			return query;
		}
예제 #24
0
		protected virtual void AddWherePK(DbManager db, SqlQueryInfo query, StringBuilder sb, int nParameter)
		{
			sb.Append("WHERE\n");

			var memberMappers = GetKeyFieldList(db, query.ObjectType);

			if (memberMappers.Length == 0)
				throw new DataAccessException(
						string.Format("No primary key field(s) in the type '{0}'.", query.ObjectType.FullName));

			foreach (var mm in memberMappers)
			{
				var p = query.AddParameter(
					db.DataProvider.Convert(mm.Name + "_W", ConvertType.NameToQueryParameter).ToString(),
					mm.Name);

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

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

			sb.Remove(sb.Length - 5, 5);
		}
예제 #25
0
		protected SqlQueryInfo CreateDeleteSqlText(DbManager db, Type type)
		{
			ObjectMapper  om    = db.MappingSchema.GetObjectMapper(type);
			StringBuilder sb    = new StringBuilder();
			SqlQueryInfo  query = new SqlQueryInfo(om);

			sb.AppendFormat("DELETE FROM\n\t{0}\n",
				db.DataProvider.Convert(GetTableName(type), ConvertType.NameToQueryTable));

			AddWherePK(db, query, sb);

			query.QueryText = sb.ToString();

			return query;
		}
예제 #26
0
        protected void AddWherePK(DbManager db, SqlQueryInfo query, StringBuilder sb, int nParameter, FullObjectMapper mapper)
        {
            sb.Append("WHERE\n");

            foreach (IMapper mm in mapper.PropertiesMapping)
            {
                if (mm is ValueMapper && mm.DataReaderIndex == mapper.DataReaderIndex)
                {
                    var valueMapper = (ValueMapper) mm;

                    string tableAlias = mapper.PropertyType.Name;

                    //mm.Name = ID_TRACK
                    SqlQueryParameterInfo p = query.AddParameter(
                        db.DataProvider.Convert(valueMapper.ColumnName + "_W", ConvertType.NameToQueryParameter).
                           ToString(),
                        valueMapper.ColumnName);

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

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

            sb.Remove(sb.Length - 5, 5);
        }
예제 #27
0
		protected SqlQueryInfo CreateSelectAllSqlText(DbManager db, Type type)
		{
			var om    = db.MappingSchema.GetObjectMapper(type);
			var sb    = new StringBuilder();
			var query = new SqlQueryInfo(om);

			sb.Append("SELECT\n");

			foreach (var mm in GetFieldList(om))
				sb.AppendFormat("\t{0},\n",
					db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));

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

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

			query.QueryText = sb.ToString();

			return query;
		}
예제 #28
0
		protected SqlQueryInfo CreateInsertSqlText(DbManager db, Type type, int nParameter, bool insertAutoSequence = false)
		{
			var typeExt = TypeExtension.GetTypeExtension(type, Extensions);
			var om      = db.MappingSchema.GetObjectMapper(type);
			var list    = new List<MemberMapper>();
			var sb      = new StringBuilder();
			var query   = new SqlQueryInfo(om);
			var mp      = MappingSchema.MetadataProvider;

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

            
			foreach (var 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;
				var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);
                if (insertAutoSequence && nonUpdatableAttribute is IdentityAttribute)
                {                    
                    sb.AppendFormat("\t{0},\n", db.DataProvider.Convert(mm.Name, ConvertType.NameToQueryField));
                    list.Add(mm);
                }
                else
                {
                    if (nonUpdatableAttribute == null || !isSet || nonUpdatableAttribute.IsIdentity || nonUpdatableAttribute.OnInsert == false)
                    {
                        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");

            MemberMapper identityMember = null;
            foreach (var mm in list)
            {
                var keyGenerator = mm.MapMemberInfo.KeyGenerator as SequenceKeyGenerator;
                if (keyGenerator != null && insertAutoSequence)
                {
                    string seqQuery = db.DataProvider.NextSequenceQuery(keyGenerator.Sequence);
                    sb.AppendFormat("\t{0},\n", seqQuery);
                    identityMember = mm;
                }
                else
                {
                    // Previously : mm.Name
                    var p = query.AddParameter(
                        db.DataProvider.Convert(mm.MemberName + "_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(")");

            if (identityMember != null)
            {
                sb.AppendFormat("\r\n{0}", db.DataProvider.GetReturningInto(identityMember.Name));

                query.AddParameter(
                    db.DataProvider.Convert("IDENTITY_PARAMETER", ConvertType.NameToQueryParameter).ToString(),
                    identityMember.Name);
            }

			query.QueryText = sb.ToString();

			return query;
		}
예제 #29
0
		protected SqlQueryInfo CreateDeleteSqlText(DbManager db, Type type, int nParameter)
		{
			var om    = db.MappingSchema.GetObjectMapper(type);
			var sb    = new StringBuilder();
			var query = new SqlQueryInfo(om);

			sb.Append("DELETE FROM\n\t");
			AppendTableName(sb, db, type);
			sb.AppendLine();

			AddWherePK(db, query, sb, nParameter);

			query.QueryText = sb.ToString();

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

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

			var fields    = GetFieldList(om);
			var hasFields = false;

			foreach (var mm in fields)
			{
				bool isSet;

				var nonUpdatableAttribute = mp.GetNonUpdatableAttribute(type, typeExt, mm.MapMemberInfo.MemberAccessor, out isSet);

				if (nonUpdatableAttribute != null && isSet && nonUpdatableAttribute.OnUpdate == true)
					continue;

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

				if (isSet)
					continue;

				hasFields = true;

				var 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;
		}