Added a SQL-Preprocessor - this way we can unmarshal recursive structures (LEFT JOIN etc)

This commit is contained in:
2022-12-21 18:14:13 +01:00
parent bbf7962e29
commit dbc014f819
19 changed files with 740 additions and 162 deletions

View File

@@ -31,6 +31,29 @@ func (c Channel) JSON(includeKey bool) ChannelJSON {
}
}
func (c Channel) WithSubscription(sub *Subscription) ChannelWithSubscription {
return ChannelWithSubscription{
Channel: c,
Subscription: sub,
}
}
type ChannelWithSubscription struct {
Channel
Subscription *Subscription
}
func (c ChannelWithSubscription) JSON(includeChannelKey bool) ChannelWithSubscriptionJSON {
var sub *SubscriptionJSON = nil
if c.Subscription != nil {
sub = langext.Ptr(c.Subscription.JSON())
}
return ChannelWithSubscriptionJSON{
ChannelJSON: c.Channel.JSON(includeChannelKey),
Subscription: sub,
}
}
type ChannelJSON struct {
ChannelID ChannelID `json:"channel_id"`
OwnerUserID UserID `json:"owner_user_id"`
@@ -42,6 +65,11 @@ type ChannelJSON struct {
MessagesSent int `json:"messages_sent"`
}
type ChannelWithSubscriptionJSON struct {
ChannelJSON
Subscription *SubscriptionJSON `json:"subscription"`
}
type ChannelDB struct {
ChannelID ChannelID `db:"channel_id"`
OwnerUserID UserID `db:"owner_user_id"`
@@ -67,6 +95,22 @@ func (c ChannelDB) Model() Channel {
}
}
type ChannelWithSubscriptionDB struct {
ChannelDB
Subscription *SubscriptionDB `db:"sub"`
}
func (c ChannelWithSubscriptionDB) Model() ChannelWithSubscription {
var sub *Subscription = nil
if c.Subscription != nil {
sub = langext.Ptr(c.Subscription.Model())
}
return ChannelWithSubscription{
Channel: c.ChannelDB.Model(),
Subscription: sub,
}
}
func DecodeChannel(r *sqlx.Rows) (Channel, error) {
data, err := sq.ScanSingle[ChannelDB](r, true)
if err != nil {
@@ -82,3 +126,19 @@ func DecodeChannels(r *sqlx.Rows) ([]Channel, error) {
}
return langext.ArrMap(data, func(v ChannelDB) Channel { return v.Model() }), nil
}
func DecodeChannelWithSubscription(r *sqlx.Rows) (ChannelWithSubscription, error) {
data, err := sq.ScanSingle[ChannelWithSubscriptionDB](r, true)
if err != nil {
return ChannelWithSubscription{}, err
}
return data.Model(), nil
}
func DecodeChannelsWithSubscription(r *sqlx.Rows) ([]ChannelWithSubscription, error) {
data, err := sq.ScanAll[ChannelWithSubscriptionDB](r, true)
if err != nil {
return nil, err
}
return langext.ArrMap(data, func(v ChannelWithSubscriptionDB) ChannelWithSubscription { return v.Model() }), nil
}

View File

@@ -45,10 +45,10 @@ func (f MessageFilter) SQL() (string, string, sq.PP, error) {
joinClause := ""
if f.ConfirmedSubscriptionBy != nil {
joinClause += " LEFT JOIN subscriptions subs on messages.channel_id = subs.channel_id "
joinClause += " LEFT JOIN subscriptions AS subs on messages.channel_id = subs.channel_id "
}
if f.SearchString != nil {
joinClause += " JOIN messages_fts mfts on (mfts.rowid = messages.scn_message_id) "
joinClause += " JOIN messages_fts AS mfts on (mfts.rowid = messages.scn_message_id) "
}
sqlClauses := make([]string, 0)