Exemplo n.º 1
0
 public void Parse(TableRecordManipulationLogic tableRecorsdManipulationLogic)
 {
     PumpTable(tableRecorsdManipulationLogic);
     if (_zipPath != null)
     {
         using (ZipArchive archive = ZipFile.Open(_zipPath, ZipArchiveMode.Update))
         {
             ZipArchiveEntry entry = archive.GetEntry(_DBPathInsideZip);
             entry.Delete();
             try
             {
                 archive.CreateEntryFromFile(_DBPathInDisk, _DBPathInsideZip, CompressionLevel.NoCompression);
                 File.Delete(_DBPathInDisk);
             }
             catch (Exception e)
             {
                 System.Threading.Thread.Sleep(1000);
             }
         }
     }
 }
Exemplo n.º 2
0
        public void PumpTable(TableRecordManipulationLogic tableRecorsdManipulationLogic)
        {
            int    numofIntactsPerRecord   = tableRecorsdManipulationLogic.Intacts;
            int    numofDeletedesPerRecord = tableRecorsdManipulationLogic.Deletedes;
            string tableName              = tableRecorsdManipulationLogic.TableName;
            string primaryKey             = tableRecorsdManipulationLogic.PrimaryKey;
            var    manipulationArgsLong   = tableRecorsdManipulationLogic.ManipulationArgsLong;
            var    manipulationArgsString = tableRecorsdManipulationLogic.ManipulationArgsString;
            long   maxValuePrimaryKey     = 1 + GetMaxValuePrimaryKey(tableName, primaryKey);

            _cmd.CommandText = $@"SELECT * FROM {tableName}";
            var dataTable        = new DataTable();
            SQLiteDataReader rdr = _cmd.ExecuteReader();

            dataTable.Load(rdr);
            List <string> columnList = GetColumnList(dataTable.Columns);
            string        cloneQuery = CreateCloneQuery(dataTable.TableName, columnList);

            _cmd.CommandText = cloneQuery;
            long numofDeletedes = numofDeletedesPerRecord * dataTable.Rows.Count;
            Dictionary <string, object> lastRec = new Dictionary <string, object>();
            List <long> deletedPrimaryKeys      = new List <long>();

            foreach (DataRow rec in dataTable.Rows)
            {
                var pumpRecord = CreatePumpRecord(rec, columnList);

                for (int k = 0; k < numofIntactsPerRecord + numofDeletedesPerRecord; k++)
                {
                    foreach (var colNameValue in pumpRecord)
                    {
                        var    colName = colNameValue.Key;
                        var    value   = colNameValue.Value;
                        string type    = value.GetType().Name;

                        if (colName == "@" + primaryKey)
                        {
                            value = maxValuePrimaryKey++;
                            if (k >= numofIntactsPerRecord)
                            {
                                deletedPrimaryKeys.Add((long)value);
                            }
                        }
                        else if (type == "Int64" && manipulationArgsLong.ContainsKey(colName))
                        {
                            value = manipulationArgsLong[colName](value);
                        }
                        else if (type == "String" && manipulationArgsString.ContainsKey(colName))
                        {
                            value = manipulationArgsString[colName](value);
                        }

                        lastRec[colName] = value;

                        _cmd.Parameters.AddWithValue(colName, value);
                    }

                    pumpRecord = new Dictionary <string, object>(lastRec);
                    try
                    {
                        _cmd.ExecuteNonQuery();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
            foreach (long key in deletedPrimaryKeys)
            {
                _cmd.CommandText = $@"delete from {tableName} where {primaryKey} = {key}";
                _cmd.ExecuteNonQuery();
            }
            _cmd.Dispose();
            //_connection.Close();
            _connection.Open();
            // save memory db to file
            _memoryConnection.BackupDatabase(_connection, "main", "main", -1, null, 0);
            _memoryConnection.Close();
            _connection.Close();
        }