@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,275 +1,275 @@
1
- // An5Adapter.cs
2
- // Standalone .NET runtime adapter for AN5 ORM.
3
-
4
- using System;
5
- using System.Collections.Generic;
6
- using System.Reflection;
7
- using System.Text;
8
- using System.Text.Json;
9
-
10
- namespace An5Orm
11
- {
12
- // ─── Adapter ────────────────────────────────────────────────────────────────
13
-
14
- public class An5Adapter : IDisposable
15
- {
16
- public string ConnectionString { get; }
17
- public Dialect Dialect => _engine.Dialect;
18
- private readonly IQueryEngine _engine;
19
- private readonly int _commandTimeout;
20
-
21
- public An5Adapter(string connectionString, int commandTimeout = 60)
22
- {
23
- ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
24
- _commandTimeout = commandTimeout;
25
- _engine = DialectDetector.Detect(connectionString) switch
26
- {
27
- Dialect.Postgres => new PostgresEngine(connectionString, commandTimeout),
28
- _ => new MssqlEngine(connectionString, commandTimeout)
29
- };
30
- }
31
-
32
- public An5Adapter(An5AdapterOptions options) : this(options.ConnectionString, options.CommandTimeout) { }
33
-
34
- // ── Raw query execution ────────────────────────────────────────────────
35
-
36
- public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters = null)
37
- => _engine.QueryRaw(sql, parameters);
38
-
39
- public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters = null) where T : new()
40
- => _engine.QueryRaw<T>(sql, parameters);
41
-
42
- public int ExecuteRaw(string sql, Dictionary<string, object> parameters = null)
43
- => _engine.ExecuteRaw(sql, parameters);
44
-
45
- // ── Transaction ────────────────────────────────────────────────────────
46
-
47
- public An5Transaction BeginTransaction()
48
- {
49
- return new An5Transaction(_engine.BeginTransaction());
50
- }
51
-
52
- public TResult Transaction<TResult>(Func<An5Adapter, TResult> fn)
53
- {
54
- using var txScope = _engine.BeginTransaction();
55
- try
56
- {
57
- var result = fn(this);
58
- txScope.Commit();
59
- return result;
60
- }
61
- catch
62
- {
63
- txScope.Rollback();
64
- throw;
65
- }
66
- }
67
-
68
- // ── Table client factory ───────────────────────────────────────────────
69
-
70
- public AdapterTableClient<T> Table<T>(string tableName) where T : new()
71
- => new AdapterTableClient<T>(this, tableName, _engine.Dialect);
72
-
73
- public void Dispose() { }
74
- }
75
-
76
- // ─── Transaction wrapper ───────────────────────────────────────────────────
77
-
78
- public class An5Transaction : IDisposable
79
- {
80
- private readonly An5TransactionBase _inner;
81
-
82
- internal An5Transaction(An5TransactionBase inner) => _inner = inner;
83
-
84
- public void Commit() => _inner.Commit();
85
- public void Rollback() => _inner.Rollback();
86
- public void Dispose() => _inner.Dispose();
87
- }
88
-
89
- // ─── Typed Table Client ────────────────────────────────────────────────────
90
-
91
- public class AdapterTableClient<T> where T : new()
92
- {
93
- private readonly An5Adapter _adapter;
94
- private readonly string _tableName;
95
- private readonly Dialect _dialect;
96
-
97
- public AdapterTableClient(An5Adapter adapter, string tableName, Dialect dialect)
98
- {
99
- _adapter = adapter;
100
- _tableName = SqlQuote.QuoteTable(tableName, dialect);
101
- _dialect = dialect;
102
- }
103
-
104
- private string Nolock => _dialect == Dialect.Mssql ? " WITH (NOLOCK)" : "";
105
-
106
- private string BuildPagination(string orderBy, int? skip, int? take)
107
- {
108
- if (take == null) return "";
109
- if (_dialect == Dialect.Postgres)
110
- return $" LIMIT {take.Value} OFFSET {skip ?? 0}";
111
- var o = !string.IsNullOrEmpty(orderBy) ? $" ORDER BY {orderBy}" : " ORDER BY (SELECT NULL)";
112
- return $"{o} OFFSET {skip ?? 0} ROWS FETCH NEXT {take.Value} ROWS ONLY";
113
- }
114
-
115
- public List<T> FindMany(string whereClause = null, Dictionary<string, object> parameters = null,
116
- string orderBy = null, int? skip = null, int? take = null)
117
- {
118
- var sb = new StringBuilder($"SELECT * FROM {_tableName}{Nolock}");
119
- if (!string.IsNullOrEmpty(whereClause)) sb.Append($" WHERE {whereClause}");
120
- if (take != null && _dialect == Dialect.Postgres)
121
- {
122
- if (!string.IsNullOrEmpty(orderBy)) sb.Append($" ORDER BY {orderBy}");
123
- sb.Append(BuildPagination(orderBy, skip, take));
124
- }
125
- else
126
- {
127
- if (!string.IsNullOrEmpty(orderBy)) sb.Append($" ORDER BY {orderBy}");
128
- sb.Append(BuildPagination(orderBy, skip, take));
129
- }
130
- return _adapter.QueryRaw<T>(sb.ToString(), parameters);
131
- }
132
-
133
- public T FindFirst(string whereClause = null, Dictionary<string, object> parameters = null, string orderBy = null)
134
- {
135
- var rows = FindMany(whereClause, parameters, orderBy, skip: null, take: 1);
136
- return rows.Count > 0 ? rows[0] : default;
137
- }
138
-
139
- public T FindUnique(object id, string idColumnName = "Id")
140
- {
141
- return FindFirst($"{SqlQuote.QuoteName(idColumnName, _dialect)} = @id",
142
- new Dictionary<string, object> { { "id", id } });
143
- }
144
-
145
- public int Count(string whereClause = null, Dictionary<string, object> parameters = null)
146
- {
147
- var sql = $"SELECT COUNT(*) FROM {_tableName}";
148
- if (!string.IsNullOrEmpty(whereClause)) sql += $" WHERE {whereClause}";
149
- var rows = _adapter.QueryRaw(sql, parameters);
150
- if (rows.Count > 0)
151
- {
152
- var val = rows[0].Values.GetEnumerator();
153
- val.MoveNext();
154
- return Convert.ToInt32(val.Current);
155
- }
156
- return 0;
157
- }
158
-
159
- public T Create(T entity)
160
- {
161
- var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
162
- var cols = new List<string>();
163
- var vals = new List<string>();
164
- var parameters = new Dictionary<string, object>();
165
-
166
- foreach (var prop in props)
167
- {
168
- var val = prop.GetValue(entity);
169
- if (val == null) continue;
170
- cols.Add(SqlQuote.QuoteName(prop.Name, _dialect));
171
- vals.Add("@" + prop.Name);
172
- parameters["@" + prop.Name] = val;
173
- }
174
-
175
- var sql = $"INSERT INTO {_tableName} ({string.Join(", ", cols)}) VALUES ({string.Join(", ", vals)})";
176
- _adapter.ExecuteRaw(sql, parameters);
177
- return entity;
178
- }
179
-
180
- public T Update(T entity, string idColumnName = "Id")
181
- {
182
- var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
183
- var sets = new List<string>();
184
- var parameters = new Dictionary<string, object>();
185
- object idVal = null;
186
-
187
- foreach (var prop in props)
188
- {
189
- var val = prop.GetValue(entity);
190
- if (prop.Name.Equals(idColumnName, StringComparison.OrdinalIgnoreCase))
191
- {
192
- idVal = val;
193
- }
194
- else if (val != null)
195
- {
196
- sets.Add($"{SqlQuote.QuoteName(prop.Name, _dialect)} = @{prop.Name}");
197
- parameters["@" + prop.Name] = val;
198
- }
199
- }
200
-
201
- if (idVal == null) throw new InvalidOperationException($"Cannot update entity without {idColumnName}");
202
- parameters["@_id"] = idVal;
203
- var quotedIdCol = SqlQuote.QuoteName(idColumnName, _dialect);
204
- var sql = $"UPDATE {_tableName} SET {string.Join(", ", sets)} WHERE {quotedIdCol} = @_id";
205
- _adapter.ExecuteRaw(sql, parameters);
206
- return entity;
207
- }
208
-
209
- public bool Delete(object id, string idColumnName = "Id")
210
- {
211
- var quotedIdCol = SqlQuote.QuoteName(idColumnName, _dialect);
212
- var sql = $"DELETE FROM {_tableName} WHERE {quotedIdCol} = @id";
213
- var n = _adapter.ExecuteRaw(sql, new Dictionary<string, object> { { "@id", id } });
214
- return n > 0;
215
- }
216
-
217
- public int DeleteMany(string whereClause = null, Dictionary<string, object> parameters = null)
218
- {
219
- var sql = $"DELETE FROM {_tableName}";
220
- if (!string.IsNullOrEmpty(whereClause)) sql += $" WHERE {whereClause}";
221
- return _adapter.ExecuteRaw(sql, parameters);
222
- }
223
-
224
- public T Upsert(T entity, string idColumnName = "Id")
225
- {
226
- var idProp = typeof(T).GetProperty(idColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
227
- if (idProp == null) throw new InvalidOperationException($"Property {idColumnName} not found on {typeof(T).Name}");
228
- var idVal = idProp.GetValue(entity);
229
- var existing = idVal != null ? FindUnique(idVal, idColumnName) : default;
230
- if (existing != null) return Update(entity, idColumnName);
231
- return Create(entity);
232
- }
233
-
234
- // ── Vector Search ──────────────────────────────────────────────────────
235
-
236
- public List<(T Item, double Distance)> VectorSearch(
237
- List<double> vector, int take = 10,
238
- string whereClause = null, Dictionary<string, object> parameters = null,
239
- string vectorField = "Embedding", string distanceMetric = "cosine")
240
- {
241
- var rows = FindMany(whereClause, parameters);
242
- var results = new List<(T, double)>();
243
- var vectorProp = typeof(T).GetProperty(vectorField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
244
- if (vectorProp == null) return results;
245
-
246
- foreach (var row in rows)
247
- {
248
- var rawVal = vectorProp.GetValue(row);
249
- if (rawVal == null) continue;
250
- List<double> vec = null;
251
- try
252
- {
253
- if (rawVal is string s) vec = JsonSerializer.Deserialize<List<double>>(s);
254
- }
255
- catch { continue; }
256
- if (vec == null || vec.Count != vector.Count) continue;
257
-
258
- double dot = 0, m1 = 0, m2 = 0;
259
- for (int i = 0; i < vector.Count; i++)
260
- {
261
- dot += vector[i] * vec[i];
262
- m1 += vector[i] * vector[i];
263
- m2 += vec[i] * vec[i];
264
- }
265
- double cosine = (m1 > 0 && m2 > 0) ? dot / (Math.Sqrt(m1) * Math.Sqrt(m2)) : 0;
266
- double dist = distanceMetric == "cosine" ? 1.0 - cosine :
267
- distanceMetric == "dot" ? -dot : Math.Sqrt(vector.Count);
268
- results.Add((row, dist));
269
- }
270
-
271
- results.Sort((a, b) => a.Item2.CompareTo(b.Item2));
272
- return results.GetRange(0, Math.Min(take, results.Count));
273
- }
274
- }
275
- }
1
+ // An5Adapter.cs
2
+ // Standalone .NET runtime adapter for AN5 ORM.
3
+
4
+ using System;
5
+ using System.Collections.Generic;
6
+ using System.Reflection;
7
+ using System.Text;
8
+ using System.Text.Json;
9
+
10
+ namespace An5Orm
11
+ {
12
+ // ─── Adapter ────────────────────────────────────────────────────────────────
13
+
14
+ public class An5Adapter : IDisposable
15
+ {
16
+ public string ConnectionString { get; }
17
+ public Dialect Dialect => _engine.Dialect;
18
+ private readonly IQueryEngine _engine;
19
+ private readonly int _commandTimeout;
20
+
21
+ public An5Adapter(string connectionString, int commandTimeout = 60)
22
+ {
23
+ ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
24
+ _commandTimeout = commandTimeout;
25
+ _engine = DialectDetector.Detect(connectionString) switch
26
+ {
27
+ Dialect.Postgres => new PostgresEngine(connectionString, commandTimeout),
28
+ _ => new MssqlEngine(connectionString, commandTimeout)
29
+ };
30
+ }
31
+
32
+ public An5Adapter(An5AdapterOptions options) : this(options.ConnectionString, options.CommandTimeout) { }
33
+
34
+ // ── Raw query execution ────────────────────────────────────────────────
35
+
36
+ public List<Dictionary<string, object>> QueryRaw(string sql, Dictionary<string, object> parameters = null)
37
+ => _engine.QueryRaw(sql, parameters);
38
+
39
+ public List<T> QueryRaw<T>(string sql, Dictionary<string, object> parameters = null) where T : new()
40
+ => _engine.QueryRaw<T>(sql, parameters);
41
+
42
+ public int ExecuteRaw(string sql, Dictionary<string, object> parameters = null)
43
+ => _engine.ExecuteRaw(sql, parameters);
44
+
45
+ // ── Transaction ────────────────────────────────────────────────────────
46
+
47
+ public An5Transaction BeginTransaction()
48
+ {
49
+ return new An5Transaction(_engine.BeginTransaction());
50
+ }
51
+
52
+ public TResult Transaction<TResult>(Func<An5Adapter, TResult> fn)
53
+ {
54
+ using var txScope = _engine.BeginTransaction();
55
+ try
56
+ {
57
+ var result = fn(this);
58
+ txScope.Commit();
59
+ return result;
60
+ }
61
+ catch
62
+ {
63
+ txScope.Rollback();
64
+ throw;
65
+ }
66
+ }
67
+
68
+ // ── Table client factory ───────────────────────────────────────────────
69
+
70
+ public AdapterTableClient<T> Table<T>(string tableName) where T : new()
71
+ => new AdapterTableClient<T>(this, tableName, _engine.Dialect);
72
+
73
+ public void Dispose() { }
74
+ }
75
+
76
+ // ─── Transaction wrapper ───────────────────────────────────────────────────
77
+
78
+ public class An5Transaction : IDisposable
79
+ {
80
+ private readonly An5TransactionBase _inner;
81
+
82
+ internal An5Transaction(An5TransactionBase inner) => _inner = inner;
83
+
84
+ public void Commit() => _inner.Commit();
85
+ public void Rollback() => _inner.Rollback();
86
+ public void Dispose() => _inner.Dispose();
87
+ }
88
+
89
+ // ─── Typed Table Client ────────────────────────────────────────────────────
90
+
91
+ public class AdapterTableClient<T> where T : new()
92
+ {
93
+ private readonly An5Adapter _adapter;
94
+ private readonly string _tableName;
95
+ private readonly Dialect _dialect;
96
+
97
+ public AdapterTableClient(An5Adapter adapter, string tableName, Dialect dialect)
98
+ {
99
+ _adapter = adapter;
100
+ _tableName = SqlQuote.QuoteTable(tableName, dialect);
101
+ _dialect = dialect;
102
+ }
103
+
104
+ private string Nolock => _dialect == Dialect.Mssql ? " WITH (NOLOCK)" : "";
105
+
106
+ private string BuildPagination(string orderBy, int? skip, int? take)
107
+ {
108
+ if (take == null) return "";
109
+ if (_dialect == Dialect.Postgres)
110
+ return $" LIMIT {take.Value} OFFSET {skip ?? 0}";
111
+ var o = !string.IsNullOrEmpty(orderBy) ? $" ORDER BY {orderBy}" : " ORDER BY (SELECT NULL)";
112
+ return $"{o} OFFSET {skip ?? 0} ROWS FETCH NEXT {take.Value} ROWS ONLY";
113
+ }
114
+
115
+ public List<T> FindMany(string whereClause = null, Dictionary<string, object> parameters = null,
116
+ string orderBy = null, int? skip = null, int? take = null)
117
+ {
118
+ var sb = new StringBuilder($"SELECT * FROM {_tableName}{Nolock}");
119
+ if (!string.IsNullOrEmpty(whereClause)) sb.Append($" WHERE {whereClause}");
120
+ if (take != null && _dialect == Dialect.Postgres)
121
+ {
122
+ if (!string.IsNullOrEmpty(orderBy)) sb.Append($" ORDER BY {orderBy}");
123
+ sb.Append(BuildPagination(orderBy, skip, take));
124
+ }
125
+ else
126
+ {
127
+ if (!string.IsNullOrEmpty(orderBy)) sb.Append($" ORDER BY {orderBy}");
128
+ sb.Append(BuildPagination(orderBy, skip, take));
129
+ }
130
+ return _adapter.QueryRaw<T>(sb.ToString(), parameters);
131
+ }
132
+
133
+ public T FindFirst(string whereClause = null, Dictionary<string, object> parameters = null, string orderBy = null)
134
+ {
135
+ var rows = FindMany(whereClause, parameters, orderBy, skip: null, take: 1);
136
+ return rows.Count > 0 ? rows[0] : default;
137
+ }
138
+
139
+ public T FindUnique(object id, string idColumnName = "Id")
140
+ {
141
+ return FindFirst($"{SqlQuote.QuoteName(idColumnName, _dialect)} = @id",
142
+ new Dictionary<string, object> { { "id", id } });
143
+ }
144
+
145
+ public int Count(string whereClause = null, Dictionary<string, object> parameters = null)
146
+ {
147
+ var sql = $"SELECT COUNT(*) FROM {_tableName}";
148
+ if (!string.IsNullOrEmpty(whereClause)) sql += $" WHERE {whereClause}";
149
+ var rows = _adapter.QueryRaw(sql, parameters);
150
+ if (rows.Count > 0)
151
+ {
152
+ var val = rows[0].Values.GetEnumerator();
153
+ val.MoveNext();
154
+ return Convert.ToInt32(val.Current);
155
+ }
156
+ return 0;
157
+ }
158
+
159
+ public T Create(T entity)
160
+ {
161
+ var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
162
+ var cols = new List<string>();
163
+ var vals = new List<string>();
164
+ var parameters = new Dictionary<string, object>();
165
+
166
+ foreach (var prop in props)
167
+ {
168
+ var val = prop.GetValue(entity);
169
+ if (val == null) continue;
170
+ cols.Add(SqlQuote.QuoteName(prop.Name, _dialect));
171
+ vals.Add("@" + prop.Name);
172
+ parameters["@" + prop.Name] = val;
173
+ }
174
+
175
+ var sql = $"INSERT INTO {_tableName} ({string.Join(", ", cols)}) VALUES ({string.Join(", ", vals)})";
176
+ _adapter.ExecuteRaw(sql, parameters);
177
+ return entity;
178
+ }
179
+
180
+ public T Update(T entity, string idColumnName = "Id")
181
+ {
182
+ var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
183
+ var sets = new List<string>();
184
+ var parameters = new Dictionary<string, object>();
185
+ object idVal = null;
186
+
187
+ foreach (var prop in props)
188
+ {
189
+ var val = prop.GetValue(entity);
190
+ if (prop.Name.Equals(idColumnName, StringComparison.OrdinalIgnoreCase))
191
+ {
192
+ idVal = val;
193
+ }
194
+ else if (val != null)
195
+ {
196
+ sets.Add($"{SqlQuote.QuoteName(prop.Name, _dialect)} = @{prop.Name}");
197
+ parameters["@" + prop.Name] = val;
198
+ }
199
+ }
200
+
201
+ if (idVal == null) throw new InvalidOperationException($"Cannot update entity without {idColumnName}");
202
+ parameters["@_id"] = idVal;
203
+ var quotedIdCol = SqlQuote.QuoteName(idColumnName, _dialect);
204
+ var sql = $"UPDATE {_tableName} SET {string.Join(", ", sets)} WHERE {quotedIdCol} = @_id";
205
+ _adapter.ExecuteRaw(sql, parameters);
206
+ return entity;
207
+ }
208
+
209
+ public bool Delete(object id, string idColumnName = "Id")
210
+ {
211
+ var quotedIdCol = SqlQuote.QuoteName(idColumnName, _dialect);
212
+ var sql = $"DELETE FROM {_tableName} WHERE {quotedIdCol} = @id";
213
+ var n = _adapter.ExecuteRaw(sql, new Dictionary<string, object> { { "@id", id } });
214
+ return n > 0;
215
+ }
216
+
217
+ public int DeleteMany(string whereClause = null, Dictionary<string, object> parameters = null)
218
+ {
219
+ var sql = $"DELETE FROM {_tableName}";
220
+ if (!string.IsNullOrEmpty(whereClause)) sql += $" WHERE {whereClause}";
221
+ return _adapter.ExecuteRaw(sql, parameters);
222
+ }
223
+
224
+ public T Upsert(T entity, string idColumnName = "Id")
225
+ {
226
+ var idProp = typeof(T).GetProperty(idColumnName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
227
+ if (idProp == null) throw new InvalidOperationException($"Property {idColumnName} not found on {typeof(T).Name}");
228
+ var idVal = idProp.GetValue(entity);
229
+ var existing = idVal != null ? FindUnique(idVal, idColumnName) : default;
230
+ if (existing != null) return Update(entity, idColumnName);
231
+ return Create(entity);
232
+ }
233
+
234
+ // ── Vector Search ──────────────────────────────────────────────────────
235
+
236
+ public List<(T Item, double Distance)> VectorSearch(
237
+ List<double> vector, int take = 10,
238
+ string whereClause = null, Dictionary<string, object> parameters = null,
239
+ string vectorField = "Embedding", string distanceMetric = "cosine")
240
+ {
241
+ var rows = FindMany(whereClause, parameters);
242
+ var results = new List<(T, double)>();
243
+ var vectorProp = typeof(T).GetProperty(vectorField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
244
+ if (vectorProp == null) return results;
245
+
246
+ foreach (var row in rows)
247
+ {
248
+ var rawVal = vectorProp.GetValue(row);
249
+ if (rawVal == null) continue;
250
+ List<double> vec = null;
251
+ try
252
+ {
253
+ if (rawVal is string s) vec = JsonSerializer.Deserialize<List<double>>(s);
254
+ }
255
+ catch { continue; }
256
+ if (vec == null || vec.Count != vector.Count) continue;
257
+
258
+ double dot = 0, m1 = 0, m2 = 0;
259
+ for (int i = 0; i < vector.Count; i++)
260
+ {
261
+ dot += vector[i] * vec[i];
262
+ m1 += vector[i] * vector[i];
263
+ m2 += vec[i] * vec[i];
264
+ }
265
+ double cosine = (m1 > 0 && m2 > 0) ? dot / (Math.Sqrt(m1) * Math.Sqrt(m2)) : 0;
266
+ double dist = distanceMetric == "cosine" ? 1.0 - cosine :
267
+ distanceMetric == "dot" ? -dot : Math.Sqrt(vector.Count);
268
+ results.Add((row, dist));
269
+ }
270
+
271
+ results.Sort((a, b) => a.Item2.CompareTo(b.Item2));
272
+ return results.GetRange(0, Math.Min(take, results.Count));
273
+ }
274
+ }
275
+ }