Permalink
Browse files

Rewrote Query skipping WebMatrix.Data

  • Loading branch information...
1 parent d5e7591 commit d8689318784d28270ac2f3a21d2454caa1fbbb92 Hans Olav Loftum committed Feb 10, 2012
@@ -1,37 +1,37 @@
using System.Collections.Generic;
+using System.Data.Common;
using System.IO;
-using DbTool.Lib.Communication.DbCommands.WebMatrix;
+using DbTool.Lib.Communication.DbCommands.Dynamic;
using DbTool.Lib.Configuration;
using Mono.CSharp;
namespace DbTool.Lib.CSharp.Mono
{
public class DbToolInteractive
{
- private static readonly WebMatrixQuery WebMatrix = new WebMatrixQuery();
+ private static readonly DynamicSqlQuery DynamicSql = new DynamicSqlQuery();
public static Evaluator Evaluator;
public static TextWriter Output = new StringWriter();
public static IEnumerable<dynamic> Schema(string collection)
{
- return WebMatrix.Schema(collection);
+ return DynamicSql.Schema(collection);
}
public static IEnumerable<dynamic> Query(string sql)
{
- return WebMatrix.Query(sql);
+ return DynamicSql.Query(sql);
}
- public static IEnumerable<T> Query<T>(string sql)
+ public static void SetDb(DbToolDatabase db)
{
- return WebMatrix.Query<T>(sql);
+ DynamicSql.ConnectionData = db.GetConnectionData();
}
- public static void SetDb(DbToolDatabase db)
+ public static void SetConnection(DbConnection connection)
{
- WebMatrix.ConnectionString = db.GetConnectionData().GetConnectionString();
- WebMatrix.ProviderName = db.GetConnectionData().ProviderName;
+ DynamicSql.DbConnection = connection;
}
public static string vars
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
-using DbTool.Lib.Communication.DbCommands.WebMatrix;
+using DbTool.Lib.Communication.DbCommands.Dynamic;
using DbTool.Lib.ExtensionMethods;
using Mono.CSharp;
using WebMatrix.Data;
@@ -13,8 +13,11 @@ public class MonoCSharpEvaluator : ICSharpEvaluator
private static readonly string[] InitialAssemblies = new[] { "System.Core.dll"};
private static readonly string[] InitialUsings = new[]
{
- "System", "System.Linq", "System.Collections.Generic",
- "WebMatrix.Data", "DbTool.Lib.Communication.DbCommands.WebMatrix"
+ "System",
+ "System.Linq",
+ "System.Collections.Generic",
+ "WebMatrix.Data",
+ typeof(DynamicSqlQuery).Namespace
};
public IEnumerable<string> Vars
@@ -47,7 +50,7 @@ public void Init()
InteractiveBaseClass = typeof (DbToolInteractive),
DescribeTypeExpressions = true,
};
- ReferenceAssemblies(typeof(Database).Assembly, typeof(WebMatrixQuery).Assembly);
+ ReferenceAssemblies(typeof(Database).Assembly, typeof(DynamicSqlQuery).Assembly);
Using(InitialUsings);
DbToolInteractive.Evaluator = _evaluator;
@@ -1,8 +1,9 @@
using System.Collections;
+using System.Data.Common;
using System.Text;
-using System.Text.RegularExpressions;
using DbTool.Lib.CSharp;
using DbTool.Lib.CSharp.Mono;
+using DbTool.Lib.Communication.DbCommands.Modifiers;
using DbTool.Lib.Communication.DbCommands.Results;
using DbTool.Lib.Configuration;
using DbTool.Lib.ExtensionMethods;
@@ -24,6 +25,17 @@ public DbToolDatabase Db
}
}
+ private DbConnection _connection;
+ public DbConnection DbConnection
+ {
+ get { return _connection; }
+ set
+ {
+ _connection = value;
+ DbToolInteractive.SetConnection(value);
+ }
+ }
+
public CSharpExecutor()
{
_collectionConverter = new CollectionConverter();
@@ -43,7 +55,7 @@ private IDbCommandResult GetResultOf(string command)
return new MessageResult(string.Format("C# Evaluator is reset"));
}
- command = ModifySql(command);
+ command = CommandModifier.Modify(command);
var builder = new StringBuilder();
builder.AppendFormat("Running: {0}", command).AppendLine();
@@ -67,19 +79,5 @@ private IDbCommandResult GetResultOf(string command)
}
return new MessageResult(builder.ToString());
}
-
- private string ModifySql(string command)
- {
- const string pattern = @"\${1}(<[^\s]+>)?\({1}[^\(^\)]+\){1}";
- if (command.Matches(pattern))
- {
- var rawSql = Regex.Match(command, pattern).Value;
- var query = rawSql.Replace("$", "Query")
- .Replace("(", "(\"")
- .Replace(")", "\")");
- command = command.Replace(rawSql, query);
- }
- return command;
- }
}
}
@@ -2,8 +2,8 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
+using DbTool.Lib.Communication.DbCommands.Dynamic;
using DbTool.Lib.Communication.DbCommands.Results;
-using DbTool.Lib.Communication.DbCommands.WebMatrix;
using DbTool.Lib.ExtensionMethods;
using WebMatrix.Data;
@@ -50,6 +50,7 @@ public virtual IDbCommandExecutor GetExecutorFor(string statement)
return new BackupExecutor();
}
_cSharpExecutor.Db = Database;
+ _cSharpExecutor.DbConnection = DbConnection;
return _cSharpExecutor;
}
}
@@ -4,7 +4,7 @@
using System.Threading;
using WebMatrix.Data;
-namespace DbTool.Lib.Communication.DbCommands.WebMatrix
+namespace DbTool.Lib.Communication.DbCommands.Dynamic
{
public class ClassGenerator
{
@@ -5,7 +5,7 @@
using System.Dynamic;
using System.Linq;
-namespace DbTool.Lib.Communication.DbCommands.WebMatrix
+namespace DbTool.Lib.Communication.DbCommands.Dynamic
{
public class DynamicDataRow : DynamicObject, ICustomTypeDescriptor
{
@@ -28,6 +28,21 @@ public DynamicDataRow(IEnumerable<string> columnNames, DataRow dataRow)
}
}
+ public DynamicDataRow(IEnumerable<string> columnNames, IDataRecord record)
+ {
+ Columns = columnNames.ToList();
+ _dictionary = new Dictionary<string, object>();
+ _values = new List<object>();
+ var index = 0;
+ foreach (var columnName in columnNames)
+ {
+ _dictionary[columnName.ToLowerInvariant()] = record[columnName];
+ _dictionary[columnName.Replace("_", string.Empty)] = record[columnName];
+ _values.Add(record[index]);
+ index++;
+ }
+ }
+
public object this[string columnName]
{
get { return _dictionary[columnName.ToLowerInvariant()]; }
@@ -1,7 +1,7 @@
using System;
using System.ComponentModel;
-namespace DbTool.Lib.Communication.DbCommands.WebMatrix
+namespace DbTool.Lib.Communication.DbCommands.Dynamic
{
public class DynamicDataRowPropertyDescriptor : PropertyDescriptor
{
@@ -0,0 +1,100 @@
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Common;
+using System.Linq;
+using DbTool.Lib.Configuration;
+using WebMatrix.Data;
+
+namespace DbTool.Lib.Communication.DbCommands.Dynamic
+{
+ public class DynamicSqlQuery
+ {
+ public ConnectionData ConnectionData { get; set; }
+ public DbConnection DbConnection { get; set; }
+
+
+ public IEnumerable<dynamic> Schema(string collection)
+ {
+ if (ConnectionData == null)
+ {
+ return Enumerable.Empty<dynamic>();
+ }
+ using (var db = Database.OpenConnectionString(ConnectionData.GetConnectionString(), ConnectionData.ProviderName))
+ {
+ try
+ {
+ db.Connection.Open();
+ var dataTable = db.Connection.GetSchema(collection);
+ return DoGetSchema(dataTable).ToList().AsReadOnly();
+ }
+ finally
+ {
+ db.Connection.Close();
+ }
+ }
+ }
+
+ private static IEnumerable<dynamic> DoGetSchema(DataTable dataTable)
+ {
+ var columns = GetColumns(dataTable);
+ return (from DataRow row in dataTable.Rows select new DynamicDataRow(columns, row));
+ }
+
+ private static IEnumerable<string> GetColumns(DataTable dataTable)
+ {
+ return from DataColumn column in dataTable.Columns select column.ColumnName;
+ }
+
+ public IEnumerable<dynamic> Query(string sql)
+ {
+ return DbConnection == null
+ ? Enumerable.Empty<dynamic>()
+ : DoQuery(sql);
+ }
+
+ private IEnumerable<dynamic> DoQuery(string sql)
+ {
+ using (var command = DbConnection.CreateCommand())
+ {
+ command.CommandText = sql.Trim();
+ DbConnection.Open();
+ try
+ {
+ using (var reader = command.ExecuteReader())
+ {
+ var columns = Getcolumns(reader);
+ foreach (IDataRecord row in reader)
+ {
+ yield return new DynamicDataRow(columns, row);
+ }
+ }
+ }
+ finally
+ {
+ command.Connection.Close();
+ }
+ }
+ }
+
+ private static IEnumerable<string> Getcolumns(IDataRecord reader)
+ {
+ for (var ii=0; ii<reader.FieldCount; ii++)
+ {
+ yield return reader.GetName(ii);
+ }
+ }
+
+ public IEnumerable<dynamic> QueryOld(string sql)
+ {
+ if (ConnectionData == null)
+ {
+ return Enumerable.Empty<dynamic>();
+ }
+ using (var db = Database.OpenConnectionString(ConnectionData.GetConnectionString(), ConnectionData.ProviderName))
+ {
+ var result = db.Query(sql);
+ return result;
+ }
+ }
+ }
+}
@@ -0,0 +1,45 @@
+using System.Text;
+using System.Text.RegularExpressions;
+using DbTool.Lib.ExtensionMethods;
+
+namespace DbTool.Lib.Communication.DbCommands.Modifiers
+{
+ public class CommandModifier
+ {
+ private const string Pattern = @"\${1}\({1}([^\(^\)]*)\){1}";
+
+ public static string Modify(string command)
+ {
+ if (command.IsNullOrEmpty())
+ {
+ return command;
+ }
+ var builder = new StringBuilder(command);
+ var regex = new Regex(Pattern);
+ foreach (Match match in regex.Matches(command))
+ {
+ var outside = match.Groups[0].Value;
+ var content = match.Groups[1].Value;
+ builder.Replace(outside, Wrap(Fnuttify(ToSelect(content))));
+ }
+ return builder.ToString();
+ }
+
+ private static string ToSelect(string text)
+ {
+ return text.IsSingleWord()
+ ? string.Format("select * from {0}", text)
+ : text;
+ }
+
+ private static string Wrap(string content)
+ {
+ return string.Format("Query({0})", content);
+ }
+
+ private static string Fnuttify(string text)
+ {
+ return string.Format("\"{0}\"", text.Trim('"'));
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit d868931

Please sign in to comment.