@an5/adapters 0.2.3 → 0.2.4

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 (96) hide show
  1. package/README.md +304 -283
  2. package/dotnet/An5Adapter.cs +275 -275
  3. package/dotnet/Base/Core.cs +82 -82
  4. package/dotnet/Mssql/MssqlEngine.cs +144 -144
  5. package/dotnet/Postgres/PostgresEngine.cs +121 -121
  6. package/package.json +20 -10
  7. package/python/an5_adapter.py +97 -97
  8. package/python/base/__init__.py +3 -3
  9. package/python/base/dialects.py +8 -8
  10. package/python/base/metadata.py +10 -10
  11. package/python/base/sql.py +121 -121
  12. package/python/mssql/__init__.py +1 -1
  13. package/python/mssql/provider.py +62 -62
  14. package/python/postgres/__init__.py +1 -1
  15. package/python/postgres/provider.py +15 -15
  16. package/python/table_client.py +216 -216
  17. package/typescript/src/an5Adapter.ts +505 -505
  18. package/typescript/src/base/index.ts +3 -3
  19. package/typescript/src/base/metadata.ts +28 -28
  20. package/typescript/src/base/sql.ts +98 -98
  21. package/typescript/src/base/types.ts +24 -24
  22. package/typescript/src/browser.ts +6 -6
  23. package/typescript/src/config.ts +95 -95
  24. package/typescript/src/googlesheets/adapter.ts +259 -259
  25. package/typescript/src/googlesheets/an5SheetsAdapter.ts +3 -3
  26. package/typescript/src/googlesheets/config.ts +47 -47
  27. package/typescript/src/googlesheets/helpers.ts +143 -143
  28. package/typescript/src/googlesheets/index.ts +3 -3
  29. package/typescript/src/googlesheets/parseConnectionString.ts +46 -46
  30. package/typescript/src/googlesheets/retry.ts +126 -126
  31. package/typescript/src/googlesheets/sqlExecutor.ts +301 -301
  32. package/typescript/src/googlesheets/tableClient.ts +455 -455
  33. package/typescript/src/googlesheets/types.ts +6 -6
  34. package/typescript/src/index.ts +18 -18
  35. package/typescript/src/mssql/engine.ts +132 -132
  36. package/typescript/src/mssql/index.ts +1 -1
  37. package/typescript/src/mysql/engine.ts +80 -80
  38. package/typescript/src/mysql/index.ts +1 -1
  39. package/typescript/src/postgres/engine.ts +87 -87
  40. package/typescript/src/postgres/index.ts +1 -1
  41. package/typescript/src/sqlite/engine.ts +55 -55
  42. package/typescript/src/sqlite/index.ts +1 -1
  43. /package/{typescript → dist}/an5Adapter.d.ts +0 -0
  44. /package/{typescript → dist}/an5Adapter.js +0 -0
  45. /package/{typescript → dist}/base/index.d.ts +0 -0
  46. /package/{typescript → dist}/base/index.js +0 -0
  47. /package/{typescript → dist}/base/metadata.d.ts +0 -0
  48. /package/{typescript → dist}/base/metadata.js +0 -0
  49. /package/{typescript → dist}/base/sql.d.ts +0 -0
  50. /package/{typescript → dist}/base/sql.js +0 -0
  51. /package/{typescript → dist}/base/types.d.ts +0 -0
  52. /package/{typescript → dist}/base/types.js +0 -0
  53. /package/{typescript → dist}/base/uuid.d.ts +0 -0
  54. /package/{typescript → dist}/base/uuid.js +0 -0
  55. /package/{typescript → dist}/browser.d.ts +0 -0
  56. /package/{typescript → dist}/browser.js +0 -0
  57. /package/{typescript → dist}/config.d.ts +0 -0
  58. /package/{typescript → dist}/config.js +0 -0
  59. /package/{typescript → dist}/googlesheets/adapter.d.ts +0 -0
  60. /package/{typescript → dist}/googlesheets/adapter.js +0 -0
  61. /package/{typescript → dist}/googlesheets/an5SheetsAdapter.d.ts +0 -0
  62. /package/{typescript → dist}/googlesheets/an5SheetsAdapter.js +0 -0
  63. /package/{typescript → dist}/googlesheets/config.d.ts +0 -0
  64. /package/{typescript → dist}/googlesheets/config.js +0 -0
  65. /package/{typescript → dist}/googlesheets/helpers.d.ts +0 -0
  66. /package/{typescript → dist}/googlesheets/helpers.js +0 -0
  67. /package/{typescript → dist}/googlesheets/index.d.ts +0 -0
  68. /package/{typescript → dist}/googlesheets/index.js +0 -0
  69. /package/{typescript → dist}/googlesheets/parseConnectionString.d.ts +0 -0
  70. /package/{typescript → dist}/googlesheets/parseConnectionString.js +0 -0
  71. /package/{typescript → dist}/googlesheets/retry.d.ts +0 -0
  72. /package/{typescript → dist}/googlesheets/retry.js +0 -0
  73. /package/{typescript → dist}/googlesheets/sqlExecutor.d.ts +0 -0
  74. /package/{typescript → dist}/googlesheets/sqlExecutor.js +0 -0
  75. /package/{typescript → dist}/googlesheets/tableClient.d.ts +0 -0
  76. /package/{typescript → dist}/googlesheets/tableClient.js +0 -0
  77. /package/{typescript → dist}/googlesheets/types.d.ts +0 -0
  78. /package/{typescript → dist}/googlesheets/types.js +0 -0
  79. /package/{typescript → dist}/index.d.ts +0 -0
  80. /package/{typescript → dist}/index.js +0 -0
  81. /package/{typescript → dist}/mssql/engine.d.ts +0 -0
  82. /package/{typescript → dist}/mssql/engine.js +0 -0
  83. /package/{typescript → dist}/mssql/index.d.ts +0 -0
  84. /package/{typescript → dist}/mssql/index.js +0 -0
  85. /package/{typescript → dist}/mysql/engine.d.ts +0 -0
  86. /package/{typescript → dist}/mysql/engine.js +0 -0
  87. /package/{typescript → dist}/mysql/index.d.ts +0 -0
  88. /package/{typescript → dist}/mysql/index.js +0 -0
  89. /package/{typescript → dist}/postgres/engine.d.ts +0 -0
  90. /package/{typescript → dist}/postgres/engine.js +0 -0
  91. /package/{typescript → dist}/postgres/index.d.ts +0 -0
  92. /package/{typescript → dist}/postgres/index.js +0 -0
  93. /package/{typescript → dist}/sqlite/engine.d.ts +0 -0
  94. /package/{typescript → dist}/sqlite/engine.js +0 -0
  95. /package/{typescript → dist}/sqlite/index.d.ts +0 -0
  96. /package/{typescript → dist}/sqlite/index.js +0 -0
