//private static int log_output(const void db, const char text)
        //{
        //    if (text)
        //    {
        //        //if (UPLogSettings.LogStatements())
        //        //{
        //        //    DDLogCSQL("%08lX: %s", (unsigned long)db, text);

        //        //    #ifdef DEBUG
        //        //        Console.WriteLine("%08lX: %s", (unsigned long)db, text);
        //        //    #endif

        //        //    return 1;
        //        //}

        //        return 0;
        //    }
        //    else
        //    {
        //        return UPLogSettings.LogStatements();
        //    }
        //}

        private static int InsertRecordLinks(UPCRMRecord record, UPRecordIdentificationMapper recordMapper, int requestNr, int recordnr, IDatabase database, int result)
        {
            var links = record.Links;

            if (result == 0 && links?.Count > 0)
            {
                var statement = new DatabaseStatement(database);
                if (statement.Prepare("INSERT INTO recordlinks (requestnr, recordnr, infoareaid, linkid, recordid) VALUES (?,?,?,?,?)"))
                {
                    foreach (var link in links)
                    {
                        statement.Reset();
                        statement.Bind(1, requestNr);
                        statement.Bind(2, recordnr);
                        var linkRecordIdentification = recordMapper.MappedRecordIdentification(link.RecordIdentification);
                        statement.Bind(3, link.InfoAreaId);
                        statement.Bind(4, link.LinkId);
                        statement.Bind(5, linkRecordIdentification.RecordId());
                        result = statement.Execute();
                        if (result != 0)
                        {
                            break;
                        }
                    }
                }
            }

            return(result);
        }
        public int SaveDocumentUpload(byte[] data, int requestNr, string fileName,
                                      string mimeType, string recordIdentification, int fieldId, IDatabase database)
        {
            DatabaseStatement            statement = new DatabaseStatement(database);
            UPRecordIdentificationMapper mapper    = ServerSession.CurrentSession.RecordIdentificationMapper;
            string origRecordIdentification        = recordIdentification;

            recordIdentification = mapper.MappedRecordIdentification(recordIdentification);
            if (recordIdentification == origRecordIdentification)
            {
                this.Logger.LogDebug(
                    $"UPSync-saveDocupload request={requestNr}, length={data.Length}, file={fileName}, recordId={recordIdentification}",
                    LogFlag.LogUpSync);
            }
            else
            {
                this.Logger.LogDebug(
                    $"UPSync-saveDocupload request={requestNr}, length={data.Length}, file={fileName}, recordId={recordIdentification}, original={origRecordIdentification}",
                    LogFlag.LogUpSync);
            }

            if (statement.Prepare("INSERT INTO documentuploads (requestnr, data, filename, mimetype, infoareaid, recordid, fieldid) VALUES (?,?,?,?,?,?,?)"))
            {
                string timepart         = DateTime.UtcNow.TimeIntervalSinceReferenceDate().ToString().Replace(":", string.Empty).Replace(".", string.Empty);
                string extendedFileName = $"{timepart}_{requestNr}_{fileName}";
                string localPath        = this.GetOfflineStoragePath(extendedFileName);
                string databaseValue    = $"file://{extendedFileName}";
                SimpleIoc.Default.GetInstance <IPlatformService>().StorageProvider.SaveFile(localPath, data);
                this.Logger.LogDebug($"Sync request {requestNr}: document {localPath} saved ({data.Length} bytes)", LogFlag.LogUpSync);
                statement.Bind(1, requestNr);
                statement.Bind(2, databaseValue);
                statement.Bind(3, fileName);
                statement.Bind(4, mimeType);
                statement.Bind(5, recordIdentification.InfoAreaId());
                statement.Bind(6, recordIdentification.RecordId());
                statement.Bind(7, fieldId);
                return(statement.Execute());
            }

            return(-1);
        }
 /// <summary>
 /// Applies the mapped record identifications.
 /// </summary>
 /// <param name="mapper">
 /// The mapper.
 /// </param>
 public void ApplyMappedRecordIdentifications(UPRecordIdentificationMapper mapper)
 {
     this.Record.ApplyMappedRecordIdentifications(mapper, true);
 }