-
Notifications
You must be signed in to change notification settings - Fork 58
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 #307 from rizwanahar/master
Postgres integration across the code base
- Loading branch information
Showing
14 changed files
with
265 additions
and
76 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,50 @@ | ||
CREATE SEQUENCE publication_seq; | ||
|
||
CREATE TABLE publication ( | ||
id int PRIMARY KEY DEFAULT NEXTVAL ('publication_seq'), | ||
uuid varchar(255) NOT NULL, /* == content id */ | ||
title varchar(255) NOT NULL, | ||
status varchar(255) NOT NULL | ||
); | ||
|
||
CREATE INDEX uuid_index ON publication (uuid); | ||
|
||
CREATE SEQUENCE user_seq; | ||
|
||
CREATE TABLE "user" ( | ||
id int PRIMARY KEY DEFAULT NEXTVAL ('user_seq'), | ||
uuid varchar(255) NOT NULL, | ||
name varchar(64) NOT NULL, | ||
email varchar(64) NOT NULL, | ||
password varchar(64) NOT NULL, | ||
hint varchar(64) NOT NULL | ||
); | ||
|
||
CREATE SEQUENCE purchase_seq; | ||
|
||
CREATE TABLE purchase ( | ||
id int PRIMARY KEY DEFAULT NEXTVAL ('purchase_seq'), | ||
uuid varchar(255) NOT NULL, | ||
publication_id int NOT NULL, | ||
user_id int NOT NULL, | ||
license_uuid varchar(255) NULL, | ||
type varchar(32) NOT NULL, | ||
transaction_date timestamp(0), | ||
start_date timestamp(0), | ||
end_date timestamp(0), | ||
status varchar(255) NOT NULL, | ||
FOREIGN KEY (publication_id) REFERENCES publication (id), | ||
FOREIGN KEY (user_id) REFERENCES "user" (id) | ||
); | ||
|
||
CREATE INDEX idx_purchase ON purchase (license_uuid); | ||
|
||
CREATE SEQUENCE license_view_seq; | ||
|
||
CREATE TABLE license_view ( | ||
id int PRIMARY KEY DEFAULT NEXTVAL ('license_view_seq'), | ||
uuid varchar(255) NOT NULL, | ||
device_count int NOT NULL, | ||
status varchar(255) NOT NULL, | ||
message varchar(255) NOT NULL | ||
); |
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,25 @@ | ||
|
||
CREATE TABLE content ( | ||
id varchar(255) PRIMARY KEY NOT NULL, | ||
encryption_key bytea NOT NULL, | ||
location text NOT NULL, | ||
length bigint, | ||
sha256 varchar(64), | ||
type varchar(255) NOT NULL DEFAULT 'application/epub+zip' | ||
); | ||
|
||
-- SQLINES LICENSE FOR EVALUATION USE ONLY | ||
CREATE TABLE license ( | ||
id varchar(255) PRIMARY KEY NOT NULL, | ||
user_id varchar(255) NOT NULL, | ||
provider varchar(255) NOT NULL, | ||
issued timestamp(0) NOT NULL, | ||
updated timestamp(0) DEFAULT NULL, | ||
rights_print int DEFAULT NULL, | ||
rights_copy int DEFAULT NULL, | ||
rights_start timestamp(0) DEFAULT NULL, | ||
rights_end timestamp(0) DEFAULT NULL, | ||
content_fk varchar(255) NOT NULL, | ||
lsd_status int default 0, | ||
FOREIGN KEY(content_fk) REFERENCES content(id) | ||
); |
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,26 @@ | ||
CREATE TABLE license_status ( | ||
id serial4 NOT NULL, | ||
status smallint NOT NULL, | ||
license_updated timestamp(3) NOT NULL, | ||
status_updated timestamp(3) NOT NULL, | ||
device_count smallint DEFAULT NULL, | ||
potential_rights_end timestamp(3) DEFAULT NULL, | ||
license_ref varchar(255) NOT NULL, | ||
rights_end timestamp(3) DEFAULT NULL, | ||
CONSTRAINT license_status_pkey PRIMARY KEY (id) | ||
); | ||
|
||
CREATE INDEX license_ref_index ON license_status (license_ref); | ||
|
||
CREATE TABLE event ( | ||
id serial4 NOT NULL, | ||
device_name varchar(255) DEFAULT NULL, | ||
timestamp timestamp(3) NOT NULL, | ||
type int NOT NULL, | ||
device_id varchar(255) DEFAULT NULL, | ||
license_status_fk int NOT NULL, | ||
CONSTRAINT event_pkey PRIMARY KEY (id), | ||
FOREIGN KEY(license_status_fk) REFERENCES license_status(id) | ||
); | ||
|
||
CREATE INDEX license_status_fk_index on event (license_status_fk); |
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,31 @@ | ||
package dbutils | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func getPostgresQuery(query string) string { | ||
var buffer bytes.Buffer | ||
idx := 1 | ||
for _, char := range query { | ||
if char == '?' { | ||
buffer.WriteString(fmt.Sprintf("$%d", idx)) | ||
idx += 1 | ||
} else { | ||
buffer.WriteRune(char) | ||
} | ||
} | ||
return buffer.String() | ||
} | ||
|
||
// GetParamQuery replaces parameter placeholders '?' in the SQL query to | ||
// placeholders supported by the selected database driver. | ||
func GetParamQuery(database, query string) string { | ||
if strings.HasPrefix(database, "postgres") { | ||
return getPostgresQuery(query) | ||
} else { | ||
return query | ||
} | ||
} |
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,17 @@ | ||
package dbutils | ||
|
||
import "testing" | ||
|
||
const demo_query = "SELECT * FROM test WHERE id = ? AND test = ? LIMIT 1" | ||
|
||
func TestGetParamQuery(t *testing.T) { | ||
q := GetParamQuery("postgres", demo_query) | ||
if q != "SELECT * FROM test WHERE id = $1 AND test = $2 LIMIT 1" { | ||
t.Fatalf("Incorrect postgres query") | ||
} | ||
|
||
q = GetParamQuery("sqlite3", demo_query) | ||
if q != "SELECT * FROM test WHERE id = ? AND test = ? LIMIT 1" { | ||
t.Fatalf("Incorrect sqlite3 query") | ||
} | ||
} |
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
Oops, something went wrong.