Example #1
0
        public void AddTable(Stream stream)
        {
            Table table = new Table();

            table.Read(stream);
            AddTable(table);
        }
Example #2
0
        private static void importRaw(SQLiteConnection dbConnection, string[] tableFiles)
        {
            foreach (string tableFile in tableFiles)
            {
                var table = new zzio.db.Table();
                using (var stream = new FileStream(tableFile, FileMode.Open, FileAccess.Read))
                    table.Read(stream);

                var tableName          = Path.GetFileNameWithoutExtension(tableFile);
                var createTableCommand = new SQLiteCommand(
                    $"CREATE TABLE IF NOT EXISTS {tableName} (" +
                    "UID TEXT, " +
                    string.Join(",", table.rows.Values.First().cells.Select(
                                    (c, i) => $"col_{i}_{c.Type} {CellDataTypeToSQLType[c.Type]}")) +
                    ", PRIMARY KEY (UID))", dbConnection);
                createTableCommand.ExecuteNonQuery();

                int columnCount = table.rows.Values.First().cells.Length;
                foreach (var row in table.rows.Values)
                {
                    var insertRowCommand = new SQLiteCommand(
                        $"REPLACE INTO {tableName} VALUES (@uid, " +
                        string.Join(",", table.rows.Values.First().cells.Select((c, i) => $"@col_{i}")) +
                        ")", dbConnection);
                    insertRowCommand.Prepare();
                    insertRowCommand.Parameters.Clear();
                    insertRowCommand.Parameters.AddWithValue("@uid", row.uid.ToString());
                    for (int i = 0; i < row.cells.Length; i++)
                    {
                        var cell = row.cells[i];
                        switch (cell.Type)
                        {
                        case CellDataType.Buffer:
                        case CellDataType.Unknown:
                            insertRowCommand.Parameters.AddWithValue($"@col_{i}", cell.Buffer);
                            break;

                        case CellDataType.Byte:
                            insertRowCommand.Parameters.AddWithValue($"@col_{i}", cell.Byte);
                            break;

                        case CellDataType.Integer:
                            insertRowCommand.Parameters.AddWithValue($"@col_{i}", cell.Integer);
                            break;

                        case CellDataType.String:
                            insertRowCommand.Parameters.AddWithValue($"@col_{i}", cell.String);
                            break;

                        case CellDataType.ForeignKey:
                            insertRowCommand.Parameters.AddWithValue($"@col_{i}", cell.ForeignKey.ToString());
                            break;

                        default: throw new Exception("WUT?");
                        }
                    }
                    for (int i = row.cells.Length; i < columnCount; i++)
                    {
                        insertRowCommand.Parameters.AddWithValue($"@col_{i}", null);
                    }
                    insertRowCommand.ExecuteNonQuery();
                }
            }
        }