Skip to content

Commit

Permalink
feat : Dialects MySQL string fuction (#162)
Browse files Browse the repository at this point in the history
* added : git ignore

* Added String fuction

* Review Correction and Debug by inspecting UI one by one
  • Loading branch information
vicheanath authored Oct 9, 2023
1 parent 0e3e6dc commit cf26feb
Show file tree
Hide file tree
Showing 45 changed files with 723 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ configs.json
query-master-data/

.env
yarn.lock
2 changes: 1 addition & 1 deletion src/dialects/mysql/dialect.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"spaceAfterDashes": true,
"specialVar": "@?",
"identifierQuotes": "`",
"keywords": "absolute action add after all allocate alter and any are as asc assertion at authorization before begin between both breadth by call cascade cascaded case cast catalog check close collate collation column commit condition connect connection constraint constraints constructor continue corresponding count create cross cube current current_date current_default_transform_group current_transform_group_for_type current_path current_role current_time current_timestamp current_user cursor cycle data day deallocate declare default deferrable deferred delete depth deref desc describe descriptor deterministic diagnostics disconnect distinct do domain drop dynamic each else elseif end end-exec equals escape except exception exec execute exists exit external fetch first for foreign found from free full function general get global go goto grant group grouping handle having hold hour identity if immediate in indicator initially inner inout input insert intersect into is isolation join key language last lateral leading leave left level like limit local localtime localtimestamp locator loop map match method minute modifies module month names natural nesting new next no none not of old on only open option or order ordinality out outer output overlaps pad parameter partial path prepare preserve primary prior privileges procedure public read reads recursive redo ref references referencing relative release repeat resignal restrict result return returns revoke right role rollback rollup routine row rows savepoint schema scroll search second section select session session_user set sets signal similar size some space specific specifictype sql sqlexception sqlstate sqlwarning start state static system_user table temporary then timezone_hour timezone_minute to trailing transaction translation treat trigger under undo union unique unnest until update usage user using value values view when whenever where while with without work write year zone accessible algorithm analyze asensitive authors auto_increment autocommit avg avg_row_length binlog btree cache catalog_name chain change changed checkpoint checksum class_origin client_statistics coalesce code collations columns comment committed completion concurrent consistent contains contributors convert database databases day_hour day_microsecond day_minute day_second delay_key_write delayed delimiter des_key_file dev_pop dev_samp deviance directory disable discard distinctrow div dual dumpfile enable enclosed ends engine engines enum errors escaped even event events every explain extended fast field fields flush force found_rows fulltext grants group_concat handler hash high_priority hosts hour_microsecond hour_minute hour_second ignore ignore_server_ids import index index_statistics infile innodb insensitive insert_method install invoker iterate keys kill linear lines list load lock logs low_priority master master_heartbeat_period master_ssl_verify_server_cert masters max max_rows maxvalue message_text middleint migrate min min_rows minute_microsecond minute_second mod mode modify mutex mysql_errno no_write_to_binlog offline offset one online optimize optionally outfile pack_keys parser partition partitions password phase plugin plugins prev processlist profile profiles purge query quick range read_write rebuild recover regexp relaylog remove rename reorganize repair repeatable replace require resume rlike row_format rtree schedule schema_name schemas second_microsecond security sensitive separator serializable server share show slave slow snapshot soname spatial sql_big_result sql_buffer_result sql_cache sql_calc_found_rows sql_no_cache sql_small_result ssl starting starts std stddev stddev_pop stddev_samp storage straight_join subclass_origin sum suspend table_name table_statistics tables tablespace terminated triggers truncate uncommitted uninstall unlock upgrade use use_frm user_resources user_statistics utc_date utc_time utc_timestamp variables views warnings xa xor year_month zerofill abs acos adddate addtime asin atan atan2 ceil ceiling conv convert_tz cos cot crc32 curdate curtime date datediff date_add date_format date_sub dayofmonth dayofweek dayofyear degrees exp extract floor format from_days from_unixtime get_format hex last_day ln log log10 log2 makedate maketime microsecond monthname now period_add period_diff pi pow power quarter rand randians round sec_to_time sign sin sqrt str_to_date subdate subtime sysdate tan time timediff timestamp timestampadd timestampdiff time_format time_to_sec to_days to_seconds unix_timestamp week weekday weekofyear yearweek",
"keywords": "absolute action add after all allocate alter and any are as asc assertion at authorization before begin between both breadth by call cascade cascaded case cast catalog check close collate collation column commit condition connect connection constraint constraints constructor continue corresponding count create cross cube current current_date current_default_transform_group current_transform_group_for_type current_path current_role current_time current_timestamp current_user cursor cycle data day deallocate declare default deferrable deferred delete depth deref desc describe descriptor deterministic diagnostics disconnect distinct do domain drop dynamic each else elseif end end-exec equals escape except exception exec execute exists exit external fetch first for foreign found from free full function general get global go goto grant group grouping handle having hold hour identity if immediate in indicator initially inner inout input insert intersect into is isolation join key language last lateral leading leave left level like limit local localtime localtimestamp locator loop map match method minute modifies module month names natural nesting new next no none not of old on only open option or order ordinality out outer output overlaps pad parameter partial path prepare preserve primary prior privileges procedure public read reads recursive redo ref references referencing relative release repeat resignal restrict result return returns revoke right role rollback rollup routine row rows savepoint schema scroll search second section select session session_user set sets signal similar size some space specific specifictype sql sqlexception sqlstate sqlwarning start state static system_user table temporary then timezone_hour timezone_minute to trailing transaction translation treat trigger under undo union unique unnest until update usage user using value values view when whenever where while with without work write year zone accessible algorithm analyze asensitive authors auto_increment autocommit avg avg_row_length binlog btree cache catalog_name chain change changed checkpoint checksum class_origin client_statistics coalesce code collations columns comment committed completion concurrent consistent contains contributors convert database databases day_hour day_microsecond day_minute day_second delay_key_write delayed delimiter des_key_file dev_pop dev_samp deviance directory disable discard distinctrow div dual dumpfile enable enclosed ends engine engines enum errors escaped even event events every explain extended fast field fields flush force found_rows fulltext grants group_concat handler hash high_priority hosts hour_microsecond hour_minute hour_second ignore ignore_server_ids import index index_statistics infile innodb insensitive insert_method install invoker iterate keys kill linear lines list load lock logs low_priority master master_heartbeat_period master_ssl_verify_server_cert masters max max_rows maxvalue message_text middleint migrate min min_rows minute_microsecond minute_second mod mode modify mutex mysql_errno no_write_to_binlog offline offset one online optimize optionally outfile pack_keys parser partition partitions password phase plugin plugins prev processlist profile profiles purge query quick range read_write rebuild recover regexp relaylog remove rename reorganize repair repeatable replace require resume rlike row_format rtree schedule schema_name schemas second_microsecond security sensitive separator serializable server share show slave slow snapshot soname spatial sql_big_result sql_buffer_result sql_cache sql_calc_found_rows sql_no_cache sql_small_result ssl starting starts std stddev stddev_pop stddev_samp storage straight_join subclass_origin sum suspend table_name table_statistics tables tablespace terminated triggers truncate uncommitted uninstall unlock upgrade use use_frm user_resources user_statistics utc_date utc_time utc_timestamp variables views warnings xa xor year_month zerofill abs acos adddate addtime ascii asin atan atan2 bin bit_length ceil ceiling char char_length character_length concat concat_ws conv convert_tz cos cot crc32 curdate curtime date date_add date_format date_sub datediff dayofmonth dayofweek dayofyear degrees elt exp export_set extract find_in_set floor format from_base64 from_days from_unixtime get_format hex instr last_day lcase length ln load_file locate log log10 log2 lower lpad ltrim make_set makedate maketime microsecond mid monthname now oct octet_length ord period_add period_diff pi position pow power quarter quote rand randians reverse round rpad rtrim sec_to_time sign sin soundex sqrt str_to_date subdate substr substring substring_index subtime sysdate tan time time_format time_to_sec timediff timestamp timestampadd timestampdiff to_base64 to_days to_seconds unix_timestamp week weekday weekofyear yearweek",
"types": "array binary bit boolean char character clob date decimal double float int integer interval large national nchar nclob numeric object precision real smallint time timestamp varchar varying bool blob long longblob longtext medium mediumblob mediumint mediumtext tinyblob tinyint tinytext text bigint int1 int2 int3 int4 int8 float4 float8 varbinary varcharacter datetime unsigned signed"
}
12 changes: 12 additions & 0 deletions src/dialects/mysql/functions/ascii.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
ASCII(str)

Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL ASCII() works for 8-bit characters.

