Skip to content

Commit

Permalink
project update, adds table edition, table creation, changes columns e…
Browse files Browse the repository at this point in the history
…dition
  • Loading branch information
jonathan-gomes committed May 13, 2015
1 parent 80672ab commit 8cceb76
Show file tree
Hide file tree
Showing 40 changed files with 1,798 additions and 511 deletions.
5 changes: 3 additions & 2 deletions OpenDataDBBuilder.Business/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenDataDBBuilder.Business.VO;

namespace OpenDataDBBuilder.Business.DB.VO
{
Expand All @@ -18,7 +19,7 @@ public Column(String ColumnName)
this.sqlType = "VARCHAR(40)";
}

public Column(String ColumnName, Boolean IsPK, Boolean IsFK, Boolean IsIDGenerated, String Reference)
public Column(String ColumnName, Boolean IsPK, Boolean IsFK, Boolean IsIDGenerated, KeyValue Reference)
{
this.ColumnName = ColumnName;
this.IsPK = IsPK;
Expand All @@ -38,7 +39,7 @@ public Column(String ColumnName, Boolean IsPK, Boolean IsFK, Boolean IsIDGenerat
public Boolean IsPK { get; set; }
public Boolean IsFK { get; set; }
public Boolean IsIDGenerated { get; set; }
public String Reference { get; set; }
public KeyValue Reference { get; set; }
public String sqlType { get; set; }
}
}
62 changes: 0 additions & 62 deletions OpenDataDBBuilder.Business/DatabaseChooser.cs

This file was deleted.

90 changes: 90 additions & 0 deletions OpenDataDBBuilder.Business/DatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using OpenDataDBBuilder.DataRepository;
using OpenDataDBBuilder.DataRepository.Mysql;
using System.Windows.Forms;
using OpenDataDBBuilder.Business.File.Util;

using MySql.Data.MySqlClient;
using System.Configuration;

namespace OpenDataDBBuilder.Business.DB
{
public class DatabaseHelper
{
String mysql = "MySQL";
String db = "";
DAO dao;
public List<String> databases = new List<String>();


public DatabaseHelper(String db)
{
this.db = db;
this.dao = getDataBaseDAO();
databases.Add(mysql);
}

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

public Boolean isConnectionAvailable(String database, String conn)
{
return dao.isConnectionAvailable(conn);
}

private String readDBConfigFile()
{
String appPath = Application.StartupPath;
String dbConfigPath = appPath + "/DBConfig" + db + ".config";
List<String> file = FileUtil.openFile(dbConfigPath);
String config = "";

foreach (String s in file)
{
config += s + ";";
}
return config;
}

public String addDatabase(String dataBase)
{
return dao.executeSQL(SQLGenerator.createDatabase(dataBase));
}

public List<String> selectDataBases()
{
return dao.selectDataBases(SQLGenerator.selectDataBases());
}

public List<String> getTablesFromDataBase(String dataBase)
{
return dao.getTablesFromDataBase(SQLGenerator.getTablesFromDataBase(dataBase));
}

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

public String runSQL(String sql)
{
return dao.executeSQL(sql);
}
public List<String> getFieldListFromDBTable(String dataBase, String table)
{
return dao.getFieldListFromDBTable(SQLGenerator.getTableDescription(dataBase, table));
}
}
}
3 changes: 3 additions & 0 deletions OpenDataDBBuilder.Business/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void createFile(String filePath, String content)
{
Byte[] info = new UTF8Encoding(true).GetBytes(content);
fs.Write(info, 0, info.Length);
fs.Close();
}
}

Expand All @@ -36,6 +37,7 @@ public static void overWriteFile(String filePath, String content)
{
Byte[] info = new UTF8Encoding(true).GetBytes(content);
fs.Write(info, 0, info.Length);
fs.Close();
}
}

