Skip to content

Instantly share code, notes, and snippets.

@kkurni
Last active January 26, 2016 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkurni/d49bfa32beb557b11684 to your computer and use it in GitHub Desktop.
Save kkurni/d49bfa32beb557b11684 to your computer and use it in GitHub Desktop.
GetSchemaMockup
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());
//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();
var columnSchema = reader.GetColumnSchema();
}
}
}
namespace System.Data.Common
{
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);
}
#region <<Not important stuff>>
public class DbColumn { }
#endregion
}
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 <<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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment