@an5/adapters 0.2.3 → 0.2.5
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.
- package/README.md +305 -283
- package/{typescript → dist}/browser.d.ts +1 -1
- package/{typescript → dist}/browser.js +2 -1
- package/{typescript → dist}/index.d.ts +2 -0
- package/{typescript → dist}/index.js +6 -1
- package/dotnet/An5Adapter.cs +275 -275
- package/dotnet/Base/Core.cs +82 -82
- package/dotnet/Mssql/MssqlEngine.cs +144 -144
- package/dotnet/Postgres/PostgresEngine.cs +121 -121
- package/package.json +21 -11
- package/python/an5_adapter.py +97 -97
- package/python/base/__init__.py +3 -3
- package/python/base/dialects.py +8 -8
- package/python/base/metadata.py +10 -10
- package/python/base/sql.py +121 -121
- package/python/mssql/__init__.py +1 -1
- package/python/mssql/provider.py +62 -62
- package/python/postgres/__init__.py +1 -1
- package/python/postgres/provider.py +15 -15
- package/python/table_client.py +216 -216
- package/typescript/src/an5Adapter.ts +505 -505
- package/typescript/src/base/index.ts +3 -3
- package/typescript/src/base/metadata.ts +28 -28
- package/typescript/src/base/sql.ts +98 -98
- package/typescript/src/base/types.ts +24 -24
- package/typescript/src/browser.ts +6 -6
- package/typescript/src/config.ts +95 -95
- package/typescript/src/googlesheets/adapter.ts +259 -259
- package/typescript/src/googlesheets/an5SheetsAdapter.ts +3 -3
- package/typescript/src/googlesheets/config.ts +47 -47
- package/typescript/src/googlesheets/helpers.ts +143 -143
- package/typescript/src/googlesheets/index.ts +3 -3
- package/typescript/src/googlesheets/parseConnectionString.ts +46 -46
- package/typescript/src/googlesheets/retry.ts +126 -126
- package/typescript/src/googlesheets/sqlExecutor.ts +301 -301
- package/typescript/src/googlesheets/tableClient.ts +455 -455
- package/typescript/src/googlesheets/types.ts +6 -6
- package/typescript/src/index.ts +28 -19
- package/typescript/src/mssql/engine.ts +132 -132
- package/typescript/src/mssql/index.ts +1 -1
- package/typescript/src/mysql/engine.ts +80 -80
- package/typescript/src/mysql/index.ts +1 -1
- package/typescript/src/postgres/engine.ts +87 -87
- package/typescript/src/postgres/index.ts +1 -1
- package/typescript/src/sqlite/engine.ts +55 -55
- package/typescript/src/sqlite/index.ts +1 -1
- /package/{typescript → dist}/an5Adapter.d.ts +0 -0
- /package/{typescript → dist}/an5Adapter.js +0 -0
- /package/{typescript → dist}/base/index.d.ts +0 -0
- /package/{typescript → dist}/base/index.js +0 -0
- /package/{typescript → dist}/base/metadata.d.ts +0 -0
- /package/{typescript → dist}/base/metadata.js +0 -0
- /package/{typescript → dist}/base/sql.d.ts +0 -0
- /package/{typescript → dist}/base/sql.js +0 -0
- /package/{typescript → dist}/base/types.d.ts +0 -0
- /package/{typescript → dist}/base/types.js +0 -0
- /package/{typescript → dist}/base/uuid.d.ts +0 -0
- /package/{typescript → dist}/base/uuid.js +0 -0
- /package/{typescript → dist}/config.d.ts +0 -0
- /package/{typescript → dist}/config.js +0 -0
- /package/{typescript → dist}/googlesheets/adapter.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/adapter.js +0 -0
- /package/{typescript → dist}/googlesheets/an5SheetsAdapter.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/an5SheetsAdapter.js +0 -0
- /package/{typescript → dist}/googlesheets/config.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/config.js +0 -0
- /package/{typescript → dist}/googlesheets/helpers.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/helpers.js +0 -0
- /package/{typescript → dist}/googlesheets/index.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/index.js +0 -0
- /package/{typescript → dist}/googlesheets/parseConnectionString.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/parseConnectionString.js +0 -0
- /package/{typescript → dist}/googlesheets/retry.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/retry.js +0 -0
- /package/{typescript → dist}/googlesheets/sqlExecutor.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/sqlExecutor.js +0 -0
- /package/{typescript → dist}/googlesheets/tableClient.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/tableClient.js +0 -0
- /package/{typescript → dist}/googlesheets/types.d.ts +0 -0
- /package/{typescript → dist}/googlesheets/types.js +0 -0
- /package/{typescript → dist}/mssql/engine.d.ts +0 -0
- /package/{typescript → dist}/mssql/engine.js +0 -0
- /package/{typescript → dist}/mssql/index.d.ts +0 -0
- /package/{typescript → dist}/mssql/index.js +0 -0
- /package/{typescript → dist}/mysql/engine.d.ts +0 -0
- /package/{typescript → dist}/mysql/engine.js +0 -0
- /package/{typescript → dist}/mysql/index.d.ts +0 -0
- /package/{typescript → dist}/mysql/index.js +0 -0
- /package/{typescript → dist}/postgres/engine.d.ts +0 -0
- /package/{typescript → dist}/postgres/engine.js +0 -0
- /package/{typescript → dist}/postgres/index.d.ts +0 -0
- /package/{typescript → dist}/postgres/index.js +0 -0
- /package/{typescript → dist}/sqlite/engine.d.ts +0 -0
- /package/{typescript → dist}/sqlite/engine.js +0 -0
- /package/{typescript → dist}/sqlite/index.d.ts +0 -0
- /package/{typescript → dist}/sqlite/index.js +0 -0
|
@@ -1,121 +1,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
|
-
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
|
+
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@an5/adapters",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Standalone language adapters for AN5 ORM - TypeScript, Python, .NET query execution.",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"main": "
|
|
7
|
-
"types": "
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
|
-
".": "./
|
|
10
|
-
"./browser": "./
|
|
11
|
-
"./
|
|
12
|
-
"./
|
|
9
|
+
".": "./dist/index.js",
|
|
10
|
+
"./browser": "./dist/browser.js",
|
|
11
|
+
"./config": "./dist/config.js",
|
|
12
|
+
"./base": "./dist/base/index.js",
|
|
13
|
+
"./base/*": "./dist/base/*",
|
|
14
|
+
"./mssql": "./dist/mssql/index.js",
|
|
15
|
+
"./postgres": "./dist/postgres/index.js",
|
|
16
|
+
"./mysql": "./dist/mysql/index.js",
|
|
17
|
+
"./sqlite": "./dist/sqlite/index.js",
|
|
18
|
+
"./googlesheets": "./dist/googlesheets/index.js",
|
|
19
|
+
"./googlesheets/*": "./dist/googlesheets/*",
|
|
20
|
+
"./typescript": "./dist/index.js",
|
|
21
|
+
"./typescript/*": "./dist/*",
|
|
13
22
|
"./python": "./python/an5_adapter.py",
|
|
14
|
-
"./dotnet": "./dotnet/An5Adapter.cs"
|
|
15
|
-
"./config": "./typescript/config.js"
|
|
23
|
+
"./dotnet": "./dotnet/An5Adapter.cs"
|
|
16
24
|
},
|
|
17
25
|
"author": "an5ORM",
|
|
18
26
|
"homepage": "https://github.com/an5ORM/an5",
|
|
@@ -29,14 +37,16 @@
|
|
|
29
37
|
"dotnet"
|
|
30
38
|
],
|
|
31
39
|
"files": [
|
|
32
|
-
"
|
|
40
|
+
"dist/**/*.{js,d.ts}",
|
|
41
|
+
"typescript/src/**/*.ts",
|
|
33
42
|
"python/**/*.py",
|
|
34
43
|
"dotnet/**/*",
|
|
35
44
|
"README.md"
|
|
36
45
|
],
|
|
37
46
|
"scripts": {
|
|
38
47
|
"build": "tsc -p tsconfig.json",
|
|
39
|
-
"
|
|
48
|
+
"build:python": "python -m build --outdir dist-py",
|
|
49
|
+
"test": "node test/unit.test.js && node test/package.test.js",
|
|
40
50
|
"test:python": "python -m compileall python 2>&1"
|
|
41
51
|
},
|
|
42
52
|
"dependencies": {
|
package/python/an5_adapter.py
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
"""Standalone Python runtime adapter for AN5 ORM."""
|
|
2
|
-
|
|
3
|
-
from typing import Dict, List, Optional
|
|
4
|
-
|
|
5
|
-
try:
|
|
6
|
-
from .base import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect, set_adapter_metadata
|
|
7
|
-
from .mssql import connect as connect_mssql
|
|
8
|
-
from .postgres import connect as connect_postgres
|
|
9
|
-
from .table_client import AdapterTableClient
|
|
10
|
-
except ImportError:
|
|
11
|
-
from base import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect, set_adapter_metadata
|
|
12
|
-
from mssql import connect as connect_mssql
|
|
13
|
-
from postgres import connect as connect_postgres
|
|
14
|
-
from table_client import AdapterTableClient
|
|
15
|
-
|
|
16
|
-
# Backward-compatible aliases used by tests and older imports.
|
|
17
|
-
_detect_dialect = detect_dialect
|
|
18
|
-
try:
|
|
19
|
-
from .mssql import parse_connection_string as _parse_connection_string
|
|
20
|
-
except ImportError:
|
|
21
|
-
from mssql import parse_connection_string as _parse_connection_string
|
|
22
|
-
|
|
23
|
-
class An5Adapter:
|
|
24
|
-
def __init__(self, connection_string: str):
|
|
25
|
-
self._dialect = detect_dialect(connection_string)
|
|
26
|
-
self._conn_str = connection_string
|
|
27
|
-
|
|
28
|
-
def _connect(self):
|
|
29
|
-
if self._dialect == DIALECT_POSTGRES:
|
|
30
|
-
return connect_postgres(self._conn_str)
|
|
31
|
-
return connect_mssql(self._conn_str)
|
|
32
|
-
|
|
33
|
-
def _to_dicts(self, cursor, query: str) -> List[Dict]:
|
|
34
|
-
if cursor.description:
|
|
35
|
-
cols = [col[0] for col in cursor.description]
|
|
36
|
-
rows = cursor.fetchall() if cursor.description else []
|
|
37
|
-
return [dict(zip(cols, row)) for row in rows]
|
|
38
|
-
return []
|
|
39
|
-
|
|
40
|
-
def exec(self, query: str, params: Optional[List] = None) -> List[Dict]:
|
|
41
|
-
conn = self._connect()
|
|
42
|
-
try:
|
|
43
|
-
cursor = conn.cursor()
|
|
44
|
-
cursor.execute(query, params or [])
|
|
45
|
-
return self._to_dicts(cursor, query)
|
|
46
|
-
finally:
|
|
47
|
-
conn.close()
|
|
48
|
-
|
|
49
|
-
def execute(self, query: str, params: Optional[List] = None) -> int:
|
|
50
|
-
conn = self._connect()
|
|
51
|
-
try:
|
|
52
|
-
cursor = conn.cursor()
|
|
53
|
-
cursor.execute(query, params or [])
|
|
54
|
-
return cursor.rowcount
|
|
55
|
-
finally:
|
|
56
|
-
conn.close()
|
|
57
|
-
|
|
58
|
-
def query_raw(self, query: str, *values) -> List[Dict]:
|
|
59
|
-
return self.exec(query, list(values))
|
|
60
|
-
|
|
61
|
-
def execute_raw(self, query: str, *values) -> int:
|
|
62
|
-
return self.execute(query, list(values))
|
|
63
|
-
|
|
64
|
-
def table(self, model_name: str) -> AdapterTableClient:
|
|
65
|
-
return AdapterTableClient(self, model_name)
|
|
66
|
-
|
|
67
|
-
def __getattr__(self, model_name: str) -> AdapterTableClient:
|
|
68
|
-
return self.table(model_name)
|
|
69
|
-
|
|
70
|
-
def transaction(self, fn):
|
|
71
|
-
conn = self._connect()
|
|
72
|
-
conn.autocommit = False
|
|
73
|
-
try:
|
|
74
|
-
result = fn(self)
|
|
75
|
-
conn.commit()
|
|
76
|
-
return result
|
|
77
|
-
except Exception:
|
|
78
|
-
conn.rollback()
|
|
79
|
-
raise
|
|
80
|
-
finally:
|
|
81
|
-
if self._dialect == DIALECT_POSTGRES:
|
|
82
|
-
conn.autocommit = True
|
|
83
|
-
conn.close()
|
|
84
|
-
|
|
85
|
-
def create_an5_adapter(connection_string: str) -> An5Adapter:
|
|
86
|
-
return An5Adapter(connection_string)
|
|
87
|
-
|
|
88
|
-
__all__ = [
|
|
89
|
-
"An5Adapter",
|
|
90
|
-
"AdapterTableClient",
|
|
91
|
-
"create_an5_adapter",
|
|
92
|
-
"DIALECT_MSSQL",
|
|
93
|
-
"DIALECT_POSTGRES",
|
|
94
|
-
"_detect_dialect",
|
|
95
|
-
"_parse_connection_string",
|
|
96
|
-
"set_adapter_metadata",
|
|
97
|
-
]
|
|
1
|
+
"""Standalone Python runtime adapter for AN5 ORM."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, List, Optional
|
|
4
|
+
|
|
5
|
+
try:
|
|
6
|
+
from .base import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect, set_adapter_metadata
|
|
7
|
+
from .mssql import connect as connect_mssql
|
|
8
|
+
from .postgres import connect as connect_postgres
|
|
9
|
+
from .table_client import AdapterTableClient
|
|
10
|
+
except ImportError:
|
|
11
|
+
from base import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect, set_adapter_metadata
|
|
12
|
+
from mssql import connect as connect_mssql
|
|
13
|
+
from postgres import connect as connect_postgres
|
|
14
|
+
from table_client import AdapterTableClient
|
|
15
|
+
|
|
16
|
+
# Backward-compatible aliases used by tests and older imports.
|
|
17
|
+
_detect_dialect = detect_dialect
|
|
18
|
+
try:
|
|
19
|
+
from .mssql import parse_connection_string as _parse_connection_string
|
|
20
|
+
except ImportError:
|
|
21
|
+
from mssql import parse_connection_string as _parse_connection_string
|
|
22
|
+
|
|
23
|
+
class An5Adapter:
|
|
24
|
+
def __init__(self, connection_string: str):
|
|
25
|
+
self._dialect = detect_dialect(connection_string)
|
|
26
|
+
self._conn_str = connection_string
|
|
27
|
+
|
|
28
|
+
def _connect(self):
|
|
29
|
+
if self._dialect == DIALECT_POSTGRES:
|
|
30
|
+
return connect_postgres(self._conn_str)
|
|
31
|
+
return connect_mssql(self._conn_str)
|
|
32
|
+
|
|
33
|
+
def _to_dicts(self, cursor, query: str) -> List[Dict]:
|
|
34
|
+
if cursor.description:
|
|
35
|
+
cols = [col[0] for col in cursor.description]
|
|
36
|
+
rows = cursor.fetchall() if cursor.description else []
|
|
37
|
+
return [dict(zip(cols, row)) for row in rows]
|
|
38
|
+
return []
|
|
39
|
+
|
|
40
|
+
def exec(self, query: str, params: Optional[List] = None) -> List[Dict]:
|
|
41
|
+
conn = self._connect()
|
|
42
|
+
try:
|
|
43
|
+
cursor = conn.cursor()
|
|
44
|
+
cursor.execute(query, params or [])
|
|
45
|
+
return self._to_dicts(cursor, query)
|
|
46
|
+
finally:
|
|
47
|
+
conn.close()
|
|
48
|
+
|
|
49
|
+
def execute(self, query: str, params: Optional[List] = None) -> int:
|
|
50
|
+
conn = self._connect()
|
|
51
|
+
try:
|
|
52
|
+
cursor = conn.cursor()
|
|
53
|
+
cursor.execute(query, params or [])
|
|
54
|
+
return cursor.rowcount
|
|
55
|
+
finally:
|
|
56
|
+
conn.close()
|
|
57
|
+
|
|
58
|
+
def query_raw(self, query: str, *values) -> List[Dict]:
|
|
59
|
+
return self.exec(query, list(values))
|
|
60
|
+
|
|
61
|
+
def execute_raw(self, query: str, *values) -> int:
|
|
62
|
+
return self.execute(query, list(values))
|
|
63
|
+
|
|
64
|
+
def table(self, model_name: str) -> AdapterTableClient:
|
|
65
|
+
return AdapterTableClient(self, model_name)
|
|
66
|
+
|
|
67
|
+
def __getattr__(self, model_name: str) -> AdapterTableClient:
|
|
68
|
+
return self.table(model_name)
|
|
69
|
+
|
|
70
|
+
def transaction(self, fn):
|
|
71
|
+
conn = self._connect()
|
|
72
|
+
conn.autocommit = False
|
|
73
|
+
try:
|
|
74
|
+
result = fn(self)
|
|
75
|
+
conn.commit()
|
|
76
|
+
return result
|
|
77
|
+
except Exception:
|
|
78
|
+
conn.rollback()
|
|
79
|
+
raise
|
|
80
|
+
finally:
|
|
81
|
+
if self._dialect == DIALECT_POSTGRES:
|
|
82
|
+
conn.autocommit = True
|
|
83
|
+
conn.close()
|
|
84
|
+
|
|
85
|
+
def create_an5_adapter(connection_string: str) -> An5Adapter:
|
|
86
|
+
return An5Adapter(connection_string)
|
|
87
|
+
|
|
88
|
+
__all__ = [
|
|
89
|
+
"An5Adapter",
|
|
90
|
+
"AdapterTableClient",
|
|
91
|
+
"create_an5_adapter",
|
|
92
|
+
"DIALECT_MSSQL",
|
|
93
|
+
"DIALECT_POSTGRES",
|
|
94
|
+
"_detect_dialect",
|
|
95
|
+
"_parse_connection_string",
|
|
96
|
+
"set_adapter_metadata",
|
|
97
|
+
]
|
package/python/base/__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
from .dialects import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect
|
|
2
|
-
from .metadata import model_to_table, model_fields, set_adapter_metadata
|
|
3
|
-
from .sql import _quote, _parse_where, _build_order_by, _resolve_table, _quote_table
|
|
1
|
+
from .dialects import DIALECT_MSSQL, DIALECT_POSTGRES, detect_dialect
|
|
2
|
+
from .metadata import model_to_table, model_fields, set_adapter_metadata
|
|
3
|
+
from .sql import _quote, _parse_where, _build_order_by, _resolve_table, _quote_table
|
package/python/base/dialects.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
DIALECT_MSSQL = "mssql"
|
|
2
|
-
DIALECT_POSTGRES = "postgres"
|
|
3
|
-
|
|
4
|
-
def detect_dialect(connection_string: str) -> str:
|
|
5
|
-
cs = connection_string.strip().lower()
|
|
6
|
-
if cs.startswith("postgres://") or cs.startswith("postgresql://"):
|
|
7
|
-
return DIALECT_POSTGRES
|
|
8
|
-
return DIALECT_MSSQL
|
|
1
|
+
DIALECT_MSSQL = "mssql"
|
|
2
|
+
DIALECT_POSTGRES = "postgres"
|
|
3
|
+
|
|
4
|
+
def detect_dialect(connection_string: str) -> str:
|
|
5
|
+
cs = connection_string.strip().lower()
|
|
6
|
+
if cs.startswith("postgres://") or cs.startswith("postgresql://"):
|
|
7
|
+
return DIALECT_POSTGRES
|
|
8
|
+
return DIALECT_MSSQL
|
package/python/base/metadata.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
from typing import Any, Dict
|
|
2
|
-
|
|
3
|
-
model_to_table: Dict[str, str] = {}
|
|
4
|
-
model_fields: Dict[str, Any] = {}
|
|
5
|
-
|
|
6
|
-
def set_adapter_metadata(metadata: Dict[str, Any]) -> None:
|
|
7
|
-
model_to_table.clear()
|
|
8
|
-
model_to_table.update(metadata.get("model_to_table") or metadata.get("modelToTable") or {})
|
|
9
|
-
model_fields.clear()
|
|
10
|
-
model_fields.update(metadata.get("model_fields") or metadata.get("modelFields") or {})
|
|
1
|
+
from typing import Any, Dict
|
|
2
|
+
|
|
3
|
+
model_to_table: Dict[str, str] = {}
|
|
4
|
+
model_fields: Dict[str, Any] = {}
|
|
5
|
+
|
|
6
|
+
def set_adapter_metadata(metadata: Dict[str, Any]) -> None:
|
|
7
|
+
model_to_table.clear()
|
|
8
|
+
model_to_table.update(metadata.get("model_to_table") or metadata.get("modelToTable") or {})
|
|
9
|
+
model_fields.clear()
|
|
10
|
+
model_fields.update(metadata.get("model_fields") or metadata.get("modelFields") or {})
|