SimplifiedChinese README/简体中文读我
ddl-to-object: a tool help to generate object files in different languages from sql ddl file.
- only tested for MySQL/MariaDB DDL SQL
PR is welcome! You can do some coding stuff to support another language.
- java: generate entity class with auto snake_style to camelStyle naming in properties, bring comments, using lombok plugin for getter/setter, with package directory support
- golang: generate to struct with tags and comments
- php: generate to simple class with namespace and comments support
- python: generate to simple object with comments support
- support any other program language?: pull request is welcome
- A good-designed pattern in MySQL DDL, such as using singular nouns as table and column name, naming in
snake_casestyle, with more comments, no table prefix, having a primary key etc. - The rest, just using this tool to help you generate target language object files
One-line install for Linux/macOS:
curl -fsSL https://raw.githubusercontent.com/ycrao/ddl-to-object/main/install-online.sh | bashThis will automatically:
- Detect your system (Linux/macOS)
- Download the latest release
- Install to
~/.local/bin/ddl-to-object - Set up templates and config in
~/.dto/ - Update your PATH
-
Download the appropriate file from Releases:
- Linux:
ddl-to-object-linux-*.tar.gz - macOS (Intel):
ddl-to-object-mac-*.tar.gz - macOS (ARM64):
ddl-to-object-mac-arm64-*.tar.gz - Windows:
ddl-to-object-windows-*.zip
- Linux:
-
Extract and install:
tar -xzf ddl-to-object-*.tar.gz sudo cp */ddl-to-object /usr/local/bin/ mkdir -p ~/.dto cp -r */template ~/.dto/ cp */config.json ~/.dto/
ddl-to-object version
ddl-to-object --help ddl-to-object go Generate golang target object file
ddl-to-object java Generate java target object file
ddl-to-object php Generate php target object file
ddl-to-object python Generate python target object file
-c, --config string config file path (default: ~/.dto/config.json)
-f, --from path from path which a single-table DDL file located
-n, --ns namespace namespace name for php, only in php command (default: App\Models)
-p, --pk package package name, only in java or go command (default: com.example.sample.domain.entity)
-s, --stdout enable stdout or not, default set false to disable
-t, --to path output to target path or location, create directory automatically if it not existed
-v, --verbose enable verbose output
--dry-run show what would be generated without creating filesddl-to-object php -f ./output/samples/example_3.ddl.txt -n Modules\\Blog\\Models -t ./output/php/
ddl-to-object java -f ./output/samples/example_2.ddl.txt -p com.douyasi.sample.domain.entity -t ./output/java/
ddl-to-object go -f ./output/samples/example_3.ddl.txt -p models -t ./output/go/See output directory.
As installation intro, default template files located in ~/.dto/template directory (note: ~ for current user home workdir; if they're not existed, you need copy them by yourself manually).
The template is a raw text by using golang text/template with ParsedResult type struct passed in. You can modify them as you can.
- not work well in one-line DDL SQL
- not work well with mixed style (such as snake_style, camelStyle, PascalStyle and other cases mixed) in DDL SQL
- special table or field name, such as
365Days_table,1st_fieldandbiz.error.code.fieldetc - some MySQL data type may not mapper well to Java or Golang data type
- some unused imports in Java and Golang, you can clean them by yourself or using some tool like
gofmt - lack of some special testing cases
You can create a configuration file to customize default settings. Configuration file location: ~/.dto/config.json
Example configuration:
{
"default_packages": {
"go": "models",
"java": "com.yourcompany.domain.entity",
"php": "App\\Models",
"python": ""
},
"template_dir": "~/.dto/template",
"log_level": "info",
"output_settings": {
"create_directories": true,
"overwrite_files": true,
"backup_existing": false
}
}- Improved error handling: Better error messages and validation
- Configuration file support: Customize default packages and settings
- Verbose mode: Use
-vflag for detailed output - Dry-run mode: Use
--dry-runto preview what would be generated - Backup functionality: Optional backup of existing files
- Better logging: Leveled log output
- Version information: Detailed build information
Use the provided Makefile:
# Build for single platform
make build
# Build for all platforms
make build-all
# Run tests
make test
# Format code
make fmt