Skip to content

Commit

Permalink
feat[tidb]:creae
Browse files Browse the repository at this point in the history
  • Loading branch information
fishTsai20 committed Jan 18, 2024
1 parent 2bcb45c commit c9713d3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
var (
inputfile1 string
inputfile2 string
outputfile string
//outputfile string
)

func init() {
flag.StringVar(&inputfile1, "transactions", "", "the filename of input data, default(./data/transactions.input.txt)")
flag.StringVar(&inputfile2, "logs", "", "the filename of input data, default(./data/logs.input.txt)")
flag.StringVar(&outputfile, "output", "./data/asc20.output.txt", "the filename of output result, default(./data/asc20.output.txt)")
//flag.StringVar(&outputfile, "output", "", "the filename of output result, default(./data/asc20.output.txt)")

flag.Parse()

Expand Down
16 changes: 6 additions & 10 deletions connector/tidb/datasource_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ var tblCreateSqlMap = make(map[string]string)
var initFilePath = "./data/init/"

func init() {
tblCreateSqlMap["token_info"] = initFilePath + "create_token_info.sql"
tblCreateSqlMap["token_activities"] = initFilePath + "create_token_activities.sql"
tblCreateSqlMap["token_balances"] = initFilePath + "create_token_balances.sql"
tblCreateSqlMap["token_info"] = "CREATE TABLE IF NOT EXISTS `token_info` (\n `block_timestamp` datetime(3) NOT NULL COMMENT 'Timestamp of the block containing the inscription (matches block_timestamp in transactions table)',\n `block_number` bigint(20) NOT NULL COMMENT 'Block number containing the inscription (matches block_number in transactions table)',\n `tx_index` int(11) NOT NULL COMMENT 'Index of the transaction containing the inscription (matches transaction_index in transactions table)',\n `tx_hash` varchar(66) NOT NULL COMMENT 'Unique identifier of the transaction containing the inscription (matches hash in transactions table)',\n `tick` varchar(255) NOT NULL COMMENT 'Token tick',\n `max_supply` decimal(38, 0) DEFAULT NULL COMMENT 'Max supply',\n `lim` decimal(38, 0) DEFAULT NULL COMMENT 'Limit of each mint',\n `wlim` decimal(38, 0) DEFAULT NULL COMMENT 'Limit of each address can maximum mint',\n `dec` int(11) DEFAULT NULL COMMENT 'Decimal for minimum divie',\n `creator` varchar(42) DEFAULT NULL COMMENT 'Address originating the inscription (matches from_address in transactions table)',\n `minted` decimal(38, 0) DEFAULT '0',\n `holders` decimal(38, 0) DEFAULT '0',\n `txs` decimal(38, 0) DEFAULT '0',\n `updated_timestamp` timestamp(3) NULL DEFAULT NULL,\n `completed_timestamp` timestamp(3) NULL DEFAULT NULL,\n `id` varchar(255) DEFAULT NULL,\n PRIMARY KEY (`tick`)\n );\n"
tblCreateSqlMap["token_activities"] = "CREATE TABLE IF NOT EXISTS `token_activities` (\n `block_timestamp` datetime(3) NOT NULL COMMENT 'Timestamp of the block containing the inscription (matches block_timestamp in transactions table)',\n `block_number` bigint(20) NOT NULL COMMENT 'Block number containing the inscription (matches block_number in transactions table)',\n `tx_index` int(11) NOT NULL COMMENT 'Index of the transaction containing the inscription (matches transaction_index in transactions table)',\n `tx_hash` varchar(66) NOT NULL COMMENT 'Unique identifier of the transaction containing the inscription (matches hash in transactions table)',\n `log_index` int(11) NOT NULL COMMENT 'Index of the log within the transaction',\n `type` varchar(255) NOT NULL COMMENT 'mint transfer burn',\n `tick` varchar(255) NOT NULL COMMENT 'Token tick',\n `id` varchar(255) NOT NULL COMMENT 'Unique identifier of the inscription',\n `amt` decimal(38, 0) DEFAULT NULL COMMENT 'Mint amount',\n `from_address` varchar(42) DEFAULT NULL COMMENT 'Address sending the inscription (matches from_address in transactions table)',\n `to_address` varchar(42) DEFAULT NULL COMMENT 'Address receiving the inscription (match to_address in transactions table)',\n PRIMARY KEY (\n `id`, `log_index`, `tx_index`, `tick`,\n `type`\n )\n);\n"
tblCreateSqlMap["token_balances"] = "CREATE TABLE `token_balances` (\n `block_number` bigint(20) unsigned DEFAULT NULL COMMENT 'Block number containing the transaction',\n `block_timestamp` datetime(3) DEFAULT NULL COMMENT 'Block timestamp containing the transaction',\n `tick` varchar(255) NOT NULL COMMENT 'Token tick',\n `wallet_address` varchar(42) NOT NULL COMMENT 'Address of owner',\n `total_supply` decimal(38, 0) DEFAULT NULL COMMENT 'Max supply',\n `amount` decimal(38, 0) DEFAULT NULL COMMENT 'The balance of wallet balance at the corresponding block height',\n PRIMARY KEY (`tick`, `wallet_address`)\n);\n"
}

type Config struct {
Expand Down Expand Up @@ -79,7 +79,7 @@ func JudgeTableExistOrNot(db *gorm.DB, tableName string) (bool, error) {
func CreateTableIfNotExist[T any](db *gorm.DB, table T, tableName string) error {
exist, err := JudgeTableExistOrNot(db, tableName)
if !exist {
filePath, ok := tblCreateSqlMap[tableName]
fileSql, ok := tblCreateSqlMap[tableName]
if !ok {
tType := reflect.TypeOf(*new(T))
instance := reflect.New(tType).Interface()
Expand All @@ -89,12 +89,7 @@ func CreateTableIfNotExist[T any](db *gorm.DB, table T, tableName string) error
return err
}
} else {
content, err := os.ReadFile(filePath)
if err != nil {
log.Fatalf("Error reading SQL file: %v", err)
}
sql := string(content)
err = db.Exec(sql).Error
err = db.Exec(fileSql).Error
if err != nil {
log.Fatalf("Create table %s failed: %v", tableName, err)
return err
Expand Down Expand Up @@ -132,6 +127,7 @@ func Upsert[T any](db *gorm.DB, datas []T) error {
var logger = handlers.GetLogger()
for _, data := range datas {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&data).Error; err != nil {
logger.Fatalf("Upsert into db failed, %v", err)
return err
}
}
Expand Down

0 comments on commit c9713d3

Please sign in to comment.