Changeset 781 in code for trunk/db_postgres.go
- Timestamp:
- Feb 11, 2022, 6:41:46 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/db_postgres.go
r774 r781 78 78 UNIQUE(network, target, client) 79 79 ); 80 81 CREATE TABLE "ReadReceipt" ( 82 id SERIAL PRIMARY KEY, 83 network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE, 84 target VARCHAR(255) NOT NULL, 85 timestamp TIMESTAMP WITH TIME ZONE NOT NULL, 86 UNIQUE(network, target) 87 ); 80 88 ` 81 89 … … 89 97 TYPE sasl_mechanism 90 98 USING sasl_mechanism::sasl_mechanism; 99 `, 100 ` 101 CREATE TABLE "ReadReceipt" ( 102 id SERIAL PRIMARY KEY, 103 network INTEGER NOT NULL REFERENCES "Network"(id) ON DELETE CASCADE, 104 target VARCHAR(255) NOT NULL, 105 timestamp TIMESTAMP WITH TIME ZONE NOT NULL, 106 UNIQUE(network, target) 107 ); 91 108 `, 92 109 } … … 501 518 return tx.Commit() 502 519 } 520 521 func (db *PostgresDB) GetReadReceipt(ctx context.Context, networkID int64, name string) (*ReadReceipt, error) { 522 ctx, cancel := context.WithTimeout(ctx, postgresQueryTimeout) 523 defer cancel() 524 525 receipt := &ReadReceipt{ 526 Target: name, 527 } 528 529 row := db.db.QueryRowContext(ctx, 530 `SELECT id, timestamp FROM "ReadReceipt" WHERE network = $1 AND target = $2`, 531 networkID, name) 532 if err := row.Scan(&receipt.ID, &receipt.Timestamp); err != nil { 533 if err == sql.ErrNoRows { 534 return nil, nil 535 } 536 return nil, err 537 } 538 return receipt, nil 539 } 540 541 func (db *PostgresDB) StoreReadReceipt(ctx context.Context, networkID int64, receipt *ReadReceipt) error { 542 ctx, cancel := context.WithTimeout(ctx, postgresQueryTimeout) 543 defer cancel() 544 545 var err error 546 if receipt.ID != 0 { 547 _, err = db.db.ExecContext(ctx, ` 548 UPDATE "ReadReceipt" 549 SET timestamp = $1 550 WHERE id = $2`, 551 receipt.Timestamp, receipt.ID) 552 } else { 553 err = db.db.QueryRowContext(ctx, ` 554 INSERT INTO "ReadReceipt" (network, target, timestamp) 555 VALUES ($1, $2, $3) 556 RETURNING id`, 557 networkID, receipt.Target, receipt.Timestamp).Scan(&receipt.ID) 558 } 559 return err 560 }
Note:
See TracChangeset
for help on using the changeset viewer.