Exemplo n.º 1
0
def create_table_def(name: str, props: card.TableDescription) -> str:
    sql = 'CREATE TABLE IF NOT EXISTS `{name}` ('
    sql += ', '.join(column_def(name, prop) for name, prop in props.items())
    fk = ', '.join(foreign_key_def(name, prop['foreign_key']) for name, prop in props.items() if prop['foreign_key'] is not None)
    uc = ', '.join(unique_constraint_def(name, prop['unique_with']) for name, prop in props.items() if prop['unique_with'] is not None)
    if fk:
        sql += ', ' + fk
    if uc:
        sql += ', ' + uc
    sql += ') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
    return sql.format(name=name)
Exemplo n.º 2
0
def insert_many(table: str,
                properties: TableDescription,
                values: List[Dict[str, Any]],
                additional_columns: Optional[List[str]] = None) -> None:
    columns = additional_columns or []
    columns += [k for k, v in properties.items() if v.get('foreign_key')]
    columns += [name for name, prop in properties.items() if prop['scryfall']]
    query = f'INSERT INTO `{table}` ('
    query += ', '.join(columns)
    query += ') VALUES ('
    query += '), ('.join(', '.join(
        str(sqlescape(entry[column])) for column in columns)
                         for entry in values)
    query += ')'
    db().execute(query)