internal/session: tidy up query
This commit is contained in:
1 parent
b6fe9824ab
commit
9a23bd14f0
3 files changed
+11
-50
No files matched your search
@@ -124,27 +124,6 @@ from
|
||||
session_connection_limit, session_connection_count;
|
||||
`
|
||||
|
||||
sessionList = `
|
||||
with
|
||||
session_ids as (
|
||||
select public_id
|
||||
from session as s
|
||||
-- where clause is constructed
|
||||
%s
|
||||
-- order by clause is constructed
|
||||
%s
|
||||
-- limit is constructed
|
||||
%s
|
||||
)
|
||||
select *
|
||||
from session_list
|
||||
where
|
||||
session_list.public_id in (select * from session_ids)
|
||||
-- order by clause again since order from cte is not guaranteed to be preserved
|
||||
%s
|
||||
;
|
||||
`
|
||||
|
||||
terminateSessionIfPossible = `
|
||||
-- is terminate_session_id in a canceling state
|
||||
with session_version as (
|
||||
@@ -427,8 +406,7 @@ where session_id = ?
|
||||
with session_ids as (
|
||||
select public_id
|
||||
from session
|
||||
-- search condition for applying permissions is constructed
|
||||
where %s
|
||||
where %s -- search condition for applying permissions is constructed
|
||||
order by create_time desc, public_id desc
|
||||
limit %d
|
||||
)
|
||||
@@ -442,8 +420,7 @@ with session_ids as (
|
||||
select public_id
|
||||
from session
|
||||
where (create_time, public_id) < (@last_item_create_time, @last_item_id)
|
||||
-- search condition for applying permissions is constructed
|
||||
and %s
|
||||
and %s -- search condition for applying permissions is constructed
|
||||
order by create_time desc, public_id desc
|
||||
limit %d
|
||||
)
|
||||
@@ -457,8 +434,7 @@ with session_ids as (
|
||||
select public_id
|
||||
from session
|
||||
where update_time > @updated_after_time
|
||||
-- search condition for applying permissions is constructed
|
||||
and %s
|
||||
and %s -- search condition for applying permissions is constructed
|
||||
order by update_time desc, public_id desc
|
||||
limit %d
|
||||
)
|
||||
@@ -473,8 +449,7 @@ with session_ids as (
|
||||
from session
|
||||
where update_time > @updated_after_time
|
||||
and (update_time, public_id) < (@last_item_update_time, @last_item_id)
|
||||
-- search condition for applying permissions is constructed
|
||||
and %s
|
||||
and %s -- search condition for applying permissions is constructed
|
||||
order by update_time desc, public_id desc
|
||||
limit %d
|
||||
)
|
||||
|
||||
@@ -290,16 +290,9 @@ func (r *Repository) listSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
var permissionWhereClause string
|
||||
if len(where) > 0 {
|
||||
permissionWhereClause = "(" + strings.Join(where, " or ") + ")"
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause += " and termination_reason is null"
|
||||
}
|
||||
} else {
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause = " where termination_reason is null"
|
||||
}
|
||||
permissionWhereClause := "(" + strings.Join(where, " or ") + ")"
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause += " and termination_reason is null"
|
||||
}
|
||||
|
||||
limit := r.defaultLimit
|
||||
@@ -339,16 +332,9 @@ func (r *Repository) listSessionsRefresh(ctx context.Context, updatedAfter time.
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
var permissionWhereClause string
|
||||
if len(where) > 0 {
|
||||
permissionWhereClause = "(" + strings.Join(where, " or ") + ")"
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause += " and termination_reason is null"
|
||||
}
|
||||
} else {
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause = " where termination_reason is null"
|
||||
}
|
||||
permissionWhereClause := "(" + strings.Join(where, " or ") + ")"
|
||||
if !opts.withTerminated {
|
||||
permissionWhereClause += " and termination_reason is null"
|
||||
}
|
||||
|
||||
limit := r.defaultLimit
|
||||
|
||||
@@ -132,7 +132,7 @@ func TestRepository_convertToSessions(t *testing.T) {
|
||||
sess, err = repo.CreateSession(ctx, sessionWrapper, sess, []string{"0.0.0.0"})
|
||||
require.NoError(t, err)
|
||||
|
||||
query := fmt.Sprintf(sessionList, "", "", "", "")
|
||||
query := fmt.Sprintf(listSessionsTemplate, "termination_reason is null", 1000)
|
||||
rows, err := rw.Query(ctx, query, nil)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
|
||||
Reference in new issue
Block a user