diff --git a/vochain/indexer/db/blocks.sql.go b/vochain/indexer/db/blocks.sql.go index b932b6f3e..b12c8e9df 100644 --- a/vochain/indexer/db/blocks.sql.go +++ b/vochain/indexer/db/blocks.sql.go @@ -26,7 +26,7 @@ type CreateBlockParams struct { } func (q *Queries) CreateBlock(ctx context.Context, arg CreateBlockParams) (sql.Result, error) { - return q.db.ExecContext(ctx, createBlock, arg.Height, arg.Time, arg.DataHash) + return q.exec(ctx, q.createBlockStmt, createBlock, arg.Height, arg.Time, arg.DataHash) } const getBlock = `-- name: GetBlock :one @@ -36,7 +36,7 @@ LIMIT 1 ` func (q *Queries) GetBlock(ctx context.Context, height int64) (Block, error) { - row := q.db.QueryRowContext(ctx, getBlock, height) + row := q.queryRow(ctx, q.getBlockStmt, getBlock, height) var i Block err := row.Scan(&i.Height, &i.Time, &i.DataHash) return i, err diff --git a/vochain/indexer/db/db.go b/vochain/indexer/db/db.go index 14eb2cdee..63d4502e3 100644 --- a/vochain/indexer/db/db.go +++ b/vochain/indexer/db/db.go @@ -7,6 +7,7 @@ package indexerdb import ( "context" "database/sql" + "fmt" ) type DBTX interface { @@ -20,12 +21,358 @@ func New(db DBTX) *Queries { return &Queries{db: db} } +func Prepare(ctx context.Context, db DBTX) (*Queries, error) { + q := Queries{db: db} + var err error + if q.countTransactionsStmt, err = db.PrepareContext(ctx, countTransactions); err != nil { + return nil, fmt.Errorf("error preparing query CountTransactions: %w", err) + } + if q.countVotesStmt, err = db.PrepareContext(ctx, countVotes); err != nil { + return nil, fmt.Errorf("error preparing query CountVotes: %w", err) + } + if q.countVotesByProcessIDStmt, err = db.PrepareContext(ctx, countVotesByProcessID); err != nil { + return nil, fmt.Errorf("error preparing query CountVotesByProcessID: %w", err) + } + if q.createBlockStmt, err = db.PrepareContext(ctx, createBlock); err != nil { + return nil, fmt.Errorf("error preparing query CreateBlock: %w", err) + } + if q.createProcessStmt, err = db.PrepareContext(ctx, createProcess); err != nil { + return nil, fmt.Errorf("error preparing query CreateProcess: %w", err) + } + if q.createTokenTransferStmt, err = db.PrepareContext(ctx, createTokenTransfer); err != nil { + return nil, fmt.Errorf("error preparing query CreateTokenTransfer: %w", err) + } + if q.createTransactionStmt, err = db.PrepareContext(ctx, createTransaction); err != nil { + return nil, fmt.Errorf("error preparing query CreateTransaction: %w", err) + } + if q.createVoteStmt, err = db.PrepareContext(ctx, createVote); err != nil { + return nil, fmt.Errorf("error preparing query CreateVote: %w", err) + } + if q.getBlockStmt, err = db.PrepareContext(ctx, getBlock); err != nil { + return nil, fmt.Errorf("error preparing query GetBlock: %w", err) + } + if q.getEntityCountStmt, err = db.PrepareContext(ctx, getEntityCount); err != nil { + return nil, fmt.Errorf("error preparing query GetEntityCount: %w", err) + } + if q.getLastTransactionsStmt, err = db.PrepareContext(ctx, getLastTransactions); err != nil { + return nil, fmt.Errorf("error preparing query GetLastTransactions: %w", err) + } + if q.getProcessStmt, err = db.PrepareContext(ctx, getProcess); err != nil { + return nil, fmt.Errorf("error preparing query GetProcess: %w", err) + } + if q.getProcessCountStmt, err = db.PrepareContext(ctx, getProcessCount); err != nil { + return nil, fmt.Errorf("error preparing query GetProcessCount: %w", err) + } + if q.getProcessIDsByFinalResultsStmt, err = db.PrepareContext(ctx, getProcessIDsByFinalResults); err != nil { + return nil, fmt.Errorf("error preparing query GetProcessIDsByFinalResults: %w", err) + } + if q.getProcessStatusStmt, err = db.PrepareContext(ctx, getProcessStatus); err != nil { + return nil, fmt.Errorf("error preparing query GetProcessStatus: %w", err) + } + if q.getTokenTransferStmt, err = db.PrepareContext(ctx, getTokenTransfer); err != nil { + return nil, fmt.Errorf("error preparing query GetTokenTransfer: %w", err) + } + if q.getTokenTransfersByFromAccountStmt, err = db.PrepareContext(ctx, getTokenTransfersByFromAccount); err != nil { + return nil, fmt.Errorf("error preparing query GetTokenTransfersByFromAccount: %w", err) + } + if q.getTransactionStmt, err = db.PrepareContext(ctx, getTransaction); err != nil { + return nil, fmt.Errorf("error preparing query GetTransaction: %w", err) + } + if q.getTransactionByHashStmt, err = db.PrepareContext(ctx, getTransactionByHash); err != nil { + return nil, fmt.Errorf("error preparing query GetTransactionByHash: %w", err) + } + if q.getTxReferenceByBlockHeightAndBlockIndexStmt, err = db.PrepareContext(ctx, getTxReferenceByBlockHeightAndBlockIndex); err != nil { + return nil, fmt.Errorf("error preparing query GetTxReferenceByBlockHeightAndBlockIndex: %w", err) + } + if q.getVoteStmt, err = db.PrepareContext(ctx, getVote); err != nil { + return nil, fmt.Errorf("error preparing query GetVote: %w", err) + } + if q.searchEntitiesStmt, err = db.PrepareContext(ctx, searchEntities); err != nil { + return nil, fmt.Errorf("error preparing query SearchEntities: %w", err) + } + if q.searchProcessesStmt, err = db.PrepareContext(ctx, searchProcesses); err != nil { + return nil, fmt.Errorf("error preparing query SearchProcesses: %w", err) + } + if q.searchVotesStmt, err = db.PrepareContext(ctx, searchVotes); err != nil { + return nil, fmt.Errorf("error preparing query SearchVotes: %w", err) + } + if q.setProcessResultsCancelledStmt, err = db.PrepareContext(ctx, setProcessResultsCancelled); err != nil { + return nil, fmt.Errorf("error preparing query SetProcessResultsCancelled: %w", err) + } + if q.setProcessResultsReadyStmt, err = db.PrepareContext(ctx, setProcessResultsReady); err != nil { + return nil, fmt.Errorf("error preparing query SetProcessResultsReady: %w", err) + } + if q.updateProcessEndBlockStmt, err = db.PrepareContext(ctx, updateProcessEndBlock); err != nil { + return nil, fmt.Errorf("error preparing query UpdateProcessEndBlock: %w", err) + } + if q.updateProcessFromStateStmt, err = db.PrepareContext(ctx, updateProcessFromState); err != nil { + return nil, fmt.Errorf("error preparing query UpdateProcessFromState: %w", err) + } + if q.updateProcessResultByIDStmt, err = db.PrepareContext(ctx, updateProcessResultByID); err != nil { + return nil, fmt.Errorf("error preparing query UpdateProcessResultByID: %w", err) + } + if q.updateProcessResultsStmt, err = db.PrepareContext(ctx, updateProcessResults); err != nil { + return nil, fmt.Errorf("error preparing query UpdateProcessResults: %w", err) + } + return &q, nil +} + +func (q *Queries) Close() error { + var err error + if q.countTransactionsStmt != nil { + if cerr := q.countTransactionsStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing countTransactionsStmt: %w", cerr) + } + } + if q.countVotesStmt != nil { + if cerr := q.countVotesStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing countVotesStmt: %w", cerr) + } + } + if q.countVotesByProcessIDStmt != nil { + if cerr := q.countVotesByProcessIDStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing countVotesByProcessIDStmt: %w", cerr) + } + } + if q.createBlockStmt != nil { + if cerr := q.createBlockStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing createBlockStmt: %w", cerr) + } + } + if q.createProcessStmt != nil { + if cerr := q.createProcessStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing createProcessStmt: %w", cerr) + } + } + if q.createTokenTransferStmt != nil { + if cerr := q.createTokenTransferStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing createTokenTransferStmt: %w", cerr) + } + } + if q.createTransactionStmt != nil { + if cerr := q.createTransactionStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing createTransactionStmt: %w", cerr) + } + } + if q.createVoteStmt != nil { + if cerr := q.createVoteStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing createVoteStmt: %w", cerr) + } + } + if q.getBlockStmt != nil { + if cerr := q.getBlockStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getBlockStmt: %w", cerr) + } + } + if q.getEntityCountStmt != nil { + if cerr := q.getEntityCountStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getEntityCountStmt: %w", cerr) + } + } + if q.getLastTransactionsStmt != nil { + if cerr := q.getLastTransactionsStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getLastTransactionsStmt: %w", cerr) + } + } + if q.getProcessStmt != nil { + if cerr := q.getProcessStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getProcessStmt: %w", cerr) + } + } + if q.getProcessCountStmt != nil { + if cerr := q.getProcessCountStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getProcessCountStmt: %w", cerr) + } + } + if q.getProcessIDsByFinalResultsStmt != nil { + if cerr := q.getProcessIDsByFinalResultsStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getProcessIDsByFinalResultsStmt: %w", cerr) + } + } + if q.getProcessStatusStmt != nil { + if cerr := q.getProcessStatusStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getProcessStatusStmt: %w", cerr) + } + } + if q.getTokenTransferStmt != nil { + if cerr := q.getTokenTransferStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getTokenTransferStmt: %w", cerr) + } + } + if q.getTokenTransfersByFromAccountStmt != nil { + if cerr := q.getTokenTransfersByFromAccountStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getTokenTransfersByFromAccountStmt: %w", cerr) + } + } + if q.getTransactionStmt != nil { + if cerr := q.getTransactionStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getTransactionStmt: %w", cerr) + } + } + if q.getTransactionByHashStmt != nil { + if cerr := q.getTransactionByHashStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getTransactionByHashStmt: %w", cerr) + } + } + if q.getTxReferenceByBlockHeightAndBlockIndexStmt != nil { + if cerr := q.getTxReferenceByBlockHeightAndBlockIndexStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getTxReferenceByBlockHeightAndBlockIndexStmt: %w", cerr) + } + } + if q.getVoteStmt != nil { + if cerr := q.getVoteStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing getVoteStmt: %w", cerr) + } + } + if q.searchEntitiesStmt != nil { + if cerr := q.searchEntitiesStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing searchEntitiesStmt: %w", cerr) + } + } + if q.searchProcessesStmt != nil { + if cerr := q.searchProcessesStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing searchProcessesStmt: %w", cerr) + } + } + if q.searchVotesStmt != nil { + if cerr := q.searchVotesStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing searchVotesStmt: %w", cerr) + } + } + if q.setProcessResultsCancelledStmt != nil { + if cerr := q.setProcessResultsCancelledStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing setProcessResultsCancelledStmt: %w", cerr) + } + } + if q.setProcessResultsReadyStmt != nil { + if cerr := q.setProcessResultsReadyStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing setProcessResultsReadyStmt: %w", cerr) + } + } + if q.updateProcessEndBlockStmt != nil { + if cerr := q.updateProcessEndBlockStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing updateProcessEndBlockStmt: %w", cerr) + } + } + if q.updateProcessFromStateStmt != nil { + if cerr := q.updateProcessFromStateStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing updateProcessFromStateStmt: %w", cerr) + } + } + if q.updateProcessResultByIDStmt != nil { + if cerr := q.updateProcessResultByIDStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing updateProcessResultByIDStmt: %w", cerr) + } + } + if q.updateProcessResultsStmt != nil { + if cerr := q.updateProcessResultsStmt.Close(); cerr != nil { + err = fmt.Errorf("error closing updateProcessResultsStmt: %w", cerr) + } + } + return err +} + +func (q *Queries) exec(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (sql.Result, error) { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).ExecContext(ctx, args...) + case stmt != nil: + return stmt.ExecContext(ctx, args...) + default: + return q.db.ExecContext(ctx, query, args...) + } +} + +func (q *Queries) query(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) (*sql.Rows, error) { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).QueryContext(ctx, args...) + case stmt != nil: + return stmt.QueryContext(ctx, args...) + default: + return q.db.QueryContext(ctx, query, args...) + } +} + +func (q *Queries) queryRow(ctx context.Context, stmt *sql.Stmt, query string, args ...interface{}) *sql.Row { + switch { + case stmt != nil && q.tx != nil: + return q.tx.StmtContext(ctx, stmt).QueryRowContext(ctx, args...) + case stmt != nil: + return stmt.QueryRowContext(ctx, args...) + default: + return q.db.QueryRowContext(ctx, query, args...) + } +} + type Queries struct { - db DBTX + db DBTX + tx *sql.Tx + countTransactionsStmt *sql.Stmt + countVotesStmt *sql.Stmt + countVotesByProcessIDStmt *sql.Stmt + createBlockStmt *sql.Stmt + createProcessStmt *sql.Stmt + createTokenTransferStmt *sql.Stmt + createTransactionStmt *sql.Stmt + createVoteStmt *sql.Stmt + getBlockStmt *sql.Stmt + getEntityCountStmt *sql.Stmt + getLastTransactionsStmt *sql.Stmt + getProcessStmt *sql.Stmt + getProcessCountStmt *sql.Stmt + getProcessIDsByFinalResultsStmt *sql.Stmt + getProcessStatusStmt *sql.Stmt + getTokenTransferStmt *sql.Stmt + getTokenTransfersByFromAccountStmt *sql.Stmt + getTransactionStmt *sql.Stmt + getTransactionByHashStmt *sql.Stmt + getTxReferenceByBlockHeightAndBlockIndexStmt *sql.Stmt + getVoteStmt *sql.Stmt + searchEntitiesStmt *sql.Stmt + searchProcessesStmt *sql.Stmt + searchVotesStmt *sql.Stmt + setProcessResultsCancelledStmt *sql.Stmt + setProcessResultsReadyStmt *sql.Stmt + updateProcessEndBlockStmt *sql.Stmt + updateProcessFromStateStmt *sql.Stmt + updateProcessResultByIDStmt *sql.Stmt + updateProcessResultsStmt *sql.Stmt } func (q *Queries) WithTx(tx *sql.Tx) *Queries { return &Queries{ - db: tx, + db: tx, + tx: tx, + countTransactionsStmt: q.countTransactionsStmt, + countVotesStmt: q.countVotesStmt, + countVotesByProcessIDStmt: q.countVotesByProcessIDStmt, + createBlockStmt: q.createBlockStmt, + createProcessStmt: q.createProcessStmt, + createTokenTransferStmt: q.createTokenTransferStmt, + createTransactionStmt: q.createTransactionStmt, + createVoteStmt: q.createVoteStmt, + getBlockStmt: q.getBlockStmt, + getEntityCountStmt: q.getEntityCountStmt, + getLastTransactionsStmt: q.getLastTransactionsStmt, + getProcessStmt: q.getProcessStmt, + getProcessCountStmt: q.getProcessCountStmt, + getProcessIDsByFinalResultsStmt: q.getProcessIDsByFinalResultsStmt, + getProcessStatusStmt: q.getProcessStatusStmt, + getTokenTransferStmt: q.getTokenTransferStmt, + getTokenTransfersByFromAccountStmt: q.getTokenTransfersByFromAccountStmt, + getTransactionStmt: q.getTransactionStmt, + getTransactionByHashStmt: q.getTransactionByHashStmt, + getTxReferenceByBlockHeightAndBlockIndexStmt: q.getTxReferenceByBlockHeightAndBlockIndexStmt, + getVoteStmt: q.getVoteStmt, + searchEntitiesStmt: q.searchEntitiesStmt, + searchProcessesStmt: q.searchProcessesStmt, + searchVotesStmt: q.searchVotesStmt, + setProcessResultsCancelledStmt: q.setProcessResultsCancelledStmt, + setProcessResultsReadyStmt: q.setProcessResultsReadyStmt, + updateProcessEndBlockStmt: q.updateProcessEndBlockStmt, + updateProcessFromStateStmt: q.updateProcessFromStateStmt, + updateProcessResultByIDStmt: q.updateProcessResultByIDStmt, + updateProcessResultsStmt: q.updateProcessResultsStmt, } } diff --git a/vochain/indexer/db/processes.sql.go b/vochain/indexer/db/processes.sql.go index 18ee4d610..f230d3666 100644 --- a/vochain/indexer/db/processes.sql.go +++ b/vochain/indexer/db/processes.sql.go @@ -67,7 +67,7 @@ type CreateProcessParams struct { } func (q *Queries) CreateProcess(ctx context.Context, arg CreateProcessParams) (sql.Result, error) { - return q.db.ExecContext(ctx, createProcess, + return q.exec(ctx, q.createProcessStmt, createProcess, arg.ID, arg.EntityID, arg.StartBlock, @@ -100,7 +100,7 @@ SELECT COUNT(DISTINCT entity_id) FROM processes ` func (q *Queries) GetEntityCount(ctx context.Context) (int64, error) { - row := q.db.QueryRowContext(ctx, getEntityCount) + row := q.queryRow(ctx, q.getEntityCountStmt, getEntityCount) var count int64 err := row.Scan(&count) return count, err @@ -146,7 +146,7 @@ type GetProcessRow struct { } func (q *Queries) GetProcess(ctx context.Context, id types.ProcessID) (GetProcessRow, error) { - row := q.db.QueryRowContext(ctx, getProcess, id) + row := q.queryRow(ctx, q.getProcessStmt, getProcess, id) var i GetProcessRow err := row.Scan( &i.ID, @@ -185,7 +185,7 @@ SELECT COUNT(*) FROM processes ` func (q *Queries) GetProcessCount(ctx context.Context) (int64, error) { - row := q.db.QueryRowContext(ctx, getProcessCount) + row := q.queryRow(ctx, q.getProcessCountStmt, getProcessCount) var count int64 err := row.Scan(&count) return count, err @@ -199,7 +199,7 @@ WHERE final_results = ? ` func (q *Queries) GetProcessIDsByFinalResults(ctx context.Context, finalResults bool) ([]types.ProcessID, error) { - rows, err := q.db.QueryContext(ctx, getProcessIDsByFinalResults, finalResults) + rows, err := q.query(ctx, q.getProcessIDsByFinalResultsStmt, getProcessIDsByFinalResults, finalResults) if err != nil { return nil, err } @@ -228,7 +228,7 @@ LIMIT 1 ` func (q *Queries) GetProcessStatus(ctx context.Context, id types.ProcessID) (int64, error) { - row := q.db.QueryRowContext(ctx, getProcessStatus, id) + row := q.queryRow(ctx, q.getProcessStatusStmt, getProcessStatus, id) var status int64 err := row.Scan(&status) return status, err @@ -255,7 +255,7 @@ type SearchEntitiesRow struct { } func (q *Queries) SearchEntities(ctx context.Context, arg SearchEntitiesParams) ([]SearchEntitiesRow, error) { - rows, err := q.db.QueryContext(ctx, searchEntities, arg.EntityIDSubstr, arg.Offset, arg.Limit) + rows, err := q.query(ctx, q.searchEntitiesStmt, searchEntities, arg.EntityIDSubstr, arg.Offset, arg.Limit) if err != nil { return nil, err } @@ -303,7 +303,7 @@ type SearchProcessesParams struct { } func (q *Queries) SearchProcesses(ctx context.Context, arg SearchProcessesParams) ([]types.ProcessID, error) { - rows, err := q.db.QueryContext(ctx, searchProcesses, + rows, err := q.query(ctx, q.searchProcessesStmt, searchProcesses, arg.EntityID, arg.Namespace, arg.Status, @@ -341,7 +341,7 @@ WHERE id = ?1 ` func (q *Queries) SetProcessResultsCancelled(ctx context.Context, id types.ProcessID) (sql.Result, error) { - return q.db.ExecContext(ctx, setProcessResultsCancelled, id) + return q.exec(ctx, q.setProcessResultsCancelledStmt, setProcessResultsCancelled, id) } const setProcessResultsReady = `-- name: SetProcessResultsReady :execresult @@ -361,7 +361,7 @@ type SetProcessResultsReadyParams struct { } func (q *Queries) SetProcessResultsReady(ctx context.Context, arg SetProcessResultsReadyParams) (sql.Result, error) { - return q.db.ExecContext(ctx, setProcessResultsReady, + return q.exec(ctx, q.setProcessResultsReadyStmt, setProcessResultsReady, arg.Votes, arg.Weight, arg.BlockHeight, @@ -381,7 +381,7 @@ type UpdateProcessEndBlockParams struct { } func (q *Queries) UpdateProcessEndBlock(ctx context.Context, arg UpdateProcessEndBlockParams) (sql.Result, error) { - return q.db.ExecContext(ctx, updateProcessEndBlock, arg.EndBlock, arg.ID) + return q.exec(ctx, q.updateProcessEndBlockStmt, updateProcessEndBlock, arg.EndBlock, arg.ID) } const updateProcessFromState = `-- name: UpdateProcessFromState :execresult @@ -408,7 +408,7 @@ type UpdateProcessFromStateParams struct { } func (q *Queries) UpdateProcessFromState(ctx context.Context, arg UpdateProcessFromStateParams) (sql.Result, error) { - return q.db.ExecContext(ctx, updateProcessFromState, + return q.exec(ctx, q.updateProcessFromStateStmt, updateProcessFromState, arg.CensusRoot, arg.CensusUri, arg.PrivateKeys, @@ -437,7 +437,7 @@ type UpdateProcessResultByIDParams struct { } func (q *Queries) UpdateProcessResultByID(ctx context.Context, arg UpdateProcessResultByIDParams) (sql.Result, error) { - return q.db.ExecContext(ctx, updateProcessResultByID, + return q.exec(ctx, q.updateProcessResultByIDStmt, updateProcessResultByID, arg.Votes, arg.Weight, arg.VoteOpts, @@ -462,7 +462,7 @@ type UpdateProcessResultsParams struct { } func (q *Queries) UpdateProcessResults(ctx context.Context, arg UpdateProcessResultsParams) (sql.Result, error) { - return q.db.ExecContext(ctx, updateProcessResults, + return q.exec(ctx, q.updateProcessResultsStmt, updateProcessResults, arg.Votes, arg.Weight, arg.BlockHeight, diff --git a/vochain/indexer/db/token_transfers.sql.go b/vochain/indexer/db/token_transfers.sql.go index 0bfc87d34..b1f19dcf2 100644 --- a/vochain/indexer/db/token_transfers.sql.go +++ b/vochain/indexer/db/token_transfers.sql.go @@ -33,7 +33,7 @@ type CreateTokenTransferParams struct { } func (q *Queries) CreateTokenTransfer(ctx context.Context, arg CreateTokenTransferParams) (sql.Result, error) { - return q.db.ExecContext(ctx, createTokenTransfer, + return q.exec(ctx, q.createTokenTransferStmt, createTokenTransfer, arg.TxHash, arg.BlockHeight, arg.FromAccount, @@ -50,7 +50,7 @@ LIMIT 1 ` func (q *Queries) GetTokenTransfer(ctx context.Context, txHash types.Hash) (TokenTransfer, error) { - row := q.db.QueryRowContext(ctx, getTokenTransfer, txHash) + row := q.queryRow(ctx, q.getTokenTransferStmt, getTokenTransfer, txHash) var i TokenTransfer err := row.Scan( &i.TxHash, @@ -78,7 +78,7 @@ type GetTokenTransfersByFromAccountParams struct { } func (q *Queries) GetTokenTransfersByFromAccount(ctx context.Context, arg GetTokenTransfersByFromAccountParams) ([]TokenTransfer, error) { - rows, err := q.db.QueryContext(ctx, getTokenTransfersByFromAccount, arg.FromAccount, arg.Offset, arg.Limit) + rows, err := q.query(ctx, q.getTokenTransfersByFromAccountStmt, getTokenTransfersByFromAccount, arg.FromAccount, arg.Offset, arg.Limit) if err != nil { return nil, err } diff --git a/vochain/indexer/db/transactions.sql.go b/vochain/indexer/db/transactions.sql.go index fd0dfbef0..a6efb4a5f 100644 --- a/vochain/indexer/db/transactions.sql.go +++ b/vochain/indexer/db/transactions.sql.go @@ -19,7 +19,7 @@ SELECT COUNT(*) FROM transactions ` func (q *Queries) CountTransactions(ctx context.Context) (int64, error) { - row := q.db.QueryRowContext(ctx, countTransactions) + row := q.queryRow(ctx, q.countTransactionsStmt, countTransactions) var count int64 err := row.Scan(&count) return count, err @@ -41,7 +41,7 @@ type CreateTransactionParams struct { } func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (sql.Result, error) { - return q.db.ExecContext(ctx, createTransaction, + return q.exec(ctx, q.createTransactionStmt, createTransaction, arg.Hash, arg.BlockHeight, arg.BlockIndex, @@ -62,7 +62,7 @@ type GetLastTransactionsParams struct { } func (q *Queries) GetLastTransactions(ctx context.Context, arg GetLastTransactionsParams) ([]Transaction, error) { - rows, err := q.db.QueryContext(ctx, getLastTransactions, arg.Limit, arg.Offset) + rows, err := q.query(ctx, q.getLastTransactionsStmt, getLastTransactions, arg.Limit, arg.Offset) if err != nil { return nil, err } @@ -97,7 +97,7 @@ LIMIT 1 ` func (q *Queries) GetTransaction(ctx context.Context, id int64) (Transaction, error) { - row := q.db.QueryRowContext(ctx, getTransaction, id) + row := q.queryRow(ctx, q.getTransactionStmt, getTransaction, id) var i Transaction err := row.Scan( &i.ID, @@ -116,7 +116,7 @@ LIMIT 1 ` func (q *Queries) GetTransactionByHash(ctx context.Context, hash types.Hash) (Transaction, error) { - row := q.db.QueryRowContext(ctx, getTransactionByHash, hash) + row := q.queryRow(ctx, q.getTransactionByHashStmt, getTransactionByHash, hash) var i Transaction err := row.Scan( &i.ID, @@ -140,7 +140,7 @@ type GetTxReferenceByBlockHeightAndBlockIndexParams struct { } func (q *Queries) GetTxReferenceByBlockHeightAndBlockIndex(ctx context.Context, arg GetTxReferenceByBlockHeightAndBlockIndexParams) (Transaction, error) { - row := q.db.QueryRowContext(ctx, getTxReferenceByBlockHeightAndBlockIndex, arg.BlockHeight, arg.BlockIndex) + row := q.queryRow(ctx, q.getTxReferenceByBlockHeightAndBlockIndexStmt, getTxReferenceByBlockHeightAndBlockIndex, arg.BlockHeight, arg.BlockIndex) var i Transaction err := row.Scan( &i.ID, diff --git a/vochain/indexer/db/votes.sql.go b/vochain/indexer/db/votes.sql.go index d6fe246a5..d25b453d9 100644 --- a/vochain/indexer/db/votes.sql.go +++ b/vochain/indexer/db/votes.sql.go @@ -19,7 +19,7 @@ SELECT COUNT(*) FROM votes ` func (q *Queries) CountVotes(ctx context.Context) (int64, error) { - row := q.db.QueryRowContext(ctx, countVotes) + row := q.queryRow(ctx, q.countVotesStmt, countVotes) var count int64 err := row.Scan(&count) return count, err @@ -31,7 +31,7 @@ WHERE process_id = ? ` func (q *Queries) CountVotesByProcessID(ctx context.Context, processID types.ProcessID) (int64, error) { - row := q.db.QueryRowContext(ctx, countVotesByProcessID, processID) + row := q.queryRow(ctx, q.countVotesByProcessIDStmt, countVotesByProcessID, processID) var count int64 err := row.Scan(&count) return count, err @@ -62,7 +62,7 @@ type CreateVoteParams struct { } func (q *Queries) CreateVote(ctx context.Context, arg CreateVoteParams) (sql.Result, error) { - return q.db.ExecContext(ctx, createVote, + return q.exec(ctx, q.createVoteStmt, createVote, arg.Nullifier, arg.ProcessID, arg.BlockHeight, @@ -101,7 +101,7 @@ type GetVoteRow struct { } func (q *Queries) GetVote(ctx context.Context, nullifier types.Nullifier) (GetVoteRow, error) { - row := q.db.QueryRowContext(ctx, getVote, nullifier) + row := q.queryRow(ctx, q.getVoteStmt, getVote, nullifier) var i GetVoteRow err := row.Scan( &i.Nullifier, @@ -152,7 +152,7 @@ type SearchVotesRow struct { } func (q *Queries) SearchVotes(ctx context.Context, arg SearchVotesParams) ([]SearchVotesRow, error) { - rows, err := q.db.QueryContext(ctx, searchVotes, + rows, err := q.query(ctx, q.searchVotesStmt, searchVotes, arg.ProcessID, arg.NullifierSubstr, arg.Offset, diff --git a/vochain/indexer/indexer.go b/vochain/indexer/indexer.go index 1ab295066..17599144d 100644 --- a/vochain/indexer/indexer.go +++ b/vochain/indexer/indexer.go @@ -77,7 +77,8 @@ type Indexer struct { // blockTx is an in-progress SQL transaction which is committed or rolled // back along with the current block. blockTx *sql.Tx - // blockQueries wraps blockTx. + // blockQueries wraps blockTx. Note that it is kept between multiple transactions + // so that we can reuse the same prepared statements. blockQueries *indexerdb.Queries // blockUpdateProcs is the list of process IDs that require sync with the state database. // The key is a types.ProcessID as a string, so that it can be used as a map key. @@ -150,7 +151,14 @@ func NewIndexer(dataDir string, app *vochain.BaseApplication, countLiveResults b return nil, fmt.Errorf("goose up: %w", err) } - idx.readOnlyQuery = indexerdb.New(idx.readOnlyDB) + idx.readOnlyQuery, err = indexerdb.Prepare(context.TODO(), idx.readOnlyDB) + if err != nil { + return nil, err + } + idx.blockQueries, err = indexerdb.Prepare(context.TODO(), idx.readWriteDB) + if err != nil { + panic(err) + } // Subscribe to events idx.App.State.AddEventListener(idx) @@ -179,7 +187,7 @@ func (idx *Indexer) blockTxQueries() *indexerdb.Queries { panic(err) // shouldn't happen, use an error return if it ever does } idx.blockTx = tx - idx.blockQueries = indexerdb.New(idx.blockTx) + idx.blockQueries = idx.blockQueries.WithTx(tx) } return idx.blockQueries } @@ -292,7 +300,6 @@ func (idx *Indexer) AfterSyncBootstrap(inTest bool) { log.Errorw(err, "could not commit tx") } idx.blockTx = nil - idx.blockQueries = nil log.Infof("live results recovery computation finished, took %s", time.Since(startTime)) } @@ -402,7 +409,6 @@ func (idx *Indexer) Commit(height uint32) error { log.Errorw(err, "could not commit tx") } idx.blockTx = nil - idx.blockQueries = nil if newVotes+overwritedVotes > 0 { log.Infow("add live votes to results", @@ -423,7 +429,6 @@ func (idx *Indexer) Rollback() { log.Errorw(err, "could not rollback tx") } idx.blockTx = nil - idx.blockQueries = nil } maps.Clear(idx.blockUpdateProcs) } diff --git a/vochain/indexer/sqlc.yaml b/vochain/indexer/sqlc.yaml index 8e2614426..99d79985c 100644 --- a/vochain/indexer/sqlc.yaml +++ b/vochain/indexer/sqlc.yaml @@ -7,6 +7,7 @@ sql: go: package: "indexerdb" out: "db" + emit_prepared_queries: true overrides: # Defaults to int32 for integers, which is a bit small. - db_type: "int"