MCPcopy
hub / github.com/pingcap/tidb / ShowCreateSequence

Function ShowCreateSequence

dumpling/export/sql.go:180–230  ·  view source on GitHub ↗

ShowCreateSequence constructs the create sequence SQL for a specified sequence returns (createSequenceSQL, error)

(tctx *tcontext.Context, db *BaseConn, database, sequence string, conf *Config)

Source from the content-addressed store, hash-verified

178// ShowCreateSequence constructs the create sequence SQL for a specified sequence
179// returns (createSequenceSQL, error)
180func ShowCreateSequence(tctx *tcontext.Context, db *BaseConn, database, sequence string, conf *Config) (string, error) {
181 var oneRow [2]string
182 handleOneRow := func(rows *sql.Rows) error {
183 return rows.Scan(&oneRow[0], &oneRow[1])
184 }
185 var (
186 createSequenceSQL strings.Builder
187 nextNotCachedValue int64
188 )
189 query := fmt.Sprintf("SHOW CREATE SEQUENCE `%s`.`%s`", escapeString(database), escapeString(sequence))
190 err := db.QuerySQL(tctx, handleOneRow, func() {
191 oneRow[0], oneRow[1] = "", ""
192 }, query)
193 if err != nil {
194 return "", err
195 }
196 createSequenceSQL.WriteString(oneRow[1])
197 createSequenceSQL.WriteString(";\n")
198
199 switch conf.ServerInfo.ServerType {
200 case version.ServerTypeTiDB:
201 // Get next not allocated auto increment id of the whole cluster
202 query := fmt.Sprintf("SHOW TABLE `%s`.`%s` NEXT_ROW_ID", escapeString(database), escapeString(sequence))
203 results, err := db.QuerySQLWithColumns(tctx, []string{"NEXT_GLOBAL_ROW_ID", "ID_TYPE"}, query)
204 if err != nil {
205 return "", err
206 }
207 for _, oneRow := range results {
208 nextGlobalRowID, idType := oneRow[0], oneRow[1]
209 if idType == "SEQUENCE" {
210 nextNotCachedValue, _ = strconv.ParseInt(nextGlobalRowID, 10, 64)
211 }
212 }
213 fmt.Fprintf(&createSequenceSQL, "SELECT SETVAL(`%s`,%d);\n", escapeString(sequence), nextNotCachedValue)
214 case version.ServerTypeMariaDB:
215 var oneRow1 string
216 handleOneRow1 := func(rows *sql.Rows) error {
217 return rows.Scan(&oneRow1)
218 }
219 query := fmt.Sprintf("SELECT NEXT_NOT_CACHED_VALUE FROM `%s`.`%s`", escapeString(database), escapeString(sequence))
220 err := db.QuerySQL(tctx, handleOneRow1, func() {
221 oneRow1 = ""
222 }, query)
223 if err != nil {
224 return "", err
225 }
226 nextNotCachedValue, _ = strconv.ParseInt(oneRow1, 10, 64)
227 fmt.Fprintf(&createSequenceSQL, "SELECT SETVAL(`%s`,%d);\n", escapeString(sequence), nextNotCachedValue)
228 }
229 return createSequenceSQL.String(), nil
230}
231
232// SetCharset builds the set charset SQLs
233func SetCharset(w *strings.Builder, characterSet, collationConnection string) {

Callers 2

TestShowCreateSequenceFunction · 0.85
dumpTableMetaFunction · 0.85

Calls 6

QuerySQLMethod · 0.80
QuerySQLWithColumnsMethod · 0.80
escapeStringFunction · 0.70
ScanMethod · 0.65
StringMethod · 0.65
WriteStringMethod · 0.45

Tested by 1

TestShowCreateSequenceFunction · 0.68