Files
linqpad/Information Schema.linq
T

141 lines
5.7 KiB
C#

<Query Kind="Program">
<Connection>
<ID>77b65bea-e766-4e3c-a1ed-c4c2534b6019</ID>
<Persist>true</Persist>
<Server>D0001-SQL-11.sdt.local</Server>
<Database>jazz dev master Prod</Database>
<ShowServer>true</ShowServer>
</Connection>
<Reference>&lt;ProgramFilesX64&gt;\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
<NuGetReference>Amazon.Lambda.S3Events</NuGetReference>
<NuGetReference>AWSSDK.Core</NuGetReference>
<NuGetReference>AWSSDK.S3</NuGetReference>
<Namespace>Amazon</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy.ActionIdentifiers</Namespace>
<Namespace>Amazon.Internal</Namespace>
<Namespace>Amazon.Lambda.S3Events</Namespace>
<Namespace>Amazon.MissingTypes</Namespace>
<Namespace>Amazon.Runtime</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement.Internal</Namespace>
<Namespace>Amazon.Runtime.EventStreams</Namespace>
<Namespace>Amazon.Runtime.EventStreams.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal.Auth</Namespace>
<Namespace>Amazon.Runtime.Internal.Settings</Namespace>
<Namespace>Amazon.Runtime.Internal.Transform</Namespace>
<Namespace>Amazon.Runtime.Internal.Util</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces.Internal</Namespace>
<Namespace>Amazon.S3</Namespace>
<Namespace>Amazon.S3.Encryption</Namespace>
<Namespace>Amazon.S3.Encryption.Internal</Namespace>
<Namespace>Amazon.S3.Internal</Namespace>
<Namespace>Amazon.S3.IO</Namespace>
<Namespace>Amazon.S3.Model</Namespace>
<Namespace>Amazon.S3.Model.Internal.MarshallTransformations</Namespace>
<Namespace>Amazon.S3.Transfer</Namespace>
<Namespace>Amazon.S3.Util</Namespace>
<Namespace>Amazon.Util</Namespace>
<Namespace>Amazon.Util.Internal</Namespace>
<Namespace>Amazon.Util.Internal.PlatformServices</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Diagnostics</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
<Namespace>System.IO.Compression</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1.Utilities</Namespace>
<Namespace>ThirdParty.BouncyCastle.Math</Namespace>
<Namespace>ThirdParty.BouncyCastle.OpenSsl</Namespace>
<Namespace>ThirdParty.BouncyCastle.Utilities.IO.Pem</Namespace>
<Namespace>ThirdParty.Ionic.Zlib</Namespace>
<Namespace>ThirdParty.Json.LitJson</Namespace>
<Namespace>ThirdParty.MD5</Namespace>
<Namespace>System.Runtime.Remoting.Contexts</Namespace>
</Query>
void Main()
{
GetColumns();
}
public void GetSchema()
{
var schemata = ExecuteQuery<Schemata>("select * from Information_Schema.SCHEMATA")
.GroupBy(g => g.CATALOG_NAME, (Key, Data) => new { Key, Data = Data.OrderBy(d => d.SCHEMA_OWNER).ThenBy(d => d.SCHEMA_NAME).ToList() })
.OrderBy(grp => grp.Key)
.ToList();
schemata.Dump();
}
public void GetTables(string schema = null)
{
var tables = ExecuteQuery<Table>($"select * from Information_Schema.TABLES AS t WHERE t.TABLE_SCHEMA = '{schema}'")
.GroupBy(x => new { x.TABLE_SCHEMA, x.TABLE_TYPE },
(Key, Tables) => new { Schema = Key.TABLE_SCHEMA, Type = Key.TABLE_TYPE, Tables = Tables.Select(ta => ta.TABLE_NAME).OrderBy(ta => ta).ToList() });
tables.Dump();
}
public void GetColumns(string schema = null)
{
// var tableDefs = tables.Where(t => t.Schema == schema).SelectMany(t => t.Tables.Select(ta =>
// ExecuteQuery<Column>($"select * from Information_Schema.COLUMNS AS c WHERE c.TABLE_SCHEMA = '{t.Schema}' AND c.TABLE_NAME = '{ta}'")));
var tableDefs = ExecuteQuery<TableColumn>($"select * from Information_Schema.COLUMNS AS c")
.GroupBy(c => new Table {TABLE_CATALOG= c.TABLE_CATALOG, TABLE_SCHEMA = c.TABLE_SCHEMA, TABLE_TYPE = c.TABLE_NAME },
(Key, Columns) => new { Key, Columns = Columns.OrderBy(c => c.ORDINAL_POSITION).ToList() })
.OrderBy(c => c.Key.TABLE_CATALOG)
.ThenBy(c => c.Key.TABLE_SCHEMA)
.ThenBy(c => c.Key.TABLE_NAME);
tableDefs.Dump();
}
// Define other methods and classes here
public class Schemata
{
public string CATALOG_NAME { get; set; }
public string SCHEMA_NAME { get; set; }
public string SCHEMA_OWNER { get; set; }
}
public class Table
{
public string TABLE_CATALOG { get; set; }
public string TABLE_SCHEMA { get; set; }
public string TABLE_NAME { get; set; }
public string TABLE_TYPE { get; set; }
}
public class TableColumn
{
public string TABLE_CATALOG { get; set; }
public string TABLE_SCHEMA { get; set; }
public string TABLE_NAME { get; set; }
public string COLUMN_NAME { get; set; }
public int? ORDINAL_POSITION { get; set; }
public string COLUMN_DEFAULT { get; set; }
public string IS_NULLABLE { get; set; }
public string DATA_TYPE { get; set; }
public int? CHARACTER_MAXIMUM_LENGTH { get; set; }
public int? CHARACTER_OCTET_LENGTH { get; set; }
public byte? NUMERIC_PRECISION { get; set; }
public short? NUMERIC_PRECISION_RADIX { get; set; }
public int? NUMERIC_SCALE { get; set; }
public short? DATETIME_PRECISION { get; set; }
public string CHARACTER_SET_CATALOG { get; set; }
public string CHARACTER_SET_SCHEMA { get; set; }
public string CHARACTER_SET_NAME { get; set; }
public string COLLATION_CATALOG { get; set; }
public string COLLATION_SCHEMA { get; set; }
public string COLLATION_NAME { get; set; }
public string DOMAIN_CATALOG { get; set; }
public string DOMAIN_SCHEMA { get; set; }
public string DOMAIN_NAME { get; set; }
}