```
SELECT ASCII('2');
-> 50
SELECT ASCII(2);
-> 50
SELECT ASCII('dx');
-> 100
```
8 changes: 8 additions & 0 deletions src/dialects/mysql/functions/bin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BIN(N)

Returns a string representation of the binary value of `N`, where `N` is a longlong (BIGINT) number. This is equivalent to CONV(N,10,2) Returns `NULL` if `N` is `NULL`.

```
SELECT BIN(12);
-> '1100'
```
8 changes: 8 additions & 0 deletions src/dialects/mysql/functions/bit_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BIT_LENGTH(str)

Returns the length of the string str in bits. Returns NULL if str is NULL.

```
SELECT BIT_LENGTH('text');
-> 32
```
75 changes: 75 additions & 0 deletions src/dialects/mysql/functions/char.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
CHAR(N,...[USING charset_name])

<!-- Though build:dialect it not working -->

CHAR() interprets each argument N as an integer and returns a string consisting of the characters given by the code values of those integers. NULL values are skipped.

```
mysql> SELECT CHAR(77,121,83,81,'76');
+--------------------------------------------------+
| CHAR(77,121,83,81,'76') |
+--------------------------------------------------+
| 0x4D7953514C |
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CHAR(77,77.3,'77.3');
+--------------------------------------------+
| CHAR(77,77.3,'77.3') |
+--------------------------------------------+
| 0x4D4D4D |
+--------------------------------------------+
1 row in set (0.00 sec)
```

