From 2ddd7ada9b283d501337dced20aea3faa23f0018 Mon Sep 17 00:00:00 2001 From: Harvey Kandola Date: Thu, 4 Apr 2019 12:08:57 +0100 Subject: [PATCH] Make storage providers generate row limit clause --- core/env/provider.go | 3 +++ domain/store/context.go | 18 ++++++++++++++++++ edition/storage/mysql.go | 5 +++++ edition/storage/postgresql.go | 5 +++++ edition/storage/sqlserver.go | 5 +++++ 5 files changed, 36 insertions(+) diff --git a/core/env/provider.go b/core/env/provider.go index 559aedd7..da314273 100644 --- a/core/env/provider.go +++ b/core/env/provider.go @@ -111,4 +111,7 @@ type StoreProvider interface { // IsFalse returns storage provider boolean FALSE: // MySQL is 0, PostgresSQL is FALSE, SQL Server is 0 IsFalse() string + + // RowLimit returns SQL for limited number of returned rows + RowLimit(max int) string } diff --git a/domain/store/context.go b/domain/store/context.go index 383662a3..4f919a6e 100644 --- a/domain/store/context.go +++ b/domain/store/context.go @@ -119,3 +119,21 @@ func (c *Context) IsTrue() string { func (c *Context) IsFalse() string { return c.Runtime.StoreProvider.IsFalse() } + +// RowLimitVariants returns the SQL clause for limiting rows. +// Depending on the storage provider, the limit clause goes either +// at the start of the query or the end. +func (c *Context) RowLimitVariants(max int) (variantStart string, variantEnd string) { + variantStart = "" + variantEnd = "" + + if c.Runtime.StoreProvider.Type() == env.StoreTypeSQLServer { + // SQL Server and variants use 'SELECT TOP nnn' syntax. + variantStart = c.Runtime.StoreProvider.RowLimit(max) + } else { + // MySQL and variants use 'SELECT ... LIMIT nnn' syntax. + variantEnd = c.Runtime.StoreProvider.RowLimit(max) + } + + return +} diff --git a/edition/storage/mysql.go b/edition/storage/mysql.go index 28b87c68..a9af2ec2 100644 --- a/edition/storage/mysql.go +++ b/edition/storage/mysql.go @@ -364,6 +364,11 @@ func (p MySQLProvider) IsFalse() string { return "0" } +// RowLimit returns SQL for limiting number of rows returned. +func (p MySQLProvider) RowLimit(max int) string { + return fmt.Sprintf("LIMIT %d", max) +} + // convertDatabaseVersion turns database version string as major,minor,patch numerics. func convertDatabaseVersion(v string) (ints []int, err error) { ints = []int{0, 0, 0} diff --git a/edition/storage/postgresql.go b/edition/storage/postgresql.go index effcc1cc..4695bd51 100644 --- a/edition/storage/postgresql.go +++ b/edition/storage/postgresql.go @@ -305,3 +305,8 @@ func (p PostgreSQLProvider) IsTrue() string { func (p PostgreSQLProvider) IsFalse() string { return "false" } + +// RowLimit returns SQL for limiting number of rows returned. +func (p PostgreSQLProvider) RowLimit(max int) string { + return fmt.Sprintf("LIMIT %d", max) +} diff --git a/edition/storage/sqlserver.go b/edition/storage/sqlserver.go index 3baf323d..50ddfc1b 100644 --- a/edition/storage/sqlserver.go +++ b/edition/storage/sqlserver.go @@ -370,3 +370,8 @@ func (p SQLServerProvider) IsTrue() string { func (p SQLServerProvider) IsFalse() string { return "0" } + +// RowLimit returns SQL for limiting number of rows returned. +func (p SQLServerProvider) RowLimit(max int) string { + return fmt.Sprintf("TOP %d", max) +}