Skip to content

Commit

Permalink
#Fix Primary key length
Browse files Browse the repository at this point in the history
  • Loading branch information
JonyBepary committed Oct 30, 2023
1 parent 9062e48 commit df16a95
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 174 deletions.
2 changes: 1 addition & 1 deletion internal/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (s *File) Save() error {
}
defer db.Close()
str := `CREATE TABLE IF NOT EXISTS FILE(
ULID VARCHAR(128) PRIMARY KEY,
ULID VARCHAR(255) PRIMARY KEY,
Name TEXT NOT NULL,
CreatedAt INTEGER NOT NULL,
UpdatedAt INTEGER,
Expand Down
10 changes: 5 additions & 5 deletions internal/model/follows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func (s *Follower) SaveFollower() error {
defer db.Close()
str := `
CREATE TABLE IF NOT EXISTS Follower (
UserId VARCHAR(128) PRIMARY KEY,
UserId VARCHAR(255) PRIMARY KEY,
Status BOOL,
Time INTEGER,
FollowAccount VARCHAR(128),
UnfollowAccount VARCHAR(128)
FollowAccount VARCHAR(255),
UnfollowAccount VARCHAR(255)
)
`
Expand Down Expand Up @@ -102,7 +102,7 @@ func (d *Follower) DeleteFollowee(FolloweeId string) error {
// another rule don't type something that can be copied
// typing on your own tends to bring more mistake
// baki table check koro okay): abar thik ache bhai
_, err = db.Exec("DELETE FROM Follower WHERE UserId= ?", FolloweeId) /// mair khabatable name thik koroe
_, err = db.Exec("DELETE FROM Follower WHERE UserId= ?", FolloweeId) /// mair khabatable name thik koroe

if err != nil {
log.Fatal(err)
Expand All @@ -122,7 +122,7 @@ func (s *Followee) SaveFollowee() error {
defer db.Close()
str := `
CREATE TABLE IF NOT EXISTS Followee (
UserId VARCHAR(128) PRIMARY KEY,
UserId VARCHAR(255) PRIMARY KEY,
BlockAccountLink TEXT,
BlockAccountName TEXT
)
Expand Down
6 changes: 3 additions & 3 deletions internal/model/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func (msg *Messages) SaveMessages() error {
defer db.Close()
str := `
CREATE TABLE IF NOT EXISTS MESSAGE (
MsgId VARCHAR(128) PRIMARY KEY,
SenderId VARCHAR(128),
ReceiverId VARCHAR(128),
MsgId VARCHAR(255) PRIMARY KEY,
SenderId VARCHAR(255),
ReceiverId VARCHAR(255),
Content TEXT,
SentTime INTEGER,
LastEdit INTEGER,
Expand Down
14 changes: 7 additions & 7 deletions internal/model/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ func (n *Notifications) SaveNotifications() error {
defer db.Close()
str := `
CREATE TABLE IF NOT EXISTS NOTIFICATIONS (
Receiver VARCHAR(128) PRIMARY KEY NOT NULL,
Title VARCHAR(128),
Body VARCHAR(128),
Source VARCHAR(128),
Image VARCHAR(128),
Sound VARCHAR(128),
Receiver VARCHAR(255) PRIMARY KEY NOT NULL,
Title VARCHAR(255),
Body VARCHAR(255),
Source VARCHAR(255),
Image VARCHAR(255),
Sound VARCHAR(255),
Time INTEGER,
Channel VARCHAR(128),
Channel VARCHAR(255),
PriorityLevel INTEGER,
ReadStatus bool,
Created INTEGER)
Expand Down
217 changes: 130 additions & 87 deletions internal/model/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ func (p *Person) SavePerson() error {
Outbox VARCHAR(255),
PreferredUsername VARCHAR(255),
PublicKey VARCHAR(255),
FragmentationKey VARCHAR(255)
FragmentationKey VARCHAR(255),
Username VARCHAR(255),
FOREIGN KEY (Username) REFERENCES USER(Username)
)`
_, err = db.Exec(sql_cmd)
if err != nil {
return err
}
statement, err := db.Prepare("INSERT INTO PERSON (Id,Attachment,AttributedTo,Context,MediaType,EndTime,Generator,Icon,Image,InReplyTo,Location,Preview,PublishedTime,StartTime,Summary,UpdatedTime,Likes,Shares,Inbox,Outbox,PreferredUsername,PublicKey,FragmentationKey) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
statement, err := db.Prepare("INSERT INTO PERSON (Id,Attachment,AttributedTo,Context,MediaType,EndTime,Generator,Icon,Image,InReplyTo,Location,Preview,PublishedTime,StartTime,Summary,UpdatedTime,Likes,Shares,Inbox,Outbox,PreferredUsername,PublicKey,FragmentationKey,Username) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
}
_, err = statement.Exec(p.Id, p.Attachment, p.AttributedTo, p.Context, p.MediaType, p.EndTime, p.Generator, p.Icon, p.Image, p.InReplyTo, p.Location, p.Preview, p.PublishedTime, p.StartTime, p.Summary, p.UpdatedTime, p.Likes, p.Shares, p.Inbox, p.Outbox, p.PreferredUsername, p.PublicKey, p.FragmentationKey)
_, err = statement.Exec(p.Id, p.Attachment, p.AttributedTo, p.Context, p.MediaType, p.EndTime, p.Generator, p.Icon, p.Image, p.InReplyTo, p.Location, p.Preview, p.PublishedTime, p.StartTime, p.Summary, p.UpdatedTime, p.Likes, p.Shares, p.Inbox, p.Outbox, p.PreferredUsername, p.PublicKey, p.FragmentationKey, p.Username)
if err != nil {
return err
}
Expand All @@ -64,7 +66,7 @@ func (p *Person) GetPerson(pid string) error {
panic(err)
}
row.Next()
err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey)
err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey, &p.Username)
if err != nil {
panic(err)
}
Expand All @@ -79,86 +81,127 @@ func (p *Person) GetPerson(pid string) error {
return nil
}

// {
// db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
// if err != nil {
// panic(err)
// }
// defer db.Close()
// // spew.Dump(rows)
// rows, err := db.Query("SELECT * FROM PERSON")
// if err != nil {
// panic(err)
// }
// defer rows.Close()
// for rows.Next() {
// err = rows.Scan(p.Id, p.Attachment, p.AttributedTo, p.Context, p.MediaType, p.EndTime, p.Generator, p.Icon, p.Image, p.InReplyTo, p.Location, p.Preview, p.PublishedTime, p.Replies, p.StartTime, p.Summary, p.UpdatedTime, p.Url, p.Too, p.Bto, p.Cc, p.Bcc, p.Likes, p.Shares, p.Inbox, p.Outbox, p.Following, p.Followers, p.Liked, p.PreferredUsername, p.Endpoints, p.Streams, p.PublicKey, p.FragmentationKey)
// if err != nil {
// panic(err)
// }
// }
// err = rows.Err()
// if err != nil {
// panic(err)
// }
// return nil
// }

// func GetPerson(msgId string) (Person, error) {
// db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
// if err != nil {
// panic(err)
// }
// defer db.Close()

// // spew.Dump(rows)
// //! fmt.Println("message id is: ", msgId)
// row, err := db.Query("SELECT * FROM PERSON WHERE Id=?", msgId)
// if err != nil {
// panic(err)
// }
// row.Next()
// err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey)
// if err != nil {
// panic(err)
// }

// err = row.Err()
// if err != nil {
// panic(err)
// }
// row.Close()

// //! spew.Dump(p.Id)
// return nil
// }

// func GetPerson(msgId string) (Person, error) {
// db, err := durable.CreateDatabase("./Database/", "Common", "Shard_0.sqlite")
// if err != nil {
// panic(err)
// }
// defer db.Close()

// // spew.Dump(rows)
// //! fmt.Println("message id is: ", msgId)
// row, err := db.Query("SELECT * FROM PERSON WHERE Id=?", msgId)
// if err != nil {
// panic(err)
// }
// p := Person{}
// err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey)
// row.Next()
// if err != nil {
// panic(err)
// }

// err = row.Err()
// if err != nil {
// panic(err)
// }
// row.Close()

// //! spew.Dump(p.Id)
// return p, nil
// }
func (p *Person) GetPersonByUsername(username string) error {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
if err != nil {
panic(err)
}
defer db.Close()

// spew.Dump(rows)
//! fmt.Println("message id is: ", pid)
row, err := db.Query("SELECT * FROM PERSON WHERE Username=?", username)
if err != nil {
panic(err)
}
row.Next()
err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey, &p.Username)
if err != nil {
panic(err)
}

err = row.Err()
if err != nil {
panic(err)
}
row.Close()

//! spew.Dump(p.Id)
return nil
}

func (p *Person) UpdatePerson() error {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
sql_cmd := `UPDATE PERSON SET Id=?,Attachment=?,AttributedTo=?,Context=?,MediaType=?,EndTime=?,Generator=?,Icon=?,Image=?,InReplyTo=?,Location=?,Preview=?,PublishedTime=?,StartTime=?,Summary=?,UpdatedTime=?,Likes=?,Shares=?,Inbox=?,Outbox=?,PreferredUsername=?,PublicKey=?,FragmentationKey=?,Username=? WHERE Id=?`
statement, err := db.Prepare(sql_cmd)
if err != nil {
panic(err)
}
_, err = statement.Exec(p.Id, p.Attachment, p.AttributedTo, p.Context, p.MediaType, p.EndTime, p.Generator, p.Icon, p.Image, p.InReplyTo, p.Location, p.Preview, p.PublishedTime, p.StartTime, p.Summary, p.UpdatedTime, p.Likes, p.Shares, p.Inbox, p.Outbox, p.PreferredUsername, p.PublicKey, p.FragmentationKey, p.Username, p.Id)
if err != nil {
panic(err)
}
return nil
}

func (p *Person) DeletePerson(pid string) error {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
if err != nil {
panic(err)
}
defer db.Close()
sql_cmd := `DELETE FROM PERSON WHERE Id=?`
statement, err := db.Prepare(sql_cmd)
if err != nil {
panic(err)
}
_, err = statement.Exec(pid)
if err != nil {
panic(err)
}
return nil
}

func (p *Person) GetPersonByFragmentationKey(fragmentationKey string) error {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
if err != nil {
panic(err)
}
defer db.Close()

// spew.Dump(rows)
//! fmt.Println("message id is: ", pid)
row, err := db.Query("SELECT * FROM PERSON WHERE FragmentationKey=?", fragmentationKey)
if err != nil {
panic(err)
}
row.Next()
err = row.Scan(&p.Id, &p.Attachment, &p.AttributedTo, &p.Context, &p.MediaType, &p.EndTime, &p.Generator, &p.Icon, &p.Image, &p.InReplyTo, &p.Location, &p.Preview, &p.PublishedTime, &p.Replies, &p.StartTime, &p.Summary, &p.UpdatedTime, &p.Url, &p.Too, &p.Bto, &p.Cc, &p.Bcc, &p.Likes, &p.Shares, &p.Inbox, &p.Outbox, &p.Following, &p.Followers, &p.Liked, &p.PreferredUsername, &p.Endpoints, &p.Streams, &p.PublicKey, &p.FragmentationKey, &p.Username)
if err != nil {
panic(err)
}

err = row.Err()
if err != nil {
panic(err)
}
row.Close()

//! spew.Dump(p.Id)
return nil
}

// horizontally fragment this table by location
func (p *Person) FragmentateByLocation() ([]Person, error) {
db, err := durable.CreateDatabase("Database/", "Common", "Shard_0.sqlite")
if err != nil {
return nil, err
}
defer db.Close()

rows, err := db.Query("SELECT * FROM PERSON WHERE Location=?", p.Location)
if err != nil {
return nil, err
}
defer rows.Close()

var persons []Person
for rows.Next() {
var person Person
err = rows.Scan(&person.Id, &person.Attachment, &person.AttributedTo, &person.Context, &person.MediaType, &person.EndTime, &person.Generator, &person.Icon, &person.Image, &person.InReplyTo, &person.Location, &person.Preview, &person.PublishedTime, &person.Replies, &person.StartTime, &person.Summary, &person.UpdatedTime, &person.Url, &person.Too, &person.Bto, &person.Cc, &person.Bcc, &person.Likes, &person.Shares, &person.Inbox, &person.Outbox, &person.Following, &person.Followers, &person.Liked, &person.PreferredUsername, &person.Endpoints, &person.Streams, &person.PublicKey, &person.FragmentationKey, &person.Username)
if err != nil {
return nil, err
}
persons = append(persons, person)
}
err = rows.Err()
if err != nil {
return nil, err
}
return persons, nil
}

Loading

0 comments on commit df16a95

Please sign in to comment.