Merge pull request #414 from sosedoff/fix-estimation-for-camelcase-schemas

Fix estimation for camelcase schemas
This commit is contained in:
Dan Sosedoff 2019-02-08 14:25:19 -06:00 committed by GitHub
commit 26cd70cbaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 4 deletions

View File

@ -224,9 +224,7 @@ func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error)
func (client *Client) EstimatedTableRowsCount(table string, opts RowsOptions) (*Result, error) { func (client *Client) EstimatedTableRowsCount(table string, opts RowsOptions) (*Result, error) {
schema, table := getSchemaAndTable(table) schema, table := getSchemaAndTable(table)
sql := fmt.Sprintf(`SELECT reltuples FROM pg_class WHERE oid = '%s.%s'::regclass;`, schema, table) result, err := client.query(statements.EstimatedTableRowCount, schema, table)
result, err := client.query(sql)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -327,6 +327,29 @@ func testTableConstraints(t *testing.T) {
assert.Equal(t, Row{"integrity", "CHECK (book_id IS NOT NULL AND edition IS NOT NULL)"}, res.Rows[1]) assert.Equal(t, Row{"integrity", "CHECK (book_id IS NOT NULL AND edition IS NOT NULL)"}, res.Rows[1])
} }
func testTableNameWithCamelCase(t *testing.T) {
testClient.db.MustExec(`CREATE TABLE "exampleTable" (id int, name varchar);`)
testClient.db.MustExec(`INSERT INTO "exampleTable" (id, name) VALUES (1, 'foo'), (2, 'bar');`)
_, err := testClient.Table("exampleTable")
assert.NoError(t, err)
_, err = testClient.TableInfo("exampleTable")
assert.NoError(t, err)
_, err = testClient.TableConstraints("exampleTable")
assert.NoError(t, err)
_, err = testClient.TableIndexes("exampleTable")
assert.NoError(t, err)
_, err = testClient.TableRowsCount("exampleTable", RowsOptions{})
assert.NoError(t, err)
_, err = testClient.EstimatedTableRowsCount("exampleTable", RowsOptions{})
assert.NoError(t, err)
}
func testQuery(t *testing.T) { func testQuery(t *testing.T) {
res, err := testClient.Query("SELECT * FROM books") res, err := testClient.Query("SELECT * FROM books")
@ -444,6 +467,7 @@ func TestAll(t *testing.T) {
testTableRowsCountWithLargeTable(t) testTableRowsCountWithLargeTable(t)
testTableIndexes(t) testTableIndexes(t)
testTableConstraints(t) testTableConstraints(t)
testTableNameWithCamelCase(t)
testQuery(t) testQuery(t)
testQueryError(t) testQueryError(t)
testQueryInvalidTable(t) testQueryInvalidTable(t)

View File

@ -37,6 +37,17 @@ SELECT
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
EstimatedTableRowCount = `
SELECT
reltuples
FROM
pg_class
WHERE
oid = ('"' || $1::text || '"."' || $2::text || '"')::regclass
`
// ---------------------------------------------------------------------------
TableIndexes = ` TableIndexes = `
SELECT SELECT
indexname, indexdef indexname, indexdef