Skip to content

Commit 9d5d273

Browse files
authored
zhars/update_mysql_python_extended_example (#524)
Updated python extended example
1 parent d9450f7 commit 9d5d273

File tree

6 files changed

+101
-43
lines changed

6 files changed

+101
-43
lines changed

examples/python/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def register_common_cli_params(parser):
1010
parser.add_argument('--db_name', type=str,
11-
default=get_default('db_name', 'acra'),
11+
default=get_default('db_name', 'test'),
1212
help='Database name')
1313
parser.add_argument('--db_user', type=str,
1414
default=get_default('db_user','test'),

examples/python/data.json

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
1-
[{
2-
"token_i32": 1234,
3-
"token_i64": 645664,
4-
"token_str": "078-05-1111",
5-
"token_bytes": "byt13es",
6-
"token_email": "[email protected]",
7-
"data": "John Wed, Senior Relationshop Manager",
8-
"masking": "$112000",
9-
"searchable": "[email protected]"
10-
},
11-
{
12-
"token_i32": 1235,
13-
"token_i64": 645665,
14-
"token_str": "078-05-1112",
15-
"token_bytes": "byt13es2",
16-
"token_email": "[email protected]",
17-
"data": "April Cassini, Marketing Manager",
18-
"masking": "$168000",
19-
"searchable": "[email protected]"
20-
},
21-
{
22-
"token_i32": 1236,
23-
"token_i64": 645667,
24-
"token_str": "078-05-1117",
25-
"token_bytes": "byt13es3",
26-
"token_email": "[email protected]",
27-
"data": "George Clooney, Famous Actor",
28-
"masking": "$780000",
29-
"searchable": "[email protected]"
30-
}]
1+
[
2+
{
3+
"token_i32": 1234,
4+
"data_i32": "1234",
5+
"token_i64": 645664,
6+
"data_i64": "645664",
7+
"token_str": "078-05-1111",
8+
"token_bytes": "byt13es",
9+
"token_email": "[email protected]",
10+
"data_str": "John Wed, Senior Relationshop Manager",
11+
"masking": "$112000",
12+
"searchable": "[email protected]"
13+
},
14+
{
15+
"token_i32": 1234,
16+
"data_i32": "1234",
17+
"token_i64": 645664,
18+
"data_i64": "645664",
19+
"token_str": "078-05-1112",
20+
"token_bytes": "byt13es2",
21+
"token_email": "[email protected]",
22+
"data_str": "April Cassini, Marketing Manager",
23+
"masking": "$168000",
24+
"searchable": "[email protected]"
25+
},
26+
{
27+
"token_i32": 1234,
28+
"data_i32": "1234",
29+
"token_i64": 645664,
30+
"data_i64": "645664",
31+
"token_str": "078-05-1117",
32+
"token_bytes": "byt13es3",
33+
"token_email": "[email protected]",
34+
"data_str": "George Clooney, Famous Actor",
35+
"masking": "$780000",
36+
"searchable": "[email protected]"
37+
}
38+
]

examples/python/extended_encryptor_config_with_zone.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ schemas:
55
- table: test
66
columns:
77
- id
8-
- data
8+
- data_str
99
- masking
1010
- token_i32
11+
- data_i32
1112
- token_i64
13+
- data_i64
1214
- token_str
1315
- token_bytes
1416
- token_email
1517
encrypted:
1618
- column: data
1719
zone_id: DDDDDDDDHHNqiSYFXkpxopYZ
20+
data_type: "str"
1821
- column: masking
1922
masking: "xxxx"
2023
plaintext_length: 3
@@ -24,10 +27,16 @@ schemas:
2427
token_type: int32
2528
tokenized: true
2629
zone_id: DDDDDDDDHHNqiSYFXkpxopYZ
30+
- column: data_i32
31+
data_type: "int32"
32+
zone_id: DDDDDDDDHHNqiSYFXkpxopYZ
2733
- column: token_i64
2834
token_type: int64
2935
tokenized: true
3036
zone_id: DDDDDDDDHHNqiSYFXkpxopYZ
37+
- column: data_i64
38+
data_type: "int64"
39+
zone_id: DDDDDDDDHHNqiSYFXkpxopYZ
3140
- column: token_bytes
3241
token_type: bytes
3342
tokenized: true

examples/python/extended_encryptor_config_without_zone.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@ schemas:
55
- table: test
66
columns:
77
- id
8-
- data
8+
- data_str
99
- masking
1010
- token_i32
11+
- data_i32
1112
- token_i64
13+
- data_i64
1214
- token_str
1315
- token_bytes
1416
- token_email
1517
encrypted:
16-
- column: data
18+
- column: data_str
19+
data_type: "str"
1720
- column: masking
1821
masking: "xxxx"
1922
plaintext_length: 3
2023
plaintext_side: "left"
24+
data_type: "str"
2125
- column: token_i32
2226
token_type: int32
2327
tokenized: true
28+
- column: data_i32
29+
data_type: "int32"
2430
- column: token_i64
2531
token_type: int64
2632
tokenized: true
33+
- column: data_i64
34+
data_type: "int64"
2735
- column: token_bytes
2836
token_type: bytes
2937
tokenized: true

examples/python/extended_example_with_zone.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,36 @@
1616
import json
1717
import ssl
1818

19+
from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, BigInteger, literal)
1920
from sqlalchemy.dialects import postgresql
2021

21-
from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, BigInteger, literal)
2222
from common import get_engine, get_default, get_zone, register_common_cli_params
2323

