@an5/adapters 0.2.5 → 0.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +283 -305
  2. package/dist/an5Adapter.d.ts +142 -7
  3. package/dist/an5Adapter.js +998 -68
  4. package/dist/base/metadata.d.ts +10 -0
  5. package/dist/base/metadata.js +107 -0
  6. package/dist/base/sql.d.ts +19 -1
  7. package/dist/base/sql.js +152 -24
  8. package/dist/base/types.d.ts +10 -1
  9. package/dist/browser.d.ts +4 -1
  10. package/dist/browser.js +9 -2
  11. package/dist/googlesheets/adapter.d.ts +1 -0
  12. package/dist/googlesheets/adapter.js +9 -2
  13. package/dist/googlesheets/config.d.ts +4 -4
  14. package/dist/googlesheets/helpers.js +2 -0
  15. package/dist/googlesheets/parseConnectionString.js +5 -2
  16. package/dist/googlesheets/sqlExecutor.js +29 -24
  17. package/dist/googlesheets/tableClient.js +14 -5
  18. package/dist/index.d.ts +5 -1
  19. package/dist/index.js +15 -1
  20. package/dist/mssql/engine.js +8 -4
  21. package/dist/mysql/engine.js +1 -1
  22. package/dist/nbase/client.d.ts +92 -0
  23. package/dist/nbase/client.js +147 -0
  24. package/dist/nbase/index.d.ts +47 -0
  25. package/dist/nbase/index.js +80 -0
  26. package/dist/postgres/engine.js +1 -1
  27. package/dist/sqlite/browserEngine.d.ts +27 -0
  28. package/dist/sqlite/browserEngine.js +161 -0
  29. package/dist/sqlite/engine.js +15 -3
  30. package/dist/sqlite/index.d.ts +1 -0
  31. package/dist/sqlite/index.js +1 -0
  32. package/dist/typed.d.ts +5 -0
  33. package/dist/typed.js +2 -0
  34. package/dotnet/An5Adapter.cs +1112 -275
  35. package/dotnet/Base/Core.cs +380 -82
  36. package/dotnet/Mssql/MssqlEngine.cs +145 -144
  37. package/dotnet/Postgres/PostgresEngine.cs +149 -121
  38. package/dotnet/Sqlite/SqliteEngine.cs +214 -0
  39. package/golang/an5_adapter.go +314 -0
  40. package/golang/base/base.go +100 -0
  41. package/golang/base/metadata.go +116 -0
  42. package/golang/base/where.go +311 -0
  43. package/golang/go.mod +3 -0
  44. package/golang/mssql/mssql.go +32 -0
  45. package/golang/postgres/postgres.go +37 -0
  46. package/golang/sqlite/sqlite.go +14 -0
  47. package/golang/sqlite_test.go +42 -0
  48. package/golang/table_client.go +1329 -0
  49. package/package.json +26 -9
  50. package/python/an5_adapter.py +184 -97
  51. package/python/base/__init__.py +3 -3
  52. package/python/base/dialects.py +11 -8
  53. package/python/base/metadata.py +59 -10
  54. package/python/base/sql.py +202 -121
  55. package/python/mssql/__init__.py +1 -1
  56. package/python/mssql/provider.py +62 -62
  57. package/python/postgres/__init__.py +1 -1
  58. package/python/postgres/provider.py +15 -15
  59. package/python/sqlite/__init__.py +3 -0
  60. package/python/sqlite/provider.py +83 -0
  61. package/python/table_client.py +636 -216
  62. package/rust/Cargo.toml +31 -0
  63. package/rust/README.md +71 -0
  64. package/rust/src/adapter.rs +1312 -0
  65. package/rust/src/base/dialect.rs +172 -0
  66. package/rust/src/base/metadata.rs +134 -0
  67. package/rust/src/base/mod.rs +17 -0
  68. package/rust/src/base/where_builder.rs +405 -0
  69. package/rust/src/lib.rs +75 -0
  70. package/typescript/src/an5Adapter.ts +1515 -505
  71. package/typescript/src/base/index.ts +3 -3
  72. package/typescript/src/base/metadata.ts +148 -28
  73. package/typescript/src/base/sql.ts +264 -98
  74. package/typescript/src/base/types.ts +33 -24
  75. package/typescript/src/browser.ts +10 -6
  76. package/typescript/src/config.ts +95 -95
  77. package/typescript/src/googlesheets/adapter.ts +269 -259
  78. package/typescript/src/googlesheets/an5SheetsAdapter.ts +3 -3
  79. package/typescript/src/googlesheets/config.ts +47 -47
  80. package/typescript/src/googlesheets/helpers.ts +144 -143
  81. package/typescript/src/googlesheets/index.ts +3 -3
  82. package/typescript/src/googlesheets/parseConnectionString.ts +48 -46
  83. package/typescript/src/googlesheets/retry.ts +126 -126
  84. package/typescript/src/googlesheets/sqlExecutor.ts +304 -301
  85. package/typescript/src/googlesheets/tableClient.ts +461 -455
  86. package/typescript/src/googlesheets/types.ts +6 -6
  87. package/typescript/src/index.ts +48 -27
  88. package/typescript/src/mssql/engine.ts +134 -132
  89. package/typescript/src/mssql/index.ts +1 -1
  90. package/typescript/src/mysql/engine.ts +80 -80
  91. package/typescript/src/mysql/index.ts +1 -1
  92. package/typescript/src/nbase/client.ts +214 -0
  93. package/typescript/src/nbase/index.ts +104 -0
  94. package/typescript/src/postgres/engine.ts +87 -87
  95. package/typescript/src/postgres/index.ts +1 -1
  96. package/typescript/src/sqlite/browserEngine.ts +192 -0
  97. package/typescript/src/sqlite/engine.ts +66 -55
  98. package/typescript/src/sqlite/index.ts +3 -1
  99. package/typescript/src/typed.ts +52 -0
