Last active
February 3, 2023 18:22
-
-
Save jingyuexing/1b93d6979073e32c5f64613313f49600 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional,Any,Literal | |
def object_item(target:object): | |
for key in target.__dict__: | |
value = getattr(target,key) | |
if(key.find("__") == -1): | |
yield (key,value) | |
def type_convert(var:Any): | |
SQLType = { | |
"str":"varchar", | |
"int":"int", | |
"float":"float", | |
"bool":"" | |
} | |
return SQLType[type(var).__name__] | |
def getClassName(cls): | |
return cls.__class__.__name__ | |
def generator_sql(obj): | |
table = [] | |
if(not hasattr(obj,"__class__")): | |
raise TypeError("not a class") | |
table_name = getClassName(obj) | |
for key,value in object_item(obj): | |
col = [] | |
col.append(key) | |
col.append(type_convert(value)) | |
table.append(" ".join(col)) | |
table_sql = "{}({})".format(table_name,",".join(table)) | |
return table_sql + ";" | |
def table_with_column(obj): | |
table = getClassName(obj) | |
col = [] | |
for key,_ in object_item(obj): | |
col.append(key) | |
return "{}({})".format(table,",".join(col)) | |
def generator_params(obj:Any): | |
expression = ["insert into"] | |
params = [] | |
for _ in object_item(obj): | |
params.append("?") | |
params_str = ",".join(params) | |
expression.append(table_with_column(obj)) | |
expression.append("values") | |
expression.append("({})".format(params_str)) | |
return " ".join(expression) | |
def params_to_value(obj): | |
params_str = generator_params(obj=obj) | |
for _,value in object_item(obj): | |
params_str = params_str.replace("?",str(value),1) | |
return params_str+";" | |
def Where(WhereType:Literal["or","and"]="or",**kws): | |
expression = ["where"] | |
selector = [] | |
for key,value in kws.items(): | |
selector.append("{}={}".format(key,value)) | |
expression.append(f" {WhereType} ".join(selector)) | |
return " ".join(expression)+";" | |
def Select(obj,all=False): | |
expression = ["select"] | |
col = [] | |
table = getClassName(obj) | |
value_map = {} | |
if not all: | |
for key,value in object_item(obj): | |
col.append(key) | |
value_map[key] = value | |
expression.append("({})".format(",".join(col))) | |
else: | |
expression.append("*") | |
expression.append("from") | |
expression.append(table) | |
return " ".join(expression) | |
if __name__ == "__main__": | |
from pydantic.dataclasses import dataclass | |
@dataclass | |
class User: | |
username:Optional[str] = "" | |
age:Optional[int] = 0 | |
phone:Optional[str] = "" | |
user = User(username="张三",age=33) | |
print(generator_params(user)) | |
print(params_to_value(user)) | |
print(Where(some=12,rep="jim")) | |
print(Select(user)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment