Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit c21bed6

Browse files
committed
Refactor PR
1 parent d86f94b commit c21bed6

File tree

3 files changed

+161
-148
lines changed

3 files changed

+161
-148
lines changed
Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Data;
43
using System.Linq.Expressions;
5-
using System.Text;
6-
using ServiceStack.OrmLite;
7-
using ServiceStack.OrmLite.SqlServer.Converters;
8-
using ServiceStack.Text;
94

105
namespace ServiceStack.OrmLite.SqlServer
116
{
@@ -53,13 +48,13 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
5348
case nameof(Sql.Custom):
5449
statement = quotedColName.ToString();
5550
break;
56-
case nameof(Sql2016.IsJson):
51+
case nameof(Sql.IsJson):
5752
statement = $"ISJSON({quotedColName})";
5853
break;
59-
case nameof(Sql2016.JsonValue):
54+
case nameof(Sql.JsonValue):
6055
statement = $"JSON_VALUE({quotedColName}, '{args[0]}')";
6156
break;
62-
case nameof(Sql2016.JsonQuery):
57+
case nameof(Sql.JsonQuery):
6358
statement = $"JSON_QUERY({quotedColName}";
6459
if (DialectProvider is SqlServer2017OrmLiteDialectProvider && args.Count > 0)
6560
{
@@ -75,120 +70,3 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
7570
}
7671
}
7772
}
78-
79-
namespace ServiceStack.OrmLite
80-
{
81-
public static class Sql2016
82-
{
83-
/// <summary>Tests whether a string contains valid JSON.</summary>
84-
/// <param name="expression">The string to test.</param>
85-
/// <returns>Returns True if the string contains valid JSON; otherwise, returns False. Returns null if expression is null.</returns>
86-
/// <remarks>ISJSON does not check the uniqueness of keys at the same level.</remarks>
87-
/// <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/isjson-transact-sql"/>
88-
public static bool? IsJson(string expression) => null;
89-
90-
/// <summary>Extracts a scalar value from a JSON string.</summary>
91-
/// <param name="expression">
92-
/// An expression. Typically the name of a variable or a column that contains JSON text.<br/><br/>
93-
/// If <b>JSON_VALUE</b> finds JSON that is not valid in expression before it finds the value identified by <i>path</i>, the function returns an error. If <b>JSON_VALUE</b> doesn't find the value identified by <i>path</i>, it scans the entire text and returns an error if it finds JSON that is not valid anywhere in <i>expression</i>.
94-
/// </param>
95-
/// <param name="path">
96-
/// A JSON path that specifies the property to extract. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/json-path-expressions-sql-server">JSON Path Expressions (SQL Server)</see>.<br/><br/>
97-
/// In SQL Server 2017 and in Azure SQL Database, you can provide a variable as the value of <i>path</i>.<br/><br/>
98-
/// If the format of path isn't valid, <b>JSON_VALUE</b> returns an error.<br/><br/>
99-
/// </param>
100-
/// <returns>
101-
/// Returns a single text value of type nvarchar(4000). The collation of the returned value is the same as the collation of the input expression.
102-
/// If the value is greater than 4000 characters: <br/><br/>
103-
/// <ul>
104-
/// <li>In lax mode, <b>JSON_VALUE</b> returns null.</li>
105-
/// <li>In strict mode, <b>JSON_VALUE</b> returns an error.</li>
106-
/// </ul>
107-
/// <br/>
108-
/// If you have to return scalar values greater than 4000 characters, use <b>OPENJSON</b> instead of <b>JSON_VALUE</b>. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql">OPENJSON (Transact-SQL)</see>.
109-
/// </returns>
110-
/// <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql"/>
111-
public static T JsonValue<T>(string expression, string path) => default(T);
112-
113-
/// <summary>Extracts a scalar value from a JSON string.</summary>
114-
/// <param name="expression">
115-
/// An expression. Typically the name of a variable or a column that contains JSON text.<br/><br/>
116-
/// If <b>JSON_VALUE</b> finds JSON that is not valid in expression before it finds the value identified by <i>path</i>, the function returns an error. If <b>JSON_VALUE</b> doesn't find the value identified by <i>path</i>, it scans the entire text and returns an error if it finds JSON that is not valid anywhere in <i>expression</i>.
117-
/// </param>
118-
/// <param name="path">
119-
/// A JSON path that specifies the property to extract. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/json-path-expressions-sql-server">JSON Path Expressions (SQL Server)</see>.<br/><br/>
120-
/// In SQL Server 2017 and in Azure SQL Database, you can provide a variable as the value of <i>path</i>.<br/><br/>
121-
/// If the format of path isn't valid, <b>JSON_VALUE</b> returns an error.<br/><br/>
122-
/// </param>
123-
/// <returns>
124-
/// Returns a single text value of type nvarchar(4000). The collation of the returned value is the same as the collation of the input expression.
125-
/// If the value is greater than 4000 characters: <br/><br/>
126-
/// <ul>
127-
/// <li>In lax mode, <b>JSON_VALUE</b> returns null.</li>
128-
/// <li>In strict mode, <b>JSON_VALUE</b> returns an error.</li>
129-
/// </ul>
130-
/// <br/>
131-
/// If you have to return scalar values greater than 4000 characters, use <b>OPENJSON</b> instead of <b>JSON_VALUE</b>. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql">OPENJSON (Transact-SQL)</see>.
132-
/// </returns>
133-
/// <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql"/>
134-
public static string JsonValue(string expression, string path) =>
135-
$"JSON_VALUE({expression}, '{path}')";
136-
137-
/// <summary>
138-
/// Extracts an object or an array from a JSON string.<br/><br/>
139-
/// To extract a scalar value from a JSON string instead of an object or an array, see <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql">JSON_VALUE(Transact-SQL)</see>.
140-
/// For info about the differences between <b>JSON_VALUE</b> and <b>JSON_QUERY</b>, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/validate-query-and-change-json-data-with-built-in-functions-sql-server#JSONCompare">Compare JSON_VALUE and JSON_QUERY</see>.
141-
/// </summary>
142-
/// <typeparam name="T">Type of objects returned</typeparam>
143-
/// <param name="expression">
144-
/// An expression. Typically the name of a variable or a column that contains JSON text.<br/><br/>
145-
/// If <b>JSON_QUERY</b> finds JSON that is not valid in <i>expression</i> before it finds the value identified by <i>path</i>, the function returns an error. If <b>JSON_QUERY</b> doesn't find the value identified by <i>path</i>, it scans the entire text and returns an error if it finds JSON that is not valid anywhere in <i>expression</i>.
146-
/// </param>
147-
/// <param name="path">
148-
/// A JSON path that specifies the object or the array to extract.<br/><br/>
149-
/// In SQL Server 2017 and in Azure SQL Database, you can provide a variable as the value of <i>path</i>.<br/><br/>
150-
/// The JSON path can specify lax or strict mode for parsing.If you don't specify the parsing mode, lax mode is the default. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/json-path-expressions-sql-server">JSON Path Expressions (SQL Server)</see>.<br/><br/>
151-
/// The default value for path is '$'. As a result, if you don't provide a value for path, <b>JSON_QUERY</b> returns the input <i>expression</i>.<br/><br/>
152-
/// If the format of <i>path</i> isn't valid, <b>JSON_QUERY</b> returns an error.
153-
/// </param>
154-
/// <returns>
155-
/// Returns a JSON fragment of type T. The collation of the returned value is the same as the collation of the input expression.<br/><br/>
156-
/// If the value is not an object or an array:
157-
/// <ul>
158-
/// <li>In lax mode, <b>JSON_QUERY</b> returns null.</li>
159-
/// <li>In strict mode, <b>JSON_QUERY</b> returns an error.</li>
160-
/// </ul>
161-
/// </returns>
162-
public static T JsonQuery<T>(string expression, string path = null) => default(T);
163-
164-
/// <summary>
165-
/// Extracts an object or an array from a JSON string.<br/><br/>
166-
/// To extract a scalar value from a JSON string instead of an object or an array, see <see cref="https://docs.microsoft.com/en-us/sql/t-sql/functions/json-value-transact-sql">JSON_VALUE(Transact-SQL)</see>.
167-
/// For info about the differences between <b>JSON_VALUE</b> and <b>JSON_QUERY</b>, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/validate-query-and-change-json-data-with-built-in-functions-sql-server#JSONCompare">Compare JSON_VALUE and JSON_QUERY</see>.
168-
/// </summary>
169-
/// <typeparam name="T">Type of objects returned</typeparam>
170-
/// <param name="expression">
171-
/// An expression. Typically the name of a variable or a column that contains JSON text.<br/><br/>
172-
/// If <b>JSON_QUERY</b> finds JSON that is not valid in <i>expression</i> before it finds the value identified by <i>path</i>, the function returns an error. If <b>JSON_QUERY</b> doesn't find the value identified by <i>path</i>, it scans the entire text and returns an error if it finds JSON that is not valid anywhere in <i>expression</i>.
173-
/// </param>
174-
/// <param name="path">
175-
/// A JSON path that specifies the object or the array to extract.<br/><br/>
176-
/// In SQL Server 2017 and in Azure SQL Database, you can provide a variable as the value of <i>path</i>.<br/><br/>
177-
/// The JSON path can specify lax or strict mode for parsing.If you don't specify the parsing mode, lax mode is the default. For more info, see <see cref="https://docs.microsoft.com/en-us/sql/relational-databases/json/json-path-expressions-sql-server">JSON Path Expressions (SQL Server)</see>.<br/><br/>
178-
/// The default value for path is '$'. As a result, if you don't provide a value for path, <b>JSON_QUERY</b> returns the input <i>expression</i>.<br/><br/>
179-
/// If the format of <i>path</i> isn't valid, <b>JSON_QUERY</b> returns an error.
180-
/// </param>
181-
/// <returns>
182-
/// Returns a JSON fragment of type T. The collation of the returned value is the same as the collation of the input expression.<br/><br/>
183-
/// If the value is not an object or an array:
184-
/// <ul>
185-
/// <li>In lax mode, <b>JSON_QUERY</b> returns null.</li>
186-
/// <li>In strict mode, <b>JSON_QUERY</b> returns an error.</li>
187-
/// </ul>
188-
/// </returns>
189-
public static string JsonQuery(string expression, string path = null) =>
190-
(path.Contains("$"))
191-
? $"JSON_QUERY({expression}, '{path}')"
192-
: $"JSON_QUERY({expression})";
193-
}
194-
}

