Skip to content

Commit

Permalink
add binary encoding to load hex or base64 blob data
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Oct 30, 2015
1 parent b208d16 commit 025f1fa
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions symmetric-client-clib/inc/io/data/Batch.h
Expand Up @@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "util/StringBuilder.h"
#include "util/BinaryEncoding.h"

typedef struct SymBatch {
long batchId;
Expand All @@ -32,6 +33,7 @@ typedef struct SymBatch {
int initialLoad;
char *channelId;
unsigned short isIgnore;
SymBinaryEncoding binaryEncoding;
void (*destroy)(struct SymBatch *this);
} SymBatch;

Expand Down
Expand Up @@ -33,6 +33,7 @@
#include "util/Map.h"
#include "util/List.h"
#include "util/Base64.h"
#include "util/Hex.h"
#include "db/sql/DmlStatement.h"
#include "db/sql/SqlTemplate.h"
#include "db/sql/SqlTransaction.h"
Expand Down
38 changes: 38 additions & 0 deletions symmetric-client-clib/inc/util/BinaryEncoding.h
@@ -0,0 +1,38 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef SYM_BINARY_ENCODING_H
#define SYM_BINARY_ENCODING_H

#include <stdio.h>
#include <stdlib.h>
#include "util/StringUtils.h"

#define SYM_BINARY_ENCODING_NONE "none"
#define SYM_BINARY_ENCODING_BASE64 "base64"
#define SYM_BINARY_ENCODING_HEX "hex"

typedef enum SymBinaryEncoding {
SymBinaryEncoding_NONE, SymBinaryEncoding_BASE64, SymBinaryEncoding_HEX
} SymBinaryEncoding;

SymBinaryEncoding SymBinaryEncoding_valueOf(char *str);

#endif
2 changes: 2 additions & 0 deletions symmetric-client-clib/src/io/reader/ProtocolDataReader.c
Expand Up @@ -91,6 +91,8 @@ static void SymProtocolDataReader_parseLine(int eol, void *userData) {
} else if (strcmp(token, SYM_CSV_BATCH) == 0) {
batch->batchId = atol(fields->get(fields, 1));
this->writer->startBatch(this->writer, batch);
} else if (strcmp(token, SYM_CSV_BINARY) == 0) {
this->batch->binaryEncoding = SymBinaryEncoding_valueOf(fields->get(fields, 1));
} else if (strcmp(token, SYM_CSV_COMMIT) == 0) {
this->writer->endBatch(this->writer, batch);
} else if (strcmp(token, SYM_CSV_IGNORE) == 0) {
Expand Down
11 changes: 10 additions & 1 deletion symmetric-client-clib/src/io/writer/DefaultDatabaseWriter.c
Expand Up @@ -79,7 +79,16 @@ void SymDefaultDatabaseWriter_buildTargetValues(SymDefaultDatabaseWriter *this,
SymColumn *column = (SymColumn *) iter->next(iter);
SymColumn *targetColumn = this->targetTable->findColumn(this->targetTable, column->name, 0);
if (targetColumn && (!onlyPrimaryKeys || targetColumn->isPrimaryKey)) {
targetValues->add(targetValues, sourceValues->array[iter->index]);
// TODO: call platform.getObjectValues() instead
if (targetColumn->sqlType == SYM_SQL_TYPE_BLOB && this->batch->binaryEncoding == SymBinaryEncoding_BASE64) {
int outSize;
targetValues->add(targetValues, (char *) SymBase64_decode(sourceValues->array[iter->index], &outSize));
} else if (targetColumn->sqlType == SYM_SQL_TYPE_BLOB && this->batch->binaryEncoding == SymBinaryEncoding_HEX) {
int outSize;
targetValues->add(targetValues, (char *) SymHex_decode(sourceValues->array[iter->index], &outSize));
} else {
targetValues->add(targetValues, sourceValues->array[iter->index]);
}
}
}
iter->destroy(iter);
Expand Down
30 changes: 30 additions & 0 deletions symmetric-client-clib/src/util/BinaryEncoding.c
@@ -0,0 +1,30 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "util/BinaryEncoding.h"

SymBinaryEncoding SymBinaryEncoding_valueOf(char *str) {
if (SymStringUtils_equalsIgnoreCase(str, SYM_BINARY_ENCODING_HEX)) {
return SymBinaryEncoding_HEX;
} else if (SymStringUtils_equalsIgnoreCase(str, SYM_BINARY_ENCODING_BASE64)) {
return SymBinaryEncoding_BASE64;
}
return SymBinaryEncoding_NONE;
}

0 comments on commit 025f1fa

Please sign in to comment.