コード例 #1
0
        /// <summary>
        /// Generates the 'find by' method.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="propertyMaps">The property maps.</param>
        /// <param name="returnedClassMap">The returned class map.</param>
        public override void GenerateFindBy(StreamWriter file, IList <IPropertyMap> propertyMaps, IClassMap returnedClassMap)
        {
            ArrayList allColumns = returnedClassMap.GetTableMap().ColumnMaps;

            // build the 'where' statement
            bool          first      = true;
            StringBuilder whereQuery = new StringBuilder(16 * propertyMaps.Count);

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                if (!first)
                {
                    whereQuery.Append(" AND ");
                }

                whereQuery
                .Append('`')
                .Append(propertyMap.GetColumnMap().Name)
                .Append("`='\" + m_state.EscapeString(")
                .Append(ClassUtility.GetParamName(propertyMap))
                .Append(".ToString()) + \"'");

                first = false;
            }

            string entityClassName = EntityGenerator.GetTypeName(returnedClassMap);
            string sqlCommand      = "SELECT  \" + c_rowFields + \""
                                     + " FROM `" + returnedClassMap.GetTableMap().Name + "`"
                                     + " WHERE " + whereQuery.ToString();

            // create an array and store results
            file.WriteLine("			"+ entityClassName + " entity;");
            file.WriteLine("			List<"+ entityClassName + "> results = null;");
            file.WriteLine();
            file.WriteLine("			m_state.ExecuteQuery(");
            file.WriteLine("				\""+ sqlCommand + "\",");
            file.WriteLine("				CommandBehavior.Default,");
            file.WriteLine("				delegate(MySqlDataReader reader)");
            file.WriteLine("				{");
            file.WriteLine("					results = new List<"+ entityClassName + ">(reader.FieldCount);");
            file.WriteLine("					while (reader.Read())");
            file.WriteLine("					{");
            file.WriteLine("						entity = new "+ entityClassName + "();");
            file.WriteLine("						FillEntityWithRow(ref entity, reader);");
            file.WriteLine("						results.Add(entity);");
            file.WriteLine("					}");
            file.WriteLine("				}");
            file.WriteLine("			);");
            file.WriteLine();
            file.WriteLine("			return results;");
        }
コード例 #2
0
        /// <summary>
        /// Generates the 'count by' method.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="propertyMaps">The property maps.</param>
        /// <param name="returnedClassMap">The returned class map.</param>
        public override void GenerateCountBy(StreamWriter file, IList <IPropertyMap> propertyMaps, IClassMap returnedClassMap)
        {
            ArrayList allColumns = returnedClassMap.GetTableMap().ColumnMaps;

            // build the 'where' statement
            bool          first      = true;
            StringBuilder whereQuery = new StringBuilder(16 * propertyMaps.Count);

            foreach (IPropertyMap propertyMap in propertyMaps)
            {
                if (!first)
                {
                    whereQuery.Append(" AND ");
                }

                whereQuery
                .Append('`')
                .Append(propertyMap.GetColumnMap().Name)
                .Append("`='\" + m_state.EscapeString(")
                .Append(ClassUtility.GetParamName(propertyMap))
                .Append(".ToString()) + \"'");

                first = false;
            }

            string entityClassName = EntityGenerator.GetTypeName(returnedClassMap);
            string sqlCommand      = "SELECT  count(*)"
                                     + " FROM `" + returnedClassMap.GetTableMap().Name + "`"
                                     + " WHERE " + whereQuery.ToString();

            // create an array and store results
            file.WriteLine();
            file.WriteLine("			return (long) m_state.ExecuteScalar(");
            file.WriteLine("				\""+ sqlCommand + "\");");
            file.WriteLine();
        }
コード例 #3
0
        /// <summary>
        /// Generates the 'find' method.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="classMap">The class map.</param>
        public override void GenerateFind(StreamWriter file, IClassMap classMap)
        {
            ArrayList primaryColumns = classMap.GetTableMap().GetPrimaryKeyColumnMaps();

            StringBuilder whereQuery = new StringBuilder();
            StringBuilder findParams = new StringBuilder();
            bool          first      = true;

            foreach (IColumnMap primColumn in primaryColumns)
            {
                IPropertyMap propertyMap = classMap.GetPropertyMapForColumnMap(primColumn);

                if (!first)
                {
                    whereQuery.Append(", ");
                    findParams.Append(", ");
                }

                whereQuery
                .Append('`')
                .Append(primColumn.Name)
                .Append("`='\" + m_state.EscapeString(")
                .Append(ClassUtility.GetParamName(propertyMap))
                .Append(".ToString()) + \"'");

                string paramName = ClassUtility.GetParamName(propertyMap);
                string paramType = ClassUtility.ConvertColumnTypeToCsType(primColumn.DataType);

                findParams.Append(paramType + " " + paramName);

                first = false;
            }

            string entityClassName = EntityGenerator.GetTypeName(classMap);
            string sqlCommand      = "SELECT \" + c_rowFields + \""
                                     + " FROM `" + classMap.GetTableMap().Name + "`"
                                     + " WHERE " + whereQuery.ToString();

            file.WriteLine("		public virtual "+ entityClassName + " Find(" + findParams.ToString() + ")");
            file.WriteLine("		{");
            file.WriteLine("			"+ entityClassName + " result = new " + entityClassName + "();");
            file.WriteLine("			string command = \""+ sqlCommand + "\";");
            file.WriteLine();
            file.WriteLine("			m_state.ExecuteQuery(");
            file.WriteLine("				command,");
            file.WriteLine("				CommandBehavior.SingleRow,");
            file.WriteLine("				delegate(MySqlDataReader reader)");
            file.WriteLine("				{");
            file.WriteLine("					if (!reader.Read())");
            file.WriteLine("					{");
            file.WriteLine("						result = null;");
            file.WriteLine("					}");
            file.WriteLine("					else");
            file.WriteLine("					{");
            file.WriteLine("						FillEntityWithRow(ref result, reader);");
            file.WriteLine("					}");
            file.WriteLine("				}");
            file.WriteLine("			);");
            file.WriteLine();
            file.WriteLine("			return result;");
            file.WriteLine("		}");
            file.WriteLine();
        }