go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

go-jet can't generate table model files and table sql builder files #241

Closed Chinnasit closed 11 months ago

Chinnasit commented 1 year ago

Describe the bug when using the command : jet -source=mysql -dsn="user:pass@tcp(localhost:3306)/dbname" -path=./gen

command output : Connecting to MySQL database... Retrieving database information... FOUND 1 table(s), 0 view(s), 0 enum(s) Destination directory: gen/book_store Cleaning up destination directory... Generating table model files... 17:1: expected '}', found 1 (and 1 more errors)

but when I delete a comment in the SQL command COMMENT 'e\r\n1' so jet generator works

Environment (please complete the following information):

Code snippet SQL Editor in DBeaver : CREATE TABLE books ( id int NOT NULL AUTO_INCREMENT, rate decimal(10,2) DEFAULT '0.00' COMMENT 'e\r\n1', PRIMARY KEY (id) )ENGINE=InnoDB AUTO_INCREMENT=250 DEFAULT CHARSET=utf8mb4;

Expected behavior can generate table model files and table sql builder files

joefrancia commented 1 year ago

I get a very similar thing with the two tables below. Let me know if it's ok to keep these in the same issue, or if I should open a new one

This one fails because someone messed up the second column name. It appears they programmatically generated the table and forgot to add an alias for the column name. Ignoring the table during the gen phase was enough to get around this, as I don't need this table in my app:

CREATE TABLE `Compliance` (
  `PatientID` int(11) DEFAULT NULL,
  `Concat(Patients.LastName, ', ', Patients.FirstName)` varchar(102) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Label` varchar(500) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB;
Connecting to MySQL database...
Retrieving database information...
    FOUND 59 table(s), 3 view(s), 3 enum(s)
Destination directory: .gen/MySchema
Cleaning up destination directory...
Generating table model files...
16:25: expected ')', found ',' (and 1 more errors)

And this one the Operator ENUM fails because of the = sign presumably. I also added this table to the -ignore-tables option, but it still tried to read the ENUM and failed. Adding this ENUM to -ignore-enums was enough to get around it:

CREATE TABLE `Rules` (
  `RuleID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Label` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `RuleSQL` varchar(4000) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Allowance` int(10) unsigned DEFAULT '0',
  `AllowEmptySet` tinyint(1) unsigned DEFAULT '0',
  `Operator` enum('=','!=','<=','>=','<','>','IN') COLLATE utf8_unicode_ci DEFAULT NULL,
`TableName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
) ENGINE=InnoDB;
Connecting to MySQL database...
Retrieving database information...
    FOUND 133 table(s), 1 view(s), 16 enum(s)
Destination directory: .gen/MySchema
Cleaning up destination directory...
Generating table model files...
Generating view model files...
Generating enum model files...
16:58: expected '==', found '=' (and 4 more errors)
go-jet commented 1 year ago

Hi @Chinnasit , @joefrancia , Good catch. It seems additional character escapes are needed.

go-jet commented 11 months ago

@Chinnasit The issue with mysql comment containing an ASCII control character is fixed with Release v2.10.1. @joefrancia At first, I thought to remove non-golang identifier characters from the name, but that would create additional problems down the road. For instance, your enum values contains only non-golang identifier characters. For situations like this, it is best to create generator customization and change the name from the code manually. Take a look at these two tests: TestRenameEnumValueName and TestGeneratorTemplate_Model_SqlBuilder_RenameStructFieldNames.