pq connection string parsing from other project
This commit is contained in:
parent
9a5feb9d54
commit
d803cd8003
10 changed files with 529 additions and 0 deletions
7
pgLabII.PgUtils/ConnectionStrings/PgConfigMapping.cs
Normal file
7
pgLabII.PgUtils/ConnectionStrings/PgConfigMapping.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace pgLabII.PgUtils.ConnectionStrings;
|
||||
|
||||
public readonly record struct PgConfigMapping(
|
||||
string pqKeyword,
|
||||
string? pqDumpParam,
|
||||
string? pqEnvironment
|
||||
);
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
namespace pgLabII.PgUtils.ConnectionStrings;
|
||||
|
||||
public interface IPqConnectionStringTokenizer
|
||||
{
|
||||
bool Eof { get; }
|
||||
//PqToken NextToken(out string? text);
|
||||
string GetKeyword();
|
||||
void ConsumeEquals();
|
||||
string GetValue();
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace pgLabII.PgUtils.ConnectionStrings;
|
||||
|
||||
public class PqConnectionStringParser
|
||||
{
|
||||
// Note possible keywords
|
||||
// host
|
||||
//hostaddr
|
||||
//port
|
||||
//dbname
|
||||
//user
|
||||
//password
|
||||
//passfile
|
||||
//channel_binding
|
||||
//connect_timeout
|
||||
//client_encoding
|
||||
//options
|
||||
//application_name
|
||||
//fallback_application_name
|
||||
//keepalives
|
||||
//keepalives_idle
|
||||
//keepalives_interval
|
||||
//keepalives_count
|
||||
//tcp_user_timeout
|
||||
//replication
|
||||
//gssencmode
|
||||
//sslmode
|
||||
//requiresll
|
||||
//sslcompression
|
||||
//sslcert
|
||||
//sslkey
|
||||
//sslpassword
|
||||
//sslrootcert
|
||||
//sslcrl
|
||||
//sslcrldir
|
||||
//sslsni
|
||||
//requirepeer
|
||||
//ssl_min_protocol_version
|
||||
//ssl_max_protocol_version
|
||||
//krbsrvname
|
||||
//gsslib
|
||||
//service
|
||||
//target_session_attrs
|
||||
|
||||
public static IDictionary<string, string> Parse(string input)
|
||||
{
|
||||
return new PqConnectionStringParser(
|
||||
new PqConnectionStringTokenizer(input)
|
||||
).Parse();
|
||||
}
|
||||
|
||||
private readonly IPqConnectionStringTokenizer tokenizer;
|
||||
private readonly Dictionary<string, string> result = new();
|
||||
|
||||
public PqConnectionStringParser(IPqConnectionStringTokenizer tokenizer)
|
||||
{
|
||||
this.tokenizer = tokenizer;
|
||||
}
|
||||
|
||||
public IDictionary<string, string> Parse()
|
||||
{
|
||||
result.Clear();
|
||||
|
||||
while (!tokenizer.Eof)
|
||||
ParsePair();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ParsePair()
|
||||
{
|
||||
string kw = tokenizer.GetKeyword();
|
||||
tokenizer.ConsumeEquals();
|
||||
string v = tokenizer.GetValue();
|
||||
result.Add(kw, v);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
namespace pgLabII.PgUtils.ConnectionStrings;
|
||||
|
||||
public class PqConnectionStringParserException : Exception
|
||||
{
|
||||
public PqConnectionStringParserException()
|
||||
{
|
||||
}
|
||||
|
||||
public PqConnectionStringParserException(string? message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public PqConnectionStringParserException(string? message, Exception? innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
using System.Text;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace pgLabII.PgUtils.ConnectionStrings;
|
||||
|
||||
public class PqConnectionStringTokenizer : IPqConnectionStringTokenizer
|
||||
{
|
||||
private readonly string input;
|
||||
private int position = 0;
|
||||
|
||||
public bool Eof
|
||||
{
|
||||
get
|
||||
{
|
||||
ConsumeWhitespace();
|
||||
return position >= input.Length;
|
||||
}
|
||||
}
|
||||
|
||||
public PqConnectionStringTokenizer(string input)
|
||||
{
|
||||
this.input = input;
|
||||
position = 0;
|
||||
}
|
||||
|
||||
public string GetKeyword()
|
||||
{
|
||||
if (Eof)
|
||||
throw new PqConnectionStringParserException($"Unexpected end of file was expecting a keyword at position {position}");
|
||||
|
||||
return GetString(forKeyword: true);
|
||||
}
|
||||
|
||||
public void ConsumeEquals()
|
||||
{
|
||||
ConsumeWhitespace();
|
||||
if (position < input.Length && input[position] == '=')
|
||||
{
|
||||
position++;
|
||||
}
|
||||
else
|
||||
throw new PqConnectionStringParserException($"Was expecting '=' after keyword at position {position}");
|
||||
}
|
||||
|
||||
public string GetValue()
|
||||
{
|
||||
if (Eof)
|
||||
throw new PqConnectionStringParserException($"Unexpected end of file was expecting a keyword at position {position}");
|
||||
|
||||
return GetString(forKeyword: false);
|
||||
}
|
||||
|
||||
private string GetString(bool forKeyword)
|
||||
{
|
||||
if (forKeyword && input[position] == '=')
|
||||
throw new PqConnectionStringParserException($"Unexpected '=' was expecting keyword at position {position}");
|
||||
|
||||
if (input[position] == '\'')
|
||||
return ParseQuotedText();
|
||||
|
||||
return UnquotedString(forKeyword);
|
||||
}
|
||||
|
||||
private void ConsumeWhitespace()
|
||||
{
|
||||
while (position < input.Length && char.IsWhiteSpace(input[position]))
|
||||
position++;
|
||||
}
|
||||
|
||||
private string UnquotedString(bool forKeyword)
|
||||
{
|
||||
int start = position;
|
||||
while (++position < input.Length && !char.IsWhiteSpace(input[position]) && (!forKeyword || input[position] != '='))
|
||||
{ }
|
||||
return input.Substring(start, position - start);
|
||||
}
|
||||
|
||||
private string ParseQuotedText()
|
||||
{
|
||||
bool escape = false;
|
||||
StringBuilder sb = new();
|
||||
int start = position;
|
||||
while (++position < input.Length)
|
||||
{
|
||||
char c = input[position];
|
||||
if (escape)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '\'':
|
||||
case '\\':
|
||||
sb.Append(c);
|
||||
escape = false;
|
||||
break;
|
||||
default:
|
||||
throw new PqConnectionStringParserException($"Invalid escape sequence at position {position}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == '\'')
|
||||
{
|
||||
++position;
|
||||
return sb.ToString();
|
||||
}
|
||||
else if (c == '\\')
|
||||
{
|
||||
escape = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new PqConnectionStringParserException($"Missing end quote on value starting at {start}");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue