承接 mathsgod/json-to-sql 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mathsgod/json-to-sql

最新稳定版本:1.0.1

Composer 安装命令:

composer require mathsgod/json-to-sql

包简介

README 文档

README

A PHP tool that converts database table definitions in JSON format to SQL CREATE TABLE statements.

Features

  • ✅ Batch conversion of multiple tables
  • ✅ Support for complex field types and attributes
  • ✅ Primary Key support
  • ✅ Unique Key support
  • ✅ Index support
  • ✅ Foreign Key support
  • ✅ AUTO_INCREMENT support
  • ✅ Storage engine configuration (InnoDB, etc.)
  • ✅ Charset and Collation support

Requirements

  • PHP >= 8.1
  • Composer

Installation

composer require mathsgod/json-to-sql

Usage

Basic Usage

  1. Create a db.json file in your project root with your table definitions (see JSON Format Specification below).

  2. Create a convert.php script to use the converter:

<?php

require_once 'src/JsonToSql.php';

try {
    $converter = new JsonToSql();
    $sql = $converter->convertJsonToSql('db.json');
    
    echo $sql;
    
    // Optional: Save to file
    // file_put_contents('output.sql', $sql);
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    exit(1);
}

Output

The converted SQL statements will be output to the console. You can also save them to a file by uncommenting the file_put_contents() line or redirecting the output.

JSON Format Specification

The JSON file should contain an array of tables, each with the following structure:

Table Structure

[
  {
    "name": "users",
    "charset": "utf8mb4",
    "collate": "utf8mb4_unicode_ci",
    "engine": "InnoDB",
    "columns": [
      {
        "name": "id",
        "type": "INT",
        "unsigned": true,
        "nullable": false,
        "auto_increment": true
      },
      {
        "name": "username",
        "type": "VARCHAR",
        "length": 255,
        "nullable": false
      },
      {
        "name": "email",
        "type": "VARCHAR",
        "length": 255,
        "nullable": false
      },
      {
        "name": "created_at",
        "type": "TIMESTAMP",
        "default": "CURRENT_TIMESTAMP",
        "nullable": false
      }
    ],
    "primary_key": ["id"],
    "unique_keys": [
      {
        "name": "uk_username",
        "columns": ["username"]
      },
      {
        "name": "uk_email",
        "columns": ["email"]
      }
    ],
    "indexes": [
      {
        "name": "idx_created_at",
        "columns": ["created_at"]
      }
    ],
    "foreign_keys": []
  }
]

Column Attributes

Attribute Type Description Required
name string Column name
type string Column type (VARCHAR, INT, DATETIME, etc.)
length integer Column length (for VARCHAR, CHAR, etc.)
unsigned boolean Whether unsigned (for numeric types)
nullable boolean Allow NULL (default: true)
auto_increment boolean Auto increment
default string|integer Default value

Table Attributes

Attribute Type Description Default
name string Table name -
columns array Array of columns []
primary_key array Primary key columns []
unique_keys array Array of unique keys []
indexes array Array of indexes []
foreign_keys array Array of foreign keys []
engine string Storage engine InnoDB
charset string Character set utf8mb4
collate string Collation null

Foreign Key Configuration

"foreign_keys": [
  {
    "columns": ["user_id"],
    "reference_table": "users",
    "reference_columns": ["id"],
    "on_update": "CASCADE",
    "on_delete": "CASCADE"
  }
]

Examples

Create your db.json file in the project root with table definitions following the JSON Format Specification below.

License

MIT

Author

Raymond Chong (mathsgod@yahoo.com)

统计信息

  • 总下载量: 41
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固