|
| 1 | +# JS stored procedure and function overview |
| 2 | + |
| 3 | +--8<--- "experimental.md" |
| 4 | + |
| 5 | +Integrating stored procedures and functions in JS within a MySQL-compatible database provides a versatile and practical approach to managing complex data processing tasks. This method significantly enhances performance, allowing developers to execute intricate operations more efficiently. For those proficient in JS, this approach streamlines the development process, reducing the load on client applications and optimizing overall system performance. By employing stored procedures and functions, developers achieve faster data processing and facilitate more manageable maintenance and scalability, making it an ideal solution for those skilled in JS. |
| 6 | + |
| 7 | +| Benefit | Description | |
| 8 | +|------------------------------------|-----------------------------------------------------------------------------------------------| |
| 9 | +| Familiarity | Developers who are already proficient in JS can leverage their existing skills. | |
| 10 | +| Efficiency | JS allows for more efficient execution of complex data processing tasks. | |
| 11 | +| Performance | Stored procedures and functions in JS can enhance database performance by reducing client load.| |
| 12 | +| Reusability | Encapsulated logic in stored procedures and functions can be reused across multiple applications.| |
| 13 | +| Scalability | Facilitates easier maintenance and scalability of database operations. | |
| 14 | +| Simplified Development Process | Streamlines the development process, making it more manageable for those skilled in JS. | |
| 15 | +| Integration with Client Applications| Seamless integration with client applications, reducing the need for additional processing. | |
| 16 | +| Optimization | Optimizes overall system performance through efficient data processing. | |
| 17 | + |
| 18 | + |
| 19 | +## Limitations |
| 20 | + |
| 21 | +The JS procedure parameters cannot be [JS reserved words](https://developer.mozilla.org/en-US/docs/Web/JS/Reference/Lexical_grammar#reserved_words) and must be [legal JS identifiers](https://www.capscode.in/blog/valid-identifier-in-js). |
| 22 | + |
| 23 | +Our implementation offers the same level of JS support as the V8 engine inside the context of a database engine. You can check out the details at [v8.dev/docs](https://v8.dev/docs) and [tc39.es/ecma262](https://v8.dev/docs). Developers have access to standard operators, data types, objects (such as Math), and functions defined in the ECMAScript standard. However, access to node.jsor a browser is not available. |
| 24 | + |
| 25 | +In a typical database environment, direct access to external files (like reading or writing files on the server's file system) and DOM objects (which are specific to browsers) is restricted. Our implementation adheres to a trusted external routine language policy, ensuring routines cannot perform operations beyond what is normally possible for database users. Consequently, file or network I/O operations are not supported within our routines. |
| 26 | + |
| 27 | +Our system supports asynchronous JS code, but it does not work well for database routines. Since everything runs on the same thread and there is nothing to wait for asynchronously, using asynchronous code is unnecessary and not recommended. |
| 28 | + |
| 29 | +We always run JS code in strict mode, and developers cannot disable or change this setting. |
| 30 | + |
| 31 | +## Convert SQL data types to JS |
| 32 | + |
| 33 | +SQL and JS use different data types, so our implementation converts values when passing SQL parameters to JS and back. The following rules explain how these conversions work: |
| 34 | + |
| 35 | +SQL `NULL` values are converted to JS `null` values. |
| 36 | + |
| 37 | +| SQL Type | JS Return Type | Notes | |
| 38 | +|-----------|----------------|--------| |
| 39 | +| BOOLEAN, TINYINT, SHORTINT, MEDIUMINT, INT | Number | | |
| 40 | +| BIGINT | Number or BigInt | Number for values [-2^53-1, 2^53-1], BigInt otherwise | |
| 41 | +| DECIMAL | String | | |
| 42 | +| FLOAT, DOUBLE | Number | | |
| 43 | +| BIT(k) | Number or BigInt | Number for k ≤ 53, BigInt for k > 53 | |
| 44 | +| TIME, DATE, TIMESTAMP, DATETIME | String | | |
| 45 | +| YEAR | Number | | |
| 46 | +| CHAR, VARCHAR, TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT | String | Fails if length exceeds 2^29 - 24 | |
| 47 | +| BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB | DataView | | |
| 48 | +| ENUM, SET | String | | |
| 49 | +| GEOMETRY and spatial types | DataView | | |
| 50 | +| JSON | Object | | |
| 51 | + |
| 52 | +When the data converts to a JS string, it automatically changes from the SQL parameter’s character set to `utf8mb4`, which JS uses. |
| 53 | + |
| 54 | +## Convert JS data types to SQL |
| 55 | + |
| 56 | +The target SQL data type defines how the system converts values. The system typically converts a JS value to one of the basic types, such as string, integer, or double, depending on the SQL data type. After the conversion, the system stores the result in the SQL parameter or return value. This step can fail if the value is too large or has an incorrect format, which will cause an error. During the process, JS strings automatically convert from `utf8mb4` to the character set of the SQL parameter. |
| 57 | + |
| 58 | +JS `null` and `undefined` values always convert to SQL `NULL` values for the target SQL type. |
| 59 | + |
| 60 | +| JS Value Type | Storage in SQL Parameters | Example | |
| 61 | +|---------------|-------------------------|---------| |
| 62 | +| Integer Numbers | Stored directly as integers | 42 → 42 | |
| 63 | +| BigInt | Stored as integers | 9007n → 9007 | |
| 64 | +| Non-integer Numbers | Converted to strings | 3.14 → "3.14" | |
| 65 | +| All other values | Converted to strings | true → "true" | |
| 66 | +| DECIMAL | Converted to strings | 123.45 → "123.45" | |
| 67 | +| FLOAT, DOUBLE | Stored as doubles for Numbers, converted to strings for other values | 3.14 → 3.14, "3.14" → "3.14" | |
| 68 | +| BIT | Converted to SQL BIT type | 1 → BIT(1) | |
| 69 | +| BigInt | For BigInt values: stored as integers. For other values: converted to Number then stored. Invalid Number conversions cause errors | 9007n → 9007, "123" → 123 | |
| 70 | +| TIME, DATE, TIMESTAMP, DATETIME | Converted to strings | Date() → "2024-01-30" | |
| 71 | +| CHAR, VARCHAR, TINYTEXT, TEXT, MEDIUMTEXT, ENUM | Converted to strings with charset conversion if needed | "hello" → "hello" | |
| 72 | +| BINARY, VARBINARY, TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB | ArrayBuffer/ArrayBufferView: stored directly. Other values: converted to strings | buffer → binary data | |
| 73 | +| SET | Numbers: stored as integers or doubles. BigInt: stored as integers. Others: converted to strings with charset conversion if needed | 1 → 1, "value" → "value" | |
| 74 | +| GEOMETRY | ArrayBuffer/ArrayBufferView: stored as binary if valid GEOMETRY. Other values: error | valid buffer → GEOMETRY | |
| 75 | +| JSON | Converted to JSON string using JSON.stringify() | {key: "value"} → "{"key":"value"}" | |
0 commit comments