| 104 | } |
| 105 | |
| 106 | func (d *dumper) buildQueryOnUniqueKey() string { |
| 107 | nCol := len(d.Table.UseUniqueKey.Columns.Columns) |
| 108 | uniqueKeyColumnAscending := make([]string, nCol, nCol) |
| 109 | for i, col := range d.Table.UseUniqueKey.Columns.Columns { |
| 110 | colName := col.EscapedName |
| 111 | switch col.Type { |
| 112 | case umconf.EnumColumnType: |
| 113 | // TODO try mysql enum type |
| 114 | uniqueKeyColumnAscending[i] = fmt.Sprintf("concat(%s) asc", colName) |
| 115 | default: |
| 116 | uniqueKeyColumnAscending[i] = fmt.Sprintf("%s asc", colName) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | var rangeStr string |
| 121 | |
| 122 | if d.Iteration == 0 { |
| 123 | rangeStr = "true" |
| 124 | } else { |
| 125 | rangeItems := make([]string, nCol) |
| 126 | |
| 127 | // The form like: (A > a) or (A = a and B > b) or (A = a and B = b and C > c) or ... |
| 128 | for x := 0; x < nCol; x++ { |
| 129 | innerItems := make([]string, x+1) |
| 130 | |
| 131 | for y := 0; y < x; y++ { |
| 132 | colName := d.Table.UseUniqueKey.Columns.Columns[y].EscapedName |
| 133 | innerItems[y] = fmt.Sprintf("(%s = %s)", colName, d.Table.UseUniqueKey.LastMaxVals[y]) |
| 134 | } |
| 135 | |
| 136 | colName := d.Table.UseUniqueKey.Columns.Columns[x].EscapedName |
| 137 | innerItems[x] = fmt.Sprintf("(%s > %s)", colName, d.Table.UseUniqueKey.LastMaxVals[x]) |
| 138 | |
| 139 | rangeItems[x] = fmt.Sprintf("(%s)", strings.Join(innerItems, " and ")) |
| 140 | } |
| 141 | |
| 142 | rangeStr = strings.Join(rangeItems, " or ") |
| 143 | } |
| 144 | |
| 145 | return fmt.Sprintf(`SELECT %s FROM %s.%s where (%s) and (%s) order by %s LIMIT %d`, |
| 146 | d.Columns, |
| 147 | d.EscapedTableSchema, |
| 148 | d.EscapedTableName, |
| 149 | // where |
| 150 | rangeStr, d.Table.GetWhere(), |
| 151 | // order by |
| 152 | strings.Join(uniqueKeyColumnAscending, ", "), |
| 153 | // limit |
| 154 | d.ChunkSize, |
| 155 | ) |
| 156 | } |
| 157 | |
| 158 | // dumps a specific chunk, reading chunk info from the channel |
| 159 | func (d *dumper) getChunkData() (nRows int64, err error) { |