@@ -1,82 +1,82 @@
1
- using System;
2
- using System.Collections.Generic;
3
- using System.Data.Common;
4
-
5
- namespace An5Orm
6
- {
7
- public enum Dialect
8
- {
9
- Mssql,
10
- Postgres
11
- }
12
-
13
- // ─── Config ────────────────────────────────────────────────────────────────
14
-
15
- public class An5AdapterOptions
16
- {
17
- public string ConnectionString { get; set; }
18
- public int CommandTimeout { get; set; } = 60;
19
- public int ConnectRetryCount { get; set; } = 3;
20
- }
21
-
22
- // ─── Query Engine Interface ────────────────────────────────────────────────
23
-
24
- internal interface IQueryEngine
25
- {
26
- Dialect Dialect { get; }
27
- List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters);
28
- List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new();
29
- int ExecuteRaw(string sql, Dictionary<string, object> parameters);
30
- An5TransactionBase BeginTransaction();
31
- }
32
-
33
- // ─── Transaction base ──────────────────────────────────────────────────────
34
-
35
- internal abstract class An5TransactionBase : IDisposable
36
- {
37
- public abstract void Commit();
38
- public abstract void Rollback();
39
- public abstract void Dispose();
40
- }
41
-
42
- // ─── Dialect detection ─────────────────────────────────────────────────────
43
-
44
- internal static class DialectDetector
45
- {
46
- public static Dialect Detect(string connectionString)
47
- {
48
- var cs = (connectionString ?? "").Trim().ToLowerInvariant();
49
- if (cs.StartsWith("postgres://") || cs.StartsWith("postgresql://") || cs.Contains("host="))
50
- return Dialect.Postgres;
51
- return Dialect.Mssql;
52
- }
53
- }
54
-
55
- // ─── Quoting ───────────────────────────────────────────────────────────────
56
-
57
- internal static class SqlQuote
58
- {
59
- public static string QuoteName(string name, Dialect dialect)
60
- {
61
- if (dialect == Dialect.Postgres)
62
- {
63
- if (name.StartsWith("[") && name.EndsWith("]"))
64
- return "\"" + name.Substring(1, name.Length - 2) + "\"";
65
- return "\"" + name + "\"";
66
- }
67
- return name.StartsWith("[") ? name : "[" + name + "]";
68
- }
69
-
70
- public static string QuoteTable(string tableName, Dialect dialect)
71
- {
72
- if (tableName.StartsWith("[") || tableName.StartsWith("\""))
73
- return tableName;
74
- if (tableName.Contains("."))
75
- {
76
- var parts = tableName.Split('.');
77
- return string.Join(".", Array.ConvertAll(parts, p => QuoteName(p, dialect)));
78
- }
79
- return QuoteName(tableName, dialect);
80
- }
81
- }
82
- }
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Data.Common;
4
+
5
+ namespace An5Orm
6
+ {
7
+ public enum Dialect
8
+ {
9
+ Mssql,
10
+ Postgres
11
+ }
12
+
13
+ // ─── Config ────────────────────────────────────────────────────────────────
14
+
15
+ public class An5AdapterOptions
16
+ {
17
+ public string ConnectionString { get; set; }
18
+ public int CommandTimeout { get; set; } = 60;
19
+ public int ConnectRetryCount { get; set; } = 3;
20
+ }
21
+
22
+ // ─── Query Engine Interface ────────────────────────────────────────────────
23
+
24
+ internal interface IQueryEngine
25
+ {
26
+ Dialect Dialect { get; }
27
+ List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters);
28
+ List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters) where T : new();
29
+ int ExecuteRaw(string sql, Dictionary<string, object> parameters);
30
+ An5TransactionBase BeginTransaction();
31
+ }
32
+
33
+ // ─── Transaction base ──────────────────────────────────────────────────────
34
+
35
+ internal abstract class An5TransactionBase : IDisposable
36
+ {
37
+ public abstract void Commit();
38
+ public abstract void Rollback();
39
+ public abstract void Dispose();
40
+ }
41
+
42
+ // ─── Dialect detection ─────────────────────────────────────────────────────
43
+
44
+ internal static class DialectDetector
45
+ {
46
+ public static Dialect Detect(string connectionString)
47
+ {
48
+ var cs = (connectionString ?? "").Trim().ToLowerInvariant();
49
+ if (cs.StartsWith("postgres://") || cs.StartsWith("postgresql://") || cs.Contains("host="))
50
+ return Dialect.Postgres;
51
+ return Dialect.Mssql;
52
+ }
53
+ }
54
+
55
+ // ─── Quoting ───────────────────────────────────────────────────────────────
56
+
57
+ internal static class SqlQuote
58
+ {
59
+ public static string QuoteName(string name, Dialect dialect)
60
+ {
61
+ if (dialect == Dialect.Postgres)
62
+ {
63
+ if (name.StartsWith("[") && name.EndsWith("]"))
64
+ return "\"" + name.Substring(1, name.Length - 2) + "\"";
65
+ return "\"" + name + "\"";
66
+ }
67
+ return name.StartsWith("[") ? name : "[" + name + "]";
68
+ }
69
+
70
+ public static string QuoteTable(string tableName, Dialect dialect)
71
+ {
72
+ if (tableName.StartsWith("[") || tableName.StartsWith("\""))
73
+ return tableName;
74
+ if (tableName.Contains("."))
75
+ {
76
+ var parts = tableName.Split('.');
77
+ return string.Join(".", Array.ConvertAll(parts, p => QuoteName(p, dialect)));
78
+ }
79
+ return QuoteName(tableName, dialect);
80
+ }
81
+ }
82
+ }
@@ -1,144 +1,144 @@
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
+
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
+ }