Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Char Parameter Type #205

Closed
masterots opened this issue Oct 10, 2014 · 7 comments
Closed

Support Char Parameter Type #205

masterots opened this issue Oct 10, 2014 · 7 comments

Comments

@masterots
Copy link

When doing a bulk load, I am getting the error TypeError: Object #<Object> has no method 'writeParameterData' when the column datatype is of type char.

Looking at the source code, writeParameterData does not exist on that type, therefore I get the error.

Was this overlooked, or am I doing something wrong here? I'm making sure to set the appropriate value of TYPES.Char, passing it as a string when creating the row.

@bretcope
Copy link
Member

The char type is sadly not currently supported as a parameter type. Notice it's missing from the parameter column in the documentation too. That's not a great answer, I know. Apparently you're the first one who has tried to use it and complained.

Someone just needs to write an implementation for it. Shouldn't be incredibly difficult. The relevant TDS documentation starts here. If you want to take a crack at it, feel free. I doubt I would be able to get to this until sometime next week, though I can't speak for the other maintainers.

@bretcope bretcope changed the title TypeError: Object #<Object> has no method 'writeParameterData' Support Char Parameter Type Oct 15, 2014
@krisklosterman
Copy link

@masterots
I am going to take a shot at this, can you send me over the an example of the code you were using to try and do the bulk load?

@bretcope
Copy link
Member

@krisklosterman you just need to make it work as a parameter type. Bulk insert should work automatically if it works as a parameter.

@masterots
Copy link
Author

There's a lot of context around this, but basically, I have some csv data parsed, I know based on column name and table what the data type is, if it's nullable, and its maxlength. I can't post a whole lot, since it's being used for a private corporate project.

function bulkLoadData(input) {
  return Q.promise(function(fulfill, reject) {
    var bulkLoad = input.connection.newBulkLoad(input.tableName, function(err, results) {
      if (err) {
        input.connection.close();
        reject(err);
      }
      input.connection.close();
      fulfill(input);
    });
    var columnNames = lazy(input.csvData[0]).keys().toArray();
    var columnInfo = tableColumnInfo.getTableColumnInfo(input.tableName);
    for (var columnIndex = 0; columnIndex < columnNames.length; columnIndex++) {
      var options = { nullable: columnInfo[columnNames[columnIndex]].isNullable };
      if (columnInfo[columnNames[columnIndex]].maxLength) {
        options.length = columnInfo[columnNames[columnIndex]].maxLength;
      }
      bulkLoad.addColumn(columnNames[columnIndex], dataTypes.getDataTypeConstant(columnInfo[columnNames[columnIndex]].dataType), options);
    }
    lazy(input.csvData).each(function(item) {
      var row = {};
      for (var columnRowIndex = 0; columnRowIndex < columnNames.length; columnRowIndex++) {
        row[columnNames[columnRowIndex]] = dataTypes.formatValue(item[columnNames[columnRowIndex]], columnInfo[columnNames[columnRowIndex]].dataType);
      }
      bulkLoad.addRow(row);
    });
    input.connection.execBulkLoad(bulkLoad);
  });
}    

@krisklosterman
Copy link

I think I may have this working.

The below code now inserts into my database

connection.on('connect', function(err){
    if (err) {
        console.log(err);
        return;
    }
    var request = new Request("INSERT INTO test.charTest (charType) VALUES (@charTypeValue)",
    function(err){
        if(err){
            console.log(err);
        };
    });

    request.addParameter('charTypeValue', TYPES.Char, 'TheFred');
    //request.addParameter('charTypeValue', TYPES.VarChar, 'TheFred');

    connection.execSql(request);
});

Database Structure

CREATE TABLE [test].[charTest] (
    [int] int IDENTITY(1,1) NOT NULL,
    [charType] char(7) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
ON [PRIMARY]
GO

I am still getting used to GIT and have not contributed to a github project before, currently I cloned the project and modified some files. How do I go about creating a branch and gitting it up so that it can be reviewed, and then merged?

@krisklosterman
Copy link

Created A Pull request from a Fork, let me know if that was done correctly (Check post above this)

@patriksimek
Copy link
Collaborator

Support for Char and NChar will be added in version 1.7.0 later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants