/// <summary> /// Gets the database column type. /// </summary> /// <param name="table">The table.</param> /// <param name="column">The column.</param> /// <returns>The Type.</returns> Type GetColumnType(string table, DbColumnInfo column) { Type ret; if (column.DatabaseType.Equals("tinyint(1) unsigned", StringComparison.OrdinalIgnoreCase)) return typeof(bool); using (var cmd = _conn.CreateCommand()) { cmd.CommandText = "SELECT `" + column.Name + "` FROM `" + table + "` WHERE 0=1"; using (var r = cmd.ExecuteReader()) { Debug.Assert(r.FieldCount == 1); ret = r.GetFieldType(0); } } return ret; }
/// <summary> /// Gets the public name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the public name for.</param> /// <returns>The public name for the <see cref="DbColumnInfo"/>.</returns> public string GetPublicName(DbColumnInfo dbColumn) { return _publicNames[dbColumn]; }
/// <summary> /// When overridden in the derived class, gets the <see cref="DbColumnInfo"/>s for the given /// <paramref name="table"/>. /// </summary> /// <param name="table">The name of the table to get the <see cref="DbColumnInfo"/>s for.</param> /// <returns> /// The <see cref="DbColumnInfo"/>s for the given <paramref name="table"/>. /// </returns> /// <exception cref="InvalidOperationException">Failed to get the type for a column on a table.</exception> protected override IEnumerable<DbColumnInfo> GetColumns(string table) { var ret = new List<DbColumnInfo>(); using (var cmd = _conn.CreateCommand()) { cmd.CommandText = "SHOW FULL COLUMNS IN `" + table + "`"; using (var r = cmd.ExecuteReader()) { while (r.Read()) { var name = r.GetString("Field"); var dbType = r.GetString("Type"); var nullable = GetColumnInfoNull(r.GetString("Null")); var defaultValue = r.GetValue(r.GetOrdinal("Default")); var keyType = GetColumnKeyType(r.GetString("Key")); var comment = r.GetString("Comment"); var column = new DbColumnInfo(name, dbType, null, nullable, defaultValue, comment, keyType); ret.Add(column); } } } foreach (var column in ret) { var type = GetColumnType(table, column); if (column.IsNullable) { if (type != typeof(string)) type = Type.GetType("System.Nullable`1[" + type.FullName + "]", true); } if (type == null) { const string errmsg = "Failed to get the type for column `{0}` on table `{1}`."; throw new InvalidOperationException(string.Format(errmsg, column.Name, table)); } column.Type = type; } return ret; }
/// <summary> /// Gets the parameter name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the parameter name for.</param> /// <returns>The parameter name for the <see cref="DbColumnInfo"/>.</returns> public string GetParameterName(DbColumnInfo dbColumn) { return _parameterNames[dbColumn]; }
/// <summary> /// Gets the private name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the private name for.</param> /// <returns>The private name for the <see cref="DbColumnInfo"/>.</returns> public string GetPrivateName(DbColumnInfo dbColumn) { return _privateNames[dbColumn]; }
/// <summary> /// Gets a string for the Type used externally for a given column. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the external type for.</param> /// <returns>A string for the Type used externally for a given column.</returns> public string GetExternalType(DbColumnInfo dbColumn) { var ret = _externalTypes[dbColumn]; if (dbColumn.IsNullable) ret = EnsureIsNullable(ret); return ret; }
/// <summary> /// Gets a string for the Type used internally for a given column. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the internal type for.</param> /// <returns>A string for the Type used internally for a given column.</returns> public string GetInternalType(DbColumnInfo dbColumn) { var ret = Formatter.GetTypeString(dbColumn.Type); if (dbColumn.IsNullable) ret = EnsureIsNullable(ret); return ret; }
/// <summary> /// Gets the code to use for the mutator for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the value mutator for.</param> /// <param name="valueName">Code to generate for the value to set.</param> /// <param name="columnSource">The name of the source collection if it is not in an instanced method. Can be null.</param> /// <returns>The code to use for the mutator for a <see cref="DbColumnInfo"/>.</returns> public string GetColumnValueMutator(DbColumnInfo dbColumn, string valueName, string columnSource = null) { ColumnCollectionItem item; var coll = GetCollectionForColumn(dbColumn, out item); var sb = new StringBuilder(); if (string.IsNullOrEmpty(columnSource) || columnSource.Trim().Length == 0) columnSource = "this"; sb.Append(columnSource); sb.Append("."); if (coll == null) { // Not part of a collection sb.Append(GetPublicName(dbColumn)); sb.Append(" = "); sb.Append(Formatter.GetCast(GetExternalType(dbColumn))); sb.Append(valueName); sb.Append(Formatter.EndOfLine); } else { // Part of a collection sb.Append("Set" + GetPublicName(coll)); sb.Append(Formatter.OpenParameterString); sb.Append(Formatter.GetCast(coll.KeyType)); sb.Append(item.Key); sb.Append(Formatter.ParameterSpacer); sb.Append(Formatter.GetCast(coll.ExternalType)); sb.Append(valueName); sb.Append(Formatter.CloseParameterString); sb.Append(Formatter.EndOfLine); } return sb.ToString(); }
/// <summary> /// Gets the code string used for accessing a database <see cref="DbColumnInfo"/>'s value from a DataReader. /// </summary> /// <param name="column">The <see cref="DbColumnInfo"/> to get the value from.</param> /// <param name="ordinalFieldName">Name of the local field used to store the ordinal. The ordinal /// must already be assigned to this field.</param> /// <param name="variableName">Name of the variable.</param> /// <returns> /// The code string used for accessing a database <see cref="DbColumnInfo"/>'s value. /// </returns> public string GetDataReaderAccessor(DbColumnInfo column, string ordinalFieldName, string variableName) { var callMethod = GetDataReaderReadMethodName(column.Type); // Find the method to use for reading the value var sb = new StringBuilder(); // Cast sb.Append(Formatter.GetCast(GetExternalType(column))); // Accessor if (column.IsNullable) { sb.Append(Formatter.OpenParameterString); sb.Append(variableName); sb.Append(".IsDBNull"); sb.Append(Formatter.OpenParameterString); sb.Append(ordinalFieldName); sb.Append(Formatter.CloseParameterString); sb.Append(" ? "); sb.Append(Formatter.GetCast(column.Type)); sb.Append("null : "); } sb.Append(variableName); sb.Append("."); sb.Append(callMethod); sb.Append(Formatter.OpenParameterString); sb.Append(ordinalFieldName); sb.Append(Formatter.CloseParameterString); if (column.IsNullable) sb.Append(Formatter.CloseParameterString); return sb.ToString(); }
/// <summary> /// Gets the <see cref="ColumnCollection"/> for a given <see cref="DbColumnInfo"/>, or null if the /// <see cref="DbColumnInfo"/> is not part of any <see cref="ColumnCollection"/> in this table. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the <see cref="ColumnCollection"/> for.</param> /// <param name="item">The <see cref="ColumnCollectionItem"/> for the <paramref name="dbColumn"/> in the /// <see cref="ColumnCollection"/>.</param> /// <returns>The <see cref="ColumnCollection"/> the <see cref="DbColumnInfo"/> is part of, or null if it /// is not part of a <see cref="ColumnCollection"/>.</returns> /// <exception cref="ArgumentException">The <paramref name="dbColumn"/> matched more than one <see cref="ColumnCollection"/>.</exception> public ColumnCollection GetCollectionForColumn(DbColumnInfo dbColumn, out ColumnCollectionItem item) { foreach (var columnCollection in ColumnCollections) { var matches = columnCollection.Columns.Where(x => x.ColumnName.Equals(dbColumn.Name, StringComparison.OrdinalIgnoreCase)); var count = matches.Count(); if (count == 1) { item = matches.First(); return columnCollection; } else if (count > 1) { const string errmsg = "DbColumnInfo for column `{0}` in table `{1}` matched more than one ColumnCollection!"; throw new ArgumentException(string.Format(errmsg, dbColumn.Name, TableName)); } } item = default(ColumnCollectionItem); return null; }
/// <summary> /// Gets the code to use for the accessor for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the value accessor for.</param> /// <returns>The code to use for the accessor for a <see cref="DbColumnInfo"/>.</returns> public string GetColumnValueAccessor(DbColumnInfo dbColumn) { ColumnCollectionItem item; var coll = GetCollectionForColumn(dbColumn, out item); if (coll == null) { // Not part of a collection return GetPublicName(dbColumn); } else { // Part of a collection var sb = new StringBuilder(); sb.Append("Get" + GetPublicName(coll)); sb.Append(Formatter.OpenParameterString); sb.Append(Formatter.GetCast(coll.KeyType)); sb.Append(item.Key); sb.Append(Formatter.CloseParameterString); return sb.ToString(); } }
/// <summary> /// Gets the <see cref="ColumnCollection"/> for a given <see cref="DbColumnInfo"/>, or null if the /// <see cref="DbColumnInfo"/> is not part of any <see cref="ColumnCollection"/> in this table. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the <see cref="ColumnCollection"/> for.</param> /// <returns>The <see cref="ColumnCollection"/> the <see cref="DbColumnInfo"/> is part of, or null if it /// is not part of a <see cref="ColumnCollection"/>.</returns> public ColumnCollection GetCollectionForColumn(DbColumnInfo dbColumn) { ColumnCollectionItem item; return GetCollectionForColumn(dbColumn, out item); }
/// <summary> /// Gets the public name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the public name for.</param> /// <returns>The public name for the <see cref="DbColumnInfo"/>.</returns> public string GetPublicName(DbColumnInfo dbColumn) { return(_publicNames[dbColumn]); }
/// <summary> /// Gets the private name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the private name for.</param> /// <returns>The private name for the <see cref="DbColumnInfo"/>.</returns> public string GetPrivateName(DbColumnInfo dbColumn) { return(_privateNames[dbColumn]); }
/// <summary> /// Gets the parameter name for a <see cref="DbColumnInfo"/>. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the parameter name for.</param> /// <returns>The parameter name for the <see cref="DbColumnInfo"/>.</returns> public string GetParameterName(DbColumnInfo dbColumn) { return(_parameterNames[dbColumn]); }
/// <summary> /// Gets the <see cref="ColumnCollection"/> for a given <see cref="DbColumnInfo"/>, or null if the /// <see cref="DbColumnInfo"/> is not part of any <see cref="ColumnCollection"/> in this table. /// </summary> /// <param name="dbColumn">The <see cref="DbColumnInfo"/> to get the <see cref="ColumnCollection"/> for.</param> /// <returns>The <see cref="ColumnCollection"/> the <see cref="DbColumnInfo"/> is part of, or null if it /// is not part of a <see cref="ColumnCollection"/>.</returns> public ColumnCollection GetCollectionForColumn(DbColumnInfo dbColumn) { ColumnCollectionItem item; return(GetCollectionForColumn(dbColumn, out item)); }