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

时间:2023-03-09 02:13:08
小程序实现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' => ,
'type' => 'word',
'filename' => 'word1.mp4',
'title'=> 'Word发展历史'
],
[
'MediaID' => ,
'type' => 'word',
'filename' => 'word2.mp4',
'title'=> 'Word基本界面'
],
[
'MediaID' => ,
'type' => 'word',
'filename' => 'word3.mp4',
'title'=> 'Word新建'
],
[
'MediaID' => ,
'type' => 'word',
'filename' => 'word4.mp4',
]);
 有限状态自动机,随便写了个小程序,应付自己临时的需求。 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#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[][];//属性
int pcnt;
bool ck(char wd[]){//不为NULL,""
for(int i=;wd[i];i++){
if(wd[i]=='\''&&i&&wd[i-]=='\'')
return ;
if(wd[i]=='N')
return ;
}
return ;
}
void solve(){
char c,wd[]="";
int f=INSERT,i,j;
while(){
c=getchar();
if(c==EOF)break;
if(f==INSERT){
if(c=='`'){
f=TABLE;
i=;
}
if(c=='(')
f=G_PRO;
}else if(f==TABLE){
if(c=='`'){
wd[i]='\0';
i=;
f=INSERT;
printf("DB::table('%s')->insert([\n",wd);
}
else
wd[i++]=c;
}else if(f==G_PRO){//属性列表
if(c=='`'){
f=PRO;
i=;
}
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=;//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=;//读过下一个值
}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=;
f=G_IN;
}else
wd[i++]=c;
}else if(f==IN2){
if(c=='\'')
f=IN;
wd[i++]=c;
}
}
printf("]);\n");
}
int main() {
solve();
return ;
}