By default, CHAR() returns a binary string. To produce a string in a given character set, use the optional USING clause:

```
mysql> SELECT CHAR(77,121,83,81,'76' USING utf8mb4);
+---------------------------------------+
| CHAR(77,121,83,81,'76' USING utf8mb4) |
+---------------------------------------+
| MySQL |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT CHAR(77,77.3,'77.3' USING utf8mb4);
+------------------------------------+
| CHAR(77,77.3,'77.3' USING utf8mb4) |
+------------------------------------+
| MMM |
+------------------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SHOW WARNINGS;
+---------+------+-------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect INTEGER value: '77.3' |
+---------+------+-------------------------------------------+
1 row in set (0.00 sec)
```

If USING is given and the result string is illegal for the given character set, a warning is issued. Also, if strict SQL mode is enabled, the result from CHAR() becomes NULL.

If CHAR() is invoked from within the mysql client, binary strings display using hexadecimal notation, depending on the value of the --binary-as-hex. For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.

CHAR() arguments larger than 255 are converted into multiple result bytes. For example, CHAR(256) is equivalent to CHAR(1,0), and CHAR(256*256) is equivalent to CHAR(1,0,0):

```
mysql> SELECT HEX(CHAR(1,0)), HEX(CHAR(256));
+----------------+----------------+
| HEX(CHAR(1,0)) | HEX(CHAR(256)) |
+----------------+----------------+
| 0100 | 0100 |
+----------------+----------------+
1 row in set (0.00 sec)
mysql> SELECT HEX(CHAR(1,0,0)), HEX(CHAR(256*256));
+------------------+--------------------+
| HEX(CHAR(1,0,0)) | HEX(CHAR(256*256)) |
+------------------+--------------------+
| 010000 | 010000 |
+------------------+--------------------+
1 row in set (0.00 sec)
```
22 changes: 22 additions & 0 deletions src/dialects/mysql/functions/char_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CHAR_LENGTH(str)

Returns the length of the string str, measured in code points. A
multibyte character counts as a single code point. This means
that, for a string containing two 3-byte characters,
LENGTH() returns 6, whereas CHAR_LENGTH() returns 2, as shown here:

```
mysql> SET @dolphin:='海豚';
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT LENGTH(@dolphin), CHAR_LENGTH(@dolphin);
+------------------+-----------------------+
| LENGTH(@dolphin) | CHAR_LENGTH(@dolphin) |
+------------------+-----------------------+
| 6 | 2 |
+------------------+-----------------------+
1 row in set (0.00 sec)
```

CHAR_LENGTH() returns NULL if str is NULL.

3 changes: 3 additions & 0 deletions src/dialects/mysql/functions/character_length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CHARACTER_LENGTH(str)

