Skip to content

Commit

Permalink
enhanced markdown outputter
Browse files Browse the repository at this point in the history
  • Loading branch information
Saveen Reddy committed Jul 3, 2017
1 parent fd0e675 commit e08c984
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,99 +8,4 @@

namespace MVADemo
{

[SqlUserDefinedOutputter(AtomicFileProcessing = true)]
public class MarkdownOutputter : IOutputter
{
private int row_count;

public MarkdownOutputter()
{
row_count = 0;
}

public override void Close()
{
}

public override void Output(IRow row, IUnstructuredWriter output)
{
var streamWriter = new StreamWriter(output.BaseStream);

// Metadata schema initialization to enumerate column names
var schema = row.Schema;


if (this.row_count == 0)
{
streamWriter.Write("|");
for (int i = 0; i < schema.Count(); i++)
{
var col = schema[i];
streamWriter.Write(" ");
streamWriter.Write(col.Name);
streamWriter.Write(" ");
streamWriter.Write("|");
}
streamWriter.Write("\r\n");
streamWriter.Flush();

streamWriter.Write("|");
for (int i = 0; i < schema.Count(); i++)
{
var col = schema[i];
streamWriter.Write(" ");
streamWriter.Write("---");
streamWriter.Write(" ");
streamWriter.Write("|");
}
streamWriter.Write("\r\n");
streamWriter.Flush();
}

// Data row output
streamWriter.Write("|");
for (int i = 0; i < schema.Count(); i++)
{
var col = schema[i];
string val = "";
try
{
// Data type enumeration--required to match the distinct list of types from OUTPUT statement
switch (col.Type.Name.ToString().ToLower())
{
case "string":
val = row.Get<string>(col.Name).ToString();
break;
case "int":
val = row.Get<int>(col.Name).ToString();
break;
case "float":
val = row.Get<float>(col.Name).ToString();
break;
case "double":
val = row.Get<double>(col.Name).ToString();
break;
case "guid":
val = row.Get<Guid>(col.Name).ToString();
break;
default: break;
}
}
catch (System.NullReferenceException)
{
// Handling NULL values--keeping them empty
val = "NULL";
}
streamWriter.Write(" ");
streamWriter.Write(val);
streamWriter.Write(" ");
streamWriter.Write("|");
}
streamWriter.Write("\n");
streamWriter.Flush();

this.row_count++;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@querylog =
/*
@querylog =
SELECT * FROM ( VALUES
("Banana" , 300, "Image" ),
("Cherry" , 300, "Image" ),
Expand Down Expand Up @@ -38,4 +39,44 @@ OUTPUT @result

OUTPUT @querylog
TO "/querylog.md"
USING new MVADemo.MarkdownOutputter();
USING new MVADemo.MarkdownOutputter();
*/


@cities =
SELECT * FROM
( VALUES
( "Vermont", "Burlington;Essex;South Burlington;Colchester;Rutland" ),
( "Virginia", "Virginia Beach;Norfolk;Chesapeake;Richmond;Newport News" ),
( "Washington", "Seattle;Spokane;Tacoma;Vancouver;Bellevue" ),
( "West Virginia", "Charleston;Huntington;Parkersburg;Morgantown;Wheeling" ),
( "Wisconsin", "Milwaukee;Madison;Green Bay;Kenosha;Racine" ),
( "Wyoming", "Cheyenne;Casper;Laramie;Gillette;Rock Springs" )
)
AS T(State, Cities);


@output =
SELECT
State,
SqlArray.Create( Cities.Split(';') ) AS Cities
FROM @cities;

@output =
SELECT
State ,
SqlArray.Create( Cities.Where( c=>c.StartsWith("C") ) ) AS Cities
FROM @output;

@output =
SELECT
State ,
Cities ,
Cities.Count AS NumCities
FROM @output;

OUTPUT @output
TO "/cities.md"
USING new MVADemo.MarkdownOutputter();


Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public override void Output(IRow row, IUnstructuredWriter output)
val = row.Get<string>(col.Name).ToString();
val = val ?? "NULL";
}
else if (coltype == typeof(System.String))
{
val = row.Get<System.String>(col.Name).ToString();
val = val ?? "NULL";
}
else if (coltype == typeof(char))
{
val = row.Get<char>(col.Name).ToString();
}
else if (coltype == typeof(float))
{
val = row.Get<float>(col.Name).ToString();
Expand All @@ -81,6 +90,10 @@ public override void Output(IRow row, IUnstructuredWriter output)
{
val = row.Get<double>(col.Name).ToString();
}
else if (coltype == typeof(int))
{
val = row.Get<int>(col.Name).ToString();
}
else if (coltype == typeof(long))
{
val = row.Get<long>(col.Name).ToString();
Expand Down Expand Up @@ -109,16 +122,49 @@ public override void Output(IRow row, IUnstructuredWriter output)
val = row.Get < double?> (col.Name).ToString();
val = val ?? "NULL";
}
else if (coltype == typeof(SqlArray<string>))
{

var arr = row.Get<SqlArray<string>>(col.Name);

if (arr != null)
{
var sb = new System.Text.StringBuilder();
sb.Append("SqlArray<");
sb.Append("string");
sb.Append(">{ ");

for (int j = 0; j < arr.Count; j++)
{
if (j > 0)
{
sb.Append(", ");
}

sb.Append("\"");
sb.Append(arr[j]);
sb.Append("\"");

}

sb.Append(" }");
val = sb.ToString();
}
else
{
val = "NULL";
}
}
else
{
val = "UNKNOWNTYPE";
val = "UNKNOWNTYPE:" + coltype.FullName;
}

}
catch (System.NullReferenceException)
{
// Handling NULL values--keeping them empty
val = "NULL";
val = "EXCEPTION";
}
streamWriter.Write(" ");
streamWriter.Write(val);
Expand All @@ -128,7 +174,8 @@ public override void Output(IRow row, IUnstructuredWriter output)
streamWriter.Write("\n");
streamWriter.Flush();

this.row_count++;
this.row_count++;
}

}
}

0 comments on commit e08c984

Please sign in to comment.