// Convertでmemberとpropertyに対して同じオペレーションを行なわなければならないので // その共通部分を抜き出した。 private void FieldEqualHelper(MemberInfo info, object obj, string field, StringBuilder sb) { DBColumnAttribute att = info.GetCustomAttributes(typeof(DBColumnAttribute), false)[0] as DBColumnAttribute; if (att == null) { return; } // フィールド名は、名前がDBColumnAttributeで指定されていればそれを用いる。 // さもなくば、メンバ名をそのまま用いる string fieldName = att.Name != null?att.Name.ToUpper() : info.Name.ToUpper(); if (field != fieldName) { return; // これではないようだ } fieldName = '`' + fieldName + '`'; // こいつのオブジェクトを取得する必要がある sb.AppendFormat("{0}={1}", fieldName, ToSQLString(obj) ); }
// Insertでmemberとpropertyに対して同じオペレーションを行なわなければならないので // その共通部分を抜き出した。 private void InsertHelper(MemberInfo info, object obj, StringBuilder fields, StringBuilder values) { DBColumnAttribute att = info.GetCustomAttributes(typeof(DBColumnAttribute), false)[0] as DBColumnAttribute; if (att == null) { return; } // 勝手に設定される値は設定してはいけない if (att.AutoIncrement) { return; } // フィールド名は、名前がDBColumnAttributeで指定されていればそれを用いる。 // さもなくば、メンバ名をそのまま用いる string fieldName = att.Name != null ? att.Name : info.Name; fieldName = '`' + fieldName.ToUpper() + '`'; fields.Append(fieldName + ",\n"); string valueString; valueString = ToSQLString(obj); if (valueString == null) { return; } values.Append(valueString + ",\n"); }
// Convertでmemberとpropertyに対して同じオペレーションを行なわなければならないので // その共通部分を抜き出した。 private void ConvertHelper <T>(MemberInfo info, Type type, int count, ref int j, DataSet dataSet, List <T> list) { DBColumnAttribute att = info.GetCustomAttributes(typeof(DBColumnAttribute), false)[0] as DBColumnAttribute; if (att == null) { return; } for (int i = 0; i < count; ++i) { object o = dataSet.Tables[0].Rows[i][j]; ConvertHelperHelper(ref o); { PropertyInfo info2 = info as PropertyInfo; if (info2 != null) { info2.SetValue(list[i], o, null); } else { FieldInfo info3 = info as FieldInfo; // if ( info3 != null ) // どっちかに決まってるから、このチェック無駄 info3.SetValue(list[i], o); } } } ++j; }
// Convertでmemberとpropertyに対して同じオペレーションを行なわなければならないので // その共通部分を抜き出した。 private void ConvertHelper2 <T>(MemberInfo info, Type type, int count, int j, string name, DataSet dataSet, List <T> list) { DBColumnAttribute att = info.GetCustomAttributes(typeof(DBColumnAttribute), false)[0] as DBColumnAttribute; if (att == null) { return; } // フィールド名は、名前がDBColumnAttributeで指定されていればそれを用いる。 // さもなくば、メンバ名をそのまま用いる string fieldName = att.Name != null ? att.Name : info.Name; if (name != fieldName.ToUpper()) { return; } for (int i = 0; i < count; ++i) { object o = dataSet.Tables[0].Rows[i][j]; ConvertHelperHelper(ref o); { PropertyInfo info2 = info as PropertyInfo; if (info2 != null) { info2.SetValue(list[i], o, null); } else { FieldInfo info3 = info as FieldInfo; // if ( info3 != null ) // どっちかに決まってるから、このチェック無駄 info3.SetValue(list[i], o); } } } }
// CreateTableでmemberとpropertyに対して同じオペレーションを行なわなければならないので // その共通部分を抜き出した。 private void CreateTableHelper(MemberInfo info, Type type, StringBuilder sb, StringBuilder primaryKeys, StringBuilder uniqueKeys) { DBColumnAttribute att2 = info.GetCustomAttributes(typeof(DBColumnAttribute), false)[0] as DBColumnAttribute; if (att2 == null) { return; } string t; int size = att2.Size; // Typeに対してswitch文が使えないの、おかしくね?(´ω`) if (type == typeof(int)) { if (size == 0) { size = 11; // おまじない } t = String.Format("INT({0})", size); } else if (type == typeof(string)) { if (size <= (1 << 8) - 1) { if (att2.VarChar) { t = String.Format("VARCHAR({0})", size); } else { t = String.Format("CHAR({0})", size); } } else if (size <= (1 << 16) - 1) { t = "TEXT"; } else { t = "LONGTEXT"; } } else if (type == typeof(DateTime)) { // t = "DATE"; t = "DATETIME"; } else if (type == typeof(byte[])) { if (size <= (1 << 8) - 1) { t = "TINYBLOB"; } else if (size <= (1 << 16) - 1) { t = "BLOB"; } else if (size <= (1 << 24) - 1) { t = "MEDIUMBLOB"; } else { t = "LONGBLOB"; } } else if (type == typeof(bool)) { t = "TINYINT(1)"; } else if (type == typeof(float)) { t = "FLOAT"; } else if (type == typeof(double)) { t = "DOUBLE"; } else { return; } // フィールド名は、名前がDBColumnAttributeで指定されていればそれを用いる。 // さもなくば、メンバ名をそのまま用いる string fieldName = att2.Name != null ? att2.Name : info.Name; fieldName = '`' + fieldName.ToUpper() + '`'; // NULL制約を示す文字列 string isNullString = att2.Null ? "NULL" : "NOT NULL"; // ディフォルト値について定義があれば、それを代入 string defaultString = att2.Default != null ? "DEFAULT " + att2.Default + " " : ""; // オプションの指定があれば、それを入れる string optionString = att2.Option != null ? " " + att2.Option : ""; // AutoIncrementの指定があるか? if (att2.AutoIncrement) { optionString += " AUTO_INCREMENT "; } sb.AppendFormat("{0}{1}{2}{3}{4},\n", fieldName.PadRight(12), t.PadRight(16), defaultString, isNullString, optionString ); // プライマリキーに指定されているならば、それを追加。 if (att2.PrimaryKey) { if (primaryKeys.Length != 0) { primaryKeys.Append(","); } primaryKeys.Append(fieldName); } // ユニークキーに指定されているならば、それを追加。 if (att2.UniqueKey) { uniqueKeys.AppendFormat("UNIQUE({0}),\n", fieldName); } }