miniSql

创建
zgc123@gmail.com authored at 11/19/2023 1:40:15 AM
6136600
Tree
0 Parent(s)
Summary: 4 changed files with 270 additions and 0 deletions.
Added +148 -0
Added +57 -0
Added +44 -0
Added +21 -0
Added +148 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/Template/HenriFormatter.cs b/minisqlquery-master/src/MiniSqlQuery.Core/Template/HenriFormatter.cs
new file mode 100644
index 0000000..87c3027
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/Template/HenriFormatter.cs
@@ -0,0 +1,148 @@
+using System;
+using System.IO;
+using System.Text;
+using System.Web;
+using System.Web.UI;
+
+namespace MiniSqlQuery.Core.Template
+{
+	/// <summary>
+	/// From an online debate on parsing, original author todo....
+	/// </summary>
+	public class HenriFormatter : ITextFormatter
+	{
+		#region ITextFormatter Members
+
+		public string Format(string text, object dataSource)
+		{
+			if (text == null)
+			{
+				throw new ArgumentNullException("text");
+			}
+
+			StringBuilder result = new StringBuilder(text.Length*2);
+
+			using (var reader = new StringReader(text))
+			{
+				StringBuilder expression = new StringBuilder();
+				int @char = -1;
+
+				TemplateParseState templateParseState = TemplateParseState.OutsideExpression;
+				do
+				{
+					switch (templateParseState)
+					{
+						case TemplateParseState.OutsideExpression:
+							@char = reader.Read();
+							switch (@char)
+							{
+								case -1:
+									templateParseState = TemplateParseState.End;
+									break;
+								case '{':
+									templateParseState = TemplateParseState.OnOpenBracket;
+									break;
+								case '}':
+									templateParseState = TemplateParseState.OnCloseBracket;
+									break;
+								default:
+									result.Append((char) @char);
+									break;
+							}
+							break;
+						case TemplateParseState.OnOpenBracket:
+							@char = reader.Read();
+							switch (@char)
+							{
+								case -1:
+									throw new FormatException();
+								case '{':
+									result.Append('{');
+									templateParseState = TemplateParseState.OutsideExpression;
+									break;
+								default:
+									expression.Append((char) @char);
+									templateParseState = TemplateParseState.InsideExpression;
+									break;
+							}
+							break;
+						case TemplateParseState.InsideExpression:
+							@char = reader.Read();
+							switch (@char)
+							{
+								case -1:
+									throw new FormatException();
+								case '}':
+									result.Append(ResolveExpression(dataSource, expression.ToString()));
+									expression.Length = 0;
+									templateParseState = TemplateParseState.OutsideExpression;
+									break;
+								default:
+									expression.Append((char) @char);
+									break;
+							}
+							break;
+						case TemplateParseState.OnCloseBracket:
+							@char = reader.Read();
+							switch (@char)
+							{
+								case '}':
+									result.Append('}');
+									templateParseState = TemplateParseState.OutsideExpression;
+									break;
+								default:
+									throw new FormatException();
+							}
+							break;
+						default:
+							throw new InvalidOperationException("Invalid state.");
+					}
+				}
+				while (templateParseState != TemplateParseState.End);
+			}
+
+			return result.ToString();
+		}
+
+		#endregion
+
+		private string ResolveExpression(object source, string expression)
+		{
+			string format = "";
+
+			int colonIndex = expression.IndexOf(':');
+			if (colonIndex > 0)
+			{
+				format = expression.Substring(colonIndex + 1);
+				expression = expression.Substring(0, colonIndex);
+			}
+
+			try
+			{
+				// yes, it uses the databinder
+				if (String.IsNullOrEmpty(format))
+				{
+					return (DataBinder.Eval(source, expression) ?? "").ToString();
+				}
+				return DataBinder.Eval(source, expression, "{0:" + format + "}") ?? "";
+			}
+			catch (HttpException exp)
+			{
+				throw new FormatException(exp.Message);
+			}
+		}
+
+		#region Nested type: TemplateParseState
+
+		private enum TemplateParseState
+		{
+			OutsideExpression,
+			OnOpenBracket,
+			InsideExpression,
+			OnCloseBracket,
+			End
+		}
+
+		#endregion
+	}
+}
\ No newline at end of file
Added +57 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs b/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs
new file mode 100644
index 0000000..bce56d2
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/Template/NVelocityWrapper.cs
@@ -0,0 +1,57 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+using System.IO;
+using NVelocity;
+using NVelocity.App;
+using NVelocity.Exception;
+
+namespace MiniSqlQuery.Core.Template
+{
+    /// <summary>The n velocity wrapper.</summary>
+    public class NVelocityWrapper : ITextFormatter
+    {
+        /// <summary>The format.</summary>
+        /// <param name="text">The text.</param>
+        /// <param name="items">The items.</param>
+        /// <returns>The format.</returns>
+        /// <exception cref="TemplateException"></exception>
+        public string Format(string text, Dictionary<string, object> items)
+        {
+            try
+            {
+                VelocityContext velocityContext = new VelocityContext();
+
+                if (items != null)
+                {
+                    foreach (var pair in items)
+                    {
+                        velocityContext.Put(pair.Key, pair.Value);
+                    }
+                }
+
+                StringWriter sw = new StringWriter();
+                VelocityEngine velocityEngine = new VelocityEngine();
+                velocityEngine.Init();
+
+                bool ok = velocityEngine.Evaluate(velocityContext, sw, "ContextTest.CaseInsensitive", text);
+
+                if (!ok)
+                {
+                    throw new TemplateException("Template run error (try adding an extra newline at the end of the file)");
+                }
+
+                return sw.ToString();
+            }
+            catch (ParseErrorException parseErrorException)
+            {
+                throw new TemplateException(parseErrorException.Message, parseErrorException);
+            }
+        }
+    }
+}
\ No newline at end of file
Added +44 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/Template/TemplateException.cs b/minisqlquery-master/src/MiniSqlQuery.Core/Template/TemplateException.cs
new file mode 100644
index 0000000..db25dce
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/Template/TemplateException.cs
@@ -0,0 +1,44 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System;
+using System.Runtime.Serialization;
+
+namespace MiniSqlQuery.Core.Template
+{
+    /// <summary>The template exception.</summary>
+    [Serializable]
+    public class TemplateException : Exception
+    {
+        /// <summary>Initializes a new instance of the <see cref="TemplateException"/> class.</summary>
+        public TemplateException()
+        {
+        }
+
+        /// <summary>Initializes a new instance of the <see cref="TemplateException"/> class.</summary>
+        /// <param name="message">The message.</param>
+        public TemplateException(string message) : base(message)
+        {
+        }
+
+        /// <summary>Initializes a new instance of the <see cref="TemplateException"/> class.</summary>
+        /// <param name="message">The message.</param>
+        /// <param name="inner">The inner.</param>
+        public TemplateException(string message, Exception inner) : base(message, inner)
+        {
+        }
+
+        /// <summary>Initializes a new instance of the <see cref="TemplateException"/> class.</summary>
+        /// <param name="info">The info.</param>
+        /// <param name="context">The context.</param>
+        protected TemplateException(
+            SerializationInfo info,
+            StreamingContext context) : base(info, context)
+        {
+        }
+    }
+}
\ No newline at end of file
Added +21 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/Template/TextFormatter.cs b/minisqlquery-master/src/MiniSqlQuery.Core/Template/TextFormatter.cs
new file mode 100644
index 0000000..0afec69
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/Template/TextFormatter.cs
@@ -0,0 +1,21 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+#endregion
+
+using System.Collections.Generic;
+
+namespace MiniSqlQuery.Core.Template
+{
+    /// <summary>The i text formatter.</summary>
+    public interface ITextFormatter
+    {
+        /// <summary>The format.</summary>
+        /// <param name="text">The text.</param>
+        /// <param name="items">The items.</param>
+        /// <returns>The format.</returns>
+        string Format(string text, Dictionary<string, object> items);
+    }
+}
\ No newline at end of file