2424
metadata = MetaData()
2525
test_table = Table(
26+
'test', MetaData(),
27+
Column('id', Integer, primary_key=True, nullable=False),
28+
Column('data_str', Text, nullable=True),
29+
Column('masking', LargeBinary, nullable=True),
30+
Column('token_i32', Integer, nullable=True),
31+
Column('data_i32', Integer, nullable=True),
32+
Column('token_i64', BigInteger, nullable=True),
33+
Column('data_i64', BigInteger, nullable=True),
34+
Column('token_str', Text, nullable=True),
35+
Column('token_bytes', LargeBinary, nullable=True),
36+
Column('token_email', Text, nullable=True),
37+
)
38+
39+
# _schema_test_table used to generate table in the database with binary column types
40+
_schema_test_table = Table(
2641
'test', metadata,
2742
Column('id', Integer, primary_key=True, nullable=False),
28-
Column('data', LargeBinary, nullable=True),
43+
Column('data_str', LargeBinary, nullable=True),
2944
Column('masking', LargeBinary, nullable=True),
3045
Column('token_i32', Integer, nullable=True),
46+
Column('data_i32', LargeBinary, nullable=True),
3147
Column('token_i64', BigInteger, nullable=True),
48+
Column('data_i64', LargeBinary, nullable=True),
3249
Column('token_str', Text, nullable=True),
3350
Column('token_bytes', LargeBinary, nullable=True),
3451
Column('token_email', Text, nullable=True),
@@ -51,7 +68,7 @@ def print_data(connection, zone_id, columns):
5168
query = select(table_columns)
5269
extra_columns = [i.name for i in test_table.columns if i.name not in default_columns]
5370
except AttributeError:
54-
print("\n\n{0}\nprobably you used incorrect column name\n{0}\n\n".format('*'*30))
71+
print("\n\n{0}\nprobably you used incorrect column name\n{0}\n\n".format('*' * 30))
5572
raise
5673
exit(1)
5774

@@ -82,7 +99,7 @@ def write_data(data, connection):
8299
if isinstance(data, dict):
83100
rows = [data]
84101
for row in rows:
85-
for k in ('data', 'token_bytes', 'masking'):
102+
for k in ('data_str', 'data_i64', 'data_i32', 'email', 'token_bytes', 'masking'):
86103
row[k] = row[k].encode('ascii')
87104
connection.execute(
88105
test_table.insert(), row)

examples/python/extended_example_without_zone.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,35 @@
1515
import argparse
1616
import json
1717

18-
from sqlalchemy.dialects import postgresql
1918
from sqlalchemy import (Table, Column, Integer, MetaData, select, LargeBinary, Text, BigInteger)
20-
from common import get_engine, get_default, register_common_cli_params
19+
from sqlalchemy.dialects import postgresql
2120

21+
from common import get_engine, get_default, register_common_cli_params
2222

2323
metadata = MetaData()
2424
test_table = Table(
25+
'test', MetaData(),
26+
Column('id', Integer, primary_key=True, nullable=False),
27+
Column('data_str', Text, nullable=True),
28+
Column('masking', LargeBinary, nullable=True),
29+
Column('token_i32', Integer, nullable=True),
30+
Column('data_i32', Integer, nullable=True),
31+
Column('token_i64', BigInteger, nullable=True),
32+
Column('data_i64', BigInteger, nullable=True),
33+
Column('token_str', Text, nullable=True),
34+
Column('token_bytes', LargeBinary, nullable=True),
35+
Column('token_email', Text, nullable=True),
36+
)
37+
# _schema_test_table used to generate table in the database with binary column types
38+
_schema_test_table = Table(
2539
'test', metadata,
2640
Column('id', Integer, primary_key=True, nullable=False),
27-
Column('data', LargeBinary, nullable=True),
41+
Column('data_str', LargeBinary, nullable=True),
2842
Column('masking', LargeBinary, nullable=True),
2943
Column('token_i32', Integer, nullable=True),
44+
Column('data_i32', LargeBinary, nullable=True),
3045
Column('token_i64', BigInteger, nullable=True),
46+
Column('data_i64', LargeBinary, nullable=True),
3147
Column('token_str', Text, nullable=True),
3248
Column('token_bytes', LargeBinary, nullable=True),
3349
Column('token_email', Text, nullable=True),
@@ -61,7 +77,7 @@ def print_data(connection, columns, table=test_table):
6177
query = select(table_columns)
6278
extra_columns = [i.name for i in table.columns if i.name not in default_columns]
6379
except AttributeError:
64-
print("\n\n{0}\nprobably you used incorrect column name\n{0}\n\n".format('*'*30))
80+
print("\n\n{0}\nprobably you used incorrect column name\n{0}\n\n".format('*' * 30))
6581
raise
6682
exit(1)
6783

@@ -92,7 +108,7 @@ def write_data(data, connection, table=test_table):
92108
if isinstance(data, dict):
93109
rows = [data]
94110
for row in rows:
95-
for k in ('data', 'email', 'token_bytes', 'masking'):
111+
for k in ('data_str', 'data_i64', 'data_i32', 'email', 'token_bytes', 'masking'):
96112
if k in row:
97113
row[k] = row[k].encode('ascii')
98114
connection.execute(

0 commit comments

Comments
 (0)