CHARACTER_LENGTH() is a synonym for CHAR_LENGTH().
24 changes: 24 additions & 0 deletions src/dialects/mysql/functions/concat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CONCAT(str1,str2,...)

Returns the string that results from concatenating the arguments. May have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string. A numeric argument is converted to its equivalent nonbinary string form.

CONCAT() returns NULL if any argument is NULL.

```
mysql> SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL'
mysql> SELECT CONCAT('My', NULL, 'QL');
-> NULL
mysql> SELECT CONCAT(14.3);
-> '14.3', mysql> SELECT 'My' 'S' 'QL';
-> 'MySQL'
```

For quoted strings, concatenation can be performed by placing the strings next to each other:

```
mysql> SELECT 'My' 'S' 'QL';
-> 'MySQL'
```

If CONCAT() is invoked from within the mysql client, binary string results display using hexadecimal notation, depending on the value of the --binary-as-hex. For more information about that option, see Section 4.5.1, [mysql — The MySQL Command-Line Client](https://dev.mysql.com/doc/refman/8.0/en/mysql.html).
13 changes: 13 additions & 0 deletions src/dialects/mysql/functions/concat_ws.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CONCAT_WS(separator,str1,str2,...)

CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.

```
mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name'
mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'
```

CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.

10 changes: 10 additions & 0 deletions src/dialects/mysql/functions/elt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ELT(N,str1,str2,str3,...)

ELT() returns the Nth element of the list of strings: str1 if N = 1, str2 if N = 2, and so on. Returns NULL if N is less than 1, greater than the number of arguments, or NULL. ELT() is the complement of FIELD().

```
mysql> SELECT ELT(1, 'Aa', 'Bb', 'Cc', 'Dd');
-> 'Aa'
mysql> SELECT ELT(4, 'Aa', 'Bb', 'Cc', 'Dd');
-> 'Dd'
```
10 changes: 10 additions & 0 deletions src/dialects/mysql/functions/export_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
EXPORT_SET(bits,on,off[,separator[,number_of_bits]])

Returns a string such that for every bit set in the value bits, you get an on string and for every bit not set in the value, you get an off string. Bits in bits are examined from right to left (from low-order to high-order bits). Strings are added to the result from left to right, separated by the separator string (the default being the comma character ,). The number of bits examined is given by number_of_bits, which has a default of 64 if not specified. number_of_bits is silently clipped to 64 if larger than 64. It is treated as an unsigned integer, so a value of −1 is effectively the same as 64.

```
mysql> SELECT EXPORT_SET(5,'Y','N',',',4);
-> 'Y,N,Y,N'
mysql> SELECT EXPORT_SET(6,'1','0',',',10);
-> '0,1,1,0,0,0,0,0,0,0'
```
14 changes: 14 additions & 0 deletions src/dialects/mysql/functions/field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FIELD(str,str1,str2,str3,...)

Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found. ,If all arguments to FIELD() are strings, all arguments are compared as strings. If all arguments are numbers, they are compared as numbers. Otherwise, the arguments are compared as double.

```
mysql> SELECT FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 2
mysql> SELECT FIELD('Gg', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 0
```

If str is NULL, the return value is 0 because NULL fails equality comparison with any value. FIELD() is the complement of ELT().


8 changes: 8 additions & 0 deletions src/dialects/mysql/functions/find_in_set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list `strlist` consisting of N substrings. A string list is a string composed of substrings separated by characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in `strlist` or if `strlist` is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (,) character.

```
mysql> SELECT FIND_IN_SET('b','a,b,c,d');
-> 2
```
10 changes: 10 additions & 0 deletions src/dialects/mysql/functions/from_base64.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM_BASE64(str)

Takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a binary string. The result is NULL if the argument is NULL or not a valid base-64 string. See the description of TO_BASE64() for details about the encoding and decoding rules.

```
mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc'));
-> 'JWJj', 'abc'
```

If FROM_BASE64() is invoked from within the mysql client, binary strings display using hexadecimal notation. You can disable this behavior by setting the value of the --binary-as-hex to 0 when starting the mysql client. For more information about that option, see Section 4.5.1, “mysql — The MySQL Command-Line Client”.
Loading

0 comments on commit cf26feb

Please sign in to comment.