-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from zendesk/sets_and_enums
Sets and enums
- Loading branch information
Showing
17 changed files
with
209 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/zendesk/maxwell/schema/columndef/EnumColumnDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.zendesk.maxwell.schema.columndef; | ||
|
||
import com.google.code.or.common.util.MySQLConstants; | ||
|
||
public class EnumColumnDef extends ColumnDef { | ||
public EnumColumnDef(String tableName, String name, String type, int pos, String[] enumValues) { | ||
super(tableName, name, type, pos); | ||
this.enumValues = enumValues; | ||
} | ||
|
||
@Override | ||
public boolean matchesMysqlType(int type) { | ||
return type == MySQLConstants.TYPE_ENUM; | ||
} | ||
|
||
@Override | ||
public String toSQL(Object value) { | ||
return "'" + asString(value) + "'"; | ||
} | ||
|
||
@Override | ||
public String asJSON(Object value) { | ||
return asString(value); | ||
} | ||
|
||
private String asString(Object value) { | ||
return enumValues[((Integer) value) - 1]; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/zendesk/maxwell/schema/columndef/SetColumnDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.zendesk.maxwell.schema.columndef; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import com.google.code.or.common.util.MySQLConstants; | ||
|
||
public class SetColumnDef extends ColumnDef { | ||
public SetColumnDef(String tableName, String name, String type, int pos, String[] enumValues) { | ||
super(tableName, name, type, pos); | ||
this.enumValues = enumValues; | ||
} | ||
|
||
@Override | ||
public boolean matchesMysqlType(int type) { | ||
return type == MySQLConstants.TYPE_SET; | ||
} | ||
|
||
@Override | ||
public String toSQL(Object value) { | ||
return "'" + StringUtils.join(asList(value), "'") + "'"; | ||
} | ||
|
||
@Override | ||
public Object asJSON(Object value) { | ||
return asList(value); | ||
} | ||
|
||
private ArrayList<String> asList(Object value) { | ||
ArrayList<String> values = new ArrayList<>(); | ||
long v = (Long) value; | ||
for(int i = 0; i < enumValues.length; i++) { | ||
if ( ((v >> i) & 1) == 1 ) { | ||
values.add(enumValues[i]); | ||
} | ||
} | ||
return values; | ||
} | ||
} |
Oops, something went wrong.