-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
Use https://mermaid.live for generate SVG. | ||
|
||
```mermaid | ||
graph TD | ||
first("SQL Query<br><br> | ||
select * from orders<br> | ||
where user_id = ? and status = ?<br> | ||
limit 10 order by id desc<br> | ||
args = [100, 1]") | ||
first--->| Gorm Query |db["Gorm DB"] | ||
subgraph "Gorm" | ||
db-->gorm_query | ||
gorm_query["connPool.QueryContext(sql, args)<br>"] | ||
end | ||
subgraph "database/sql - Conn" | ||
ExecContext[/"ExecContext"/] | ||
QueryContext[/"QueryContext"/] | ||
QueryRowContext[/"QueryRowContext"/] | ||
gorm_query-->Conn | ||
Conn(["Conn"]) | ||
Conn-->ExecContext | ||
Conn-->QueryContext | ||
Conn-->QueryRowContext | ||
end | ||
subgraph sharding ["MyConnPool"] | ||
QueryContext-->router-->format_sql-->parse-->check_table | ||
router[["router(sql, args)<br><br>"]] | ||
format_sql>"Format sql, args for get full SQL<br><br> | ||
sql = select * from orders<br> | ||
where user_id = 100 and status = 1<br> | ||
limit 10 order by id desc"] | ||
check_table{"Check sharding rules<br>by table name"} | ||
check_table-->| Exist |process_ast | ||
check_table_1{{"Return Raw SQL"}} | ||
not_match_error[/"Return Error<br>SQL query must has sharding key"\] | ||
parse[["Parser SQL to get AST<br> | ||
<br> | ||
ast = sqlparser.Parse(sql)"]] | ||
check_table-.->| Not exist |check_table_1 | ||
process_ast(("Sharding rules")) | ||
get_new_table_name[["Use value in WhereValue (100) for get sharding table index<br>orders + (100 % 16)<br>Sharding Table = orders_4"]] | ||
new_sql{{"select * from orders_4<br>where user_id = 100 and status = 1<br>limit 10 order by id desc"}} | ||
process_ast-.->| Not match ShardingKey |not_match_error | ||
process_ast-->| Match ShardingKey |match_sharding_key-->| Get table name |get_new_table_name-->| Replace TableName to get new SQL |new_sql | ||
end | ||
subgraph database [Database] | ||
posts_other[("orders_0, orders_1 ... orders_3")] | ||
posts_4[(orders_4)] | ||
posts_last[("orders_5 ... orders_15")] | ||
other_tables[(Other non-sharding tables<br>users, stocks, topics ...)] | ||
new_sql-->| Sharding Query | orders_4 | ||
check_table_1-.->| None sharding Query |other_tables | ||
end | ||
posts_4-->result | ||
other_tables-.->result | ||
result[/Query results\] | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.