Skip to content

Instantly share code, notes, and snippets.

@kkurni
Last active January 26, 2016 06:52
Show Gist options
  • Select an option

  • Save kkurni/d49bfa32beb557b11684 to your computer and use it in GitHub Desktop.

Select an option

Save kkurni/d49bfa32beb557b11684 to your computer and use it in GitHub Desktop.

Revisions

  1. kkurni revised this gist Jan 26, 2016. 1 changed file with 11 additions and 6 deletions.
    17 changes: 11 additions & 6 deletions GetSchemaMockup.cs
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,12 @@ static void Main(string[] args)
    ProcessSomething(new SqlCommand().ExecuteReader());
    ProcessSomething(new PostgreDataReader());

    //or you can callit directly from sqldatareader
    var colSchema = new SqlCommand().ExecuteReader().GetColumnSchema();

    }

    //you can access it in data reader
    static void ProcessSomething(DbDataReader reader)
    {
    //this will replace reader.GetSchemaTable();
    @@ -30,9 +34,6 @@ static void ProcessSomething(DbDataReader reader)

    namespace System.Data.Common
    {
    #region <<Mock Class>>
    public class DbColumn { }
    #endregion

    public static class DbReaderExtension
    {
    @@ -57,6 +58,11 @@ public static void RegisterDbSchemaGenerator(Type dbReaderType, Func<DbSchemaGen

    public abstract Collection<DbColumn> GetColumnSchema(DbDataReader reader);
    }


    #region <<Not important stuff>>
    public class DbColumn { }
    #endregion
    }

    namespace System.Data.SqlClient
    @@ -113,9 +119,8 @@ public override Collection<DbColumn> GetColumnSchema(DbDataReader reader)

    }


    #region <<Mock Class>>
    #region <<not important stuff>>
    //just to mock postgre Data reader
    public class PostgreDataReader : DbDataReader { public override object this[string name] { get { throw new NotImplementedException(); } } public override object this[int ordinal] { get { throw new NotImplementedException(); } } public override int Depth { get { throw new NotImplementedException(); } } public override int FieldCount { get { throw new NotImplementedException(); } } public override bool HasRows { get { throw new NotImplementedException(); } } public override bool IsClosed { get { throw new NotImplementedException(); } } public override int RecordsAffected { get { throw new NotImplementedException(); } } public override bool GetBoolean(int ordinal) { throw new NotImplementedException(); } public override byte GetByte(int ordinal) { throw new NotImplementedException(); } public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length) { throw new NotImplementedException(); } public override char GetChar(int ordinal) { throw new NotImplementedException(); } public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length) { throw new NotImplementedException(); } public override string GetDataTypeName(int ordinal) { throw new NotImplementedException(); } public override DateTime GetDateTime(int ordinal) { throw new NotImplementedException(); } public override decimal GetDecimal(int ordinal) { throw new NotImplementedException(); } public override double GetDouble(int ordinal) { throw new NotImplementedException(); } public override IEnumerator GetEnumerator() { throw new NotImplementedException(); } public override Type GetFieldType(int ordinal) { throw new NotImplementedException(); } public override float GetFloat(int ordinal) { throw new NotImplementedException(); } public override Guid GetGuid(int ordinal) { throw new NotImplementedException(); } public override short GetInt16(int ordinal) { throw new NotImplementedException(); } public override int GetInt32(int ordinal) { throw new NotImplementedException(); } public override long GetInt64(int ordinal) { throw new NotImplementedException(); } public override string GetName(int ordinal) { throw new NotImplementedException(); } public override int GetOrdinal(string name) { throw new NotImplementedException(); } public override string GetString(int ordinal) { throw new NotImplementedException(); } public override object GetValue(int ordinal) { throw new NotImplementedException(); } public override int GetValues(object[] values) { throw new NotImplementedException(); } public override bool IsDBNull(int ordinal) { throw new NotImplementedException(); } public override bool NextResult() { throw new NotImplementedException(); } public override bool Read() { throw new NotImplementedException(); } };
    #endregion
    }
    }
  2. kkurni created this gist Jan 26, 2016.
    121 changes: 121 additions & 0 deletions GetSchemaMockup.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,121 @@
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;


    namespace SchemaMockup.Console
    {
    using System.Data.Common;
    using System.Data.SqlClient;
    using PostgreClient;

    class Program
    {
    static void Main(string[] args)
    {
    //if it is called from sql client
    ProcessSomething(new SqlCommand().ExecuteReader());
    ProcessSomething(new PostgreDataReader());

    }

    static void ProcessSomething(DbDataReader reader)
    {
    //this will replace reader.GetSchemaTable();
    var columnSchema = reader.GetColumnSchema();
    }
    }
    }


    namespace System.Data.Common
    {
    #region <<Mock Class>>
    public class DbColumn { }
    #endregion

    public static class DbReaderExtension
    {

    //this is will be exposed to the client application
    public static Collection<DbColumn> GetColumnSchema(this DbDataReader reader)
    {
    return DbSchemaGenerator.Generators[reader.GetType()]().GetColumnSchema(reader);
    }
    }

    public abstract class DbSchemaGenerator
    {

    //this is internal only DataCommon and access this
    internal static Dictionary<Type, Func<DbSchemaGenerator>> Generators = new Dictionary<Type, Func<DbSchemaGenerator>>();
    public static void RegisterDbSchemaGenerator(Type dbReaderType, Func<DbSchemaGenerator> constuctorDelegate)
    {
    Generators.Add(dbReaderType, constuctorDelegate);
    }


    public abstract Collection<DbColumn> GetColumnSchema(DbDataReader reader);
    }
    }

    namespace System.Data.SqlClient
    {
    using System.Data.Common;

    //this can be internal which no one can create this generator except the client
    internal class SqlDbSchemaGenerator : DbSchemaGenerator
    {
    static SqlDbSchemaGenerator()
    {
    //Register this Generator
    RegisterDbSchemaGenerator(typeof(SqlDataReader), () =>
    {
    return new SqlDbSchemaGenerator();
    });
    }

    public override Collection<DbColumn> GetColumnSchema(DbDataReader reader)
    {
    //implement sql schema specifically.
    throw new NotImplementedException();
    }
    }
    }

    namespace PostgreClient
    {
    using System.Collections;
    using System.Data.Common;

    //this can be internal which no one can create this generator except the client
    internal class PostgreDbSchemaGenerator : DbSchemaGenerator
    {
    static PostgreDbSchemaGenerator()
    {
    //Register this Generator
    RegisterDbSchemaGenerator(typeof(PostgreDataReader), () =>
    {
    return new PostgreDbSchemaGenerator("Some parameter");
    });
    }

    private PostgreDbSchemaGenerator(string param)
    {
    //creat custom param
    }

    public override Collection<DbColumn> GetColumnSchema(DbDataReader reader)
    {
    //implement sql schema specifically.
    throw new NotImplementedException();
    }

    }


    #region <<Mock Class>>
    //just to mock postgre Data reader
    public class PostgreDataReader : DbDataReader { public override object this[string name] { get { throw new NotImplementedException(); } } public override object this[int ordinal] { get { throw new NotImplementedException(); } } public override int Depth { get { throw new NotImplementedException(); } } public override int FieldCount { get { throw new NotImplementedException(); } } public override bool HasRows { get { throw new NotImplementedException(); } } public override bool IsClosed { get { throw new NotImplementedException(); } } public override int RecordsAffected { get { throw new NotImplementedException(); } } public override bool GetBoolean(int ordinal) { throw new NotImplementedException(); } public override byte GetByte(int ordinal) { throw new NotImplementedException(); } public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length) { throw new NotImplementedException(); } public override char GetChar(int ordinal) { throw new NotImplementedException(); } public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length) { throw new NotImplementedException(); } public override string GetDataTypeName(int ordinal) { throw new NotImplementedException(); } public override DateTime GetDateTime(int ordinal) { throw new NotImplementedException(); } public override decimal GetDecimal(int ordinal) { throw new NotImplementedException(); } public override double GetDouble(int ordinal) { throw new NotImplementedException(); } public override IEnumerator GetEnumerator() { throw new NotImplementedException(); } public override Type GetFieldType(int ordinal) { throw new NotImplementedException(); } public override float GetFloat(int ordinal) { throw new NotImplementedException(); } public override Guid GetGuid(int ordinal) { throw new NotImplementedException(); } public override short GetInt16(int ordinal) { throw new NotImplementedException(); } public override int GetInt32(int ordinal) { throw new NotImplementedException(); } public override long GetInt64(int ordinal) { throw new NotImplementedException(); } public override string GetName(int ordinal) { throw new NotImplementedException(); } public override int GetOrdinal(string name) { throw new NotImplementedException(); } public override string GetString(int ordinal) { throw new NotImplementedException(); } public override object GetValue(int ordinal) { throw new NotImplementedException(); } public override int GetValues(object[] values) { throw new NotImplementedException(); } public override bool IsDBNull(int ordinal) { throw new NotImplementedException(); } public override bool NextResult() { throw new NotImplementedException(); } public override bool Read() { throw new NotImplementedException(); } };
    #endregion
    }