public static int getIndexColumnPerName(String name, Table table) { for(int i = 0; i < table.Columns.Count;i++) { if(table.Columns[i].ColumnName.Equals(name)) return i; } return -1; }
public static Table getTableFromFile(String filePath) { int counter = 0; String line = ""; 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); while ((line = reader.ReadLine()) != null)// && counter < 50) { String[] fields = line.Split(';'); if (counter == 0) { int i = 1; foreach (String s in fields) { table.Columns.Add(new Column("column" + i)); i++; } } Row row = new Row(); row.Values = new List<KeyValue>(); int index = 1; foreach (String s in fields) { row.Values.Add(new KeyValue("column" + index, cleanValue(s).Replace("\"", ""))); index++; } table.Rows.Add(row); counter++; } } catch (Exception e) { Console.Out.Write(e); } table.TableName = "Table1"; table.OriginalTableName = "Table1"; return table; }
public static String getInsertStart(Table t) { StringBuilder sql = new StringBuilder(); List<String> columnNames = t.columnNames(); if ((columnNames != null && columnNames.Count > 0) && (t.Rows != null && t.Rows.Count > 0)) { sql.Append("INSERT INTO " + t.TableName); sql.Append("("); sql.Append(t.columnNames()[0]); for (int i = 1; i < t.columnNames().Count(); i++) sql.Append("," + t.columnNames()[i]); sql.Append(") "); sql.Append(Environment.NewLine); sql.Append("VALUES "); sql.Append(Environment.NewLine); } return sql.ToString(); }
public Template getTemplateFile(String templateFilePath) { this.templateFilePath = templateFilePath; List<String> file = openFile(this.templateFilePath); DBConfig dbConfig = new DBConfig(); TableList tablelist = new TableList(new List<Table>()); Table table = null; foreach (String s in file) { if (!s.Equals("")) { if ("//".Equals(s)) tablelist.Tables.Add(table); else { String key = s.Split('=')[0]; String value = s.Split('=')[1]; if ("Db".Equals(key)) dbConfig.Db = value; if ("Server".Equals(key)) dbConfig.Server = value; if ("Port".Equals(key)) dbConfig.Port = value; if ("User".Equals(key)) dbConfig.User = value; if ("Password".Equals(key)) dbConfig.Password = value; if("DbName".Equals(key)) dbConfig.DbName = value; if ("Table".Equals(key)) { table = new Table(); table.OriginalTableName = value.Split(':')[0]; table.TableName = value.Split(':')[1]; table.Columns = new List<Column>(); } if ("Column".Equals(key)) { String columnNames = value.Split(';')[0]; String sqlType = value.Split(';')[1]; String pk = value.Split(';')[2]; String fk = value.Split(';')[3]; String nullabe = value.Split(';')[4]; String unique = value.Split(';')[5]; String defValue = value.Split(';')[6]; String defValueOnError = value.Split(';')[7]; String referenceDB = value.Split(';')[8]; String referenceTableColum = value.Split(';')[9]; String skip = value.Split(';')[10]; Column column = new Column(columnNames.Split(':')[0]); column.ColumnName = columnNames.Split(':')[1] == null || columnNames.Split(':')[1].Equals("") ? column.ColumnName : columnNames.Split(':')[1]; column.sqlType = sqlType.Split(':')[1] == null || sqlType.Split(':')[1].Equals("") ? column.sqlType : sqlType.Split(':')[1]; column.IsPK = pk.Split(':')[1] != null && pk.Split(':')[1].ToLower().Equals("true"); column.IsFK = fk.Split(':')[1] != null && fk.Split(':')[1].ToLower().Equals("true"); column.IsNullable = nullabe.Split(':')[1] != null && nullabe.Split(':')[1].ToLower().Equals("true"); column.IsUnique = unique.Split(':')[1] != null && unique.Split(':')[1].ToLower().Equals("true"); column.defaulValue = defValue.Split(':')[1] != null ? defValue.Split(':')[1] : ""; column.defaulValueOnError = defValueOnError.Split(':')[1] != null ? defValueOnError.Split(':')[1] : ""; column.SKIP = skip.Split(':')[1] != null && skip.Split(':')[1].ToLower().Equals("true"); if (column.IsFK) column.Reference = new VO.KeyValue(referenceDB.Split(':')[1], referenceTableColum.Split(':')[1]); table.Columns.Add(column); } } } } return new Template(tablelist, dbConfig); }