|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + _ "github.com/lib/pq" |
| 8 | + |
| 9 | + "github.com/nanopack/shaman/config" |
| 10 | + shaman "github.com/nanopack/shaman/core/common" |
| 11 | +) |
| 12 | + |
| 13 | +type postgresDb struct { |
| 14 | + pg *sql.DB |
| 15 | +} |
| 16 | + |
| 17 | +func (p *postgresDb) connect() error { |
| 18 | + // todo: example: config.DatabaseConnection = "postgres://[email protected]?sslmode=disable" |
| 19 | + db, err := sql.Open("postgres", config.L2Connect) |
| 20 | + if err != nil { |
| 21 | + return fmt.Errorf("Failed to connect to postgres - %v", err) |
| 22 | + } |
| 23 | + err = db.Ping() |
| 24 | + if err != nil { |
| 25 | + return fmt.Errorf("Failed to ping postgres on connect - %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + p.pg = db |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func (p postgresDb) createTables() error { |
| 33 | + // create records table |
| 34 | + _, err := p.pg.Exec(` |
| 35 | +CREATE TABLE IF NOT EXISTS records ( |
| 36 | + recordId SERIAL PRIMARY KEY NOT NULL, |
| 37 | + domain TEXT NOT NULL, |
| 38 | + address TEXT NOT NULL, |
| 39 | + ttl INTEGER, |
| 40 | + class TEXT, |
| 41 | + type TEXT |
| 42 | +)`) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("Failed to create records table - %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func (p *postgresDb) initialize() error { |
| 51 | + err := p.connect() |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("Failed to create new connection - %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // create tables |
| 57 | + err = p.createTables() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("Failed to create tables - %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (p postgresDb) addRecord(resource shaman.Resource) error { |
| 66 | + resources, err := p.listRecords() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + for i := range resources { |
| 72 | + if resources[i].Domain == resource.Domain { |
| 73 | + // if domains match, check address |
| 74 | + for k := range resources[i].Records { |
| 75 | + next: |
| 76 | + for j := range resource.Records { |
| 77 | + // check if the record exists... |
| 78 | + if resource.Records[j].RType == resources[i].Records[k].RType && |
| 79 | + resource.Records[j].Address == resources[i].Records[k].Address && |
| 80 | + resource.Records[j].Class == resources[i].Records[k].Class { |
| 81 | + // if so, skip |
| 82 | + config.Log.Trace("Record exists in persistent, skipping...") |
| 83 | + resource.Records = append(resource.Records[:i], resource.Records[i+1:]...) |
| 84 | + goto next |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // add records |
| 92 | + for i := range resource.Records { |
| 93 | + config.Log.Trace("Adding record to database...") |
| 94 | + _, err = p.pg.Exec(fmt.Sprintf(` |
| 95 | +INSERT INTO records(domain, address, ttl, class, type) |
| 96 | +VALUES('%v', '%v', '%v', '%v', '%v')`, |
| 97 | + resource.Domain, resource.Records[i].Address, resource.Records[i].TTL, |
| 98 | + resource.Records[i].Class, resource.Records[i].RType)) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("Failed to insert into records table - %v", err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func (p postgresDb) getRecord(domain string) (*shaman.Resource, error) { |
| 108 | + // read from records table |
| 109 | + rows, err := p.pg.Query(fmt.Sprintf("SELECT address, ttl, class, type FROM records WHERE domain = '%v'", domain)) |
| 110 | + if err != nil { |
| 111 | + return nil, fmt.Errorf("Failed to select from records table - %v", err) |
| 112 | + } |
| 113 | + defer rows.Close() |
| 114 | + |
| 115 | + records := make([]shaman.Record, 0, 0) |
| 116 | + |
| 117 | + // get data |
| 118 | + for rows.Next() { |
| 119 | + rcrd := shaman.Record{} |
| 120 | + err = rows.Scan(&rcrd.Address, &rcrd.TTL, &rcrd.Class, &rcrd.RType) |
| 121 | + if err != nil { |
| 122 | + return nil, fmt.Errorf("Failed to save results into record - %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + records = append(records, rcrd) |
| 126 | + } |
| 127 | + |
| 128 | + // check for errors |
| 129 | + if err = rows.Err(); err != nil { |
| 130 | + return nil, fmt.Errorf("Error with results - %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + if len(records) == 0 { |
| 134 | + return nil, errNoRecordError |
| 135 | + } |
| 136 | + |
| 137 | + return &shaman.Resource{Domain: domain, Records: records}, nil |
| 138 | +} |
| 139 | + |
| 140 | +func (p postgresDb) updateRecord(domain string, resource shaman.Resource) error { |
| 141 | + // delete old from records |
| 142 | + err := p.deleteRecord(domain) |
| 143 | + if err != nil { |
| 144 | + return fmt.Errorf("Failed to clean old records - %v", err) |
| 145 | + } |
| 146 | + |
| 147 | + return p.addRecord(resource) |
| 148 | +} |
| 149 | + |
| 150 | +func (p postgresDb) deleteRecord(domain string) error { |
| 151 | + _, err := p.pg.Exec(fmt.Sprintf(`DELETE FROM records WHERE domain = '%v'`, domain)) |
| 152 | + if err != nil { |
| 153 | + return fmt.Errorf("Failed to delete from records table - %v", err) |
| 154 | + } |
| 155 | + |
| 156 | + return nil |
| 157 | +} |
| 158 | + |
| 159 | +func (p postgresDb) resetRecords(resources []shaman.Resource) error { |
| 160 | + // truncate records table |
| 161 | + _, err := p.pg.Exec("TRUNCATE records") |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("Failed to truncate records table - %v", err) |
| 164 | + } |
| 165 | + for i := range resources { |
| 166 | + err = p.addRecord(resources[i]) // prevents duplicates |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("Failed to save records - %v", err) |
| 169 | + } |
| 170 | + } |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func (p postgresDb) listRecords() ([]shaman.Resource, error) { |
| 175 | + // read from records table |
| 176 | + rows, err := p.pg.Query("SELECT DISTINCT domain FROM records") |
| 177 | + if err != nil { |
| 178 | + return nil, fmt.Errorf("Failed to select from records table - %v", err) |
| 179 | + } |
| 180 | + defer rows.Close() |
| 181 | + |
| 182 | + resources := make([]shaman.Resource, 0) |
| 183 | + |
| 184 | + // get data |
| 185 | + for rows.Next() { |
| 186 | + var domain string |
| 187 | + err = rows.Scan(&domain) |
| 188 | + if err != nil { |
| 189 | + return nil, fmt.Errorf("Failed to save domain - %v", err) |
| 190 | + } |
| 191 | + resource, err := p.getRecord(domain) |
| 192 | + if err != nil { |
| 193 | + return nil, fmt.Errorf("Failed to get record for domain - %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + resources = append(resources, *resource) |
| 197 | + } |
| 198 | + |
| 199 | + // check for errors |
| 200 | + if err = rows.Err(); err != nil { |
| 201 | + return nil, fmt.Errorf("Error with results - %v", err) |
| 202 | + } |
| 203 | + return resources, nil |
| 204 | +} |
0 commit comments