Commit 7688cb6d authored by Jason Moiron's avatar Jason Moiron

quote table names in Get (upstream 49e6e8b3d452020144f1666f6d5a615fbd1e45a5),...

quote table names in Get (upstream 49e6e8b3d452020144f1666f6d5a615fbd1e45a5), remove sql_test (upstream 75ffcc666f1ad3d82525dcbccb5e5bc758840947)
parent dce79110
......@@ -371,7 +371,7 @@ func (t *TableMap) bindGet() bindPlan {
}
}
s.WriteString(" from ")
s.WriteString(t.TableName)
s.WriteString(t.dbmap.Dialect.QuoteField(t.TableName))
s.WriteString(" where ")
for x := range t.keys {
col := t.keys[x]
......
package modl
import (
"bytes"
"database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
_ "github.com/ziutek/mymysql/godrv"
"log"
"os"
"reflect"
......@@ -710,6 +711,35 @@ func initDbMap() *DbMap {
return dbmap
}
func TestQuoteTableNames(t *testing.T) {
dbmap := initDbMap()
defer dbmap.DropTables()
quotedTableName := dbmap.Dialect.QuoteField("person_test")
// Use a buffer to hold the log to check generated queries
logBuffer := &bytes.Buffer{}
dbmap.TraceOn("", log.New(logBuffer, "modltest:", log.Lmicroseconds))
// Create some rows
p1 := &Person{0, 0, 0, "bob", "smith", 0}
errorTemplate := "Expected quoted table name %v in query but didn't find it"
// Check if Insert quotes the table name
id := dbmap.Insert(p1)
if !bytes.Contains(logBuffer.Bytes(), []byte(quotedTableName)) {
t.Errorf(errorTemplate, quotedTableName)
}
logBuffer.Reset()
// Check if Get quotes the table name
dbmap.Get(Person{}, id)
if !bytes.Contains(logBuffer.Bytes(), []byte(quotedTableName)) {
t.Errorf(errorTemplate, quotedTableName)
}
logBuffer.Reset()
}
func initDbMapNulls() *DbMap {
dbmap := newDbMap()
//dbmap.TraceOn("", log.New(os.Stdout, "modltest: ", log.Lmicroseconds))
......@@ -742,7 +772,7 @@ func connect(driver string) *sql.DB {
func dialectAndDriver() (Dialect, string) {
switch os.Getenv("MODL_TEST_DIALECT") {
case "mysql":
return MySQLDialect{"InnoDB", "UTF8"}, "mymysql"
return MySQLDialect{"InnoDB", "UTF8"}, "mysql"
case "postgres":
return PostgresDialect{}, "postgres"
case "sqlite":
......
package modl_test
import (
"database/sql"
_ "github.com/ziutek/mymysql/godrv"
"testing"
)
func connectDb() *sql.DB {
db, err := sql.Open("mymysql", "gomysql_test/gomysql_test/abc123")
if err != nil {
panic("Error connecting to db: " + err.Error())
}
return db
}
// This fails on my machine with:
//
// panic: Received #1461 error from MySQL server:
// "Can't create more than max_prepared_stmt_count statements
// (current value: 16382)"
//
// Cause: stmt.Exec() is opening a new db connection for each call
// because each connection is still considered in use
//
func _TestPrepareExec(t *testing.T) {
db := connectDb()
defer db.Close()
db.Exec("drop table if exists test")
db.Exec("create table test (id int primary key, str varchar(20))")
query := "insert into test values (?, ?)"
stmt, err := db.Prepare(query)
if err != nil {
panic(err)
}
defer stmt.Close()
for i := 0; i < 20000; i++ {
_, err := stmt.Exec(i, "some str")
if err != nil {
panic(err)
}
}
}
// This works
func _TestQuery(t *testing.T) {
db := connectDb()
defer db.Close()
db.Exec("drop table if exists test")
db.Exec("create table test (id int primary key, str varchar(20))")
query := "insert into test values (?, ?)"
for i := 0; i < 20000; i++ {
_, err := db.Exec(query, i, "some str")
if err != nil {
panic(err)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment