58 lines
1.1 KiB
JavaScript
58 lines
1.1 KiB
JavaScript
export const ENSURE_TYPE = `
|
|
IF NOT EXISTS (
|
|
SELECT 1
|
|
FROM numbers
|
|
WHERE company_id = @companyId AND [type] = @type
|
|
)
|
|
BEGIN
|
|
INSERT INTO numbers (company_id, [type], prefix, padding, [next])
|
|
VALUES (@companyId, @type, ISNULL(@prefix, ''), ISNULL(@padding, 0), ISNULL(@next, 1));
|
|
END
|
|
`;
|
|
|
|
export const LIST_BY_COMPANY = `
|
|
SELECT
|
|
id,
|
|
[type],
|
|
prefix,
|
|
padding,
|
|
[next] AS [next]
|
|
FROM numbers
|
|
WHERE company_id = @companyId
|
|
ORDER BY [type]
|
|
`;
|
|
|
|
export const GET_BY_COMPANY_AND_ID = `
|
|
SELECT
|
|
id,
|
|
[type],
|
|
prefix,
|
|
padding,
|
|
[next] AS [next]
|
|
FROM numbers
|
|
WHERE id = @id
|
|
AND company_id = @companyId
|
|
`;
|
|
|
|
export const UPDATE_BY_COMPANY_AND_ID = `
|
|
UPDATE numbers
|
|
SET
|
|
prefix = @prefix,
|
|
padding = @padding,
|
|
[next] = @next
|
|
OUTPUT INSERTED.id, INSERTED.[type], INSERTED.prefix, INSERTED.padding, INSERTED.[next]
|
|
WHERE id = @id
|
|
AND company_id = @companyId
|
|
`;
|
|
|
|
export const GET_FOR_UPDATE = `
|
|
SELECT id, [type], prefix, padding, [next] AS [next]
|
|
FROM numbers WITH (UPDLOCK, ROWLOCK)
|
|
WHERE company_id = @companyId AND [type] = @type;
|
|
`;
|
|
|
|
export const INCREMENT = `
|
|
UPDATE numbers SET [next] = [next] + 1 WHERE id = @id;
|
|
`;
|
|
|