yangzhg commented on a change in pull request #5033:
URL: https://github.com/apache/incubator-doris/pull/5033#discussion_r537254626



##########
File path: be/src/exec/odbc_connecter.cpp
##########
@@ -172,7 +180,175 @@ Status ODBCScanner::get_next_row(bool* eos) {
     return Status::OK();
 }
 
-Status ODBCScanner::error_status(const std::string& prefix, const std::string& 
error_msg) {
+Status ODBCConnecter::init_to_write() {
+    if (!_is_open) {
+        return Status::InternalError( "Init before open.");
+    }
+
+    // Allocate a statement handle
+    ODBC_DISPOSE(_dbc, SQL_HANDLE_DBC, SQLAllocHandle(SQL_HANDLE_STMT, _dbc, 
&_stmt), "alloc statement");
+
+    return Status::OK();
+}
+
+Status ODBCConnecter::append(const std::string& table_name, RowBatch *batch) {
+    if (batch == nullptr || batch->num_rows() == 0) {
+        return Status::OK();
+    }
+
+    int num_rows = batch->num_rows();
+    for (int i = 0; i < num_rows; ++i) {
+        RETURN_IF_ERROR(insert_row(table_name, batch->get_row(i)));
+    }
+
+    return Status::OK();
+}
+
+Status ODBCConnecter::insert_row(const std::string& table_name, TupleRow *row) 
{
+    std::stringstream ss;
+
+    // Construct Insert statement of mysql
+    ss << "INSERT INTO " << table_name << " VALUES (";
+    int num_columns = _output_expr_ctxs.size();
+    for (int i = 0; i < num_columns; ++i) {
+        if (i != 0) {
+            ss << ", ";
+        }
+        void* item = _output_expr_ctxs[i]->get_value(row);
+        if (item == nullptr) {
+            ss << "NULL";
+            continue;
+        }
+        switch (_output_expr_ctxs[i]->root()->type().type) {
+            case TYPE_BOOLEAN:
+            case TYPE_TINYINT:
+                ss << (int)*static_cast<int8_t*>(item);
+                break;
+            case TYPE_SMALLINT:
+                ss << *static_cast<int16_t*>(item);
+                break;
+            case TYPE_INT:
+                ss << *static_cast<int32_t*>(item);
+                break;
+            case TYPE_BIGINT:
+                ss << *static_cast<int64_t*>(item);
+                break;
+            case TYPE_FLOAT:
+                ss << *static_cast<float*>(item);
+                break;
+            case TYPE_DOUBLE:
+                ss << *static_cast<double*>(item);
+                break;
+            case TYPE_DATE:
+            case TYPE_DATETIME: {
+                char buf[64];
+                const DateTimeValue* time_val = (const DateTimeValue*)(item);
+                time_val->to_string(buf);
+                ss << "\'" << buf << "\'";
+                break;
+            }
+            case TYPE_VARCHAR:
+            case TYPE_CHAR: {
+                const StringValue* string_val = (const StringValue*)(item);
+
+                if (string_val->ptr == NULL) {
+                    if (string_val->len == 0) {
+                        ss << "\'\'";
+                    } else {
+                        ss << "NULL";
+                    }
+                } else {
+                    ss << "\'";
+                    for (int j = 0; j < string_val->len ; ++j) {
+                        ss << string_val->ptr[j];
+                    }
+                    ss << "\'";
+                }
+                break;
+            }
+            case TYPE_DECIMAL: {
+                const DecimalValue* decimal_val = reinterpret_cast<const 
DecimalValue*>(item);
+                std::string decimal_str;
+                int output_scale = 
_output_expr_ctxs[i]->root()->output_scale();
+
+                if (output_scale > 0 && output_scale <= 30) {
+                    decimal_str = decimal_val->to_string(output_scale);
+                } else {
+                    decimal_str = decimal_val->to_string();
+                }
+                ss << decimal_str;
+                break;
+            }
+            case TYPE_DECIMALV2: {
+                const DecimalV2Value decimal_val(reinterpret_cast<const 
PackedInt128*>(item)->value);
+                std::string decimal_str;
+                int output_scale = 
_output_expr_ctxs[i]->root()->output_scale();
+
+                if (output_scale > 0 && output_scale <= 30) {
+                    decimal_str = decimal_val.to_string(output_scale);
+                } else {
+                    decimal_str = decimal_val.to_string();
+                }
+                ss << decimal_str;
+                break;
+            }
+
+            default: {
+                std::stringstream err_ss;
+                err_ss << "can't convert this type to mysql type. type = " <<
+                       _output_expr_ctxs[i]->root()->type();
+                return Status::InternalError(err_ss.str());
+            }
+        }
+    }
+    ss << ")";
+
+    // Translate utf8 string to utf16 to use unicode codeing

Review comment:
       ```suggestion
       // Translate utf8 string to utf16 to use unicode encoding
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to