/** * Verifies that the provided table schema has an auto increment column name * * @param schema The table schema to verify * @return True if the schema has an auto increment column name, false otherwise */ public static boolean hasAutoIncrementColumn(final TableSchema schema) { return schema.getAutoIncrementColumn() != null; }
@Test public void testGetAutoIncColumns() { assertEquals(COLUMN_A, tableSchema.getAutoIncrementColumn()); }
@Test public void testGetAutoIncColumnNoAutoIncColumns() { final TableSchema schema = new TableSchema(ImmutableList.<ColumnSchema>of(FAKE_COL_SCHEMA), INDICES); assertEquals(null, schema.getAutoIncrementColumn()); }
/** * Insert row into table. * * @param rowBytes Serialized row to be written */ public void insertRow(byte[] rowBytes) { checkTableOpen(); checkNotNull(rowBytes); TableSchema schema = store.getSchema(tableName); Row row = Row.deserialize(rowBytes); row.setRandomUUID(); String auto_inc_col = schema.getAutoIncrementColumn(); if (auto_inc_col != null) { ByteBuffer bb = row.getRecords().get(auto_inc_col); if (bb != null) { long auto_inc = bb.getLong(); long next_auto_inc = auto_inc + 1; if (auto_inc > next_auto_inc) { // The autoincrement will wrap around. MySQL says don't wrap. next_auto_inc = auto_inc; } bb.rewind(); store.setAutoInc(tableName, next_auto_inc); } } table.insertRow(row); if (schema.hasUniqueIndices()) { table.flush(); } }