|
2 | 2 | using Npgsql; |
3 | 3 | using NpgsqlTypes; |
4 | 4 | using NpgsqlRest.Extensions; |
| 5 | +using System.Text.RegularExpressions; |
5 | 6 |
|
6 | 7 | namespace NpgsqlRest; |
7 | 8 |
|
@@ -85,11 +86,67 @@ public class RoutineSource( |
85 | 86 | returnTypeDescriptor = returnRecordTypes.Select(x => new TypeDescriptor(x)).ToArray(); |
86 | 87 | } |
87 | 88 | var returnRecordNames = reader.Get<string[]>(9);//"return_record_names"); |
88 | | - var paramDefaults = reader.Get<string?[]>(15);//"param_defaults"); |
| 89 | + |
89 | 90 | bool isUnnamedRecord = reader.Get<bool>(11);// "is_unnamed_record"); |
90 | 91 | var routineType = type.GetEnum<RoutineType>(); |
91 | 92 | var callIdent = routineType == RoutineType.Procedure ? "call " : "select "; |
92 | 93 | var paramCount = reader.Get<int>(12);// "param_count"); |
| 94 | + |
| 95 | + var argumentDef = reader.Get<string>(15); |
| 96 | + string?[] paramDefaults = new string?[paramCount]; |
| 97 | + bool[] hasParamDefaults = new bool[paramCount]; |
| 98 | + |
| 99 | + if (string.IsNullOrEmpty(argumentDef)) |
| 100 | + { |
| 101 | + for (int i = 0; i < paramCount; i++) |
| 102 | + { |
| 103 | + paramDefaults[i] = null; |
| 104 | + hasParamDefaults[i] = false; |
| 105 | + } |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + const string defaultArgExp = " DEFAULT "; |
| 110 | + for (int i = 0; i < paramCount; i++) |
| 111 | + { |
| 112 | + string paramName = paramNames[i] ?? ""; |
| 113 | + string? nextParamName = i < paramCount - 1 ? paramNames[i + 1] : null; |
| 114 | + |
| 115 | + int startIndex = argumentDef.IndexOf(paramName); |
| 116 | + int endIndex = nextParamName != null ? argumentDef.IndexOf(string.Concat(", " + nextParamName, " ")) : argumentDef.Length; |
| 117 | + |
| 118 | + if (startIndex != -1 && endIndex != -1 && startIndex < endIndex) |
| 119 | + { |
| 120 | + string paramDef = argumentDef[startIndex..endIndex]; |
| 121 | + |
| 122 | + int defaultIndex = paramDef.IndexOf(defaultArgExp); |
| 123 | + if (defaultIndex != -1) |
| 124 | + { |
| 125 | + string defaultValue = paramDef[(defaultIndex + 9)..].Trim(); |
| 126 | + |
| 127 | + if (defaultValue.EndsWith(',')) |
| 128 | + { |
| 129 | + defaultValue = defaultValue[..^1].Trim(); |
| 130 | + } |
| 131 | + |
| 132 | + paramDefaults[i] = defaultValue; |
| 133 | + hasParamDefaults[i] = true; |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + paramDefaults[i] = null; |
| 138 | + hasParamDefaults[i] = false; |
| 139 | + } |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + paramDefaults[i] = null; |
| 144 | + hasParamDefaults[i] = false; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
93 | 150 | var returnRecordCount = reader.Get<int>(8);// "return_record_count"); |
94 | 151 | var variadic = reader.Get<bool>(16);// "has_variadic"); |
95 | 152 | var expression = string.Concat( |
@@ -166,7 +223,7 @@ public class RoutineSource( |
166 | 223 | paramCount: paramCount, |
167 | 224 | paramNames: paramNames, |
168 | 225 | paramTypeDescriptor: paramTypes |
169 | | - .Select((x, i) => new TypeDescriptor(x, hasDefault: paramDefaults[i] is not null)) |
| 226 | + .Select((x, i) => new TypeDescriptor(x, hasDefault: hasParamDefaults[i] is true)) |
170 | 227 | .ToArray(), |
171 | 228 |
|
172 | 229 | expression: expression, |
|
0 commit comments