/// <summary> /// Gets the columns schema information for the given database table. /// </summary> /// <param name="dataTable">The data table to get the schema info for.</param> /// <returns>Table with schema information regarding its columns.</returns> public static MySqlColumnsInformationTable GetColumnsInformationTable(this DataTable dataTable) { if (dataTable == null) { return(null); } var schemaInfoTable = new MySqlColumnsInformationTable(); foreach (DataColumn column in dataTable.Columns) { var newRow = schemaInfoTable.NewRow(); newRow["Name"] = column.ColumnName; newRow["Type"] = MySqlDataType.GetMySqlDataType(column.DataType); newRow["Null"] = column.AllowDBNull ? "YES" : "NO"; newRow["Key"] = dataTable.PrimaryKey.Any(indexCol => indexCol.ColumnName == column.ColumnName) ? "PRI" : string.Empty; newRow["Default"] = column.DefaultValue != null?column.DefaultValue.ToString() : string.Empty; newRow["CharSet"] = null; newRow["Collation"] = null; newRow["Extra"] = column.AutoIncrement ? MySqlDataColumn.ATTRIBUTE_AUTO_INCREMENT : string.Empty; schemaInfoTable.Rows.Add(newRow); } return(schemaInfoTable); }
/// <summary> /// Returns a table containing schema information for columns contained in a MySQL table with the given name. /// </summary> /// <param name="connection">MySQL Workbench connection to a MySQL server instance selected by users.</param> /// <param name="schemaName">The schema the MySQL table belongs to.</param> /// <param name="tableName">The name of a MySQL table.</param> /// <param name="beautifyDataTypes">Flag indicating whether the data types are camel cased as shown in the Export Data data type combo box.</param> /// <returns>A table containing schema information for columns contained in a MySQL table with the given name.</returns> public static MySqlColumnsInformationTable GetColumnsInformationTable(this MySqlWorkbenchConnection connection, string schemaName, string tableName, bool beautifyDataTypes = false) { if (connection == null) { return(null); } schemaName = string.IsNullOrEmpty(schemaName) ? connection.Schema : schemaName; var schemaTable = connection.GetSchemaCollection("Columns", null, schemaName, tableName); if (schemaTable == null) { return(null); } var columnsInfoTable = new MySqlColumnsInformationTable(schemaTable.TableName); foreach (DataRow row in schemaTable.Rows) { string dataType = row["COLUMN_TYPE"].ToString(); if (beautifyDataTypes) { var mySqlDataType = new MySqlDataType(dataType, false); dataType = mySqlDataType.FullType; } var infoRow = columnsInfoTable.NewRow(); infoRow["Name"] = row["COLUMN_NAME"]; infoRow["Type"] = dataType; infoRow["Null"] = row["IS_NULLABLE"]; infoRow["Key"] = row["COLUMN_KEY"]; infoRow["Default"] = row["COLUMN_DEFAULT"]; infoRow["CharSet"] = row["CHARACTER_SET_NAME"]; infoRow["Collation"] = row["COLLATION_NAME"]; infoRow["Extra"] = row["EXTRA"]; columnsInfoTable.Rows.Add(infoRow); } return(columnsInfoTable); }