Skip to content

Commit 5832fac

Browse files
committed
Add MS SQL Server Table hint
1 parent 7e0c943 commit 5832fac

17 files changed

Lines changed: 4398 additions & 4067 deletions

MiniSqlParser/Enums/MsSqlHint.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace MiniSqlParser
2+
{
3+
public enum MsSqlHint
4+
{
5+
None,
6+
NoLock,
7+
ReadCommitted,
8+
RepeatableRead,
9+
Serializable
10+
}
11+
}

MiniSqlParser/FromSources/Table.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ public FromSourceType Type {
131131
public Identifier IndexName { get; private set; }
132132
public bool HasNotIndexed { get; private set; }
133133

134+
// MS SQL Serverでのテーブルヒント
135+
public MsSqlHint MsSqlHint { get; private set; }
136+
134137
public Table Clone() {
135138
var ret = new Table(_serverName
136139
, _dataBaseName
@@ -144,6 +147,7 @@ public Table Clone() {
144147
, this.IndexSchemaName
145148
, this.IndexName
146149
, this.HasNotIndexed
150+
, this.MsSqlHint
147151
, this.Comments.Clone());
148152
ret.Attachment = this.Attachment;
149153
return ret;
@@ -165,7 +169,9 @@ internal Table(Identifier serverName
165169
, null
166170
, null
167171
, null
168-
, false,comments){
172+
, false
173+
, MsSqlHint.None
174+
, comments){
169175
}
170176

171177
internal Table(Identifier serverName
@@ -188,6 +194,7 @@ internal Table(Identifier serverName
188194
, null
189195
, null
190196
, false
197+
, MsSqlHint.None
191198
, comments) {
192199
}
193200

@@ -203,6 +210,7 @@ internal Table(Identifier serverName
203210
, Identifier indexSchemaName
204211
, Identifier indexName
205212
, bool hasNotIndexed
213+
, MsSqlHint msSqlHint
206214
, Comments comments) {
207215
_serverName = serverName;
208216
_dataBaseName = databaseName;
@@ -216,6 +224,7 @@ internal Table(Identifier serverName
216224
this.IndexSchemaName = indexSchemaName;
217225
this.IndexName = indexName;
218226
this.HasNotIndexed = hasNotIndexed;
227+
this.MsSqlHint = msSqlHint;
219228
this.Comments = comments;
220229
}
221230

@@ -230,7 +239,8 @@ public Table(Identifier name)
230239
, null
231240
, null
232241
, null
233-
, false) {
242+
, false
243+
, MsSqlHint.None) {
234244
}
235245

236246
public Table(Identifier name
@@ -246,7 +256,8 @@ public Table(Identifier name
246256
, null
247257
, null
248258
, null
249-
, false) {
259+
, false
260+
, MsSqlHint.None) {
250261
}
251262

252263
public Table(Identifier schemaName
@@ -261,7 +272,8 @@ public Table(Identifier schemaName
261272
, null
262273
, null
263274
, null
264-
, false) {
275+
, false
276+
, MsSqlHint.None) {
265277
}
266278

267279
public Table(Identifier serverName
@@ -278,7 +290,8 @@ public Table(Identifier serverName
278290
, null
279291
, null
280292
, null
281-
, false) {
293+
, false
294+
, MsSqlHint.None) {
282295
}
283296

284297
public Table(Identifier serverName
@@ -297,7 +310,8 @@ public Table(Identifier serverName
297310
, null
298311
, null
299312
, null
300-
, false) {
313+
, false
314+
, MsSqlHint.None) {
301315
}
302316

303317
public Table(Identifier serverName
@@ -310,7 +324,8 @@ public Table(Identifier serverName
310324
, Identifier indexDatabaseName
311325
, Identifier indexSchemaName
312326
, Identifier indexName
313-
, bool hasNotIndexed) {
327+
, bool hasNotIndexed
328+
, MsSqlHint msSqlHint) {
314329
_serverName = serverName;
315330
_dataBaseName = databaseName;
316331
_schemaName = schemaName;
@@ -322,8 +337,10 @@ public Table(Identifier serverName
322337
this.IndexSchemaName = indexSchemaName;
323338
this.IndexName = indexName;
324339
this.HasNotIndexed = hasNotIndexed;
340+
this.MsSqlHint = msSqlHint;
325341

326342
// コメントスロット数を計算する
343+
var h = CountTrue(msSqlHint != MsSqlHint.None) * 4;
327344
var n = CountTrue(!string.IsNullOrEmpty(indexName)) * 3;
328345
var m = CountTrue(!string.IsNullOrEmpty(serverName)
329346
, !string.IsNullOrEmpty(databaseName)
@@ -335,7 +352,7 @@ public Table(Identifier serverName
335352
var l = CountTrue(HasAs
336353
, !string.IsNullOrEmpty(aliasName));
337354

338-
this.Comments = new Comments(n + m + l + 1);
355+
this.Comments = new Comments(h + n + m + l + 1);
339356
}
340357

341358
protected override void AcceptImp(IVisitor visitor) {

MiniSqlParser/MiniSqlParser.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="Assignment.cs" />
5151
<Compile Include="Assignments.cs" />
5252
<Compile Include="BestCaseDictionary.cs" />
53+
<Compile Include="Enums\MsSqlHint.cs" />
5354
<Compile Include="Parser\MiniSqlParserBaseListener.cs" />
5455
<Compile Include="Parser\MiniSqlParserLexer.cs" />
5556
<Compile Include="Parser\MiniSqlParserListener.cs" />

MiniSqlParser/Parser/MakeASTListener.cs

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,85 +1124,104 @@ public override void ExitAliased_table_name(MiniSqlParserParser.Aliased_table_na
11241124
, comments));
11251125
}
11261126

1127-
public override void ExitIndexed_table_name(MiniSqlParserParser.Indexed_table_nameContext context) {
1127+
public override void ExitHinted_table_name(MiniSqlParserParser.Hinted_table_nameContext context) {
1128+
if(context.table_hint() == null) {
1129+
// table_hintが存在しない場合はなにもしない
1130+
return;
1131+
}
1132+
11281133
var tableNode = (Table)_stack.Pop();
11291134
var comments = tableNode.Comments;
11301135
comments.AddRange(this.GetComments(context));
1131-
1132-
Identifier indexServerName = null;
1133-
Identifier indexDatabaseName = null;
1134-
Identifier indexSchemaName = null;
1135-
Identifier indexName = null;
1136-
bool hasNotIndexed = false;
1137-
if(context.K_NOT() != null) {
1138-
hasNotIndexed = true;
1139-
} else if(context.K_INDEXED() != null) {
1140-
comments.AddRange(this.GetComments(context.index_name().qualified_schema_name()));
1141-
comments.AddRange(this.GetComments(context.index_name()));
1142-
if(context.index_name().qualified_schema_name() != null) {
1143-
indexServerName = this.GetIdentifier(context.index_name().qualified_schema_name().s);
1144-
indexDatabaseName = this.GetIdentifier(context.index_name().qualified_schema_name().d);
1145-
indexSchemaName = this.GetIdentifier(context.index_name().qualified_schema_name().n);
1146-
}
1147-
indexName = this.GetIdentifier(context.index_name().identifier());
1148-
}
1136+
comments.AddRange(this.GetComments(context.table_hint()));
11491137

11501138
// コメントでテーブル別名の指定があれば取得する
11511139
var implicitAliasName = this.GetTableAliasNameFromDocComment(context);
11521140

1153-
_stack.Push(new Table(tableNode.ServerName
1154-
, tableNode.DataBaseName
1155-
, tableNode.SchemaName
1156-
, tableNode.Name
1157-
, false
1158-
, null
1159-
, implicitAliasName
1160-
, indexServerName
1161-
, indexDatabaseName
1162-
, indexSchemaName
1163-
, indexName
1164-
, hasNotIndexed
1165-
, comments));
1141+
var hinted_table = this.CreateHintedTable(context.table_hint()
1142+
, tableNode
1143+
, implicitAliasName
1144+
, comments);
1145+
_stack.Push(hinted_table);
11661146
}
11671147

1168-
public override void ExitIndexed_aliased_table_name(MiniSqlParserParser.Indexed_aliased_table_nameContext context) {
1148+
public override void ExitHinted_aliased_table_name(MiniSqlParserParser.Hinted_aliased_table_nameContext context) {
1149+
if(context.table_hint() == null) {
1150+
// table_hintが存在しない場合はなにもしない
1151+
return;
1152+
}
1153+
11691154
var tableNode = (Table)_stack.Pop();
11701155
var comments = tableNode.Comments;
11711156
comments.AddRange(this.GetComments(context));
1157+
comments.AddRange(this.GetComments(context.table_hint()));
1158+
1159+
var hinted_table = this.CreateHintedTable(context.table_hint()
1160+
, tableNode
1161+
, tableNode.ImplicitAliasName
1162+
, comments);
1163+
_stack.Push(hinted_table);
1164+
}
11721165

1166+
private Table CreateHintedTable(MiniSqlParserParser.Table_hintContext table_hintContext
1167+
, Table tableNode
1168+
, string implicitAliasName
1169+
, Comments comments) {
11731170
Identifier indexServerName = null;
11741171
Identifier indexDatabaseName = null;
11751172
Identifier indexSchemaName = null;
11761173
Identifier indexName = null;
11771174
bool hasNotIndexed = false;
1178-
if(context.K_NOT() != null) {
1175+
1176+
if(table_hintContext.K_NOT() != null) {
11791177
hasNotIndexed = true;
1180-
} else if(context.K_INDEXED() != null) {
1181-
comments.AddRange(this.GetComments(context.index_name().qualified_schema_name()));
1182-
comments.AddRange(this.GetComments(context.index_name()));
1183-
if(context.index_name().qualified_schema_name() != null) {
1184-
indexServerName = this.GetIdentifier(context.index_name().qualified_schema_name().s);
1185-
indexDatabaseName = this.GetIdentifier(context.index_name().qualified_schema_name().d);
1186-
indexSchemaName = this.GetIdentifier(context.index_name().qualified_schema_name().n);
1178+
} else if(table_hintContext.K_INDEXED() != null) {
1179+
comments.AddRange(this.GetComments(table_hintContext.index_name().qualified_schema_name()));
1180+
comments.AddRange(this.GetComments(table_hintContext.index_name()));
1181+
if(table_hintContext.index_name().qualified_schema_name() != null) {
1182+
indexServerName = this.GetIdentifier(table_hintContext.index_name().qualified_schema_name().s);
1183+
indexDatabaseName = this.GetIdentifier(table_hintContext.index_name().qualified_schema_name().d);
1184+
indexSchemaName = this.GetIdentifier(table_hintContext.index_name().qualified_schema_name().n);
11871185
}
1188-
indexName = this.GetIdentifier(context.index_name().identifier());
1189-
}
1186+
indexName = this.GetIdentifier(table_hintContext.index_name().identifier());
1187+
}
1188+
var msSqlHint = this.ConvToMsSqlHint(table_hintContext.h);
1189+
1190+
return new Table(tableNode.ServerName
1191+
, tableNode.DataBaseName
1192+
, tableNode.SchemaName
1193+
, tableNode.Name
1194+
, tableNode.HasAs
1195+
, tableNode.AliasName
1196+
, implicitAliasName
1197+
, indexServerName
1198+
, indexDatabaseName
1199+
, indexSchemaName
1200+
, indexName
1201+
, hasNotIndexed
1202+
, msSqlHint
1203+
, comments);
1204+
}
11901205

1191-
_stack.Push(new Table(tableNode.ServerName
1192-
, tableNode.DataBaseName
1193-
, tableNode.SchemaName
1194-
, tableNode.Name
1195-
, tableNode.HasAs
1196-
, tableNode.AliasName
1197-
, tableNode.ImplicitAliasName
1198-
, indexServerName
1199-
, indexDatabaseName
1200-
, indexSchemaName
1201-
, indexName
1202-
, hasNotIndexed
1203-
, comments));
1206+
private MsSqlHint ConvToMsSqlHint(IToken msSqlHint) {
1207+
if(msSqlHint == null) {
1208+
return MsSqlHint.None;
1209+
}
1210+
var hintType = msSqlHint.Type;
1211+
MsSqlHint hint = MsSqlHint.None;
1212+
if(hintType == MiniSqlParserLexer.K_NOLOCK) {
1213+
hint = MsSqlHint.NoLock;
1214+
} else if(hintType == MiniSqlParserLexer.K_READCOMMITTED) {
1215+
hint = MsSqlHint.ReadCommitted;
1216+
} else if(hintType == MiniSqlParserLexer.K_REPEATABLEREAD) {
1217+
hint = MsSqlHint.RepeatableRead;
1218+
} else if(hintType == MiniSqlParserLexer.K_SERIALIZABLE) {
1219+
hint = MsSqlHint.Serializable;
1220+
} else {
1221+
throw new CannotBuildASTException("Undifined Ms SQL Hint is used");
1222+
}
1223+
return hint;
12041224
}
1205-
12061225
}
12071226

12081227
}

MiniSqlParser/Parser/MakeASTListener_Util.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private string GetTableAliasNameFromDocComment(MiniSqlParserParser.Aliased_table
236236
return this.GetTableAliasNameFromDocComment(context.table_name());
237237
}
238238

239-
private string GetTableAliasNameFromDocComment(MiniSqlParserParser.Indexed_table_nameContext context) {
239+
private string GetTableAliasNameFromDocComment(MiniSqlParserParser.Hinted_table_nameContext context) {
240240
return this.GetTableAliasNameFromDocComment(context.table_name());
241241
}
242242

0 commit comments

Comments
 (0)