Created
January 24, 2020 16:45
-
-
Save amirrajan/ba5b04b802db62f9de06c276fe5affa9 to your computer and use it in GitHub Desktop.
OSql clone written in C# (BetterOSql).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Data; | |
using System.Data.SqlClient; | |
namespace BetterOSql | |
{ | |
class Program | |
{ | |
static int Main(string[] args) | |
{ | |
Console.WriteLine("* args"); | |
foreach (var arg in args) System.Console.WriteLine(arg); | |
if (args.Length == 0) { System.Console.WriteLine("args is empty...."); } | |
var opts = ParseArgs(args); | |
if (!opts.ContainsKey("connection-string")) throw new InvalidOperationException("Connection string required: --connection-string=\"Data Source=(local);User ID=USERID;password=PASSWORD;Initial Catalog=INITIALCATALOG;\""); | |
if (!opts.ContainsKey("query") && !opts.ContainsKey("query-file")) throw new InvalidOperationException("Query required: --query=\"select * from TABLE\" OR --query-file=path.sql"); | |
var query = ""; | |
if (opts.ContainsKey("query")) | |
{ | |
query = opts["query"]; | |
} | |
else if (opts.ContainsKey("query-file")) | |
{ | |
query = System.IO.File.ReadAllText(opts["query-file"].Replace("/", "\\")); | |
} | |
try | |
{ | |
PrintResults(query, opts["connection-string"]); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
return -1; | |
} | |
return 0; | |
} | |
static void PrintResults(string query, string connectionString) | |
{ | |
var reader = ExecuteReader(query, connectionString); | |
Console.WriteLine("* Query"); | |
Console.WriteLine("#+begin_src sql"); | |
Console.WriteLine(query.Trim()); | |
Console.WriteLine("#+end_src"); | |
while (reader.HasRows) | |
{ | |
Console.WriteLine("* Result"); | |
while (reader.Read()) | |
{ | |
Console.WriteLine("** Row"); | |
for (int i = 0; i < reader.FieldCount; i++) | |
{ | |
var name = reader.GetName(i); | |
var value = reader.GetValue(i).ToString(); | |
Console.WriteLine($"*** {name}: {value}"); | |
} | |
} | |
reader.NextResult(); | |
} | |
} | |
static Dictionary<string, string> ParseArgs(string[] args) | |
{ | |
var results = new Dictionary<string, string>(); | |
foreach (var arg in args) | |
{ | |
var key = arg.Split('=').First().Replace("--", ""); | |
var value = string.Join("=", arg.Split('=').Skip(1).Take(int.MaxValue)); | |
results.Add(key, value); | |
} | |
return results; | |
} | |
public static SqlDataReader ExecuteReader(string query, string connectionString) | |
{ | |
SqlCommand sqlCommand = new SqlCommand(); | |
sqlCommand.Connection = new SqlConnection(connectionString); | |
sqlCommand.Connection.Open(); | |
sqlCommand.CommandText = String.Format(query); | |
return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment