Skip to content

Commit

Permalink
add: 编译选定文件也可以插入到sqlite中
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoqingqing committed Jun 11, 2017
1 parent 6cd2cf8 commit eb11773
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 20 deletions.
5 changes: 4 additions & 1 deletion TableML/TableMLCompilerConsole/LocalDebug.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
//#define LOCAL_TEST
#if LOCAL_TEST
using System;
using System.Collections.Generic;
using System.IO;
using CommandLine;
Expand Down Expand Up @@ -144,3 +146,4 @@ public static void CopyToHome()

}
}
#endif
2 changes: 1 addition & 1 deletion TableML/TableMLCompilerConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string GetUsage()
}

/// <summary>
/// 此类生成的Exe可以接收命令行参数
/// 接收命令行参数,进行编译excel
/// 修改生成的Exe名(程序集),不为TableML,因为同解决方案下有同名的类库,导致调试时无法获取局部变量
/// </summary>
class TableCompilerConsole
Expand Down
10 changes: 7 additions & 3 deletions TableML/TableMLGUI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
如果要把编译后的代码和表拷贝到客户端,可以使用拷贝功能或修改路径配置
-->
<!--编译后的表格式-->
<add key="TmlExtensions" value=".k" />
<add key="TmlExtensions" value=".tsv" />

<!--是否使用绝对路径;true:所有路径都是绝对的,完整路径;false:所有路径是相对于此exe的-->
<add key="UseAbsolutePath" value="false" />

<!--excel源文件路径-->
<add key="srcExcelPath" value=".\..\Src\" />

<!--excel编译后的database保存路径-->
<add key="DBPath" value=".\..\client_setting\data.db" />

<!--excel编译后的tml保存路径-->
<add key="GenTmlPath" value=".\..\client_setting\" />

Expand All @@ -28,9 +31,10 @@
<add key="dstClientTmlPath" value=".\..\..\..\client\trunk\\Product\Config\" />

</appSettings>
<system.data>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data></configuration>
</system.data>
</configuration>
34 changes: 31 additions & 3 deletions TableML/TableMLGUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ private void tbFileDir_DragDrop(object sender, DragEventArgs e)
this.tbFileDir.Text = dragDir;
}

/// <summary>
/// 框中的文件列表
/// </summary>
public string[] fileList
{
get
Expand All @@ -120,6 +123,25 @@ public string[] fileList
}

private void btnCompileSelect_Click(object sender, EventArgs e)
{
List<string> tmlList = new List<string>();
CompileSelect(ref tmlList);
//传递编译后的文件列表
if (tmlList != null)
{
SQLiteHelper.UpdateDB(tmlList.ToArray());
}
else
{
ConsoleHelper.Error("编译选中的Excel失败,获取编译后文件列表为空!");
}
}

/// <summary>
/// 编译选中的excel
/// </summary>
/// <param name="msgResult"></param>
private void CompileSelect(ref List<string> tmlList, bool msgResult = false)
{
//编译选定的表
Console.Clear();
Expand All @@ -144,6 +166,7 @@ private void btnCompileSelect_Click(object sender, EventArgs e)
}
//编译表时,生成代码
TableCompileResult compileResult = compiler.Compile(filePath, savePath);
tmlList.Add(Path.GetFullPath(savePath));
Console.WriteLine("编译结果:{0}---->{1}", filePath, savePath);
Console.WriteLine();
//生成代码
Expand All @@ -159,9 +182,14 @@ private void btnCompileSelect_Click(object sender, EventArgs e)
}
}

ShowCompileResult(comileCount);
if (msgResult) { ShowCompileResult(comileCount); }
}
private void CompileAllExcel()

/// <summary>
/// 编译所有的excel
/// </summary>
/// <param name="msgResult">是否弹出编译结果</param>
private void CompileAllExcel(bool msgResult = false)
{
//编译整个目录
var startPath = Environment.CurrentDirectory;
Expand All @@ -175,7 +203,7 @@ private void CompileAllExcel()

var results = batchCompiler.CompileTableMLAllInSingleFile(srcDirectory, GenTmlPath, GenCodePath,
templateString, "AppSettings", ".k", null, !string.IsNullOrEmpty(GenCodePath));
ShowCompileResult(results.Count);
if (msgResult) { ShowCompileResult(results.Count); }
}

private void btnCompileAll_Click(object sender, EventArgs e)
Expand Down
58 changes: 46 additions & 12 deletions TableML/TableMLGUI/SQLiteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,22 @@ public static bool CheckIsSqlKeyword(string key)
return false;
}

public static void UpdateDB(string srcPath)
/// <summary>
/// 更新数据库
/// </summary>
/// <param name="fileList">文件列表[完整路径]</param>
public static void UpdateDB(string[] fileList)
{
if (Directory.Exists(srcPath) == false)
if (fileList == null)
{
ConsoleHelper.Error("{0} 目录不存在!", srcPath);
ConsoleHelper.Error("文件列表不能为空!");
return;
}
if (fileList.Length < 1)
{
ConsoleHelper.Warning("文件列表为空!");
return;
}


if (File.Exists(dbfile) == false)
{
Expand Down Expand Up @@ -85,19 +93,20 @@ public static void UpdateDB(string srcPath)
DbTransaction trans = conn.BeginTransaction();
try
{
var fileList = FileHelper.GetAllFiles(srcPath);
List<string> failList = new List<string>();
List<string> successList = new List<string>();
foreach (FileInfo fileInfo in fileList)
foreach (var filePath in fileList)
{
bool success = UpdateTable(fileInfo.FullName, cmd);
bool success = UpdateTable(filePath, cmd);
var fileName = Path.GetFileName(filePath);
if (!success)
{
failList.Add(fileInfo.Name);

failList.Add(fileName);
}
else
{
successList.Add(fileInfo.Name);
successList.Add(fileName);
}
}

Expand All @@ -108,7 +117,10 @@ public static void UpdateDB(string srcPath)
{
ConsoleHelper.Confirmation("{0}", fileName);
}
ConsoleHelper.Error("更新失败{0}张表", failList.Count);
if (failList.Count > 0)
{ //不打印 没有失败
ConsoleHelper.Error("更新失败{0}张表", failList.Count);
}
foreach (var fileName in failList)
{
ConsoleHelper.Error("{0}", fileName);
Expand All @@ -133,6 +145,28 @@ public static void UpdateDB(string srcPath)
}
}

/// <summary>
/// 更新数据库
/// </summary>
/// <param name="srcPath">文件目录</param>
public static void UpdateDB(string srcPath)
{
if (Directory.Exists(srcPath) == false)
{
ConsoleHelper.Error("{0} 目录不存在!", srcPath);
return;
}

var fileList = FileHelper.GetAllFiles(srcPath);
List<string> pathList = new List<string>();

foreach (FileInfo fileInfo in fileList)
{
pathList.Add(fileInfo.FullName);
}
UpdateDB(pathList.ToArray());
}



/// <summary>
Expand Down Expand Up @@ -181,7 +215,7 @@ public static bool UpdateTable(string filePath, DbCommand dbCmd)
sb.AppendFormat("CREATE TABLE [{0}] (", fileName);
for (int i = 0; i < columnNames.Length; i++)
{
//DROP是SQL关键字,TODO:需要加上所有关键字限定
//DROP是SQL关键字,TODO:需要加上所有关键字限定
if (CheckIsSqlKeyword(columnNames[i]))
{
//NOTE 字段名可以包含sql关键字
Expand All @@ -201,7 +235,7 @@ public static bool UpdateTable(string filePath, DbCommand dbCmd)
}
else
{
ConsoleHelper.Error("index={0} ,columnTypes索引超出", i);
ConsoleHelper.Error("index={0} ,超出索引columnTypes.Length={1}", i, columnTypes.Length);
}
}
sb.Remove(sb.Length - 1, 1);
Expand Down

0 comments on commit eb11773

Please sign in to comment.