/// <summary> /// Generates custom fields and methods of the class. /// </summary> /// <param name="file">The file.</param> /// <param name="classMap">The class map.</param> public override void GenerateCustomFieldsAndMethods(StreamWriter file, IClassMap classMap) { ArrayList allColumns = classMap.GetTableMap().ColumnMaps; IColumnMap[] allColumnsTyped = (IColumnMap[])allColumns.ToArray(typeof(IColumnMap)); string columnNames = StringUtility.CombineObjects(allColumnsTyped, MapToStringConverters.Columns).ToString(); file.WriteLine(" protected static readonly string c_rowFields = \""+ columnNames + "\";"); base.GenerateCustomFieldsAndMethods(file, classMap); }
/// <summary> /// Generates the 'verify schema' code. /// </summary> /// <param name="file">The file.</param> /// <param name="classMap">The class map.</param> public override void GenerateVerifySchema(StreamWriter file, IClassMap classMap) { #warning TODO: finish VerifySchema method if (classMap.PropertyMaps.Count <= 0) { file.WriteLine(" return null; // no property maps"); return; // ??? } // no table - create it bool firstColumn = true; StringBuilder sqlCommandText = new StringBuilder("\"CREATE TABLE IF NOT EXISTS `" + classMap.Table + "` (\"", classMap.PropertyMaps.Count * 16); foreach (IPropertyMap propertyMap in classMap.PropertyMaps) { IColumnMap columnMap = propertyMap.GetColumnMap(); // close prev statement's quote, start new line if (firstColumn) { sqlCommandText.Append("\n"); firstColumn = false; } else { sqlCommandText.Append(",\"\n"); } // opening quote sqlCommandText.Append(" +\""); // build one field creation statement sqlCommandText .Append("`") .Append(columnMap.Name) .Append("` ") .Append(MySqlUtility.GetDataType(columnMap)); // is nulls allowed if (!columnMap.AllowNulls) { sqlCommandText.Append(" NOT NULL"); } // default value if (columnMap.DefaultValue != null && columnMap.DefaultValue.Length > 0) { sqlCommandText .Append(" default '") .Append(MySqlUtility.Escape(columnMap.DefaultValue)) .Append("'"); } // auto_increment? if (columnMap.IsAutoIncrease) { sqlCommandText.Append(" auto_increment"); } } // close last statement's quotes, start new line sqlCommandText.Append("\"\n"); // primary keys ArrayList primaryKeyMaps = classMap.GetTableMap().GetPrimaryKeyColumnMaps(); if (primaryKeyMaps.Count > 0) { // opening quote sqlCommandText.Append(" +\""); // write primary key name and open fields list string pkName = StringUtility.CombineObjects((IColumnMap[])primaryKeyMaps.ToArray(typeof(IColumnMap)), MapToStringConverters.Join).ToString(); sqlCommandText.Append(", primary key `" + pkName + "`"); // combine primary key fields IColumnMap[] primaryKeyMapsTyped = (IColumnMap[])primaryKeyMaps.ToArray(typeof(IColumnMap)); StringBuilder primaryKeyMapNames = StringUtility.CombineObjects(primaryKeyMapsTyped, MapToStringConverters.Columns); sqlCommandText.Append(" ("); sqlCommandText.Append(primaryKeyMapNames); sqlCommandText.Append(")\"\n"); } else { // no primary // sqlCommandText.Append("\n"); } // close the field list sqlCommandText.Append(" +\")\""); // write to file file.WriteLine(" m_state.ExecuteNonQuery("+ sqlCommandText.ToString() + "\n );"); file.WriteLine(" m_state.ExecuteNonQuery(\"OPTIMIZE TABLE `"+ classMap.GetTableMap().Name + "`\");"); file.WriteLine(" return null;"); }