// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.29.0 // source: workspaces.sql package organization import ( "context" "database/sql" ) const countWorkspacesByOrgID = `-- name: CountWorkspacesByOrgID :one SELECT COUNT(*) FROM core.workspaces WHERE org_id = $1 ` func (q *Queries) CountWorkspacesByOrgID(ctx context.Context, orgID string) (int64, error) { row := q.db.QueryRowContext(ctx, countWorkspacesByOrgID, orgID) var count int64 err := row.Scan(&count) return count, err } const createWorkspace = `-- name: CreateWorkspace :one INSERT INTO core.workspaces (org_id, name, key) VALUES ($1, $2, $3) RETURNING workspace_id, org_id, name, key, description, status, created_at, updated_at ` type CreateWorkspaceParams struct { OrgID string `json:"org_id"` Name string `json:"name"` Key sql.NullString `json:"key"` } // The name is what people see and the only thing the create form asks for; // uq_workspaces_org_id_name_ci keeps it unambiguous within the organization // among live workspaces. `key` is the optional declarative address // (entity-keys §3), unique within the organization rather than globally // because a workspace is a child; UI-created workspaces leave it NULL, and a // singleton workspace is addressed through its organization. func (q *Queries) CreateWorkspace(ctx context.Context, arg CreateWorkspaceParams) (Workspace, error) { row := q.db.QueryRowContext(ctx, createWorkspace, arg.OrgID, arg.Name, arg.Key) var i Workspace err := row.Scan( &i.WorkspaceID, &i.OrgID, &i.Name, &i.Key, &i.Description, &i.Status, &i.CreatedAt, &i.UpdatedAt, ) return i, err } const ensureSystemWorkspace = `-- name: EnsureSystemWorkspace :exec INSERT INTO core.workspaces (org_id, name) VALUES ($1, $2) ON CONFLICT (org_id, lower(name)) WHERE status <> 'deleted' DO NOTHING ` type EnsureSystemWorkspaceParams struct { OrgID string `json:"org_id"` Name string `json:"name"` } // Idempotently insert the System tenant's workspace, keyed on its name // within the organization (entity-keys). The conflict target // repeats the predicate of the partial index uq_workspaces_org_id_name_ci // so Postgres can infer it. Only called when the system org has no // workspace yet. func (q *Queries) EnsureSystemWorkspace(ctx context.Context, arg EnsureSystemWorkspaceParams) error { _, err := q.db.ExecContext(ctx, ensureSystemWorkspace, arg.OrgID, arg.Name) return err } const getSystemOrgWorkspace = `-- name: GetSystemOrgWorkspace :one SELECT w.workspace_id, w.org_id, w.name, w.key, w.description, w.status, w.created_at, w.updated_at FROM core.workspaces w JOIN core.organizations o ON o.org_id = w.org_id WHERE o.org_type = 'system' ORDER BY w.created_at LIMIT 1 ` // Natural-key resolver: the workspace of the singleton organization whose // org_type is 'system'. Returns the oldest workspace so a pre-existing // workspace (e.g. an ad-hoc seed using a different name) is adopted rather // than duplicated. func (q *Queries) GetSystemOrgWorkspace(ctx context.Context) (Workspace, error) { row := q.db.QueryRowContext(ctx, getSystemOrgWorkspace) var i Workspace err := row.Scan( &i.WorkspaceID, &i.OrgID, &i.Name, &i.Key, &i.Description, &i.Status, &i.CreatedAt, &i.UpdatedAt, ) return i, err } const getWorkspaceByID = `-- name: GetWorkspaceByID :one SELECT workspace_id, org_id, name, key, description, status, created_at, updated_at FROM core.workspaces WHERE workspace_id = $1 ` func (q *Queries) GetWorkspaceByID(ctx context.Context, workspaceID string) (Workspace, error) { row := q.db.QueryRowContext(ctx, getWorkspaceByID, workspaceID) var i Workspace err := row.Scan( &i.WorkspaceID, &i.OrgID, &i.Name, &i.Key, &i.Description, &i.Status, &i.CreatedAt, &i.UpdatedAt, ) return i, err } const getWorkspaceByOrgAndKey = `-- name: GetWorkspaceByOrgAndKey :one SELECT workspace_id, org_id, name, key, description, status, created_at, updated_at FROM core.workspaces WHERE org_id = $1 AND key = $2 ` type GetWorkspaceByOrgAndKeyParams struct { OrgID string `json:"org_id"` Key sql.NullString `json:"key"` } // Child key resolver (entity-keys §5): the declarative address of a // workspace is its organization's key plus its own, never a name. // uq_workspaces_org_id_key guarantees at most one row matches. func (q *Queries) GetWorkspaceByOrgAndKey(ctx context.Context, arg GetWorkspaceByOrgAndKeyParams) (Workspace, error) { row := q.db.QueryRowContext(ctx, getWorkspaceByOrgAndKey, arg.OrgID, arg.Key) var i Workspace err := row.Scan( &i.WorkspaceID, &i.OrgID, &i.Name, &i.Key, &i.Description, &i.Status, &i.CreatedAt, &i.UpdatedAt, ) return i, err } const getWorkspacesByOrgID = `-- name: GetWorkspacesByOrgID :many SELECT workspace_id, org_id, name, key, description, status, created_at, updated_at FROM core.workspaces WHERE org_id = $1 ORDER BY name ` func (q *Queries) GetWorkspacesByOrgID(ctx context.Context, orgID string) ([]Workspace, error) { rows, err := q.db.QueryContext(ctx, getWorkspacesByOrgID, orgID) if err != nil { return nil, err } defer rows.Close() items := []Workspace{} for rows.Next() { var i Workspace if err := rows.Scan( &i.WorkspaceID, &i.OrgID, &i.Name, &i.Key, &i.Description, &i.Status, &i.CreatedAt, &i.UpdatedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Close(); err != nil { return nil, err } if err := rows.Err(); err != nil { return nil, err } return items, nil }