Skip to content

Commit 6a5da16

Browse files
committed
fix: fixed issues
1 parent 16d8ee5 commit 6a5da16

File tree

13 files changed

+908
-657
lines changed

13 files changed

+908
-657
lines changed

lib/builder.js

Lines changed: 68 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {Buffer} = require('buffer');
1+
const { Buffer } = require('buffer');
22

33
const dialectsHash = {
44
base: require('./dialects/base/index.js'),
@@ -53,81 +53,92 @@ Builder.prototype._pushValue = function (value) {
5353
}
5454

5555
if (typeof value === 'string' || value instanceof Date) {
56-
if (this.options.separatedValues) {
57-
const placeholder = this._getPlaceholder();
56+
return this._handleStringOrDateValue(value);
57+
}
5858

59-
if (this.options.namedValues) {
60-
this._values[placeholder] = value;
61-
} else {
62-
this._values.push(value);
63-
}
59+
if (Buffer.isBuffer(value)) {
60+
return this._handleBufferValue(value);
61+
}
6462

65-
return this._wrapPlaceholder(placeholder);
66-
}
63+
if (typeof value === 'object' && value !== null) {
64+
return this._handleObjectValue(value);
65+
}
66+
67+
throw new Error('Wrong value type "' + typeof value + '"');
68+
};
6769

68-
if (value instanceof Date) {
69-
value = value.toISOString();
70+
Builder.prototype._handleStringOrDateValue = function (value) {
71+
if (this.options.separatedValues) {
72+
const placeholder = this._getPlaceholder();
73+
74+
if (this.options.namedValues) {
75+
this._values[placeholder] = value;
76+
} else {
77+
this._values.push(value);
7078
}
7179

72-
return '\'' + value + '\'';
80+
return this._wrapPlaceholder(placeholder);
7381
}
7482

75-
if (Buffer.isBuffer(value)) {
76-
// Issue #56: Support for Buffers (BLOB/Hex)
77-
const hexValue = value.toString('hex');
78-
if (this.options.separatedValues) {
79-
const placeholder = this._getPlaceholder();
80-
if (this.options.namedValues) {
81-
this._values[placeholder] = hexValue;
82-
} else {
83-
this._values.push(hexValue);
84-
}
83+
if (value instanceof Date) {
84+
value = value.toISOString();
85+
}
8586

86-
return this._wrapPlaceholder(placeholder);
87+
return "'" + value + "'";
88+
};
89+
90+
Builder.prototype._handleBufferValue = function (value) {
91+
const hexValue = value.toString('hex');
92+
if (this.options.separatedValues) {
93+
const placeholder = this._getPlaceholder();
94+
if (this.options.namedValues) {
95+
this._values[placeholder] = hexValue;
96+
} else {
97+
this._values.push(hexValue);
8798
}
8899

89-
return '\'' + hexValue + '\'';
100+
return this._wrapPlaceholder(placeholder);
90101
}
91102

92-
if (typeof value === 'object' && value !== null) {
93-
// Issue #55: Support for BSON ObjectId conversion to String
94-
// Issue #57: Support for null Object type
95-
let stringValue;
103+
return "'" + hexValue + "'";
104+
};
96105

97-
// Handle null objects (objects that are explicitly null-like) first
98-
if (Object.keys(value).length === 0) {
99-
return 'null';
100-
}
106+
Builder.prototype._handleObjectValue = function (value) {
107+
// Issue #55: Support for BSON ObjectId conversion to String
108+
// Issue #57: Support for null Object type
109+
let stringValue;
101110

102-
// Check for BSON ObjectId (has toHexString method)
103-
if (typeof value.toHexString === 'function') {
104-
stringValue = value.toHexString();
105-
} else if (
106-
value.constructor !== Object
107-
&& typeof value.toString === 'function'
108-
) {
109-
// Check for objects with toString that aren't plain objects
110-
stringValue = value.toString();
111-
} else {
112-
// Still throw error for plain objects that can't be converted
113-
throw new Error('Wrong value type "' + typeof value + '"');
114-
}
111+
// Handle null objects (objects that are explicitly null-like) first
112+
if (Object.keys(value).length === 0) {
113+
return 'null';
114+
}
115115

116-
if (this.options.separatedValues) {
117-
const placeholder = this._getPlaceholder();
118-
if (this.options.namedValues) {
119-
this._values[placeholder] = stringValue;
120-
} else {
121-
this._values.push(stringValue);
122-
}
116+
// Check for BSON ObjectId (has toHexString method)
117+
if (typeof value.toHexString === 'function') {
118+
stringValue = value.toHexString();
119+
} else if (
120+
value.constructor !== Object &&
121+
typeof value.toString === 'function'
122+
) {
123+
// Check for objects with toString that aren't plain objects
124+
stringValue = value.toString();
125+
} else {
126+
// Still throw error for plain objects that can't be converted
127+
throw new Error('Wrong value type "' + typeof value + '"');
128+
}
123129

124-
return this._wrapPlaceholder(placeholder);
130+
if (this.options.separatedValues) {
131+
const placeholder = this._getPlaceholder();
132+
if (this.options.namedValues) {
133+
this._values[placeholder] = stringValue;
134+
} else {
135+
this._values.push(stringValue);
125136
}
126137

127-
return '\'' + stringValue + '\'';
138+
return this._wrapPlaceholder(placeholder);
128139
}
129140

130-
throw new Error('Wrong value type "' + typeof value + '"');
141+
return "'" + stringValue + "'";
131142
};
132143

133144
Builder.prototype.configure = function (options) {
@@ -143,7 +154,7 @@ Builder.prototype.configure = function (options) {
143154

144155
if (options.namedValues && !options.indexedValues) {
145156
throw new Error(
146-
'namedValues option can not be used without indexedValues option',
157+
'namedValues option can not be used without indexedValues option'
147158
);
148159
}
149160

0 commit comments

Comments
 (0)