Skip to content

Commit

Permalink
FINAL
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-gomes committed Jun 29, 2015
1 parent 7a9b6c1 commit 47c70f5
Show file tree
Hide file tree
Showing 62 changed files with 5,762 additions and 927 deletions.
9 changes: 7 additions & 2 deletions OpenDataDBBuilder.Business/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
33 changes: 23 additions & 10 deletions OpenDataDBBuilder.Business/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,50 @@ namespace OpenDataDBBuilder.Business.DB.VO
{
public class Column
{
public Column() { }
public String ColumnName { get; set; }
public String OriginalColumnName { get; set; }
public Boolean IsPK { get; set; }
public Boolean IsFK { get; set; }
public Boolean IsIDGenerated { get; set; }
public KeyValue Reference { get; set; }
public String sqlType { get; set; }
public Boolean IsNullable { get; set; }
public Boolean IsUnique { get; set; }
public Boolean SKIP { get; set; }
public String defaulValue { get; set; }
public String defaulValueOnError { get; set; }

public Column()
{
this.IsPK = false;
this.IsFK = false;
this.SKIP = false;
}
public Column(String ColumnName)
{
this.ColumnName = ColumnName;
this.OriginalColumnName = ColumnName;
this.IsPK = false;
this.IsFK = false;
this.SKIP = false;
this.IsIDGenerated = false;
this.sqlType = "VARCHAR(40)";
}

public Column(String ColumnName, Boolean IsPK, Boolean IsFK, Boolean IsIDGenerated, KeyValue Reference)
{
this.ColumnName = ColumnName;
this.OriginalColumnName = ColumnName;
this.IsPK = IsPK;
this.IsFK = IsFK;
this.IsIDGenerated = IsIDGenerated;

this.defaulValue = "def";

if (this.IsFK)
if (Reference == null || "".Equals(Reference))
throw new Exception("NO REFERENCE ON FK");
else
this.Reference = Reference;
}


public String ColumnName { get; set; }
public Boolean IsPK { get; set; }
public Boolean IsFK { get; set; }
public Boolean IsIDGenerated { get; set; }
public KeyValue Reference { get; set; }
public String sqlType { get; set; }
}
}
39 changes: 39 additions & 0 deletions OpenDataDBBuilder.Business/DBConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OpenDataDBBuilder.Business.VO
{
public class DBConfig
{
public String Db { get; set; }
public String Server { get; set; }
public String Port { get; set; }
public String User { get; set; }
public String Password { get; set; }
public String DbName { get; set; }

public String getConnectionString()
{
StringBuilder conn = new StringBuilder("Server={0};Port={1};Database={2};Uid={3};Pwd={4};");
conn.Replace("{0}", Server);
conn.Replace("{1}", Port);
conn.Replace("{2}", DbName);
conn.Replace("{3}", User);
conn.Replace("{4}", Password);
return conn.ToString();
}

public String getConnectionStringNoDB()
{
StringBuilder conn = new StringBuilder("Server={0};Port={1};Uid={3};Pwd={4};");
conn.Replace("{0}", Server);
conn.Replace("{1}", Port);
conn.Replace("{3}", User);
conn.Replace("{4}", Password);
return conn.ToString();
}
}
}
164 changes: 144 additions & 20 deletions OpenDataDBBuilder.Business/DatabaseHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,70 @@
using OpenDataDBBuilder.DataRepository.Mysql;
using System.Windows.Forms;
using OpenDataDBBuilder.Business.File.Util;

using MySql.Data.MySqlClient;
using System.Configuration;
using System.Threading;
using System.Collections.Concurrent;
using OpenDataDBBuilder.Business.VO;

namespace OpenDataDBBuilder.Business.DB
{
public class DatabaseHelper
{
String mysql = "MySQL";
String db = "";
Int16 nConnections;
List<Thread> insertThreads = new List<Thread>();
DBConfig dbconfig;
String mysql = "MYSQL";
DAO dao;
String ret;
public bool skip { get; set; }
public String msgInserting { get; set; }
public String LogInsert { get; set; }
public ConcurrentQueue<String> RowsInsert { get; set; }
int initialSize = 0;
public Exception threadException { get; set; }

public List<String> databases = new List<String>();


public DatabaseHelper(String db)
public DatabaseHelper(DBConfig dbconfig)
{
this.db = db;
this.dbconfig = dbconfig;
RowsInsert = new ConcurrentQueue<String>();
this.msgInserting = "";
this.dao = getDataBaseDAO();
skip = true;
databases.Add(mysql);
}

public DAO getDataBaseDAO()
{
if (db.Equals(mysql))
if (dbconfig.Db.ToUpper().Equals(mysql))
{
MysqlDao dao = MysqlDao.getInstancy(readDBConfigFile());
MysqlDao dao = MysqlDao.getInstancy(dbconfig.getConnectionStringNoDB());
return dao;
}
return null;
}

public Boolean isConnectionAvailable(String database, String conn)
public DAO getDataBaseDAODB()
{
return dao.isConnectionAvailable(conn);
if (dbconfig.Db.ToUpper().Equals(mysql))
{
MysqlDao dao = MysqlDao.getInstancy(dbconfig.getConnectionString());
return dao;
}
return null;
}

private String readDBConfigFile()
public Boolean isConnectionAvailable(DBConfig dbconfig)
{
String appPath = Application.StartupPath;
String dbConfigPath = appPath + "/DBConfig" + db + ".config";
List<String> file = FileUtil.openFile(dbConfigPath);
String config = "";
return dao.isConnectionAvailable(dbconfig.getConnectionString());
}

foreach (String s in file)
{
config += s + ";";
}
return config;
public Boolean isConnectionAvailableNoDB(DBConfig dbconfig)
{
return dao.isConnectionAvailable(dbconfig.getConnectionStringNoDB());
}

public String addDatabase(String dataBase)
Expand All @@ -75,7 +91,7 @@ public List<String> getTablesFromDataBase(String dataBase)

public DataTable getTableDescription(String dataBase, String table)
{
return dao.getTableDescription(SQLGenerator.getTableDescription(dataBase, table));
return dao.getDataTable(SQLGenerator.getTableDescription(dataBase, table));
}

public String runSQL(String sql)
Expand All @@ -86,5 +102,113 @@ public List<String> getFieldListFromDBTable(String dataBase, String table)
{
return dao.getFieldListFromDBTable(SQLGenerator.getTableDescription(dataBase, table));
}

public DataTable getTableRows(String dataBase, String tableName, Int16 limit)
{
return dao.getDataTable(SQLGenerator.getTableRows(dataBase, tableName, limit));
}

public void insertRowsEnqueue(VO.TableList tablesList)
{
this.dao = getDataBaseDAODB();
if (tablesList != null && tablesList.Tables != null && tablesList.Tables.Count > 0)
{
foreach(VO.Table t in tablesList.Tables)
{
String insert = SQLGenerator.getInsertStart(t);

foreach(VO.Row r in t.Rows)
{
try
{
RowsInsert.Enqueue(insert + SQLGenerator.insertRow(t.getListColumnsExcluded(), r));
}
catch(Exception e)
{
Console.Write(e.Message);
}
}
}
initialSize = RowsInsert.Count;
}
}

public String insertRowsDequeue(Int16 nConnections)
{
msgInserting = "";
this.nConnections = nConnections;
if (RowsInsert.Count > 0)
{
initiateInsertThreads();
}
return null;
}

public void initiateInsertThreads()
{
int count = 0;
while (count <= nConnections)
{
Thread thread = new Thread(insertRowThread);
insertThreads.Add(thread);
count++;
}

foreach(Thread t in insertThreads)
{
t.Start();
}
}

private void insertRowThread()
{
object conn = null;
try
{
conn = dao.getOpenConnection();
}
catch (Exception ex)
{
threadException = ex;
}


while (RowsInsert.Count > 0)
{
int currentItem = initialSize - RowsInsert.Count > 0 ? (initialSize - RowsInsert.Count) + 1 : 1;
msgInserting = currentItem + "/" + initialSize;
String insert = "";
try
{
if (RowsInsert.TryDequeue(out insert))
{
dao.executeSQLConnectionKeepAlive(insert, conn);
}

}
catch (Exception e)
{
LogInsert += insert + "error:" + e.Message +Environment.NewLine;
msgInserting = e.Message;
Console.Write(e.Message);
if (!skip)
{
threadException = e;
break;
}

}
}
dao.closeConnection(conn);
}
public void terminateAllInsertThreads()
{
foreach (Thread t in insertThreads)
t.Abort();
}
public string dropTable(string tableName)
{
return dao.executeSQL(SQLGenerator.dropTable(tableName));
}
}
}
32 changes: 13 additions & 19 deletions OpenDataDBBuilder.Business/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,13 @@ public class FileUtil
{
public FileUtil(){}

public static void createFile(String filePath, String content)
public static void createFile(String filePath, String content, Boolean overwrite)
{
if (!System.IO.File.Exists(filePath))
if (!System.IO.File.Exists(filePath) || (overwrite && System.IO.File.Exists(filePath)))
{
using (FileStream fs = System.IO.File.Create(filePath))
{
Byte[] info = new UTF8Encoding(true).GetBytes(content);
fs.Write(info, 0, info.Length);
fs.Close();
}
}

}
if (overwrite && System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);


public static void overWriteFile(String filePath, String content)
{
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
using (FileStream fs = System.IO.File.Create(filePath))
{
Byte[] info = new UTF8Encoding(true).GetBytes(content);
Expand Down Expand Up @@ -150,7 +137,6 @@ public static Table getTableFromFile(String filePath)
Table table = new Table();
table.Columns = new List<Column>();
table.Rows = new List<Row>();

try
{
StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.Default);
Expand All @@ -171,7 +157,7 @@ public static Table getTableFromFile(String filePath)
int index = 1;
foreach (String s in fields)
{
row.Values.Add(new KeyValue("column"+index, s.Replace("\"", "")));
row.Values.Add(new KeyValue("column" + index, cleanValue(s).Replace("\"", "")));
index++;
}
table.Rows.Add(row);
Expand All @@ -183,7 +169,15 @@ public static Table getTableFromFile(String filePath)
Console.Out.Write(e);
}
table.TableName = "Table1";
table.OriginalTableName = "Table1";
return table;
}

public static String cleanValue(String value)
{
value = value.Replace("'", "");
return value;
}

}
}
Loading

0 comments on commit 47c70f5

Please sign in to comment.