Pastebin

Check-in [bbe49e890d]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplify DB queries
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: bbe49e890d5d8a52ce217714413164e3156d8bae0cee9cb52d4fa38e92752c16
User & Date: jef 2019-11-06 20:27:24
Context
2019-11-06
21:03
Custom error message on invalid paste check-in: 064b0cd9a2 user: jef tags: trunk
20:27
Simplify DB queries check-in: bbe49e890d user: jef tags: trunk
2019-11-05
12:30
Add -ldflags to reduce binary size check-in: b702abe83b user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to db.go.

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
		pastes = append(pastes, &paste)
	}

	return
}

func (p *PasteBinDb) Add(data string) (url string, mimetype string, err error) {
	stmt, err := p.db.Prepare(`INSERT INTO pastes (pasteid, mimetype, data) VALUES (?, ?, ?)`)
	if err != nil {
		return
	}
	defer stmt.Close()

	mimetype = MimeString(data)
	for _ = range []int{1, 2, 3} {
		pasteid := PasteIdRandom()

		_, err = stmt.Exec(pasteid, mimetype, data)
		if err == nil {
			url = fmt.Sprintf("%s/%s", strings.TrimRight(p.cfg.PasteURL.String(), "/"), pasteid)
			return
		}
	}

	return
}

func (p *PasteBinDb) Del(pasteId string) error {
	if !PasteIdIsValid(pasteId) {
		return fmt.Errorf("invalid pasteid")
	}

	stmt, err := p.db.Prepare(`DELETE FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(pasteId)
	return err
}

func (p *PasteBinDb) Purge(ndays int) error {
	if ndays < 0 {
		return nil
	}

	delay := fmt.Sprintf("-%d days", ndays)
	stmt, err := p.db.Prepare(`DELETE FROM pastes WHERE inserttime < datetime('now', ?, 'localtime');`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(delay)
	return err
}

func (p *PasteBinDb) Get(pasteid string) (pdata *PasteData, err error) {
	stmt, err := p.db.Prepare(`SELECT data, mimetype FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query(pasteid)
	if err != nil {
		return
	}

	if !rows.Next() {
		return nil, nil
	}

	pdata = &PasteData{}
	err = rows.Scan(&pdata.Data, &pdata.MimeType)
	if err != nil {
		return
	}

	return pdata, nil
}







<
<
<
<
<
<



>
|









|




|
<
|
<
<
<
<
<








|
<
<
<
<
<
<




<
<
<
<
<
<
<
<
<
<
<
<
<
<
<

|
<
|
|
<
<
<
91
92
93
94
95
96
97






98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

118





119
120
121
122
123
124
125
126
127






128
129
130
131















132
133

134
135



		pastes = append(pastes, &paste)
	}

	return
}

func (p *PasteBinDb) Add(data string) (url string, mimetype string, err error) {






	mimetype = MimeString(data)
	for _ = range []int{1, 2, 3} {
		pasteid := PasteIdRandom()
		_, err = p.db.Exec(`INSERT INTO pastes (pasteid, mimetype, data) VALUES (?, ?, ?)`,
			pasteid, mimetype, data)
		if err == nil {
			url = fmt.Sprintf("%s/%s", strings.TrimRight(p.cfg.PasteURL.String(), "/"), pasteid)
			return
		}
	}

	return
}

func (p *PasteBinDb) Del(pasteId string) (err error) {
	if !PasteIdIsValid(pasteId) {
		return fmt.Errorf("invalid pasteid")
	}

	_, err = p.db.Exec(`DELETE FROM pastes WHERE pasteid = ?`, pasteId)

	return





}

func (p *PasteBinDb) Purge(ndays int) error {
	if ndays < 0 {
		return nil
	}

	delay := fmt.Sprintf("-%d days", ndays)
	_, err := p.db.Exec(`DELETE FROM pastes WHERE inserttime < datetime('now', ?, 'localtime');`, delay)






	return err
}

func (p *PasteBinDb) Get(pasteid string) (pdata *PasteData, err error) {















	pdata = &PasteData{}
	err = p.db.QueryRow(`SELECT data, mimetype FROM pastes WHERE pasteid = ?`, pasteid).Scan(&pdata.Data, &pdata.MimeType)

	return
}