src/ServiceStack.OrmLite.SqlServerTests/Expressions/JsonExpressionsTest.cs

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Data;
3-
using System.Linq;
1+
using System.Collections.Generic;
42
using NUnit.Framework;
53

64
namespace ServiceStack.OrmLite.SqlServerTests.Expressions
@@ -10,40 +8,61 @@ public class JsonExpressionsTest : ExpressionsTestBase
108
[Test]
119
public void Can_select_json_scalar_value()
1210
{
13-
OrmLiteConfig.DialectProvider = SqlServer2016Dialect.Provider;
11+
using (var db = OpenDbConnection(dialectProvider: SqlServer2016Dialect.Provider))
12+
{
13+
db.DropAndCreateTable<TestType>();
1414

15-
OpenDbConnection().CreateTableIfNotExists<TestType>();
16-
OpenDbConnection().DeleteAll<TestType>();
15+
var obj = new
16+
{
17+
Address = new Address
18+
{
19+
Line1 = "1234 Main Street",
20+
Line2 = "Apt. 404",
21+
City = "Las Vegas",
22+
State = "NV"
23+
}
24+
};
1725

18-
var obj = new { Address = new Address { Line1 = "1234 Main Street", Line2 = "Apt. 404", City = "Las Vegas", State = "NV" } };
19-
var stringValue = obj.ToJson(); //"{ \"Address\": { \"Line1\": \"1234 Main Street\", \"Line2\": \"Apt. 404\", \"City\": \"Las Vegas\", \"State\": \"NV\" } }"
26+
//{ "Address": { "Line1": "1234 Main Street", "Line2": "Apt. 404", "City": "Las Vegas", "State": "NV" } }
27+
var stringValue = obj.ToJson();
2028

21-
OpenDbConnection().Insert(new TestType() { StringColumn = stringValue });
29+
db.Insert(new TestType { StringColumn = stringValue });
2230

23-
var actual = OpenDbConnection().Select<TestType>(q =>
24-
Sql2016.JsonValue(q.StringColumn, "$.address.state") == "NV");
31+
List<TestType> actual = db.Select<TestType>(q =>
32+
Sql.JsonValue(q.StringColumn, "$.address.state") == "NV");
2533

26-
Assert.AreEqual(obj.Address.State, actual);
34+
Assert.That(actual, Is.EqualTo(obj.Address.State));
35+
}
2736
}
2837