Expand All @@ -53,6 +55,7 @@ public static List<String> openFile(String filePath)
file.Add(line);
counter++;
}
reader.Close();
return file;
}
public static int countLines(String filePath)
Expand Down
3 changes: 2 additions & 1 deletion OpenDataDBBuilder.Business/OpenDataDBBuilder.Business.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Column.cs" />
<Compile Include="DatabaseChooser.cs" />
<Compile Include="DatabaseHelper.cs" />
<Compile Include="DBInitializer.cs" />
<Compile Include="FileUtil.cs" />
<Compile Include="KeyValue.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Row.cs" />
<Compile Include="SQLGenerator.cs" />
<Compile Include="Table.cs" />
<Compile Include="ClassUtil.cs" />
<Compile Include="XMLFileUtil.cs" />
Expand Down
Binary file not shown.
71 changes: 71 additions & 0 deletions OpenDataDBBuilder.Business/SQLGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenDataDBBuilder.Business.DB.VO;

namespace OpenDataDBBuilder.Business.DB
{
public static class SQLGenerator
{

public static String createDatabase(String dataBase)
{
String sql = "CREATE DATABASE " + dataBase + ";";
return sql;
}

public static String selectDataBases()
{
String sql = "SHOW DATABASES;";
return sql;
}


public static void prepareTablesSQL(ref List<Table> tables)
{
foreach (Table t in tables)
{
StringBuilder sql = new StringBuilder("CREATE TABLE " + t.TableName + " (\n");
StringBuilder sqlConstraints = new StringBuilder();
for (int i = 0; i < t.Columns.Count; i++)
{
sql.Append(t.Columns.ElementAt(i).ColumnName + " " + t.Columns.ElementAt(i).sqlType);
if (t.Columns.ElementAt(i).IsPK)
sql.Append(" PRIMARY KEY");
if (t.Columns.ElementAt(i).IsFK)
{
sqlConstraints.Append(",\n");
sqlConstraints.Append("FOREIGN KEY ("+t.Columns.ElementAt(i).ColumnName+") ");
sqlConstraints.Append("REFERENCES " + t.Columns.ElementAt(i).Reference.Key + "(");
sqlConstraints.Append(t.Columns.ElementAt(i).Reference.Value + ")");
}
if (i < t.Columns.Count - 1)
sql.Append(",\n");
}
sql.Append(sqlConstraints.ToString());
sql.Append("\n);");
t.SQLcreate = sql.ToString();
}
}

public static String useDataBase(String dataBase)
{
return "USE " + dataBase + ";";
}
public static String getTablesFromDataBase(String dataBase)
{
StringBuilder sql = new StringBuilder(useDataBase(dataBase));
sql.Append("SHOW TABLES;");
return sql.ToString();
}

public static String getTableDescription(String dataBase, String table)
{
StringBuilder sql = new StringBuilder(useDataBase(dataBase));
sql.Append("DESCRIBE "+table+";");
return sql.ToString();
}
}
}
2 changes: 1 addition & 1 deletion OpenDataDBBuilder.Business/XMLFileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static void addNodeToTables(XElement element, ref List<Table> tables)
{
String parentName = element.Parent.Name.ToString();
String columnParentIDName = parentName + "ID";
String reference = parentName + "." + columnParentIDName;
KeyValue reference = new KeyValue(parentName, columnParentIDName);
Column columnParentID = new Column(columnParentIDName, false, true, true,reference);
tables[index].Columns.Add(columnParentID);
}
Expand Down
9 changes: 6 additions & 3 deletions OpenDataDBBuilder.DataRepository/DAO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;

namespace OpenDataDBBuilder.DataRepository
{
public interface DAO
{
Boolean isConnectionAvailable();
Boolean isConnectionAvailable(String conn);
List<String> selectDataBases();
void addDatabase(String dataBase);
List<String> selectDataBases(String sql);
String executeSQL(String sql);
List<String> getTablesFromDataBase(String sql);
DataTable getTableDescription(String sql);
List<String> getFieldListFromDBTable(String sql);
}
}
Loading

0 comments on commit 8cceb76

Please sign in to comment.