Skip to content

Commit

Permalink
Feature/enhance (#59)
Browse files Browse the repository at this point in the history
* feat: support show for _post_parsed field

* fix: post_parsed return json value

* fmt

* feat: support for vector; quick mode for geo && vector data for dump

* feat: disable ryu

* feat: dp

---------

Co-authored-by: yongcai <[email protected]>
  • Loading branch information
WinChua and yongcai authored Dec 29, 2024
1 parent 480930b commit d5aced1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
30 changes: 27 additions & 3 deletions devtools/deploy_mysqld.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from dataclasses import dataclass, asdict
from testcontainers.mysql import MySqlContainer
from docker.models.containers import Container
from testcontainers.core.config import testcontainers_config as c

from sqlalchemy import create_engine

c.ryuk_disabled = True


@click.group()
Expand Down Expand Up @@ -80,7 +85,6 @@ def mDeploy(version):
)
return

os.environ["TESTCONTAINERS_RYUK_DISABLED"] = "true"
mContainer = MySqlContainer(f"mysql:{version}")
datadir = os.getcwd() + f"/datadir/{version}"
mContainer.with_volume_mapping(datadir, "/var/lib/mysql", "rw")
Expand All @@ -105,14 +109,34 @@ def deploy(version):

@main.command()
@click.option("--version", type=click.STRING, default="8.0.17")
def connect(version):
@click.option("--sql", type=click.STRING, default="")
def connect(version, sql):
deploy_container = load_deploy()
if version not in deploy_container:
mDeploy(version)
deploy_container = load_deploy()

os.system(deploy_container.get(version).cmd)
if sql == "":
os.system(deploy_container.get(version).cmd)
else:
os.system(deploy_container.get(version).cmd + f" -e '{sql}'")

@main.command()
@click.option("--version", type=click.STRING, default="")
@click.option("--sql", type=click.STRING, default="")
@click.option("--file", type=click.STRING, default="")
def exec(version, sql, file):
deploy_container = load_deploy()
if version not in deploy_container:
mDeploy(version)
deploy_container = load_deploy()
engine = create_engine(deploy_container.get(version).url)
if file != "":
with open(file, "r") as f:
sql = f.read()
with engine.connect() as conn:
result = conn.exec_driver_sql(sql)
print(result.all()[0][1])

if __name__ == "__main__":
main()
12 changes: 8 additions & 4 deletions src/pyinnodb/cli/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
@click.pass_context
@click.option("--mode", type=click.Choice(["sdi", "ddl", "dump"]), default="ddl")
@click.option("--sdi-idx", type=click.INT, default=0)
def tosql(ctx, mode, sdi_idx):
@click.option("--schema/--no-schema", default=True)
def tosql(ctx, mode, sdi_idx, schema):
"""dump the ddl/dml/sdi of the ibd table
ddl) output the create table ddl;
Expand All @@ -35,7 +36,10 @@ def tosql(ctx, mode, sdi_idx):
elif mode == "ddl":
table_object = Table(**sdi_page.ddl(f, sdi_idx)["dd_object"])

table_name = f"`{table_object.schema_ref}`.`{table_object.name}`"
if schema:
table_name = f"`{table_object.schema_ref}`.`{table_object.name}`"
else:
table_name = f"`{table_object.name}`"
columns_dec = []
for c in table_object.columns:
if (
Expand All @@ -55,7 +59,7 @@ def tosql(ctx, mode, sdi_idx):
columns_dec.extend(constraints)
foreign_keys = table_object.gen_foreign_key()
columns_dec.extend(foreign_keys)
columns_dec = "\n " + ",\n ".join(columns_dec) + "\n"
columns_dec = "\n " + ",\n ".join(columns_dec) + "\n"
table_collation = const.get_collation_by_id(table_object.collation_id)
parts = table_object.gen_sql_for_partition()
desc = f"ENGINE={table_object.engine} DEFAULT CHARSET={table_collation.CHARACTER_SET_NAME} COLLATE={table_collation.COLLATION_NAME}"
Expand All @@ -65,7 +69,7 @@ def tosql(ctx, mode, sdi_idx):
else ""
)
print(
f"CREATE TABLE {table_name} ({columns_dec}) {desc} {chr(10)+parts if parts else ''}{comment}"
f"CREATE TABLE {table_name} ({columns_dec}) {desc}{parts}{comment}"
)
else:
table_object = Table(**sdi_page.ddl(f, sdi_idx)["dd_object"])
Expand Down
15 changes: 15 additions & 0 deletions src/pyinnodb/const/dd_column_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def is_var(cls, t, mysqld_version=None):
else:
return True

@classmethod
def is_string(cls, t):
tt = cls(t)
return tt in _string_type

@classmethod
def is_big(cls, t):
return cls(t) in _big_type
Expand Down Expand Up @@ -92,6 +97,16 @@ def is_big(cls, t):
DDColumnType.VECTOR,
]

_string_type = [
DDColumnType.VARCHAR,
DDColumnType.STRING,
DDColumnType.VAR_STRING,
DDColumnType.BLOB,
DDColumnType.MEDIUM_BLOB,
DDColumnType.LONG_BLOB,
DDColumnType.TINY_BLOB,
]

_big_type = [
DDColumnType.MEDIUM_BLOB,
DDColumnType.LONG_BLOB,
Expand Down
17 changes: 12 additions & 5 deletions src/pyinnodb/sdi/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ def get_collation(self):
return coll

def gen_sql(self):
sql = f"`{self.name}` {self.column_type_utf8}{'' if self.is_nullable or self.is_virtual else ' NOT NULL'}"
sql = f"`{self.name}` {self.column_type_utf8}"
if self.collation_id != 255 and self.collation_id != 63:
collation = const.get_collation_by_id(self.collation_id)
if DDColumnType.is_string(self.type):
pass
sql += f"{'' if self.is_nullable or self.is_virtual else ' NOT NULL'}"
sql += f"{' AUTO_INCREMENT' if self.is_auto_increment else ''}"
if self.default_option != "":
sql += f" DEFAULT ({self.default_option})"
Expand Down Expand Up @@ -1024,18 +1029,20 @@ def gen_sql_for_partition(self) -> str:
f"PARTITION {par.name} VALUES LESS THAN ({par.description_utf8})"
)
parts = ",\n ".join(parts) + "\n"
return f"{p}{parts})*/"
return "\n" + f"{p}{parts})*/"
elif pt == const.partition.PartitionType.PT_HASH:
return f"/*!50100 PARTITION BY HASH ({self.partition_expression_utf8}) PARTITIONS ({len(self.partitions)})*/"
return "\n" + f"/*!50100 PARTITION BY HASH ({self.partition_expression_utf8}) PARTITIONS ({len(self.partitions)})*/"
elif pt == const.partition.PartitionType.PT_KEY_55:
return f"/*!50100 PARTITION BY KEY ({self.partition_expression_utf8}) PARTITIONS ({len(self.partitions)})*/"
return "\n" + f"/*!50100 PARTITION BY KEY ({self.partition_expression_utf8}) PARTITIONS ({len(self.partitions)})*/"
elif pt == const.partition.PartitionType.PT_LIST:
p = f"/*!50100 PARTITION BY LIST ({self.partition_expression_utf8}) (\n "
parts = []
for par in self.partitions:
parts.append(f"PARTITION {par.name} VALUES IN ({par.description_utf8})")
parts = ",\n ".join(parts) + "\n"
return f"{p}{parts})*/"
return "\n" + f"{p}{parts})*/"
else:
return ""


def should_ext():
Expand Down

0 comments on commit d5aced1

Please sign in to comment.