1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hanchuanchuan-goInception

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
session_inception_test.go 120 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hanchuanchuan Отправлено 4 лет назад 9b24954
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package session_test
import (
"fmt"
"strconv"
"strings"
"testing"
_ "github.com/go-sql-driver/mysql"
"github.com/hanchuanchuan/goInception/config"
"github.com/hanchuanchuan/goInception/session"
"github.com/hanchuanchuan/goInception/util/testkit"
. "github.com/pingcap/check"
"golang.org/x/net/context"
)
var _ = Suite(&testSessionIncSuite{})
type SQLError = session.SQLError
// SQLAudit 审核类
type SQLAudit struct {
sql string
errors []*SQLError
}
func TestAudit(t *testing.T) {
TestingT(t)
}
type testSessionIncSuite struct {
testCommon
audits []SQLAudit
}
func (s *testSessionIncSuite) add(sql string, errors ...*SQLError) {
s.audits = append(s.audits, SQLAudit{
sql: sql,
errors: errors,
})
}
func (s *testSessionIncSuite) SetUpSuite(c *C) {
s.initSetUp(c)
inc := &s.defaultInc
inc.EnableFingerprint = true
inc.SqlSafeUpdates = 0
inc.EnableDropTable = true
inc.EnableIdentiferKeyword = true
}
func (s *testSessionIncSuite) TearDownSuite(c *C) {
s.tearDownSuite(c)
}
func (s *testSessionIncSuite) TearDownTest(c *C) {
s.reset()
s.tearDownTest(c)
}
func (s *testSessionIncSuite) testErrorCode(c *C, sql string, errors ...*session.SQLError) {
if s.isAPI {
s.sessionService.LoadOptions(session.SourceOptions{
Host: s.defaultInc.BackupHost,
Port: int(s.defaultInc.BackupPort),
User: s.defaultInc.BackupUser,
Password: s.defaultInc.BackupPassword,
RealRowCount: s.realRowCount,
})
s.testAuditResult(c, sql, errors...)
return
}
if s.tk == nil {
s.tk = testkit.NewTestKitWithInit(c, s.store)
}
// session.CheckAuditSetting(config.GetGlobalConfig())
s.runCheck(sql)
row := s.rows[s.getAffectedRows()-1]
errCode := 0
if len(errors) > 0 {
for _, e := range errors {
level := session.GetErrorLevel(e.Code)
if int(level) > errCode {
errCode = int(level)
}
}
}
if errCode > 0 {
errMsgs := []string{}
for _, e := range errors {
errMsgs = append(errMsgs, e.Error())
}
c.Assert(row[4], Equals, strings.Join(errMsgs, "\n"), Commentf("%v", s.rows))
}
c.Assert(row[2], Equals, strconv.Itoa(errCode), Commentf("%v", row))
}
func (s *testSessionIncSuite) testAuditResult(c *C, sql string, errors ...*session.SQLError) {
result, err := s.sessionService.Audit(context.Background(), s.useDB+sql)
c.Assert(err, IsNil)
// for _, row := range result {
// if row.ErrLevel == 2 {
// fmt.Println(fmt.Sprintf("sql: %v, err: %v", row.Sql, row.ErrorMessage))
// } else {
// fmt.Println(fmt.Sprintf("[%v] sql: %v", session.StatusList[row.StageStatus], row.Sql))
// }
// }
s.records = result
row := result[len(result)-1]
errCode := uint8(0)
if len(errors) > 0 {
for _, e := range errors {
level := session.GetErrorLevel(e.Code)
if level > errCode {
errCode = level
}
}
}
if errCode > 0 {
errMsgs := []string{}
for _, e := range errors {
errMsgs = append(errMsgs, e.Error())
}
c.Assert(row.ErrorMessage, Equals, strings.Join(errMsgs, "\n"), Commentf("%v", result))
}
c.Assert(row.ErrLevel, Equals, errCode, Commentf("%#v", row))
}
func (s *testSessionIncSuite) testManyErrors(c *C, sql string, errors ...*session.SQLError) {
if s.tk == nil {
s.tk = testkit.NewTestKitWithInit(c, s.store)
}
s.runCheck(sql)
errCode := 0
if len(errors) > 0 {
for _, e := range errors {
level := session.GetErrorLevel(e.Code)
if int(level) > errCode {
errCode = int(level)
}
}
}
allErrors := []string{}
for _, row := range s.getResultRows() {
if v, ok := row[4].(string); ok {
v = strings.TrimSpace(v)
if v != "<nil>" && v != "" {
allErrors = append(allErrors, v)
}
}
}
errMsgs := []string{}
for _, e := range errors {
errMsgs = append(errMsgs, e.Error())
}
// c.Assert(len(errMsgs), Equals, len(allErrors), Commentf("%v", allErrors))
c.Assert(len(errMsgs), Equals, len(allErrors), Commentf("%#v", s.rows))
for index, err := range allErrors {
c.Assert(err, Equals, errMsgs[index], Commentf("%v", s.rows))
}
}
func (s *testSessionIncSuite) runAudit(c *C) {
c.Assert(len(s.audits), Not(Equals), 0)
var sqls []string
for _, a := range s.audits {
sqls = append(sqls, a.sql)
}
session.TestCheckAuditSetting(config.GetGlobalConfig())
a := `/*%s;--check=1;--backup=0;--enable-ignore-warnings;real_row_count=%v;*/
inception_magic_start;
%s
%s;
inception_magic_commit;`
res := s.tk.MustQueryInc(fmt.Sprintf(a, s.getAddr(), s.realRowCount, s.useDB,
strings.Join(sqls, ";\n"),
))
s.rows = res.Rows()
rows := s.rows
c.Assert(len(s.audits), Equals, len(rows)-1)
for index, row := range rows {
errors := s.audits[index].errors
errCode := 0
if len(errors) > 0 {
for _, e := range errors {
level := session.GetErrorLevel(e.Code)
if int(level) > errCode {
errCode = int(level)
}
}
}
if errCode > 0 {
errMsgs := []string{}
for _, e := range errors {
errMsgs = append(errMsgs, e.Error())
}
c.Assert(row[4], Equals, strings.Join(errMsgs, "\n"), Commentf("%v", rows))
}
c.Assert(row[2], Equals, strconv.Itoa(errCode), Commentf("%v", row))
}
s.audits = nil
}
func (s *testCommon) assertAudit(c *C,
rows [][]interface{},
allErrors ...[]*SQLError) {
c.Assert(len(rows), Not(Equals), 0)
c.Assert(len(rows), Equals, len(allErrors), Commentf("%v", rows))
for index, row := range rows {
errors := allErrors[index]
errCode := 0
if len(errors) > 0 {
for _, e := range errors {
level := session.GetErrorLevel(e.Code)
if int(level) > errCode {
errCode = int(level)
}
}
}
if errCode > 0 {
errMsgs := []string{}
for _, e := range errors {
errMsgs = append(errMsgs, e.Error())
}
c.Assert(row[4], Equals, strings.Join(errMsgs, "\n"), Commentf("%v", rows))
}
if s.isAPI {
c.Assert(row[2], Equals, int64(errCode), Commentf("%v", row))
} else {
c.Assert(row[2], Equals, strconv.Itoa(errCode), Commentf("%v", row))
}
}
}
func (s *testSessionIncSuite) TestBegin(c *C) {
if testing.Short() {
c.Skip("skipping test; in TRAVIS mode")
}
res := s.tk.MustQueryInc("create table t1(id int);")
s.rows = res.Rows()
c.Assert(len(s.rows), Equals, 1, Commentf("%v", s.rows))
for _, row := range s.rows {
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Must start as begin statement.")
}
}
func (s *testSessionIncSuite) TestNoSourceInfo(c *C) {
res := s.tk.MustQueryInc(`inception_magic_start;
create table t1(id int);`)
s.rows = res.Rows()
c.Assert(s.getAffectedRows(), Equals, 1)
for _, row := range s.rows {
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Invalid source infomation(inception语法格式错误).")
}
}
func (s *testSessionIncSuite) TestNoSourceInfo2(c *C) {
res := s.tk.MustQueryInc(`/*--check=1;*/
inception_magic_start;create table t1(id int)`)
s.rows = res.Rows()
c.Assert(s.getAffectedRows(), Equals, 1)
for _, row := range s.rows {
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Invalid source infomation(主机名为空,端口为0,用户名为空).")
}
}
func (s *testSessionIncSuite) TestWrongDBName(c *C) {
res := s.tk.MustQueryInc(fmt.Sprintf(`/*%s;--check=1;--backup=0;--ignore-warnings=1;*/
inception_magic_start;create table t1(id int);inception_magic_commit;`, s.getAddr()))
s.rows = res.Rows()
c.Assert(s.getAffectedRows(), Equals, 1)
for _, row := range s.rows {
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Incorrect database name ''.")
}
}
func (s *testSessionIncSuite) TestEnd(c *C) {
res := s.tk.MustQueryInc(fmt.Sprintf(`/*%s;--check=1;--backup=0;--ignore-warnings=1;*/
inception_magic_start;use test_inc;create table t1(id int);`, s.getAddr()))
s.rows = res.Rows()
c.Assert(s.getAffectedRows(), Equals, 3)
row := s.rows[s.getAffectedRows()-1]
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Must end with commit.")
}
func (s *testSessionIncSuite) TestCreateTable(c *C) {
sql := ""
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
// 表存在
sql = "create table t1(id int);create table t1(id int);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_EXISTS_ERROR, "t1"))
// 重复列
sql = "create table test_error_code1 (c1 int, c2 int, c2 int)"
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_FIELDNAME, "c2"))
// 主键
config.GetGlobalConfig().Inc.CheckPrimaryKey = true
sql = ("create table t1(id int);")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_MUST_HAVE_PK, "t1"))
config.GetGlobalConfig().Inc.CheckPrimaryKey = false
// 数据类型 警告
sql = "create table t1(id int,c1 bit);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c1"))
sql = "create table t1(id int,c1 enum('red', 'blue', 'black'));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c1"))
sql = "create table t1(id int,c1 set('red', 'blue', 'black'));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c1"))
config.GetGlobalConfig().Inc.EnableTimeStampType = false
sql = "create table t1(id int,c1 timestamp);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c1"))
config.GetGlobalConfig().Inc.EnableTimeStampType = true
sql = "create table t1(id int,c1 timestamp);"
s.testErrorCode(c, sql)
// char列建议
config.GetGlobalConfig().Inc.MaxCharLength = 100
sql = `create table t1(id int,c1 char(200),c2 char(100));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHAR_TO_VARCHAR_LEN, "c1"))
config.GetGlobalConfig().Inc.MaxCharLength = 0
sql = `create table t1(id int,c1 char(200));`
s.testErrorCode(c, sql)
// 主键的默认值为函数
sql = "create table t1(c1 datetime(6) not null default current_timestamp(6) primary key);"
s.testErrorCode(c, sql)
// 关键字
config.GetGlobalConfig().Inc.EnableIdentiferKeyword = false
config.GetGlobalConfig().Inc.CheckIdentifier = true
sql = ("create table t1(id int, TABLES varchar(20),`c1$` varchar(20),c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20));")
s.testErrorCode(c, sql,
session.NewErr(session.ER_IDENT_USE_KEYWORD, "TABLES"),
session.NewErr(session.ER_INVALID_IDENT, "c1$"),
session.NewErr(session.ER_TOO_LONG_IDENT, "c1234567890123456789012345678901234567890123456789012345678901234567890"),
)
// 列注释
config.GetGlobalConfig().Inc.CheckColumnComment = true
sql = "create table t1(c1 varchar(20));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_HAVE_NO_COMMENT, "c1", "t1"))
config.GetGlobalConfig().Inc.CheckColumnComment = false
// 表注释
config.GetGlobalConfig().Inc.CheckTableComment = true
sql = ("create table t1(c1 varchar(20));")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_MUST_HAVE_COMMENT, "t1"))
config.GetGlobalConfig().Inc.CheckTableComment = false
s.mustCheck(c, "create table t1(c1 varchar(20));")
// 无效默认值
config.GetGlobalConfig().Inc.EnableEnumSetBit = true
sql = "create table t1(id int,c1 int default '');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
// sql = "create table t1(id int,c1 bit default '');"
// s.testErrorCode(c, sql,
// session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = "create table t1(id int,c1 bit default '0');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = "create table t1(id int,c1 bit default b'0');"
s.testErrorCode(c, sql)
// blob/text字段
config.GetGlobalConfig().Inc.EnableBlobType = false
sql = ("create table t1(id int,c1 blob, c2 text);")
s.testErrorCode(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c2"),
)
config.GetGlobalConfig().Inc.EnableBlobType = true
sql = ("create table t1(id int,c1 blob not null);")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"),
)
// 检查默认值
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true
sql = "create table t1(c1 varchar(10));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_DEFAULT_ADD_COLUMN, "c1", "t1"))
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false
// 支持innodb引擎
config.GetGlobalConfig().Inc.EnableSetEngine = true
config.GetGlobalConfig().Inc.SupportEngine = "innodb"
s.mustCheck(c, "create table t1(c1 varchar(10))engine = innodb;")
sql = ("create table t1(c1 varchar(10))engine = myisam;")
s.testErrorCode(c, sql,
session.NewErr(session.ErrEngineNotSupport, "innodb"))
// 时间戳 timestamp默认值
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp default CURRENT_TIMESTAMP);"
s.testErrorCode(c, sql,
session.NewErrf("Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause"))
config.GetGlobalConfig().Inc.CheckTimestampDefault = false
sql = "create table t1(id int primary key,c1 timestamp default CURRENT_TIMESTAMP,c2 timestamp ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp || !(strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE"))) {
s.testErrorCode(c, sql)
} else {
s.testErrorCode(c, sql, session.NewErr(session.ER_INVALID_DEFAULT, "c2"))
}
config.GetGlobalConfig().Inc.CheckTimestampDefault = true
sql = "create table t1(id int primary key,c1 timestamp default CURRENT_TIMESTAMP,c2 timestamp ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp {
s.testErrorCode(c, sql,
session.NewErr(session.ER_TIMESTAMP_DEFAULT, "c2"))
} else if strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE")) {
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c2"))
} else {
s.testErrorCode(c, sql)
}
config.GetGlobalConfig().Inc.CheckTimestampDefault = false
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp not null ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp || !(strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE"))) {
s.testErrorCode(c, sql)
} else {
s.testErrorCode(c, sql, session.NewErr(session.ER_INVALID_DEFAULT, "t2"))
}
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 date default CURRENT_TIMESTAMP);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "t2"))
// 时间戳 timestamp数量
config.GetGlobalConfig().Inc.CheckTimestampCount = false
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp || !(strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE"))) {
s.testErrorCode(c, sql)
} else {
s.testErrorCode(c, sql, session.NewErr(session.ER_INVALID_DEFAULT, "t2"))
}
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckTimestampCount = true
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp || !(strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE"))) {
s.testErrorCode(c, sql)
} else {
s.testErrorCode(c, sql, session.NewErr(session.ER_INVALID_DEFAULT, "t2"))
}
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp default CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);"
s.testErrorCode(c, sql,
session.NewErrf("Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause"))
sql = "create table test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
s.testErrorCode(c, sql, session.NewErr(session.ER_TOO_LONG_IDENT, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
sql = "create table aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_IDENT, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
sql = "create table test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))"
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_INDEX, "aa", "test_inc", "test_error_code1"),
session.NewErr(session.ER_DUP_KEYNAME, "aa"))
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code1"),
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "test_error_code1.c_not_exist"))
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "test_error_code1.c_not_exist"))
sql = "create table test_error_code1 (c1 int not null default '')"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "a"))
sql = "CREATE TABLE `t` (`a` double DEFAULT now());"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "a"))
// 字符集
config.GetGlobalConfig().Inc.EnableSetCharset = false
config.GetGlobalConfig().Inc.SupportCharset = ""
sql = "create table t1(a int) character set utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_CHARSET_MUST_NULL, "t1"))
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8mb4"
sql = "create table t1(a int) character set utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8mb4"))
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8,utf8mb4"
sql = "create table t1(a int) character set utf8;"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8,utf8mb4"
sql = "create table t1(a int) character set latin1;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"))
sql = "create table t1(a int) character set latin123;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"),
session.NewErrf("Unknown charset %s.", "latin123"))
// 外键
sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));"
s.testErrorCode(c, sql,
// session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code"),
session.NewErr(session.ER_FOREIGN_KEY, "test_error_code"))
sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key fk_1(b, c) references product(id));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_FOREIGN_KEY, "test_error_code"))
sql = "create table test_error_code_2;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_MUST_AT_LEAST_ONE_COLUMN))
sql = "create table test_error_code_2 (unique(c1));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_MUST_AT_LEAST_ONE_COLUMN))
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_MULTIPLE_PRI_KEY))
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), key cca(c2));"
s.testErrorCode(c, sql)
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), key(c2));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code_2"))
config.GetGlobalConfig().Inc.EnableNullIndexName = true
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), key(c2));"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.EnableNullIndexName = false
indexMaxLength := 767
if s.innodbLargePrefix {
indexMaxLength = 3072
}
config.GetGlobalConfig().Inc.EnableBlobType = false
config.GetGlobalConfig().Inc.CheckIndexPrefix = false
sql = "create table test_error_code_3(pt text ,primary key (pt));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "pt"),
session.NewErr(session.ER_TOO_LONG_KEY, "PRIMARY", indexMaxLength))
config.GetGlobalConfig().Inc.EnableBlobType = true
// 索引长度
sql = "create table test_error_code_3(a text, unique (a(3073)));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code_3"),
session.NewErr(session.ER_TOO_LONG_KEY, "", indexMaxLength))
sql = "create table test_error_code_3(c1 int,c2 text, unique uq_1(c1,c2(3069)));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_KEY, "uq_1", indexMaxLength))
// ----------------- 索引长度审核 varchar ----------------------
if s.innodbLargePrefix {
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(1024),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_KEY, "uq_1", indexMaxLength))
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(1023),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_KEY, "uq_1", indexMaxLength))
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(1022),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql)
} else {
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(256),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_KEY, "uq_1", indexMaxLength))
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(255),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql)
sql = "create table test_error_code_3(c1 int primary key,c2 varchar(254),c3 int, key uq_1(c2,c3)) default charset utf8;"
s.testErrorCode(c, sql)
}
// sql = "create table test_error_code_3(c1 int,c2 text, unique uq_1(c1,c2(3068)));"
// if indexMaxLength == 3072 {
// s.testErrorCode(c, sql)
// } else {
// s.testErrorCode(c, sql,
// session.NewErr(session.ER_TOO_LONG_KEY, "", indexMaxLength))
// }
sql = "create table test_error_code_3(pt blob ,primary key (pt));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_BLOB_USED_AS_KEY, "pt"))
sql = "create table test_error_code_3(`id` int, key `primary`(`id`));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "primary", "test_error_code_3"))
sql = "create table t2(c1.c2 varchar(10));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_TABLE_NAME, "c1"))
sql = "create table t2 (c1 int default null primary key , age int);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PRIMARY_CANT_HAVE_NULL))
sql = "create table t2 (id int null primary key , age int);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PRIMARY_CANT_HAVE_NULL))
sql = "create table t2 (id int default null, age int, primary key(id));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PRIMARY_CANT_HAVE_NULL))
sql = "create table t2 (id int null, age int, primary key(id));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PRIMARY_CANT_HAVE_NULL))
sql = `drop table if exists t1;create table t1(
id int auto_increment comment 'test',
crtTime datetime not null DEFAULT CURRENT_TIMESTAMP comment 'test',
uptTime datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment 'test',
primary key(id)) comment 'test';`
s.testErrorCode(c, sql)
// 5.7版本新增计算列
config.GetGlobalConfig().Inc.EnableJsonType = true
if s.DBVersion >= 50700 {
sql = `CREATE TABLE t1(c1 json DEFAULT '{}' COMMENT '日志记录',
type tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_BLOB_CANT_HAVE_DEFAULT, "c1"),
session.NewErr(session.ER_IDENT_USE_KEYWORD, "type"),
)
sql = `CREATE TABLE t1(c1 json DEFAULT NULL COMMENT '日志记录',
type1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
sql = `CREATE TABLE t1(c1 json COMMENT '日志记录',
type1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
// 计算列移除默认值校验
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true
sql = `CREATE TABLE t1(c1 json DEFAULT '{}' COMMENT '日志记录',
type1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_BLOB_CANT_HAVE_DEFAULT, "c1"))
sql = `CREATE TABLE t1(c1 json DEFAULT NULL COMMENT '日志记录',
type1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
sql = `CREATE TABLE t1(c1 json COMMENT '日志记录',
type1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL COMMENT '操作类型')
ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false
} else if s.DBVersion >= 50600 {
sql = `CREATE TABLE t1(c1 json DEFAULT '{}' COMMENT '日志记录') ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_BLOB_CANT_HAVE_DEFAULT, "c1"))
sql = `CREATE TABLE t1(c1 json DEFAULT NULL COMMENT '日志记录') ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
sql = `CREATE TABLE t1(c1 json COMMENT '日志记录') ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='xxx';`
s.testErrorCode(c, sql)
}
config.GetGlobalConfig().Inc.EnableNullable = false
sql = `drop table if exists t1;CREATE TABLE t1(c1 int);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NOT_ALLOWED_NULLABLE, "c1", "t1"))
if s.DBVersion >= 50700 {
sql = `CREATE TABLE t1(c1 tinyint(10) GENERATED ALWAYS AS (json_extract(operate_info, '$.type')) VIRTUAL);`
s.testErrorCode(c, sql)
}
config.GetGlobalConfig().Inc.EnableNullable = true
sql = `drop table if exists t1;CREATE TABLE t1(c1 int);`
s.testErrorCode(c, sql)
// 检查必须的字段
config.GetGlobalConfig().Inc.MustHaveColumns = "c1"
sql = `drop table if exists t1;CREATE TABLE t1(id int);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_MUST_HAVE_COLUMNS, "c1"))
sql = `drop table if exists t1;CREATE TABLE t1(c1 int);`
s.testErrorCode(c, sql)
// 配置参数时添加多余空格,判断对类型解析是否正确
config.GetGlobalConfig().Inc.MustHaveColumns = "c1 int,c2 datetime"
sql = `drop table if exists t1;CREATE TABLE t1(id int);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_MUST_HAVE_COLUMNS, "c1 int,c2 datetime"))
sql = `drop table if exists t1;CREATE TABLE t1(c1 bigint,c2 int);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_MUST_HAVE_COLUMNS, "c1 int,c2 datetime"))
sql = `drop table if exists t1;CREATE TABLE t1(c1 int,c2 datetime);`
s.testErrorCode(c, sql)
sql = `drop table if exists t1;CREATE TABLE t1(c1 int,c2 DATETIME);`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.MustHaveColumns = ""
// 如果表包含以下列,列必须有索引。
config.GetGlobalConfig().Inc.ColumnsMustHaveIndex = "c1 , c2 int "
sql = `drop table if exists t1;CREATE TABLE t1(id int, c1 int, c2 int, index idx_c1 (c1));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrColumnsMustHaveIndex, "c2"))
sql = `drop table if exists t1;CREATE TABLE t1(id int, c1 int, c2 varchar (20), index idx_c1 (c1));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrColumnsMustHaveIndexTypeErr, "c2", "int", "varchar"),
session.NewErr(session.ErrColumnsMustHaveIndex, "c2"))
sql = `drop table if exists t1;create table t1(id int,c2 int unique);`
s.testErrorCode(c, sql)
sql = `drop table if exists t1;create table t1(c1 int primary key,c2 int unique)`
s.testErrorCode(c, sql)
sql = `drop table if exists t1;CREATE TABLE t1(id int, c1 int, c2 int, index idx_1 (c1,c2));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrColumnsMustHaveIndex, "c2"))
sql = `drop table if exists t1;CREATE TABLE t1(id int,c1 int ,c2 int,index idx_c1(c1),index idx_c2(c2));`
s.testErrorCode(c, sql)
sql = `drop table if exists t1;CREATE TABLE t1(id int);ALTER TABLE t1 ADD COLUMN c2 varchar(20);`
s.testErrorCode(c, sql,
session.NewErr(session.ErrColumnsMustHaveIndexTypeErr, "c2", "int", "varchar"),
session.NewErr(session.ErrColumnsMustHaveIndex, "c2"))
sql = `drop table if exists t1;CREATE TABLE t1(id int);ALTER TABLE t1 ADD COLUMN c2 int,add index idx_c2(c2);`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.ColumnsMustHaveIndex = ""
config.GetGlobalConfig().Inc.CheckInsertField = false
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 0
// 测试表名大小写
sql = `drop table if exists t1;CREATE TABLE t1(c1 int);insert into T1 values(1);`
if s.ignoreCase {
s.testErrorCode(c, sql)
} else {
s.runCheck(sql)
row := s.rows[s.getAffectedRows()-1]
c.Assert(row[2], Equals, "2")
c.Assert(row[4], Equals, "Table 'test_inc.T1' doesn't exist.")
}
// 无效默认值
config.GetGlobalConfig().Inc.CheckAutoIncrementName = true
sql = `create table t1(c1 int auto_increment primary key,c2 int);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_AUTO_INCR_ID_WARNING, "c1"))
// 禁止设置存储引擎
config.GetGlobalConfig().Inc.EnableSetEngine = false
sql = ("drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;")
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_SET_ENGINE, "t1"))
// 允许设置存储引擎
config.GetGlobalConfig().Inc.EnableSetEngine = true
config.GetGlobalConfig().Inc.SupportEngine = "innodb"
s.mustCheck(c, "drop table if exists t1;create table t1(c1 varchar(10))engine = innodb;")
// 允许blob,text,json列设置为NOT NULL
config.GetGlobalConfig().Inc.EnableBlobNotNull = false
sql = `create table t1(id int auto_increment primary key,c1 blob not null);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"))
sql = `create table t1(id int auto_increment primary key,c1 text not null);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"))
if s.DBVersion >= 50600 {
sql = `create table t1(id int auto_increment primary key,c1 json not null);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"))
}
config.GetGlobalConfig().Inc.EnableBlobNotNull = true
sql = `create table t1(id int auto_increment primary key,c1 blob not null);`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckIndexPrefix = true
sql = "create table test_error_code_3(a text, unique (a(3073)));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code_3"),
session.NewErr(session.ER_INDEX_NAME_UNIQ_PREFIX, "",
config.GetGlobalConfig().Inc.UniqIndexPrefix, "test_error_code_3"),
session.NewErr(session.ER_TOO_LONG_KEY, "", indexMaxLength))
sql = "create table test_error_code_3(a text, key (a(3073)));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_NAME_FOR_INDEX, "NULL", "test_error_code_3"),
session.NewErr(session.ER_INDEX_NAME_IDX_PREFIX, "",
"test_error_code_3", config.GetGlobalConfig().Inc.IndexPrefix),
session.NewErr(session.ER_TOO_LONG_KEY, "", indexMaxLength))
config.GetGlobalConfig().Inc.IndexPrefix = "idx_,idx1_"
sql = "create table test_error_code_3(c1 int,c2 int,key idx_1(c1),key idx1_2(c2));"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.IndexPrefix = "idx_,idx1_"
sql = "create table test_error_code_3(c1 int,c2 int,key idx_1(c1),key idx2_2(c2));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INDEX_NAME_IDX_PREFIX, "idx2_2",
"test_error_code_3", config.GetGlobalConfig().Inc.IndexPrefix),
)
config.GetGlobalConfig().Inc.TablePrefix = "t_"
sql = "create table t1(id int primary key);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_PREFIX,
config.GetGlobalConfig().Inc.TablePrefix))
// tidb [v3.1.0 版本开始引入]
sql = "create table t1(a bigint primary key auto_random);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_PREFIX,
config.GetGlobalConfig().Inc.TablePrefix))
}
func (s *testSessionIncSuite) TestCreateTableAsSelect(c *C) {
if s.enforeGtidConsistency {
sql = "create table t1(id int primary key);create table t11 as select * from t1;"
s.testErrorCode(c, sql,
session.NewErrf("Statement violates GTID consistency: CREATE TABLE ... SELECT."))
} else {
sql = "create table t1(id int primary key);create table t11 as select * from t1;"
s.testErrorCode(c, sql)
}
}
func (s *testSessionIncSuite) TestDropTable(c *C) {
config.GetGlobalConfig().Inc.EnableDropTable = false
sql := ""
sql = "create table t1(id int);drop table t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_TABLE, "t1"))
config.GetGlobalConfig().Inc.EnableDropTable = true
sql = "create table t1(id int);drop table t1;"
s.testErrorCode(c, sql)
if s.DBVersion < 80000 {
s.mustRunExec(c, `drop table if exists t1;
create table t1(id int auto_increment primary key,c1 int);
insert into t1(id,c1)values(1,1),(2,2);`)
config.GetGlobalConfig().Inc.MaxDDLAffectRows = 1
sql = "drop table t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_TOO_MUCH_ROWS, "Drop", 2, 1))
}
}
func (s *testSessionIncSuite) TestAlterTableAddColumn(c *C) {
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
config.GetGlobalConfig().Inc.EnableDropTable = true
s.mustCheck(c, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int;")
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int;alter table t1 add column c1 int;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_EXISTED, "t1.c1"))
s.mustCheck(c, "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int first;alter table t1 add column c2 int after c1;")
// after 不存在的列
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c2 int after c1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c1"))
// 数据类型 警告
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c2 bit;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c2"))
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c2 enum('red', 'blue', 'black');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c2"))
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c2 set('red', 'blue', 'black');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "c2"))
// char列建议
config.GetGlobalConfig().Inc.MaxCharLength = 100
sql = `drop table if exists t1;create table t1(id int);
alter table t1 add column c1 char(200);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHAR_TO_VARCHAR_LEN, "c1"))
// 字符集
sql = `drop table if exists t1;create table t1(id int);
alter table t1 add column c1 varchar(20) character set utf8;
alter table t1 add column c2 varchar(20) COLLATE utf8_bin;`
s.testManyErrors(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c2"))
// 关键字
config.GetGlobalConfig().Inc.EnableIdentiferKeyword = false
config.GetGlobalConfig().Inc.CheckIdentifier = true
sql = ("drop table if exists t1;create table t1(id int);alter table t1 add column TABLES varchar(20);alter table t1 add column `c1$` varchar(20);alter table t1 add column c1234567890123456789012345678901234567890123456789012345678901234567890 varchar(20);")
s.testManyErrors(c, sql,
session.NewErr(session.ER_IDENT_USE_KEYWORD, "TABLES"),
session.NewErr(session.ER_INVALID_IDENT, "c1$"),
session.NewErr(session.ER_TOO_LONG_IDENT, "c1234567890123456789012345678901234567890123456789012345678901234567890"),
)
// 列注释
config.GetGlobalConfig().Inc.CheckColumnComment = true
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(20);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_HAVE_NO_COMMENT, "c1", "t1"))
config.GetGlobalConfig().Inc.CheckColumnComment = false
// 无效默认值
sql = ("drop table if exists t1;create table t1(id int);alter table t1 add column c1 int default '';")
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 int default '';"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
config.GetGlobalConfig().Inc.EnableEnumSetBit = true
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 bit default '0';"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
// blob/text字段
config.GetGlobalConfig().Inc.EnableBlobType = false
sql = ("drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob;alter table t1 add column c2 text;")
s.testManyErrors(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c2"),
)
config.GetGlobalConfig().Inc.EnableBlobType = true
sql = ("drop table if exists t1;create table t1(id int);alter table t1 add column c1 blob not null;")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"),
)
// 检查默认值
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true
sql = "drop table if exists t1;create table t1(id int);alter table t1 add column c1 varchar(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_DEFAULT_ADD_COLUMN, "c1", "t1"))
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false
sql = "drop table if exists t1;create table t1(id int primary key , age int);"
s.testErrorCode(c, sql)
// // add column
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add column c1 int"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_EXISTED, "t1.c1"))
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add column aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_IDENT, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
sql = "drop table if exists t1;alter table t1 comment 'test comment'"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t1"))
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add column `a ` int ;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_IDENT, "a "),
session.NewErr(session.ER_WRONG_COLUMN_NAME, "a "))
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add column c2 int on update current_timestamp;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_ON_UPDATE, "c2"))
sql = "drop table if exists t1;create table t1(c2 int on update current_timestamp);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_ON_UPDATE, "c2"))
if s.DBVersion >= 50600 {
config.GetGlobalConfig().Inc.EnableJsonType = true
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add c2 json;"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.EnableJsonType = false
sql = "drop table if exists t1;create table t1 (c1 int primary key);alter table t1 add c2 json;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrJsonTypeSupport, "c2"))
}
sql = "drop table if exists t1;create table t1 (id int primary key);alter table t1 add column (c1 int,c2 varchar(20));"
s.testErrorCode(c, sql)
// 指定特殊选项
sql = "drop table if exists t1;create table t1 (id int primary key);alter table t1 add column c1 int,ALGORITHM=INPLACE, LOCK=NONE;"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckIdentifier = false
// 特殊字符
sql = "drop table if exists `t3!@#$^&*()`;create table `t3!@#$^&*()`(id int primary key);alter table `t3!@#$^&*()` add column `c3!@#$^&*()2` int comment '123';"
s.testErrorCode(c, sql)
sql = "drop table if exists t1;create table t1(id int primary key);alter table t1 add column c1 int primary key;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_INDEX, "PRIMARY", "test_inc", "t1"))
sql = "drop table if exists t1;create table t1(id int,key c1(id));alter table t1 add column c1 int unique;"
s.testErrorCode(c, sql)
sql = `drop table if exists t1;
create table t1(id int,key c1(id));
alter table t1 add column c1 int unique;
alter table t1 add index c1(c1);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_INDEX, "c1", "test_inc", "t1"))
}
func (s *testSessionIncSuite) TestAlterTableAlterColumn(c *C) {
sql = ("create table t1(id int);alter table t1 alter column id set default '';")
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "id"))
s.mustCheck(c, "create table t1(id int);alter table t1 alter column id set default '1';")
s.mustCheck(c, "create table t1(id int);alter table t1 alter column id drop default ;alter table t1 alter column id set default '1';")
}
func (s *testSessionIncSuite) TestAlterTableModifyColumn(c *C) {
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
s.runCheck("create table t1(id int,c1 int);alter table t1 modify column c1 int first;")
c.Assert(s.getAffectedRows(), GreaterEqual, 2)
for _, row := range s.rows {
c.Assert(row[2], Not(Equals), "2")
}
s.runCheck("create table t1(id int,c1 int);alter table t1 modify column id int after c1;")
c.Assert(s.getAffectedRows(), GreaterEqual, 2)
for _, row := range s.rows {
c.Assert(row[2], Not(Equals), "2")
}
// after 不存在的列
sql = "create table t1(id int);alter table t1 modify column c1 int after id;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c1"))
sql = "create table t1(id int,c1 int);alter table t1 modify column c1 int after id1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.id1"))
// 数据类型 警告
sql = "create table t1(id bit);alter table t1 modify column id bit;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "id"))
sql = "create table t1(id enum('red', 'blue'));alter table t1 modify column id enum('red', 'blue', 'black');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "id"))
sql = "create table t1(id set('red'));alter table t1 modify column id set('red', 'blue', 'black');"
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "id"))
// char列建议
config.GetGlobalConfig().Inc.MaxCharLength = 100
sql = `create table t1(id int,c1 char(10));
alter table t1 modify column c1 char(200);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHAR_TO_VARCHAR_LEN, "c1"))
// 字符集
sql = `create table t1(id int,c1 varchar(20));
alter table t1 modify column c1 varchar(20) character set utf8;
alter table t1 modify column c1 varchar(20) COLLATE utf8_bin;`
s.testManyErrors(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"))
// 列注释
config.GetGlobalConfig().Inc.CheckColumnComment = true
sql = ("create table t1(id int,c1 varchar(10));alter table t1 modify column c1 varchar(20);")
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_HAVE_NO_COMMENT, "c1", "t1"),
)
config.GetGlobalConfig().Inc.CheckColumnComment = false
// 无效默认值
sql = ("create table t1(id int,c1 int);alter table t1 modify column c1 int default '';")
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
// blob/text字段
config.GetGlobalConfig().Inc.EnableBlobType = false
sql = ("create table t1(id int,c1 varchar(10));alter table t1 modify column c1 blob;alter table t1 modify column c1 text;")
s.testManyErrors(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
)
config.GetGlobalConfig().Inc.EnableBlobType = true
sql = ("create table t1(id int,c1 blob);alter table t1 modify column c1 blob not null;")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TEXT_NOT_NULLABLE_ERROR, "c1", "t1"),
)
// 检查默认值
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = true
sql = "create table t1(id int,c1 varchar(5));alter table t1 modify column c1 varchar(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_DEFAULT_ADD_COLUMN, "c1", "t1"))
config.GetGlobalConfig().Inc.CheckColumnDefaultValue = false
// 变更类型
config.GetGlobalConfig().Inc.CheckColumnTypeChange = false
sql = "create table t1(c1 int,c1 int);alter table t1 modify column c1 varchar(10);"
s.testErrorCode(c, sql)
// ----------------- 列类型变更 -----------------
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 int);alter table t1 modify column c1 varchar(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "int(11)", "varchar(10)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 smallint);alter table t1 modify column c1 int;"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 int);alter table t1 modify column c1 smallint;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "int(11)", "smallint(6)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 int);alter table t1 modify column c1 smallint(5);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "int(11)", "smallint(5)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 varchar(100));alter table t1 modify column c1 varchar(20);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "varchar(100)", "varchar(20)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 float);alter table t1 modify column c1 double;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "float", "double"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 decimal(10,4));alter table t1 modify column c1 int;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "decimal(10,4)", "int(11)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 decimal(10,4));alter table t1 modify column c1 decimal(8,4);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "decimal(10,4)", "decimal(8,4)"))
config.GetGlobalConfig().Inc.CheckColumnTypeChange = true
sql = "create table t1(c1 decimal(10,4));alter table t1 modify column c1 decimal(12,4);"
s.testErrorCode(c, sql)
// 变更长度时不影响(仅长度变小时警告)
sql = "create table t1(c1 char(100));alter table t1 modify column c1 char(20);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "char(100)", "char(20)"))
sql = "create table t1(c1 varchar(100));alter table t1 modify column c1 char(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_COLUMN_TYPE, "t1.c1", "varchar(100)", "char(10)"))
sql = "create table t1(id int primary key,t1 timestamp default CURRENT_TIMESTAMP,t2 timestamp ON UPDATE CURRENT_TIMESTAMP);"
if s.explicitDefaultsForTimestamp || !(strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE"))) {
s.testErrorCode(c, sql)
} else {
s.testErrorCode(c, sql, session.NewErr(session.ER_INVALID_DEFAULT, "t2"))
}
// modify column
sql = "create table t1(id int primary key,c1 int);alter table t1 modify testx.t1.c1 int"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_DB_NAME, "testx"))
sql = "create table t1(id int primary key,c1 int);alter table t1 modify t.c1 int"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_TABLE_NAME, "t"))
config.GetGlobalConfig().Inc.CheckColumnPositionChange = true
sql = "create table t1(id int primary key,c1 int,c2 int);alter table t1 add column c3 int first"
s.testErrorCode(c, sql,
session.NewErr(session.ErCantChangeColumnPosition, "t1.c3"))
sql = "create table t1(id int primary key,c1 int,c2 int);alter table t1 add column c3 int after c1"
s.testErrorCode(c, sql,
session.NewErr(session.ErCantChangeColumnPosition, "t1.c3"))
sql = "create table t1(id int primary key,c1 int,c2 int);alter table t1 modify column c1 int after c2"
s.testErrorCode(c, sql,
session.NewErr(session.ErCantChangeColumnPosition, "t1.c1"))
sql = "create table t1(id int primary key,c1 int,c2 int);alter table t1 change column c1 c3 int after id"
s.testErrorCode(c, sql,
session.NewErr(session.ErCantChangeColumnPosition, "t1.c3"))
config.GetGlobalConfig().Inc.CheckColumnPositionChange = false
// modify column后,列信息更新
s.mustRunExec(c, "drop table if exists t1;create table t1(id int not null,c1 int);")
sql = "alter table t1 add primary key(id,c1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PRIMARY_CANT_HAVE_NULL))
sql = "alter table t1 modify c1 int not null;alter table t1 add primary key(id,c1);"
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestAlterTableChangeColumn(c *C) {
var sql string
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
s.mustCheck(c, "create table t1(id int,c1 int);alter table t1 modify column c1 int first;")
s.mustCheck(c, "create table t1(id int,c1 int);alter table t1 modify column id int after c1;")
config.GetGlobalConfig().Inc.EnableChangeColumn = false
sql = "create table t1(id int primary key,c1 int,c2 int);alter table t1 change column c1 c3 int after id"
s.testErrorCode(c, sql,
session.NewErr(session.ErCantChangeColumn, "c1"))
config.GetGlobalConfig().Inc.EnableChangeColumn = true
}
func (s *testSessionIncSuite) TestAlterTableDropColumn(c *C) {
sql := ""
sql = "create table t1(id int,c1 int);alter table t1 drop column c2;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c2"))
sql = "create table t1(id int,c1 int);alter table t1 drop column c1;"
s.testErrorCode(c, sql)
// // drop column
sql = "create table t2 (id int null);alter table t2 drop c1"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.c1"))
sql = "create table t2 (id int null);alter table t2 drop id;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrCantRemoveAllFields))
}
func (s *testSessionIncSuite) TestInsert(c *C) {
config.GetGlobalConfig().Inc.CheckInsertField = false
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 0
// 表不存在
sql = "insert into t1 values(1,1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t1"))
// 列数不匹配
sql = "create table t1(id int,c1 int);insert into t1(id) values(1,1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_VALUE_COUNT_ON_ROW, 1))
sql = "create table t1(id int,c1 int);insert into t1(id) values(1),(2,1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_VALUE_COUNT_ON_ROW, 2))
sql = "create table t1(id int,c1 int not null);insert into t1(id,c1) select 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WRONG_VALUE_COUNT_ON_ROW, 1))
// 列重复
sql = "create table t1(id int,c1 int);insert into t1(id,id) values(1,1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_FIELD_SPECIFIED_TWICE, "id", "t1"))
sql = "create table t1(id int,c1 int);insert into t1(id,id) select 1,1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_FIELD_SPECIFIED_TWICE, "id", "t1"))
// 字段警告
config.GetGlobalConfig().Inc.CheckInsertField = true
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 1
sql = "create table t1(id int,c1 int);insert into t1 values(1,1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_INSERT_FIELD))
config.GetGlobalConfig().Inc.CheckInsertField = false
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 0
sql = "create table t1(id int,c1 int);insert into t1(id) values();"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_INSERT_VALUES))
// 列不允许为空
sql = "create table t1(id int,c1 int not null);insert into t1(id,c1) values(1,null);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_BAD_NULL_ERROR, "test_inc.t1.c1", 1))
sql = "create table t1(id int,c1 int not null default 1);insert into t1(id,c1) values(1,null);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_BAD_NULL_ERROR, "test_inc.t1.c1", 1))
// insert select 表不存在
sql = "create table t1(id int,c1 int );insert into t1(id,c1) select 1,null from t2;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t2"))
s.mustRunExec(c, "create table t1(id int,c1 int );")
config.GetGlobalConfig().Inc.CheckDMLWhere = true
sql = "insert into t1(id,c1) select 1,null from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_NO_WHERE_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLWhere = false
// limit
config.GetGlobalConfig().Inc.CheckDMLLimit = true
sql = "insert into t1(id,c1) select 1,null from t1 limit 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_LIMIT_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLLimit = false
// order by rand()
// config.GetGlobalConfig().Inc.CheckDMLOrderBy = true
sql = "insert into t1(id,c1) select 1,null from t1 order by rand();"
s.testErrorCode(c, sql,
session.NewErr(session.ER_ORDERY_BY_RAND))
// config.GetGlobalConfig().Inc.CheckDMLOrderBy = false
// 受影响行数
s.runCheck("insert into t1 values(1,1),(2,2);")
s.testAffectedRows(c, 2)
s.runCheck("insert into t1(id,c1) select 1,null;")
s.testAffectedRows(c, 1)
s.mustRunExec(c, "drop table if exists t1; create table t1(c1 char(100) not null);")
sql = "create table t1(c1 char(100) not null);insert into t1(c1) values(null);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_BAD_NULL_ERROR, "test_inc.t1.c1", 1))
sql = "create table t1(c1 char(100) not null);insert into t1(c1) select t1.c1 from t1 inner join t1 on t1.id=t1.id;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrNonUniqTable, "t1"))
// 由于是否报错依赖于实际的mysql版本,所以暂时忽略
// sql = "create table t1(c1 char(100) not null);insert into t1(c1) select t1.c1 from t1 limit 1 union all select t1.c1 from t1;"
// s.testErrorCode(c, sql,
// session.NewErr(session.ErrWrongUsage, "UNION", "LIMIT"))
// sql = "create table t1(c1 char(100) not null);insert into t1(c1) select t1.c1 from t1 order by 1 union all select t1.c1 from t1;"
// s.testErrorCode(c, sql,
// session.NewErr(session.ErrWrongUsage, "UNION", "ORDER BY"))
// insert 行数
config.GetGlobalConfig().Inc.MaxInsertRows = 1
sql = `drop table if exists t1;create table t1(id int);
insert into t1 values(1);`
s.testErrorCode(c, sql)
s.testAffectedRows(c, 1)
sql = `drop table if exists t1;create table t1(id int);
insert into t1 values(1),(2);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INSERT_TOO_MUCH_ROWS, 2, 1))
config.GetGlobalConfig().Inc.MaxInsertRows = 3
sql = `drop table if exists t1;create table t1(id int);
insert into t1 values(1),(2),(3);`
s.testErrorCode(c, sql)
s.testAffectedRows(c, 3)
s.mustRunExec(c, "drop table if exists t1;create table t1(id int);")
sql = `drop table if exists t2;create table t2 like t1;
insert into t2 select id from t1;`
s.testErrorCode(c, sql)
if s.realRowCount {
s.testAffectedRows(c, 0)
} else {
s.testAffectedRows(c, 1)
}
config.GetGlobalConfig().Inc.EnableSelectStar = true
s.mustRunExec(c, "drop table if exists tt1;create table tt1(id int,c1 int);insert into tt1 values(1,1);")
sql = `insert into tt1 select a.* from tt1 a inner join tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into tt1 select a.* from tt1 a inner join tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into tt1 select A.* from (select * from tt1) a inner join tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into tt1 select A.* from (select c2.* from tt1 c1 inner join tt1 c2 on c1.id=c2.id) a inner join test_inc.tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into test_inc.tt1 select A.* from (select * from tt1 c1 union all select * from tt1 c2) a inner join test_inc.tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into test_inc.tt1 select test_inc.B.* from tt1 a inner join test_inc.tt1 b on a.id=b.id;`
s.testErrorCode(c, sql)
sql = `insert into test_inc.tt1 select * from test_inc.tt1 ;`
s.testErrorCode(c, sql)
sql = `insert into test_inc.tt1(id)values(c1);`
s.testErrorCode(c, sql)
sql = `insert into test_inc.tt1(id)values(now);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "now"))
sql = `insert into t1(id) values(nullif(a,'123'));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "a"))
sql = `insert into t1(id) values(now);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "now"))
sql = `insert into t1(id) values(now());`
s.testErrorCode(c, sql)
sql = `insert into t1(id) values(max(1));`
s.testErrorCode(c, sql)
sql = `insert into t1(id) values(max(a));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "a"))
sql = `insert into t1(id) values(abs(-1));`
s.testErrorCode(c, sql)
sql = `insert into t1(id) values(cast(a as signed));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "a"))
sql = `drop table if exists tt1;create table tt1(id int,c1 int);insert into tt1(id) select max(id) from tt1 where id in (select id1 from tt1);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "id1"))
sql = `drop table if exists tt1;create table tt1(id int,c1 int);
drop table if exists t1;create table t1(id int primary key,c1 int);
insert into tt1(id)
select s1.id from t1 as s1 inner join t1 as s2 on s1.c1 = s2.c1 where s1.id > s2.id;`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.EnableSelectStar = false
sql = `drop table if exists tt1;create table tt1(id int,c1 int);insert into tt1 select * from tt1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_SELECT_ONLY_STAR))
// datetime format 验证
sql = `drop table if exists t1;
create table t1(id int auto_increment primary key,c1 date,c2 time,c3 datetime,c4 timestamp);`
s.mustRunExec(c, sql)
sql = `insert into t1(c1) values('2020-1-32');`
s.testErrorCode(c, sql,
session.NewErr(session.ErrIncorrectDateTimeValue, "2020-1-32", "test_inc.t1.c1"))
sql = `insert into t1(c2) values('10:70');`
s.testErrorCode(c, sql,
session.NewErr(session.ErrIncorrectDateTimeValue, "10:70", "test_inc.t1.c2"))
sql = `insert into t1(c3) values('2020-1-32');`
s.testErrorCode(c, sql,
session.NewErr(session.ErrIncorrectDateTimeValue, "2020-1-32", "test_inc.t1.c3"))
sql = `insert into t1(c4) values('2020-1-32');`
s.testErrorCode(c, sql,
session.NewErr(session.ErrIncorrectDateTimeValue, "2020-1-32", "test_inc.t1.c4"))
}
func (s *testSessionIncSuite) TestSelect(c *C) {
config.GetGlobalConfig().Inc.EnableSelectStar = false
s.mustRunExec(c, "drop table if exists t1;create table t1(id int,c1 integer);")
sql = `select * from t1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_SELECT_ONLY_STAR))
sql = `select id,c1 from t1 union all select * from t1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_SELECT_ONLY_STAR))
// 列隐式转换审核
config.GetGlobalConfig().Inc.CheckImplicitTypeConversion = true
sql = `select id,c1 from t1 where c1 ="1";`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "t1", "c1", "int"))
sql = `select id from t1 order by id;`
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestPartition(c *C) {
config.GetGlobalConfig().Inc.EnablePartitionTable = false
s.mustRunExec(c, "drop table if exists test_partition;")
sql = `create table test_partition(
id int unsigned not null auto_increment comment 'id',
a varchar(10) comment 'a',
buss_day varchar(8) comment 'day',
primary key (id,buss_day)
) comment '分区表'
partition by list columns (buss_day)
(
partition p1 values in('20200101'),
partition p2 values in('20200102'),
partition p3 values in('20200103')
)`
s.testErrorCode(c, sql,
session.NewErr(session.ER_PARTITION_NOT_ALLOWED))
sql = `create table test_partition(
id int unsigned not null auto_increment comment 'id',
a varchar(10) comment 'a',
buss_day varchar(8) comment 'day',
primary key (id,buss_day)
) comment '分区表'
partition by list columns (buss_day)
(
partition p1 values in('20200101'),
partition p2 values in('20200102'),
partition p3 values in('20200103')
);
alter table test_partition add partition (
partition p4 values in('20200104'),
partition p5 values in('20200105'),
partition p6 values in('20200106')
);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_PARTITION_NOT_ALLOWED))
// ----------- enable partition -----------
config.GetGlobalConfig().Inc.EnablePartitionTable = true
sql = `create table test_partition(
id int unsigned not null auto_increment comment 'id',
a varchar(10) comment 'a',
buss_day varchar(8) comment 'day',
primary key (id,buss_day)
) comment '分区表'
partition by list columns (buss_day)
(
partition p1 values in('20200101'),
partition p2 values in('20200102'),
partition p3 values in('20200103')
)`
s.testErrorCode(c, sql)
s.mustRunExec(c, sql)
sql = `alter table test_partition add partition (
partition p4 values in('20200104'),
partition p5 values in('20200105'),
partition p6 values in('20200106')
);`
s.testErrorCode(c, sql)
s.mustRunExec(c, sql)
sql = `alter table test_partition drop partition p5,p6;`
s.testErrorCode(c, sql)
s.mustRunExec(c, sql)
sql = `alter table test_partition drop partition p5,p6;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrPartitionNotExisted, "p5"),
session.NewErr(session.ErrPartitionNotExisted, "p6"))
sql = `CREATE TABLE my_range_datetime(
id INT,
hiredate DATETIME
)
PARTITION BY RANGE (TO_DAYS(hiredate) ) (
PARTITION p1 VALUES LESS THAN ( TO_DAYS('20171202') ),
PARTITION p2 VALUES LESS THAN ( TO_DAYS('20171203') ),
PARTITION p3 VALUES LESS THAN ( TO_DAYS('20171204') ),
PARTITION p4 VALUES LESS THAN ( TO_DAYS('20171205') ),
PARTITION p5 VALUES LESS THAN ( TO_DAYS('20171206') ),
PARTITION p6 VALUES LESS THAN ( TO_DAYS('20171207') ),
PARTITION p7 VALUES LESS THAN ( TO_DAYS('20171208') ),
PARTITION p8 VALUES LESS THAN ( TO_DAYS('20171209') ),
PARTITION p9 VALUES LESS THAN ( TO_DAYS('20171210') ),
PARTITION p10 VALUES LESS THAN ( TO_DAYS('20171211') ),
PARTITION p11 VALUES LESS THAN (MAXVALUE)
);`
s.mustRunExec(c, sql)
sql = `create table t_list(a int(11),b int(11))
partition by list (b)(
partition p0 values in (1,3,5,7,9),
partition p1 values in (2,4,6,8,0));`
s.mustRunExec(c, sql)
sql = `CREATE TABLE my_member (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
created DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY HASH(id)
PARTITIONS 4;`
s.mustRunExec(c, sql)
sql = `CREATE TABLE my_members (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30),
hired DATE NOT NULL DEFAULT '1970-01-01',
separated DATE NOT NULL DEFAULT '9999-12-31',
job_code INT,
store_id INT
)
PARTITION BY LINEAR HASH( id )
PARTITIONS 4;`
s.mustRunExec(c, sql)
sql = `CREATE TABLE k1 (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20)
)
PARTITION BY KEY()
PARTITIONS 2;`
s.mustRunExec(c, sql)
sql = `CREATE TABLE tm1 (
s1 CHAR(32)
)
PARTITION BY KEY(s1)
PARTITIONS 10;`
s.mustRunExec(c, sql)
sql = `CREATE TABLE customer_login_log (
customer_id int(10) unsigned NOT NULL COMMENT '登录用户ID',
login_time DATETIME NOT NULL COMMENT '用户登录时间',
login_ip int(10) unsigned NOT NULL COMMENT '登录IP',
login_type tinyint(4) NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB
PARTITION BY RANGE (YEAR(login_time))(
PARTITION p0 VALUES LESS THAN (2017),
PARTITION p1 VALUES LESS THAN (2018),
PARTITION p2 VALUES LESS THAN (2019)
);
CREATE TABLE arch_customer_login_log (
customer_id INT unsigned NOT NULL COMMENT '登录用户ID',
login_time DATETIME NOT NULL COMMENT '用户登录时间',
login_ip INT unsigned NOT NULL COMMENT '登录IP',
login_type TINYINT NOT NULL COMMENT '登录类型:0未成功 1成功'
) ENGINE=InnoDB ;`
s.mustRunExec(c, sql)
sql = `ALTER TABLE customer_login_log
exchange PARTITION p1 WITH TABLE arch_customer_login_log;`
s.mustRunExec(c, sql)
}
func (s *testSessionIncSuite) TestSubSelect(c *C) {
config.GetGlobalConfig().Inc.EnableSelectStar = true
s.mustRunExec(c, `drop table if exists t1,t2;
CREATE TABLE t1(id INT,NAME VARCHAR(30));
CREATE TABLE t2(id INT ,salesid INT ,title VARCHAR(100));`)
sql = `SELECT a.id,a.title,(SELECT b.name FROM t1 b WHERE b.id = a.salesid) AS salesname
FROM t2 a;`
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestUpdate(c *C) {
saved := config.GetGlobalConfig().Inc
defer func() {
config.GetGlobalConfig().Inc = saved
s.realRowCount = true
}()
config.GetGlobalConfig().Inc.CheckInsertField = false
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 0
config.GetGlobalConfig().Inc.EnableSetEngine = true
// 表不存在
sql = "update t1 set c1 = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t1"))
sql = "create table t1(id int);update t1 set id = 1;"
s.testErrorCode(c, sql)
sql = "create table t1(id int);update t1 as tmp set t1.id = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.id"))
sql = "create table t1(id int);update t1 set c1 = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "c1"))
sql = "create table t1(id int,c1 int);update t1 set c1 = 1,c2 = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c2"))
sql = `create table t1(id int primary key,c1 int);
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1=1;`
s.testErrorCode(c, sql)
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
update t1 inner join t2 on t1.id=t2.id2 set t1.c1=t2.c1 where c11=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.id2"),
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "c11"))
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
update t1,t2 t3 set t1.c1=t2.c3 where t1.id=t3.id;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.c3"))
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
update t1,t2 t3 set t1.c1=t2.c3 where t1.id=t3.id;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.c3"))
// where
config.GetGlobalConfig().Inc.CheckDMLWhere = true
sql = "create table t1(id int,c1 int);update t1 set c1 = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_NO_WHERE_CONDITION))
sql = `create table t1(id int,c1 int);
create table t2(id int,c1 int);`
s.mustRunExec(c, sql)
sql = `update t1 join t2 set t1.c1 = 1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrJoinNoOnCondition),
session.NewErr(session.ER_NO_WHERE_CONDITION))
sql = `update t1,t2 set t1.c1 = 1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrJoinNoOnCondition),
session.NewErr(session.ER_NO_WHERE_CONDITION))
sql = `update t1 NATURAL join t2 set t1.c1 = 1 ;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NO_WHERE_CONDITION))
sql = `create table t1(id int,c1 int);
create table t2(id int,c1 int);
update t1 join t2 using(id) set t1.c1 = 1 ;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NO_WHERE_CONDITION))
sql = `update t1,t2 set t1.c1 = 1 where t1.id=1;`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.CheckDMLWhere = false
// limit
config.GetGlobalConfig().Inc.CheckDMLLimit = true
sql = "create table t1(id int,c1 int);update t1 set c1 = 1 limit 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_LIMIT_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLLimit = false
// order by rand()
config.GetGlobalConfig().Inc.CheckDMLOrderBy = true
sql = "create table t1(id int,c1 int);update t1 set c1 = 1 order by rand();"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_ORDERBY_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLOrderBy = false
// 受影响行数
s.realRowCount = false
s.mustCheck(c, "drop table if exists t1,t2;create table t1(id int,c1 int);update t1 set c1 = 1;")
s.testAffectedRows(c, 0)
// 受影响行数: explain计算规则
s.mustRunExec(c, `drop table if exists t1,t2;
create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int);
insert into t1(id,c1)values(1,1);
insert into t2(id,c1)values(1,1),(2,2),(3,3);`)
config.GetGlobalConfig().Inc.ExplainRule = "first"
sql = `update t1 inner join t2 on 1=1 set t1.c1=t2.c1 where 1=1;`
s.mustCheck(c, sql)
s.testAffectedRows(c, 1)
config.GetGlobalConfig().Inc.ExplainRule = "max"
s.mustCheck(c, sql)
s.testAffectedRows(c, 3)
s.mustRunExec(c, `drop table if exists t1,t2`)
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id int primary key,c1 int);
create table table2(id int primary key,c1 int,c2 int);
update table1 t1,table2 t2 set t1.c1=t2.c1 where t1.id=t2.id;`
s.testErrorCode(c, sql)
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int);
create table table2(id2 int primary key,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=t2.c2 where t1.id1=t2.id2;`
s.testErrorCode(c, sql)
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int);
create table table2(id2 int primary key,c2 int,c22 int);
update table1 a1,table2 a2 set a1.c1=a2.c2 where a1.id1=a2.id2 and a1.c1=a2.c2 and a1.id1 in (1,2,3);`
s.testErrorCode(c, sql)
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int);
create table test.table2(id2 int primary key,c2 int,c22 int);
update table1 a1,test.table2 a2 set a1.c1=a2.c2 where a1.id1=a2.id2 and a1.c1=a2.c2 and a1.id1 in (1,2,3);`
s.testErrorCode(c, sql)
s.mustRunExec(c, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1);")
sql = `update t1 set c1=1 where id =1;`
s.testErrorCode(c, sql)
s.testAffectedRows(c, 1)
sql = `drop table if exists tt1,t1;
create table tt1(id int primary key,table_schema varchar(20),table_name varchar(64),version int);
create table t1 like tt1;
UPDATE tt1
INNER JOIN
(SELECT table_schema,
max(VERSION) AS VERSION,
table_name
FROM t1
GROUP BY table_schema)t2 ON tt1.table_schema=t2.table_schema
SET tt1.VERSION=t2.VERSION
WHERE tt1.id=1;`
if strings.Contains(s.sqlMode, "ONLY_FULL_GROUP_BY") {
s.testErrorCode(c, sql,
session.NewErr(session.ErrFieldNotInGroupBy, 3, "SELECT list", "table_name"))
} else {
s.testErrorCode(c, sql)
}
sql = `drop table if exists tt1,t1;
create table tt1(id int primary key,table_schema varchar(20),table_name varchar(64),version int);
create table t1 like tt1;
UPDATE tt1
INNER JOIN
(SELECT table_schema,
max(VERSION) AS VERSION
FROM t1
GROUP BY table_schema)t2 ON tt1.table_schema=t2.table_schema
SET tt1.VERSION=t2.VERSION
WHERE tt1.id=1;`
s.testErrorCode(c, sql)
sql = `drop table if exists tt1,t1;
create table tt1(id int primary key,table_schema varchar(20),table_name varchar(64),version int);
create table t1 like tt1;
UPDATE tt1
INNER JOIN
(SELECT table_schema,
max(VERSION) AS VERSION
FROM t1)t2 ON tt1.table_schema=t2.table_schema
SET tt1.VERSION=t2.VERSION
WHERE tt1.id=1;`
if strings.Contains(s.sqlMode, "ONLY_FULL_GROUP_BY") {
s.testErrorCode(c, sql,
session.NewErr(session.ErrMixOfGroupFuncAndFields, 1, "table_schema"))
} else {
s.testErrorCode(c, sql)
}
sql = `drop table if exists t1;create table t1(id int primary key,c1 int,c2 int);
update t1 set c1=1 and c2 = 1 where id=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrWrongAndExpr))
sql = `drop table if exists t1;create table t1(id int primary key,c1 int,c2 int);
update t1 set c1=1,c2 = 1 where id=1;`
s.testErrorCode(c, sql)
// 列隐式转换审核
s.mustRunExec(c, `drop table if exists t1;`)
config.GetGlobalConfig().Inc.CheckImplicitTypeConversion = true
sql = `create table t1(id int primary key,c1 char(100));
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "s1", "c1", "char"))
sql = `create table t1(id int primary key,c1 varchar(100));
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "s1", "c1", "varchar"))
sql = `create table t1(id int primary key,c1 json);
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "s1", "c1", "json"))
sql = `create table t1(id int primary key,c1 int);
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1="1";`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "s1", "c1", "int"))
sql = `create table t1(id int primary key,c1 int);
update t1 s1 inner join t1 s2 on s1.id=s2.id set s1.c1=s2.c1 where s1.c1="1";`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "s1", "c1", "int"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set c1=t2.c2 where t1.id1=t2.id2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=t2.c2 where t1.id1=t2.id2 and c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int,c2 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=c2 where t1.id1=t2.id2 and c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c2"),
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
// -------------------- 多表update -------------------
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int,c2 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=c2,t2.c22=2 where t1.id1=t2.id2 and c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c2"),
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int,c2 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=c2,t2.c222=2 where t1.id1=t2.id2 and c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c2"),
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.c222"),
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int,c2 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set t1.c1=c2,c222=2 where t1.id1=t2.id2 and c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c2"),
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "c222"),
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1;drop table if exists table2;
create table table1(id1 int primary key,c1 int,c2 int);
create table table2(id2 int primary key,c1 int,c2 int,c22 int);
update table1 t1,table2 t2 set c1=2 where t1.id1=t2.id2 and t2.c1=2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NON_UNIQ_ERROR, "c1"))
sql = `drop table if exists table1,table2;
create table table1(id1 int primary key,c1 int,c2 int);
update table1 t1 join (select 1 as id2) t2 set c1=2 where t1.id1=t2.id2;`
s.testErrorCode(c, sql)
sql = `drop table if exists table1,table2;
create table table1(id1 int primary key,c1 int,c2 int);
update table1 t1 join (select 1 as id2 union all select 2) t2 set c1=2 where t1.id1=t2.id2;`
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestDelete(c *C) {
config.GetGlobalConfig().Inc.CheckInsertField = false
config.GetGlobalConfig().IncLevel.ER_WITH_INSERT_FIELD = 0
// 表不存在
sql = "delete from t1 where c1 = 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t1"))
// where
config.GetGlobalConfig().Inc.CheckDMLWhere = true
sql = "create table t1(id int,c1 int);delete from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_NO_WHERE_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLWhere = false
// limit
config.GetGlobalConfig().Inc.CheckDMLLimit = true
sql = "create table t1(id int,c1 int);delete from t1 where id = 1 limit 1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_LIMIT_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLLimit = false
// order by rand()
config.GetGlobalConfig().Inc.CheckDMLOrderBy = true
sql = "create table t1(id int,c1 int);delete from t1 where id = 1 order by rand();"
s.testErrorCode(c, sql,
session.NewErr(session.ER_WITH_ORDERBY_CONDITION))
config.GetGlobalConfig().Inc.CheckDMLOrderBy = false
// 表不存在
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
delete from t3 where id1 =1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t3"))
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
delete from t1 where id1 =1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "id1"))
sql = `create table t1(id int primary key,c1 int);
create table t2(id int primary key,c1 int,c2 int);
delete t2 from t1 inner join t2 on t1.id=t2.id2 where c11=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.id2"))
// 受影响行数
s.mustCheck(c, "create table t1(id int,c1 int);delete from t1 where id = 1;")
s.testAffectedRows(c, 0)
s.mustRunExec(c, "drop table if exists t1;create table t1(id int,c1 int);insert into t1(id) values(1);")
sql = `delete from t1 where id =1;`
s.testErrorCode(c, sql)
s.testAffectedRows(c, 1)
sql = `drop table if exists t1;create table t1(id int primary key,c1 int);
delete tmp from t1 as tmp where tmp.id=1;`
s.testErrorCode(c, sql)
sql = `drop table if exists t1;create table t1(id int primary key,c1 int);
delete t1 from t1 as tmp where tmp.id=1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_NOT_EXISTED_ERROR, "test_inc.t1"))
sql = `drop table if exists t1;create table t1(id int primary key,c1 int);
delete s1 from t1 as s1 inner join t1 as s2 on s1.c1 = s2.c1 where s1.id > s2.id;`
s.testErrorCode(c, sql)
s.mustRunExec(c, "drop table if exists t1;create table t1(id int primary key,c1 int);")
sql = `create table t2(id int primary key,c1 int);
delete t1 from t1 inner join t2 where t2.c1 = 1;`
s.testErrorCode(c, sql)
// 列隐式转换审核
config.GetGlobalConfig().Inc.CheckImplicitTypeConversion = true
sql = `drop table if exists t1;
create table t1(id int primary key,c1 char(100));
delete from t1 where c1 =1;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrImplicitTypeConversion, "t1", "c1", "char"))
s.mustRunExec(c, `drop table if exists t1;CREATE TABLE t1 (
id bigint(20) AUTO_INCREMENT primary key,
goods_id bigint(20) unsigned NOT NULL DEFAULT '0' ,
sku_id bigint(20) unsigned NOT NULL DEFAULT '0' ,
bar_code varchar(30) NOT NULL DEFAULT ''
);`)
sql = "delete from t1 where id in (select id from (select any_value(id) as id,count(*) as num from t1 group by `goods_id`, `sku_id`, `bar_code` having num > 1) as t)"
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestCreateDataBase(c *C) {
config.GetGlobalConfig().Inc.EnableDropDatabase = false
// 不存在
sql = "drop database if exists test1111111111111111111;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_DATABASE, "test1111111111111111111"))
sql = "drop database test1111111111111111111;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_DATABASE, "test1111111111111111111"))
config.GetGlobalConfig().Inc.EnableDropDatabase = true
sql = "drop database if exists test1111111111111111111;create database test1111111111111111111;"
s.testErrorCode(c, sql)
// 存在
sql = "create database test1111111111111111111;create database test1111111111111111111;"
s.testErrorCode(c, sql,
session.NewErrf("数据库'test1111111111111111111'已存在."))
// if not exists 创建
sql = "create database if not exists test1111111111111111111;create database if not exists test1111111111111111111;"
s.testErrorCode(c, sql)
// create database
sql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_LONG_IDENT, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
sql = "create database mysql"
s.testErrorCode(c, sql,
session.NewErrf("数据库'%s'已存在.", "mysql"))
// 字符集
config.GetGlobalConfig().Inc.EnableSetCharset = false
config.GetGlobalConfig().Inc.SupportCharset = ""
sql = "drop database test1;create database test1 character set utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_SET_CHARSET, "utf8"))
config.GetGlobalConfig().Inc.SupportCharset = "utf8mb4"
sql = "drop database test1;create database test1 character set utf8;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_SET_CHARSET, "utf8"))
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8,utf8mb4"
sql = "drop database test1;create database test1 character set utf8;"
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8,utf8mb4"
sql = "drop database test1;create database test1 character set latin1;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"))
sql = `drop database test1;create database test1;
use test1;
create table t1(id int primary key);
insert into t1 values(1);
insert into t1 select count(1)+1 from t1;
alter table t1 add column c1 int;
update t1 set c1=1 where id=1;
delete from t1 where id =1;`
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestTimestampColumn(c *C) {
sql := ""
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '2100-1-1 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '1900-1-1 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '2100-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '1900-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 date default '');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 date default '0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 date default '2100-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 date default '1900-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp(6) default '1900-1-1 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp(7) default '2000-1-1 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp(0) default '2000-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp(3) default '2000-1-1 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp(6) default '2000-1-1 1:1:1');`
s.testErrorCode(c, sql)
// 零值审核
if strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_DATE")) {
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '0000-0-0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '0000-0-0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 date default '0000-0-0');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 date default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
} else {
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '0000-0-0');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '0000-0-0');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 date default '0000-0-0');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 date default '0000-0-0 00:00:00');`
s.testErrorCode(c, sql)
}
// 月,日零值审核
if strings.Contains(s.sqlMode, "TRADITIONAL") ||
(strings.Contains(s.sqlMode, "STRICT_") && strings.Contains(s.sqlMode, "NO_ZERO_IN_DATE")) {
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 date default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
} else {
sql = `drop table if exists timeTable;create table timeTable(c1 timestamp default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "c1"))
sql = `drop table if exists timeTable;create table timeTable(c1 datetime default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql)
sql = `drop table if exists timeTable;create table timeTable(c1 date default '2000-1-0 1:1:1');`
s.testErrorCode(c, sql)
}
}
func (s *testSessionIncSuite) TestRenameTable(c *C) {
// 不存在
sql = "drop table if exists t1;create table t1(id int primary key);alter table t1 rename t2;"
s.testErrorCode(c, sql)
sql = "drop table if exists t1;create table t1(id int primary key);rename table t1 to t2;"
s.testErrorCode(c, sql)
// 存在
sql = "drop table if exists t1;create table t1(id int primary key);rename table t1 to t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_EXISTS_ERROR, "t1"))
}
func (s *testSessionIncSuite) TestCreateView(c *C) {
s.mustRunExec(c, "drop table if exists t1;drop view if exists v_1;")
sql = "create table t1(id int primary key);create view v_1 as select * from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrViewSupport, "v_1"))
config.GetGlobalConfig().Inc.EnableUseView = true
sql = "create table t1(id int primary key);create view v_1 as select * from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_SELECT_ONLY_STAR))
sql = "create table t1(id int primary key);create view v_1 as select id from t1;"
s.testErrorCode(c, sql)
sql = "create table t1(id int primary key,c1 int);create view v_1(id,id) as select id,c1 from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_FIELD_SPECIFIED_TWICE, "id", "v_1"))
sql = "create table t1(id int primary key,c1 int);create view v_1(id,c1,c2) as select id,c1 from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrViewColumnCount))
sql = "create table t1(id int primary key,c1 int);create view v_1(id,c1,c2) as select * from t1;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_SELECT_ONLY_STAR),
session.NewErr(session.ErrViewColumnCount))
sql = "create table t1(id int primary key,c1 int);create view v_1 as select id,c1 from t1;"
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestAlterTableAddIndex(c *C) {
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
// add index
sql = "create table t1(id int);alter table t1 add index idx (c1)"
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.c1"))
sql = "create table t1(id int,c1 int);alter table t1 add index idx (c1);"
s.testErrorCode(c, sql)
sql = "create table t1(id int,c1 int);alter table t1 add index idx (c1);alter table t1 add index idx (c1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_INDEX, "idx", "test_inc", "t1"))
if s.DBVersion >= 50701 {
sql = "create table t1(id int,c1 int);alter table t1 add index idx (c1);alter table t1 rename index idx to idx2;"
s.testErrorCode(c, sql)
sql = `create table t1(id int,c1 int);
alter table t1 add index idx (c1),add index idx2 (c1);
alter table t1 rename index idx to idx2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_KEYNAME, "idx2"))
sql = `create table t1(id int,c1 int);
alter table t1 add index idx (c1),add index idx2 (c1);
alter table t1 rename index idx3 to idx2;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_FIELD_OR_KEY, "t1.idx3"))
}
sql = "create table t1(id int,c1 int,key idx (c1));alter table t1 add index idx (c1);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_DUP_INDEX, "idx", "test_inc", "t1"))
sql = "create table t1(id int primary key);alter table t1 drop primary key;"
s.testErrorCode(c, sql)
sql = "CREATE TABLE geom (g GEOMETRY NOT NULL, SPATIAL INDEX ix_1(g));"
s.testErrorCode(c, sql)
sql = "CREATE TABLE geom (g GEOMETRY NULL, SPATIAL INDEX ix_1(g));"
s.testErrorCode(c, sql,
&session.SQLError{Code: 0,
Message: "All parts of a SPATIAL index must be NOT NULL."})
sql = "CREATE TABLE geom (id int,g GEOMETRY NOT NULL, SPATIAL INDEX ix_1(id,g));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_MANY_KEY_PARTS, "ix_1", "geom", 1))
sql = "CREATE TABLE geom (id int,g GEOMETRY NOT NULL);alter table geom add SPATIAL INDEX ix_1(id,g);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_MANY_KEY_PARTS, "ix_1", "geom", 1))
sql = `create table t1(id int,c1 int);
alter table t1 add index idx (c1) visible;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrUseIndexVisibility))
sql = `create table t1(id int,c1 int);
alter table t1 add index idx2 (c1) invisible;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrUseIndexVisibility))
}
func (s *testSessionIncSuite) TestAlterTableDropIndex(c *C) {
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
// drop index
sql = "create table t1(id int);alter table t1 drop index idx"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CANT_DROP_FIELD_OR_KEY, "t1.idx"))
sql = "create table t1(c1 int);alter table t1 add index idx (c1);alter table t1 drop index idx;"
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestAlterTable(c *C) {
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
config.GetGlobalConfig().Inc.EnableDropTable = true
// 删除后添加列
sql = "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c1;alter table t1 add column c1 varchar(20);"
s.testErrorCode(c, sql)
sql = "drop table if exists t1;create table t1(id int,c1 int);alter table t1 drop column c1,add column c1 varchar(20);"
s.testErrorCode(c, sql)
// 删除后添加索引
sql = "drop table if exists t1;create table t1(id int,c1 int,key ix(c1));alter table t1 drop index ix;alter table t1 add index ix(c1);"
s.testErrorCode(c, sql)
sql = "drop table if exists t1;create table t1(id int,c1 int,key ix(c1));alter table t1 drop index ix,add index ix(c1);"
s.testErrorCode(c, sql)
s.mustRunExec(c, "drop table if exists t1;create table t1(id int auto_increment primary key,c1 int);")
sql = "alter table t1 auto_increment 20 comment '123';"
s.testErrorCode(c, sql)
if s.DBVersion < 80000 {
config.GetGlobalConfig().Inc.MaxDDLAffectRows = 1
s.mustRunExec(c, "insert into t1(id,c1)values(1,1),(2,2);")
sql = "alter table t1 add column c2 int;"
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHANGE_TOO_MUCH_ROWS, "Alter", 2, 1))
}
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c1 geometry;
alter table t1 add column c2 point;
alter table t1 add column c3 linestring;
alter table t1 add column c4 polygon;
alter table t1 drop column c1;
alter table t1 drop column c2;
alter table t1 drop column c3;
alter table t1 drop column c4; `
s.testErrorCode(c, sql)
// 列字符集&排序规则
config.GetGlobalConfig().Inc.EnableColumnCharset = false
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c4"))
config.GetGlobalConfig().Inc.EnableColumnCharset = true
config.GetGlobalConfig().Inc.SupportCharset = "utf8mb4"
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8mb4"))
config.GetGlobalConfig().Inc.SupportCharset = "utf8"
config.GetGlobalConfig().Inc.SupportCollation = "utf8_bin"
sql = `drop table if exists t1;
create table t1(id int primary key);
alter table t1 add column c4 varchar(22) charset utf8 collate utf8mb4_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCollationNotSupport, "utf8_bin"))
}
func (s *testSessionIncSuite) TestCreateTablePrimaryKey(c *C) {
sql := ""
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
// EnablePKColumnsOnlyInt
config.GetGlobalConfig().Inc.EnablePKColumnsOnlyInt = true
sql = "create table t1(id tinyint, primary key(id));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PK_COLS_NOT_INT, "id", "test_inc", "t1"))
sql = "create table t1(id mediumint, primary key(id));"
s.testErrorCode(c, sql)
sql = "create table t1(id int, primary key(id));"
s.testErrorCode(c, sql)
sql = "create table t1(id bigint, primary key(id));"
s.testErrorCode(c, sql)
sql = "create table t1(id varchar(10), primary key(id));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PK_COLS_NOT_INT, "id", "test_inc", "t1"))
sql = "create table t1(id tinyint primary key);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PK_COLS_NOT_INT, "id", "test_inc", "t1"))
sql = "create table t1(id mediumint primary key);"
s.testErrorCode(c, sql)
sql = "create table t1(id int primary key);"
s.testErrorCode(c, sql)
sql = "create table t1(id bigint primary key);"
s.testErrorCode(c, sql)
sql = "create table t1(id varchar(10) primary key);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PK_COLS_NOT_INT, "id", "test_inc", "t1"))
}
func (s *testSessionIncSuite) TestTableCharsetCollation(c *C) {
sql := ""
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
// 表存在
sql = ("create table t1(id int);create table t1(id int);")
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_EXISTS_ERROR, "t1"))
// 字符集
sql = `create table t1(id int,c1 varchar(20) character set utf8,
c2 varchar(20) COLLATE utf8_bin);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c1"),
session.NewErr(session.ER_CHARSET_ON_COLUMN, "t1", "c2"))
config.GetGlobalConfig().Inc.EnableSetCharset = false
sql = `create table t1(id int,c1 varchar(20)) character set utf8;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_CHARSET_MUST_NULL, "t1"))
config.GetGlobalConfig().Inc.EnableSetCollation = false
sql = `create table t1(id int,c1 varchar(20)) COLLATE utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrTableCollationNotSupport, "t1"))
sql = `create table t1(id int,c1 varchar(20)) character set utf8 COLLATE utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_CHARSET_MUST_NULL, "t1"),
session.NewErr(session.ErrTableCollationNotSupport, "t1"))
config.GetGlobalConfig().Inc.EnableSetCharset = true
config.GetGlobalConfig().Inc.EnableSetCollation = true
config.GetGlobalConfig().Inc.SupportCharset = ""
config.GetGlobalConfig().Inc.SupportCollation = ""
sql = `create table t1(id int,c1 varchar(20)) character set utf8 COLLATE utf8_bin;`
s.testErrorCode(c, sql)
config.GetGlobalConfig().Inc.SupportCharset = "utf8"
sql = `create table t1(id int,c1 varchar(20)) character set utf8mb4 COLLATE utf8_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8"),
session.NewErrf("字符集和排序规则不匹配!"))
config.GetGlobalConfig().Inc.SupportCollation = "utf8_bin"
sql = `create table t1(id int,c1 varchar(20)) character set utf8mb4 COLLATE utf8mb4_bin;`
s.testErrorCode(c, sql,
session.NewErr(session.ErrCharsetNotSupport, "utf8"),
session.NewErr(session.ErrCollationNotSupport, "utf8_bin"))
}
func (s *testSessionIncSuite) TestForeignKey(c *C) {
sql := ""
config.GetGlobalConfig().Inc.EnableForeignKey = false
s.mustRunExec(c, "drop table if exists t2; create table t2(id int primary key,c1 int,index ix_1(c1));drop table if exists t1; ")
sql = `create table t1(id int primary key,pid int,constraint FK_1 foreign key (pid) references t2(id));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_FOREIGN_KEY, "t1"))
config.GetGlobalConfig().Inc.EnableForeignKey = true
sql = `create table t1(id int primary key,pid int,constraint FK_1 foreign key (pid) references t2(id));`
s.testErrorCode(c, sql)
sql = `create table t1(id int primary key,pid int,constraint FK_1 foreign key (pid) references t2(id1));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t2.id1"))
sql = `create table t1(id int primary key,pid int,constraint FK_1 foreign key (pid1) references t2(id));`
s.testErrorCode(c, sql,
session.NewErr(session.ER_COLUMN_NOT_EXISTED, "t1.pid1"))
sql = `create table t1(id int primary key,c1 int,c2 int,
constraint foreign key (c1,c2) references t2(id));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrWrongFkDefWithMatch, ""))
sql = `create table t1(id int primary key,c1 int,c2 int,
constraint fk_1 foreign key (c1) references t2(id));`
s.testErrorCode(c, sql)
s.mustRunExec(c, sql)
sql = `alter table t1 drop foreign key fk_1;`
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestZeroDate(c *C) {
sql := ""
config.GetGlobalConfig().Inc.EnableZeroDate = false
sql = `create table t4 (id int unsigned not null auto_increment primary key
comment 'primary key', a datetime not null default 0 comment 'a') comment 'test';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DEFAULT, "a"))
config.GetGlobalConfig().Inc.EnableZeroDate = true
}
func (s *testSessionIncSuite) TestTimestampType(c *C) {
sql := ""
config.GetGlobalConfig().Inc.EnableTimeStampType = false
// sql = `create table t4 (id int unsigned not null auto_increment primary key comment 'primary key', a timestamp not null default 0 comment 'a') comment 'test';`
sql = `create table t4 (id int unsigned not null auto_increment primary key comment 'primary key', a timestamp not null comment 'a') comment 'test';`
s.testErrorCode(c, sql,
session.NewErr(session.ER_INVALID_DATA_TYPE, "a"))
config.GetGlobalConfig().Inc.EnableTimeStampType = true
}
func (s *testSessionIncSuite) TestAlterNoOption(c *C) {
sql := `drop table if exists t1;create table t1(id int,c1 int,key ix(c1));alter table t1;`
s.testErrorCode(c, sql,
session.NewErr(session.ER_NOT_SUPPORTED_YET))
}
func (s *testSessionIncSuite) TestFloatDouble(c *C) {
config.GetGlobalConfig().Inc.CheckFloatDouble = true
sql := `drop table if exists t1;create table t1(id int,c1 float,key ix(c1));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrFloatDoubleToDecimal, "c1"))
sql = `drop table if exists t1;create table t1(id int, c2 double,key ix(c2));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrFloatDoubleToDecimal, "c2"))
config.GetGlobalConfig().Inc.CheckFloatDouble = false
}
func (s *testSessionIncSuite) TestIdentifierUpper(c *C) {
config.GetGlobalConfig().Inc.CheckIdentifierUpper = true
sql := `drop table if exists hello;create table HELLO(ID int,C1 float, C2 double,key IDX_C1(C1),UNIQUE INDEX uniq_A(C2));`
s.testErrorCode(c, sql,
session.NewErr(session.ErrIdentifierUpper, "uniq_A"),
)
config.GetGlobalConfig().Inc.CheckIdentifierUpper = false
}
func (s *testSessionIncSuite) TestMaxKeys(c *C) {
//er_too_many_keys
config.GetGlobalConfig().Inc.MaxKeys = 2
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10),age varchar(10));alter table t1 add index idx_test(id),add index idx_test2(name);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_MANY_KEYS, "t1", 2))
config.GetGlobalConfig().Inc.MaxKeys = 3
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10),age varchar(10));alter table t1 add index idx_test(id),add index idx_test2(name);"
s.testErrorCode(c, sql)
//er_too_many_key_parts
config.GetGlobalConfig().Inc.MaxKeyParts = 2
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10),age varchar(10));alter table t1 add index idx_test(id,name,age);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_TOO_MANY_KEY_PARTS, "idx_test", "t1", 2))
config.GetGlobalConfig().Inc.MaxKeyParts = 3
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10),age varchar(10));alter table t1 add index idx_test(id,name,age);"
s.testErrorCode(c, sql)
//er_pk_too_many_parts
config.GetGlobalConfig().Inc.MaxPrimaryKeyParts = 2
sql = "drop table if exists t1; create table t1(id int,name varchar(10),age varchar(10),primary key(id,name,age));"
s.testErrorCode(c, sql,
session.NewErr(session.ER_PK_TOO_MANY_PARTS, "test_inc", "t1", 2))
config.GetGlobalConfig().Inc.MaxPrimaryKeyParts = 3
sql = "drop table if exists t1; create table t1(id int,name varchar(10),age varchar(10),primary key(id,name));"
s.testErrorCode(c, sql)
}
func (s *testSessionIncSuite) TestSetStmt(c *C) {
sql = `set names abc;
set names '';
set names utf8;
set names utf8mb4;
set autocommit = 1;
`
s.runCheck(sql)
s.assertAudit(c, s.getResultRows(),
[]*SQLError{
session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"),
},
[]*SQLError{
session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"),
},
nil,
nil,
[]*SQLError{
session.NewErr(session.ER_NOT_SUPPORTED_YET),
})
// s.add(`set names abc`, session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"))
// s.add(`set names ''`, session.NewErr(session.ErrCharsetNotSupport, "utf8,utf8mb4"))
// s.add(`set names utf8`)
// s.add(`set names utf8mb4`)
// s.add(`set autocommit = 1`, session.NewErr(session.ER_NOT_SUPPORTED_YET))
// s.runAudit(c)
}
func (s *testSessionIncSuite) TestMergeAlterTable(c *C) {
//er_alter_table_once
config.GetGlobalConfig().Inc.MergeAlterTable = true
sql = `drop table if exists t1;
create table t1(id int primary key,name varchar(10));
alter table t1 add age varchar(10);
alter table t1 add sex varchar(10);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_ALTER_TABLE_ONCE, "t1"))
//er_alter_table_once
config.GetGlobalConfig().Inc.MergeAlterTable = true
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10));alter table t1 modify name varchar(10);alter table t1 modify name varchar(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_ALTER_TABLE_ONCE, "t1"),
)
//er_alter_table_once
config.GetGlobalConfig().Inc.MergeAlterTable = true
sql = "drop table if exists t1; create table t1(id int primary key,name varchar(10));alter table t1 change name name varchar(10);alter table t1 change name name varchar(10);"
s.testErrorCode(c, sql,
session.NewErr(session.ER_ALTER_TABLE_ONCE, "t1"))
}
func (s *testSessionIncSuite) TestNewRewrite(c *C) {
var (
newSql string
rw *session.Rewrite
)
sqls := []struct {
sql string
selectSql string
countSql string
}{
{
"insert into t2 select * from t1 where id >0;",
"select * from t1 where id > 0",
"select count(*) from t1 where id > 0",
},
{
"insert into t2 select * from t1 where id >0 limit 10;",
"select * from t1 where id > 0 limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"insert into t2 select * from t1 where id >0 order by c1 desc limit 10;",
"select * from t1 where id > 0 order by c1 desc limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"insert into t2 select distinct id from t1 where id >0 order by c1 desc limit 10;",
"select distinct id from t1 where id > 0 order by c1 desc limit 10",
"SELECT COUNT(1) FROM (select distinct id from t1 where id > 0 order by c1 desc limit 10)t",
},
{
"insert into t2 select c1,count(1) as cnt from t1 where id >0 group by c1 limit 10;",
"select c1, count(1) as cnt from t1 where id > 0 group by c1 limit 10",
"SELECT COUNT(1) FROM (select c1, count(1) as cnt from t1 where id > 0 group by c1 limit 10)t",
},
{
"delete from t1 where id >0;",
"select * from t1 where id > 0",
"select count(*) from t1 where id > 0",
},
{
"delete from t1 where id >0 limit 10;",
"select * from t1 where id > 0 limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"delete from t1 where id >0 order by c1 desc limit 10;",
"select * from t1 where id > 0 order by c1 desc limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"update t1 set c1=1 where id >0;",
"select * from t1 where id > 0",
"select count(*) from t1 where id > 0",
},
{
"update t1 set c1=1 where id >0 limit 10;",
"select * from t1 where id > 0 limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"update t1 set c1=1 where id >0 order by c1 desc limit 10;",
"select * from t1 where id > 0 order by c1 desc limit 10",
"SELECT COUNT(1) FROM (select * from t1 where id > 0 limit 10)t",
},
{
"update t1 inner join t2 on t1.id=t2.id2 set t1.c1=t2.c1 where c11=1;",
"select * from t1 join t2 on t1.id = t2.id2 where c11 = 1",
"select count(*) from t1 join t2 on t1.id = t2.id2 where c11 = 1",
},
{
"update t1,t2 set t1.c1=t2.c1 where t1.id=t2.id2 and c11=1;",
"select * from t1, t2 where t1.id = t2.id2 and c11 = 1",
"select count(*) from t1, t2 where t1.id = t2.id2 and c11 = 1",
},
{
"update t1,t2 set t1.c1=t2.c1 where t1.id=t2.id2 and c11=1 limit 10;",
"select * from t1, t2 where t1.id = t2.id2 and c11 = 1 limit 10",
"SELECT COUNT(1) FROM (select * from t1, t2 where t1.id = t2.id2 and c11 = 1 limit 10)t",
},
}
for _, row := range sqls {
rw, _ = session.NewRewrite(row.sql)
rw.RewriteDML2Select()
c.Assert(rw.SQL, Equals, row.selectSql)
newSql = rw.TestSelect2Count()
c.Assert(newSql, Equals, row.countSql)
}
}
func (s *testSessionIncSuite) TestGetAlterTablePostPart(c *C) {
sqls := []struct {
sql string
outPT string
outGhost string
}{
{
"alter table tb_archery add unique index uniq_test_ghost_3 (test_ghost_3);",
"ADD UNIQUE \\`uniq_test_ghost_3\\`(\\`test_ghost_3\\`)",
"ADD UNIQUE `uniq_test_ghost_3`(`test_ghost_3`)",
},
{
"alter table tb_archery add COLUMN c1 varchar(100) default null comment '!@#$%^&*(){}:<>?,./' after id123;",
"ADD COLUMN \\`c1\\` VARCHAR(100) DEFAULT NULL COMMENT '!@#\\$%^&*(){}:<>?,./' AFTER \\`id123\\`",
"ADD COLUMN `c1` VARCHAR(100) DEFAULT NULL COMMENT '!@#$%^&*(){}:<>?,./' AFTER `id123`",
},
{
"alter table tb_archery add primary key(id);",
"ADD PRIMARY KEY(\\`id\\`)",
"ADD PRIMARY KEY(`id`)",
},
{
"alter table tb_archery add unique key uniq_1(c1);",
"ADD UNIQUE \\`uniq_1\\`(\\`c1\\`)",
"ADD UNIQUE `uniq_1`(`c1`)",
},
{
"alter table tb_archery alter column c1 drop default;",
"ALTER COLUMN \\`c1\\` DROP DEFAULT",
"ALTER COLUMN `c1` DROP DEFAULT",
},
{
"alter table tb_archery default character set utf8 collate utf8_bin;",
"CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN",
"CONVERT TO CHARACTER SET UTF8 COLLATE UTF8_BIN",
},
{
"alter table tb_archery collate = utf8_bin;",
"DEFAULT COLLATE = UTF8_BIN",
"DEFAULT COLLATE = UTF8_BIN",
},
{
"alter table t1 modify c1 varchar(100) character set utf8 collate utf8_bin;",
"MODIFY COLUMN \\`c1\\` VARCHAR(100) CHARACTER SET UTF8 COLLATE utf8_bin",
"MODIFY COLUMN `c1` VARCHAR(100) CHARACTER SET UTF8 COLLATE utf8_bin",
},
{
"alter table t1 modify column c1 varchar(100) collate utf8_bin;",
"MODIFY COLUMN \\`c1\\` VARCHAR(100) COLLATE utf8_bin",
"MODIFY COLUMN `c1` VARCHAR(100) COLLATE utf8_bin",
},
}
for _, row := range sqls {
out := s.session.GetAlterTablePostPart(row.sql, true)
c.Assert(out, Equals, row.outPT, Commentf("%v", row.sql))
out = s.session.GetAlterTablePostPart(row.sql, false)
c.Assert(out, Equals, row.outGhost, Commentf("%v", row.sql))
}
}
// TestDisplayWidth 测试列指定长度参数
func (s *testSessionIncSuite) TestDisplayWidth(c *C) {
sql := ""
config.GetGlobalConfig().Inc.CheckColumnComment = false
config.GetGlobalConfig().Inc.CheckTableComment = false
config.GetGlobalConfig().Inc.EnableEnumSetBit = true
s.mustRunExec(c, "drop table if exists t1;")
// 数据类型 警告
sql = `create table t1(c1 bit(100),
c2 tinyint(1000),
c3 smallint(1000),
c4 mediumint(1000),
c5 int(1000),
c6 bigint(1000) );`
s.testErrorCode(c, sql,
session.NewErrf("Too big display width for column '%s' (max = 64).", "c1"),
session.NewErrf("Too big display width for column '%s' (max = 255).", "c2"),
session.NewErrf("Too big display width for column '%s' (max = 255).", "c3"),
session.NewErrf("Too big display width for column '%s' (max = 255).", "c4"),
session.NewErrf("Too big display width for column '%s' (max = 255).", "c5"),
session.NewErrf("Too big display width for column '%s' (max = 255).", "c6"),
)
// 数据类型 警告
sql = `create table t1(id int);
alter table t1 add column c1 tinyint(1000);
`
s.testErrorCode(c, sql,
session.NewErrf("Too big display width for column '%s' (max = 255).", "c1"))
}
// TestSetVariables 设置会话级变量进行审核
func (s *testSessionIncSuite) TestSetSessionVariables(c *C) {
sql := ""
s.mustRunExec(c, "drop table if exists t1;")
config.GetGlobalConfig().Inc.CheckTableComment = true
sql = `create table t1(id int primary key);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_TABLE_MUST_HAVE_COMMENT, "t1"))
sql = `inception set check_table_comment = 0;
create table t1(id int primary key);`
s.testErrorCode(c, sql)
sql = `inception set check_table_comment = "123";
create table t1(id int primary key);`
s.testManyErrors(c, sql,
session.NewErrf("[variable:1231]Variable 'check_table_comment' can't be set to the value of '123'."),
session.NewErr(session.ER_TABLE_MUST_HAVE_COMMENT, "t1"))
sql = `inception set level er_table_must_have_comment = 2;`
s.testManyErrors(c, sql,
session.NewErrf("暂不支持会话级的自定义审核级别."))
sql = `inception set global check_table_comment = 1;`
s.testManyErrors(c, sql,
session.NewErrf("全局变量仅支持单独设置."))
sql = `inception set lang = 'zh_cn';
create table t1(id int primary key);
inception set lang = 'en_us';
create table t2(id int primary key);`
s.testManyErrors(c, sql,
session.NewErrf("表 't1' 需要设置注释."),
session.NewErrf("Set comments for table 't2'."))
config.GetGlobalConfig().Inc.CheckTableComment = false
sql = `inception set osc_chunk_time = 1.1;;
create table t1(id int primary key);`
s.testErrorCode(c, sql)
}
// TestSetVariables 设置会话级变量进行审核
func (s *testSessionIncSuite) TestBlobAndText(c *C) {
sql := ""
s.mustRunExec(c, "drop table if exists t1,t2;")
config.GetGlobalConfig().Inc.EnableBlobType = false
sql = `create table t1(id int primary key,
c1 tinyblob ,
c2 blob,
c3 mediumblob,
c4 longblob);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c2"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c3"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c4"))
sql = `create table t2(id int primary key,
c1 tinytext ,
c2 text,
c3 mediumtext,
c4 longtext);`
s.testErrorCode(c, sql,
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c1"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c2"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c3"),
session.NewErr(session.ER_USE_TEXT_OR_BLOB, "c4"))
}
func (s *testSessionIncSuite) TestWhereCondition(c *C) {
sql := ""
s.mustRunExec(c, "drop table if exists t1,t2;create table t1(id int);")
sql = "update t1 set id = 1 where 123;"
config.GetGlobalConfig().IncLevel.ErrUseValueExpr = 0
s.testErrorCode(c, sql)
config.GetGlobalConfig().IncLevel.ErrUseValueExpr = 1
s.testErrorCode(c, sql,
session.NewErr(session.ErrUseValueExpr))
sql = "update t1 set id = 1 where null;"
s.testErrorCode(c, sql,
session.NewErr(session.ErrUseValueExpr))
sql = `
update t1 set id = 1 where 1+2;
update t1 set id = 1 where 1-2;
update t1 set id = 1 where 1*2;
delete from t1 where 1/2;
delete from t1 where 1&2;
delete from t1 where 1|2;
delete from t1 where 1^2;
delete from t1 where 1 div 2;
`
s.testManyErrors(c, sql,
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
session.NewErr(session.ErrUseValueExpr),
)
sql = `
update t1 set id = 1 where 1+2=3;
update t1 set id = 1 where id is null;
update t1 set id = 1 where id is not null;
update t1 set id = 1 where 1=1;
update t1 set id = 1 where id in (1,2);
update t1 set id = 1 where id is true;
`
s.testManyErrors(c, sql)
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/hanchuanchuan-goInception.git
git@gitlife.ru:oschina-mirror/hanchuanchuan-goInception.git
oschina-mirror
hanchuanchuan-goInception
hanchuanchuan-goInception
v1.2.4