@@ -1,144 +1,145 @@
1
- using System;
2
- using System.Collections.Generic;
3
- using System.Data;
4
- using System.Reflection;
5
-
6
- namespace An5Orm
7
- {
8
- // ─── MSSQL Engine ──────────────────────────────────────────────────────────
9
-
10
- internal class MssqlEngine : IQueryEngine
11
- {
12
- public Dialect Dialect => Dialect.Mssql;
13
- private readonly string _connectionString;
14
- private readonly int _commandTimeout;
15
-
16
- [ThreadStatic] private static System.Data.SqlClient.SqlConnection _txConn;
17
- [ThreadStatic] private static System.Data.SqlClient.SqlTransaction _tx;
18
-
19
- public MssqlEngine(string connectionString, int commandTimeout)
20
- {
21
- _connectionString = connectionString;
22
- _commandTimeout = commandTimeout;
23
- }
24
-
25
- private System.Data.SqlClient.SqlConnection OpenConnection(out bool isInTransaction)
26
- {
27
- if (_txConn != null) { isInTransaction = true; return _txConn; }
28
- isInTransaction = false;
29
- var conn = new System.Data.SqlClient.SqlConnection(_connectionString);
30
- conn.Open();
31
- return conn;
32
- }
33
-
34
- private System.Data.SqlClient.SqlCommand BuildCommand(
35
- System.Data.SqlClient.SqlConnection conn, string sql, Dictionary<string, object> parameters)
36
- {
37
- var cmd = new System.Data.SqlClient.SqlCommand(sql, conn) { CommandTimeout = _commandTimeout };
38
- if (_tx != null) cmd.Transaction = _tx;
39
- if (parameters != null)
40
- {
41
- foreach (var kv in parameters)
42
- {
43
- var paramName = kv.Key.StartsWith("@") ? kv.Key : "@" + kv.Key;
44
- cmd.Parameters.AddWithValue(paramName, kv.Value ?? DBNull.Value);
45
- }
46
- }
47
- return cmd;
48
- }
49
-
50
- public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters)
51
- {
52
- var results = new List<Dictionary<string, object>>();
53
- var conn = OpenConnection(out bool isTx);
54
- try
55
- {
56
- using var cmd = BuildCommand(conn, sql, parameters);
57
- using var reader = cmd.ExecuteReader();
58
- while (reader.Read())
59
- {
60
- var row = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
61
- for (int i = 0; i < reader.FieldCount; i++)
62
- {
63
- row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
64
- }
65
- results.Add(row);
66
- }
67
- }
68
- finally { if (!isTx) conn.Dispose(); }
69
- return results;
70
- }
71
-
72
- public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new()
73
- {
74
- var results = new List<T>();
75
- var conn = OpenConnection(out bool isTx);
76
- try
77
- {
78
- using var cmd = BuildCommand(conn, sql, parameters);
79
- using var reader = cmd.ExecuteReader();
80
- var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
81
- while (reader.Read())
82
- {
83
- var item = new T();
84
- foreach (var prop in props)
85
- {
86
- if (!HasColumn(reader, prop.Name)) continue;
87
- var val = reader[prop.Name];
88
- if (val == DBNull.Value) continue;
89
- try { prop.SetValue(item, Convert.ChangeType(val, prop.PropertyType)); } catch { }
90
- }
91
- results.Add(item);
92
- }
93
- }
94
- finally { if (!isTx) conn.Dispose(); }
95
- return results;
96
- }
97
-
98
- public int ExecuteRaw(string sql, Dictionary<string, object> parameters)
99
- {
100
- var conn = OpenConnection(out bool isTx);
101
- try
102
- {
103
- using var cmd = BuildCommand(conn, sql, parameters);
104
- return cmd.ExecuteNonQuery();
105
- }
106
- finally { if (!isTx) conn.Dispose(); }
107
- }
108
-
109
- private static bool HasColumn(System.Data.SqlClient.SqlDataReader reader, string name)
110
- {
111
- for (int i = 0; i < reader.FieldCount; i++)
112
- if (reader.GetName(i).Equals(name, StringComparison.OrdinalIgnoreCase)) return true;
113
- return false;
114
- }
115
-
116
- public An5TransactionBase BeginTransaction()
117
- {
118
- var conn = new System.Data.SqlClient.SqlConnection(_connectionString);
119
- conn.Open();
120
- var tx = conn.BeginTransaction();
121
- _txConn = conn;
122
- _tx = tx;
123
- return new MssqlTransaction(conn, tx, () => { _txConn = null; _tx = null; });
124
- }
125
- }
126
-
127
- internal class MssqlTransaction : An5TransactionBase
128
- {
129
- private readonly System.Data.SqlClient.SqlConnection _conn;
130
- private readonly System.Data.SqlClient.SqlTransaction _tx;
131
- private readonly Action _cleanup;
132
-
133
- public MssqlTransaction(System.Data.SqlClient.SqlConnection conn, System.Data.SqlClient.SqlTransaction tx, Action cleanup)
134
- {
135
- _conn = conn;
136
- _tx = tx;
137
- _cleanup = cleanup;
138
- }
139
-
140
- public override void Commit() { _tx.Commit(); _cleanup(); }
141
- public override void Rollback() { _tx.Rollback(); _cleanup(); }
142
- public override void Dispose() { _tx.Dispose(); _conn.Dispose(); _cleanup(); }
143
- }
144
- }
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Data;
4
+ using System.Reflection;
5
+ using Microsoft.Data.SqlClient;
6
+
7
+ namespace An5Orm
8
+ {
9
+ // ─── MSSQL Engine ──────────────────────────────────────────────────────────
10
+
11
+ internal class MssqlEngine : IQueryEngine
12
+ {
13
+ public Dialect Dialect => Dialect.Mssql;
14
+ private readonly string _connectionString;
15
+ private readonly int _commandTimeout;
16
+
17
+ [ThreadStatic] private static SqlConnection _txConn;
18
+ [ThreadStatic] private static SqlTransaction _tx;
19
+
20
+ public MssqlEngine(string connectionString, int commandTimeout)
21
+ {
22
+ _connectionString = connectionString;
23
+ _commandTimeout = commandTimeout;
24
+ }
25
+
26
+ private SqlConnection OpenConnection(out bool isInTransaction)
27
+ {
28
+ if (_txConn != null) { isInTransaction = true; return _txConn; }
29
+ isInTransaction = false;
30
+ var conn = new SqlConnection(_connectionString);
31
+ conn.Open();
32
+ return conn;
33
+ }
34
+
35
+ private SqlCommand BuildCommand(
36
+ SqlConnection conn, string sql, Dictionary<string, object> parameters)
37
+ {
38
+ var cmd = new SqlCommand(sql, conn) { CommandTimeout = _commandTimeout };
39
+ if (_tx != null) cmd.Transaction = _tx;
40
+ if (parameters != null)
41
+ {
42
+ foreach (var kv in parameters)
43
+ {
44
+ var paramName = kv.Key.StartsWith("@") ? kv.Key : "@" + kv.Key;
45
+ cmd.Parameters.AddWithValue(paramName, kv.Value ?? DBNull.Value);
46
+ }
47
+ }
48
+ return cmd;
49
+ }
50
+
51
+ public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters)
52
+ {
53
+ var results = new List<Dictionary<string, object>>();
54
+ var conn = OpenConnection(out bool isTx);
55
+ try
56
+ {
57
+ using var cmd = BuildCommand(conn, sql, parameters);
58
+ using var reader = cmd.ExecuteReader();
59
+ while (reader.Read())
60
+ {
61
+ var row = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
62
+ for (int i = 0; i < reader.FieldCount; i++)
63
+ {
64
+ row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
65
+ }
66
+ results.Add(row);
67
+ }
68
+ }
69
+ finally { if (!isTx) conn.Dispose(); }
70
+ return results;
71
+ }
72
+
73
+ public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new()
74
+ {
75
+ var results = new List<T>();
76
+ var conn = OpenConnection(out bool isTx);
77
+ try
78
+ {
79
+ using var cmd = BuildCommand(conn, sql, parameters);
80
+ using var reader = cmd.ExecuteReader();
81
+ var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
82
+ while (reader.Read())
83
+ {
84
+ var item = new T();
85
+ foreach (var prop in props)
86
+ {
87
+ if (!HasColumn(reader, prop.Name)) continue;
88
+ var val = reader[prop.Name];
89
+ if (val == DBNull.Value) continue;
90
+ try { prop.SetValue(item, Convert.ChangeType(val, prop.PropertyType)); } catch { }
91
+ }
92
+ results.Add(item);
93
+ }
94
+ }
95
+ finally { if (!isTx) conn.Dispose(); }
96
+ return results;
97
+ }
98
+
99
+ public int ExecuteRaw(string sql, Dictionary<string, object> parameters)
100
+ {
101
+ var conn = OpenConnection(out bool isTx);
102
+ try
103
+ {
104
+ using var cmd = BuildCommand(conn, sql, parameters);
105
+ return cmd.ExecuteNonQuery();
106
+ }
107
+ finally { if (!isTx) conn.Dispose(); }
108
+ }
109
+
110
+ private static bool HasColumn(SqlDataReader reader, string name)
111
+ {
112
+ for (int i = 0; i < reader.FieldCount; i++)
113
+ if (reader.GetName(i).Equals(name, StringComparison.OrdinalIgnoreCase)) return true;
114
+ return false;
115
+ }
116
+
117
+ public An5TransactionBase BeginTransaction()
118
+ {
119
+ var conn = new SqlConnection(_connectionString);
120
+ conn.Open();
121
+ var tx = conn.BeginTransaction();
122
+ _txConn = conn;
123
+ _tx = tx;
124
+ return new MssqlTransaction(conn, tx, () => { _txConn = null; _tx = null; });
125
+ }
126
+ }
127
+
128
+ internal class MssqlTransaction : An5TransactionBase
129
+ {
130
+ private readonly SqlConnection _conn;
131
+ private readonly SqlTransaction _tx;
132
+ private readonly Action _cleanup;
133
+
134
+ public MssqlTransaction(SqlConnection conn, SqlTransaction tx, Action cleanup)
135
+ {
136
+ _conn = conn;
137
+ _tx = tx;
138
+ _cleanup = cleanup;
139
+ }
140
+
141
+ public override void Commit() { _tx.Commit(); _cleanup(); }
142
+ public override void Rollback() { _tx.Rollback(); _cleanup(); }
143
+ public override void Dispose() { _tx.Dispose(); _conn.Dispose(); _cleanup(); }
144
+ }
145
+ }
@@ -1,121 +1,149 @@
1
- using System;
2
- using System.Collections.Generic;
3
- using System.Data;
4
- using System.Reflection;
5
-
6
- namespace An5Orm
7
- {
8
- // ─── PostgreSQL Engine ─────────────────────────────────────────────────────
9
-
10
- internal class PostgresEngine : IQueryEngine
11
- {
12
- public Dialect Dialect => Dialect.Postgres;
13
- private readonly string _connectionString;
14
- private readonly int _commandTimeout;
15
-
16
- public PostgresEngine(string connectionString, int commandTimeout)
17
- {
18
- _connectionString = connectionString;
19
- _commandTimeout = commandTimeout;
20
- }
21
-
22
- private Npgsql.NpgsqlConnection OpenConnection()
23
- {
24
- var conn = new Npgsql.NpgsqlConnection(_connectionString);
25
- conn.Open();
26
- return conn;
27
- }
28
-
29
- private Npgsql.NpgsqlCommand BuildCommand(
30
- Npgsql.NpgsqlConnection conn, string sql, Dictionary<string, object> parameters)
31
- {
32
- var cmd = new Npgsql.NpgsqlCommand(sql, conn) { CommandTimeout = _commandTimeout };
33
- if (parameters != null)
34
- {
35
- foreach (var kv in parameters)
36
- {
37
- var paramName = kv.Key.StartsWith("@") ? kv.Key : "@" + kv.Key;
38
- cmd.Parameters.AddWithValue(paramName, kv.Value ?? DBNull.Value);
39
- }
40
- }
41
- return cmd;
42
- }
43
-
44
- public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters)
45
- {
46
- var results = new List<Dictionary<string, object>>();
47
- using var conn = OpenConnection();
48
- using var cmd = BuildCommand(conn, sql, parameters);
49
- using var reader = cmd.ExecuteReader();
50
- while (reader.Read())
51
- {
52
- var row = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
53
- for (int i = 0; i < reader.FieldCount; i++)
54
- {
55
- row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
56
- }
57
- results.Add(row);
58
- }
59
- return results;
60
- }
61
-
62
- public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new()
63
- {
64
- var results = new List<T>();
65
- using var conn = OpenConnection();
66
- using var cmd = BuildCommand(conn, sql, parameters);
67
- using var reader = cmd.ExecuteReader();
68
- var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
69
- while (reader.Read())
70
- {
71
- var item = new T();
72
- foreach (var prop in props)
73
- {
74
- if (!HasColumn(reader, prop.Name)) continue;
75
- var val = reader[prop.Name];
76
- if (val == DBNull.Value) continue;
77
- try { prop.SetValue(item, Convert.ChangeType(val, prop.PropertyType)); } catch { }
78
- }
79
- results.Add(item);
80
- }
81
- return results;
82
- }
83
-
84
- public int ExecuteRaw(string sql, Dictionary<string, object> parameters)
85
- {
86
- using var conn = OpenConnection();
87
- using var cmd = BuildCommand(conn, sql, parameters);
88
- return cmd.ExecuteNonQuery();
89
- }
90
-
91
- private static bool HasColumn(Npgsql.NpgsqlDataReader reader, string name)
92
- {
93
- for (int i = 0; i < reader.FieldCount; i++)
94
- if (reader.GetName(i).Equals(name, StringComparison.OrdinalIgnoreCase)) return true;
95
- return false;
96
- }
97
-
98
- public An5TransactionBase BeginTransaction()
99
- {
100
- var conn = OpenConnection();
101
- var tx = conn.BeginTransaction();
102
- return new PostgresTransaction(conn, tx);
103
- }
104
- }
105
-
106
- internal class PostgresTransaction : An5TransactionBase
107
- {
108
- private readonly Npgsql.NpgsqlConnection _conn;
109
- private readonly Npgsql.NpgsqlTransaction _tx;
110
-
111
- public PostgresTransaction(Npgsql.NpgsqlConnection conn, Npgsql.NpgsqlTransaction tx)
112
- {
113
- _conn = conn;
114
- _tx = tx;
115
- }
116
-
117
- public override void Commit() { _tx.Commit(); }
118
- public override void Rollback() { _tx.Rollback(); }
119
- public override void Dispose() { _tx.Dispose(); _conn.Dispose(); }
120
- }
121
- }
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Data;
4
+ using System.Reflection;
5
+
6
+ namespace An5Orm
7
+ {
8
+ // ─── PostgreSQL Engine ─────────────────────────────────────────────────────
9
+
10
+ internal class PostgresEngine : IQueryEngine
11
+ {
12
+ public Dialect Dialect => Dialect.Postgres;
13
+ private readonly string _connectionString;
14
+ private readonly int _commandTimeout;
15
+
16
+ // Same shape as MssqlEngine: while a transaction is open, statements
17
+ // have to run on its connection and carry its transaction, or they land
18
+ // outside it and commit/rollback do not cover them.
19
+ [ThreadStatic] private static Npgsql.NpgsqlConnection _txConn;
20
+ [ThreadStatic] private static Npgsql.NpgsqlTransaction _tx;
21
+
22
+ public PostgresEngine(string connectionString, int commandTimeout)
23
+ {
24
+ _connectionString = connectionString;
25
+ _commandTimeout = commandTimeout;
26
+ }
27
+
28
+ private Npgsql.NpgsqlConnection OpenConnection(out bool isInTransaction)
29
+ {
30
+ if (_txConn != null) { isInTransaction = true; return _txConn; }
31
+ isInTransaction = false;
32
+ var conn = new Npgsql.NpgsqlConnection(_connectionString);
33
+ conn.Open();
34
+ return conn;
35
+ }
36
+
37
+ private Npgsql.NpgsqlCommand BuildCommand(
38
+ Npgsql.NpgsqlConnection conn, string sql, Dictionary<string, object> parameters)
39
+ {
40
+ var cmd = new Npgsql.NpgsqlCommand(sql, conn) { CommandTimeout = _commandTimeout };
41
+ if (_tx != null) cmd.Transaction = _tx;
42
+ if (parameters != null)
43
+ {
44
+ foreach (var kv in parameters)
45
+ {
46
+ var paramName = kv.Key.StartsWith("@") ? kv.Key : "@" + kv.Key;
47
+ cmd.Parameters.AddWithValue(paramName, kv.Value ?? DBNull.Value);
48
+ }
49
+ }
50
+ return cmd;
51
+ }
52
+
53
+ public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters)
54
+ {
55
+ var results = new List<Dictionary<string, object>>();
56
+ var conn = OpenConnection(out bool isTx);
57
+ try
58
+ {
59
+ using var cmd = BuildCommand(conn, sql, parameters);
60
+ using var reader = cmd.ExecuteReader();
61
+ while (reader.Read())
62
+ {
63
+ var row = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
64
+ for (int i = 0; i < reader.FieldCount; i++)
65
+ {
66
+ row[reader.GetName(i)] = reader.IsDBNull(i) ? null : reader.GetValue(i);
67
+ }
68
+ results.Add(row);
69
+ }
70
+ }
71
+ finally { if (!isTx) conn.Dispose(); }
72
+ return results;
73
+ }
74
+
75
+ public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new()
76
+ {
77
+ var results = new List<T>();
78
+ var conn = OpenConnection(out bool isTx);
79
+ try
80
+ {
81
+ using var cmd = BuildCommand(conn, sql, parameters);
82
+ using var reader = cmd.ExecuteReader();
83
+ var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
84
+ while (reader.Read())
85
+ {
86
+ var item = new T();
87
+ foreach (var prop in props)
88
+ {
89
+ if (!HasColumn(reader, prop.Name)) continue;
90
+ var val = reader[prop.Name];
91
+ if (val == DBNull.Value) continue;
92
+ try { prop.SetValue(item, Convert.ChangeType(val, prop.PropertyType)); } catch { }
93
+ }
94
+ results.Add(item);
95
+ }
96
+ }
97
+ finally { if (!isTx) conn.Dispose(); }
98
+ return results;
99
+ }
100
+
101
+ public int ExecuteRaw(string sql, Dictionary<string, object> parameters)
102
+ {
103
+ var conn = OpenConnection(out bool isTx);
104
+ try
105
+ {
106
+ using var cmd = BuildCommand(conn, sql, parameters);
107
+ return cmd.ExecuteNonQuery();
108
+ }
109
+ finally { if (!isTx) conn.Dispose(); }
110
+ }
111
+
112
+ private static bool HasColumn(Npgsql.NpgsqlDataReader reader, string name)
113
+ {
114
+ for (int i = 0; i < reader.FieldCount; i++)
115
+ if (reader.GetName(i).Equals(name, StringComparison.OrdinalIgnoreCase)) return true;
116
+ return false;
117
+ }
118
+
119
+ public An5TransactionBase BeginTransaction()
120
+ {
121
+ if (_txConn != null)
122
+ throw new InvalidOperationException("A transaction is already open on this thread.");
123
+ var conn = new Npgsql.NpgsqlConnection(_connectionString);
124
+ conn.Open();
125
+ var tx = conn.BeginTransaction();
126
+ _txConn = conn;
127
+ _tx = tx;
128
+ return new PostgresTransaction(conn, tx, () => { _txConn = null; _tx = null; });
129
+ }
130
+ }
131
+
132
+ internal class PostgresTransaction : An5TransactionBase
133
+ {
134
+ private readonly Npgsql.NpgsqlConnection _conn;
135
+ private readonly Npgsql.NpgsqlTransaction _tx;
136
+ private readonly Action _cleanup;
137
+
138
+ public PostgresTransaction(Npgsql.NpgsqlConnection conn, Npgsql.NpgsqlTransaction tx, Action cleanup)
139
+ {
140
+ _conn = conn;
141
+ _tx = tx;
142
+ _cleanup = cleanup;
143
+ }
144
+
145
+ public override void Commit() { _tx.Commit(); _cleanup(); }
146
+ public override void Rollback() { _tx.Rollback(); _cleanup(); }
147
+ public override void Dispose() { _tx.Dispose(); _conn.Dispose(); _cleanup(); }
148
+ }
149
+ }