• 小程序实现sql插入语句转换成Laravel迁移语句


    sql的插入语句长这样:

    INSERT INTO `media` (`MediaID`, `type`, `filename`, `title`) VALUES
    (1, 'word', 'word1.mp4', 'Word发展历史'),
    (2, 'word', 'word2.mp4', 'Word基本界面'),
    (3, 'word', 'word3.mp4', 'Word新建'),
    (4, 'word', 'word4.mp4', 'Word保存');

    我需要的Laravel的迁移插入语句长这样:

    DB::table('media')->insert([
    [
        'MediaID' => 1,
        'type' =>  'word',
        'filename' =>  'word1.mp4',
        'title'=>  'Word发展历史'
    ],
    [
        'MediaID' => 2,
        'type' =>  'word',
        'filename' =>  'word2.mp4',
        'title'=>  'Word基本界面'
    ],
    [
        'MediaID' => 3,
        'type' =>  'word',
        'filename' =>  'word3.mp4',
        'title'=>  'Word新建'
    ],
    [
        'MediaID' => 4,
        'type' =>  'word',
        'filename' =>  'word4.mp4',
    ]);
     有限状态自动机,随便写了个小程序,应付自己临时的需求。
    
    #include 
    #include 
    #include 
    #include 
    #define INSERT 0
    #define TABLE 1
    #define G_PRO 2
    #define PRO 3
    #define G_IN 4
    #define IN 5
    #define IN2 6
    using namespace std;
    char pro[100][300];//属性
    int pcnt;
    bool ck(char wd[1000]){//不为NULL,""
        for(int i=0;wd[i];i++){
            if(wd[i]=='\''&&i&&wd[i-1]=='\'')
                return 0;
            if(wd[i]=='N')
                return 0;
        }    
        return 1;
    }
    void solve(){
        char c,wd[1000]="";
        int f=INSERT,i,j;
        while(1){
            c=getchar();
            if(c==EOF)break;
            if(f==INSERT){
                if(c=='`'){
                    f=TABLE;
                    i=0;
                }
                if(c=='(')
                    f=G_PRO;
            }else if(f==TABLE){
                if(c=='`'){
                    wd[i]='\0';
                    i=0;
                    f=INSERT;
                    printf("DB::table('%s')->insert([\n",wd);
                }
                else
                    wd[i++]=c;
            }else if(f==G_PRO){//属性列表
                if(c=='`'){
                    f=PRO;
                    i=0;
                }
                else if(c==')')
                    f=G_IN;
            }else if(f==PRO){//属性
                if(c=='`'){
                    wd[i]='\0';//输出前截断后面的。
                    sprintf(pro[pcnt++],"%s",wd);
                    f=G_PRO;
                }else wd[i++]=c;
            }else if(f==G_IN){//等待一个插入
                if(c=='('){
                    j=i=0;//j是属性下标
                    f=IN;
                }
                else if(c=='I')
                    f=INSERT;
            }else if(f==IN){
                if(c==','){
                    wd[i]='\0';
                    if(!j)
                        printf("[\n");
                    if(ck(wd))
                        printf("\t'%s' => %s,\n",pro[j],wd);
                    j++;
                    i=0;//读过下一个值
                }else if(c=='\''){
                    wd[i++]=c;
                    f=IN2;//读入字符串
                }else if(c==')'){
                    wd[i]='\0';
                    if(ck(wd))
                        printf("\t'%s'=> %s\n",pro[j],wd);
                    puts("],");
                    i=j=0;
                    f=G_IN;
                }else
                    wd[i++]=c;
            }else if(f==IN2){
                if(c=='\'')
                    f=IN;
                wd[i++]=c;
            }
        }
        printf("]);\n");
    }
    int main() {
        solve();
        return 0;
    }

  • 相关阅读:
    Java如何将两个数组合并为一个数组呢?
    MySQL备份与恢复
    Java实现桥接模式(设计模式 五)
    一种基于宏和serde_json实现的rust web中统一返回类
    Postgresql数据类型-布尔类型
    学长教你学C-day9-C语言循环结构与选择结构
    【Rust日报】2022-08-13 Rust Sitter 轻松编写快速且健壮的解析器
    安卓项目结构分析
    追求极致性能!Qwik 1.0版本发布
    智能排班系统 【管理系统功能、操作说明——中篇】
  • 原文地址:https://blog.csdn.net/m0_72495985/article/details/126742582