/// <summary> /// Generates the 'create' method. /// </summary> /// <param name="file">The file.</param> /// <param name="classMap">The class map.</param> public override void GenerateCreate(StreamWriter file, IClassMap classMap) { bool first = true; // create fields list StringBuilder valuesQuery = new StringBuilder(16 * classMap.PropertyMaps.Count); foreach (IPropertyMap propertyMap in classMap.PropertyMaps) { if (!first) { valuesQuery.Append(","); } valuesQuery.Append("'\" + m_state.EscapeString(obj." + propertyMap.Name + ".ToString()) + \"'"); first = false; } string sqlCommand = "INSERT INTO `" + classMap.GetTableMap().Name + "`" + " VALUES (" + valuesQuery + ");"; // find auto_incremented values IPropertyMap autoIncrementProperty = null; foreach (IPropertyMap propertyMap in classMap.PropertyMaps) { IColumnMap columnMap = propertyMap.GetColumnMap(); if (columnMap.IsAutoIncrease) { // just one auto_increment column autoIncrementProperty = propertyMap; break; } } file.WriteLine(" m_state.ExecuteNonQuery("); file.WriteLine(" \""+ sqlCommand + "\");"); if (autoIncrementProperty != null) { file.WriteLine(" object insertedId = m_state.ExecuteScalar(\"SELECT LAST_INSERT_ID();\");"); file.WriteLine(" obj."+ autoIncrementProperty.Name + " = (" + ClassUtility.ConvertColumnTypeToCsType(autoIncrementProperty.GetColumnMap().DataType) + ") (long) insertedId;"); } }
/// <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(); }