2938
[Test]
3039
public void Can_select_json_object_value()
3140
{
32-
OrmLiteConfig.DialectProvider = SqlServer2016Dialect.Provider;
41+
using (var db = OpenDbConnection(dialectProvider: SqlServer2016Dialect.Provider))
42+
{
43+
db.DropAndCreateTable<TestType>();
3344

34-
OpenDbConnection().CreateTableIfNotExists<TestType>();
35-
OpenDbConnection().DeleteAll<TestType>();
45+
var expected = new Address
46+
{
47+
Line1 = "1234 Main Street",
48+
Line2 = "Apt. 404",
49+
City = "Las Vegas",
50+
State = "NV"
51+
};
52+
var obj = new { Address = expected };
3653

37-
var expected = new Address { Line1 = "1234 Main Street", Line2 = "Apt. 404", City = "Las Vegas", State = "NV" };
38-
var obj = new { Address = expected };
39-
var stringValue = obj.ToJson(); //"{ \"Address\": { \"Line1\": \"1234 Main Street\", \"Line2\": \"Apt. 404\", \"City\": \"Las Vegas\", \"State\": \"NV\" } }"
54+
//{ "Address": { "Line1": "1234 Main Street", "Line2": "Apt. 404", "City": "Las Vegas", "State": "NV" } }
55+
var stringValue = obj.ToJson();
4056

41-
OpenDbConnection().Insert(new TestType() { StringColumn = stringValue });
57+
db.Insert(new TestType { StringColumn = stringValue });
4258

43-
var address = OpenDbConnection().From<TestType>().Select(q =>
44-
Sql2016.JsonQuery<Address>(q.StringColumn, "$.address")).ConvertTo<Address>();
59+
SqlExpression<TestType> q = db.From<TestType>().Select(x =>
60+
Sql.JsonQuery<Address>(x.StringColumn, "$.address"));
4561

46-
Assert.AreEqual(obj.Address, address);
62+
var address = q.ConvertTo<Address>();
63+
64+
Assert.That(address, Is.EqualTo(obj.Address));
65+
}
4766
}
4867

4968
internal class Address

0 commit comments

Comments
 (0)