-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from sonroyaalmerol/use-sqlite
Switch to SQLite
- Loading branch information
Showing
10 changed files
with
247 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package database | ||
|
||
import ( | ||
"testing" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func TestSaveAndLoadFromSQLite(t *testing.T) { | ||
sqliteDBPath := filepath.Join(".", "data", "database.sqlite") | ||
|
||
// Test InitializeSQLite and check if the database file exists | ||
err := InitializeSQLite() | ||
if err != nil { | ||
t.Errorf("InitializeSQLite returned error: %v", err) | ||
} | ||
defer os.Remove(sqliteDBPath) // Cleanup the database file after the test | ||
|
||
// Test LoadFromSQLite with existing data in the database | ||
expected := []StreamInfo{{ | ||
Title: "stream1", | ||
TvgID: "test1", | ||
LogoURL: "http://test.com/image.png", | ||
Group: "test", | ||
URLs: []StreamURL{{ | ||
Content: "testing", | ||
M3UIndex: 1, | ||
}}, | ||
}, { | ||
Title: "stream2", | ||
TvgID: "test2", | ||
LogoURL: "http://test2.com/image.png", | ||
Group: "test2", | ||
URLs: []StreamURL{{ | ||
Content: "testing2", | ||
M3UIndex: 2, | ||
}}, | ||
}} | ||
err = SaveToSQLite(expected) // Insert test data into the database | ||
if err != nil { | ||
t.Errorf("SaveToSQLite returned error: %v", err) | ||
} | ||
|
||
result, err := LoadFromSQLite() | ||
if err != nil { | ||
t.Errorf("LoadFromSQLite returned error: %v", err) | ||
} | ||
|
||
if len(result) != len(expected) { | ||
t.Errorf("LoadFromSQLite returned %+v, expected %+v", result, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
var db *sql.DB | ||
|
||
func InitializeSQLite() error { | ||
foldername := filepath.Join(".", "data") | ||
filename := filepath.Join(foldername, "database.sqlite") | ||
|
||
err := os.MkdirAll(foldername, 0755) | ||
if err != nil { | ||
return fmt.Errorf("error creating data folder: %v\n", err) | ||
} | ||
|
||
file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0666) | ||
if err != nil { | ||
return fmt.Errorf("error creating database file: %v\n", err) | ||
} | ||
file.Close() | ||
|
||
db, err = sql.Open("sqlite3", filename) | ||
if err != nil { | ||
return fmt.Errorf("error opening SQLite database: %v\n", err) | ||
} | ||
|
||
// Create table if not exists | ||
_, err = db.Exec(` | ||
CREATE TABLE IF NOT EXISTS streams ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
title TEXT UNIQUE, | ||
tvg_id TEXT, | ||
logo_url TEXT, | ||
group_name TEXT | ||
) | ||
`) | ||
if err != nil { | ||
return fmt.Errorf("error creating table: %v\n", err) | ||
} | ||
|
||
_, err = db.Exec(` | ||
CREATE TABLE IF NOT EXISTS stream_urls ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
stream_id INTEGER, | ||
content TEXT, | ||
m3u_index INTEGER, | ||
FOREIGN KEY(stream_id) REFERENCES streams(id) | ||
) | ||
`) | ||
if err != nil { | ||
return fmt.Errorf("error creating table: %v\n", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func SaveToSQLite(streams []StreamInfo) error { | ||
tx, err := db.Begin() | ||
if err != nil { | ||
return fmt.Errorf("error beginning transaction: %v", err) | ||
} | ||
defer tx.Rollback() | ||
|
||
stmt, err := tx.Prepare("INSERT INTO streams(title, tvg_id, logo_url, group_name) VALUES(?, ?, ?, ?)") | ||
if err != nil { | ||
return fmt.Errorf("error preparing statement: %v", err) | ||
} | ||
defer stmt.Close() | ||
|
||
for _, s := range streams { | ||
res, err := stmt.Exec(s.Title, s.TvgID, s.LogoURL, s.Group) | ||
if err != nil { | ||
return fmt.Errorf("error inserting stream: %v", err) | ||
} | ||
|
||
streamID, err := res.LastInsertId() | ||
if err != nil { | ||
return fmt.Errorf("error getting last inserted ID: %v", err) | ||
} | ||
|
||
urlStmt, err := tx.Prepare("INSERT INTO stream_urls(stream_id, content, m3u_index) VALUES(?, ?, ?)") | ||
if err != nil { | ||
return fmt.Errorf("error preparing statement: %v", err) | ||
} | ||
defer urlStmt.Close() | ||
|
||
for _, u := range s.URLs { | ||
_, err := urlStmt.Exec(streamID, u.Content, u.M3UIndex) | ||
if err != nil { | ||
return fmt.Errorf("error inserting stream URL: %v", err) | ||
} | ||
} | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return fmt.Errorf("error committing transaction: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func LoadFromSQLite() ([]StreamInfo, error) { | ||
rows, err := db.Query("SELECT id, title, tvg_id, logo_url, group_name FROM streams") | ||
if err != nil { | ||
return nil, fmt.Errorf("error querying streams: %v", err) | ||
} | ||
defer rows.Close() | ||
|
||
var streams []StreamInfo | ||
for rows.Next() { | ||
var s StreamInfo | ||
var streamId int | ||
err := rows.Scan(&streamId, &s.Title, &s.TvgID, &s.LogoURL, &s.Group) | ||
if err != nil { | ||
return nil, fmt.Errorf("error scanning stream: %v", err) | ||
} | ||
|
||
urlRows, err := db.Query("SELECT content, m3u_index FROM stream_urls WHERE stream_id = ?", streamId) | ||
if err != nil { | ||
return nil, fmt.Errorf("error querying stream URLs: %v", err) | ||
} | ||
defer urlRows.Close() | ||
|
||
var urls []StreamURL | ||
for urlRows.Next() { | ||
var u StreamURL | ||
err := urlRows.Scan(&u.Content, &u.M3UIndex) | ||
if err != nil { | ||
return nil, fmt.Errorf("error scanning stream URL: %v", err) | ||
} | ||
urls = append(urls, u) | ||
} | ||
if err := urlRows.Err(); err != nil { | ||
return nil, fmt.Errorf("error iterating over URL rows: %v", err) | ||
} | ||
|
||
s.URLs = urls | ||
streams = append(streams, s) | ||
} | ||
if err := rows.Err(); err != nil { | ||
return nil, fmt.Errorf("error iterating over rows: %v", err) | ||
} | ||
|
||
return streams, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package m3u | ||
package database | ||
|
||
type StreamInfo struct { | ||
Title string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= | ||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= | ||
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= | ||
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
package m3u | ||
|
||
var Streams []StreamInfo | ||
var NewStreams []StreamInfo | ||
import ( | ||
"m3u-stream-merger/database" | ||
) | ||
|
||
var Streams []database.StreamInfo | ||
var NewStreams []database.StreamInfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.