Files
apitdn/fluig_rag_docs/Biblioteca de Snippets/Snippets SQL.md
T

193 lines
7.1 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Biblioteca de Snippets: SQL
## Origem: Configuração do banco de dados Oracle
```sql
DECLARE AUX_USERCONNECTED VARCHAR2(30);
BEGIN
SELECT USER
INTO AUX_USERCONNECTED
FROM DUAL;
IF (AUX_USERCONNECTED <> 'SYS') AND (AUX_USERCONNECTED <> 'SYSTEM') THEN
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_OBJECTS AS' ||
' SELECT * FROM SYS.ALL_OBJECTS WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_SYNONYMS AS' ||
' SELECT * FROM SYS.ALL_SYNONYMS WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_SEQUENCES AS' ||
' SELECT * FROM SYS.ALL_SEQUENCES WHERE SEQUENCE_OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_CONSTRAINTS AS' ||
' SELECT * FROM SYS.ALL_CONSTRAINTS WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_CONS_COLUMNS AS' ||
' SELECT * FROM SYS.ALL_CONS_COLUMNS WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_INDEXES AS' ||
' SELECT * FROM SYS.ALL_INDEXES WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_IND_COLUMNS AS' ||
' SELECT * FROM SYS.ALL_IND_COLUMNS WHERE TABLE_OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_TABLES AS' ||
' SELECT * FROM SYS.ALL_TABLES WHERE OWNER IN (SELECT USER FROM DUAL)');
EXECUTE IMMEDIATE ('CREATE OR REPLACE VIEW ' ||AUX_USERCONNECTED|| '.ALL_TAB_COLUMNS AS' ||
' SELECT * FROM SYS.ALL_TAB_COLUMNS WHERE OWNER IN (SELECT USER FROM DUAL)');
END IF;
END;
```
## Origem: Configuração do banco de dados SQL Server
```sql
SELECT suser_sname(owner_sid) FROM sys.databases WHERE name = 'nome_do_banco';
```
## Origem: Configuração do banco de dados SQL Server
```sql
SELECT is_read_committed_snapshot_on FROM sys.databases WHERE name= 'nome_do_banco';
```
## Origem: Transparent Data Encryption (TDE)
```sql
SELECT TABLE_SCHEMA, TABLE_NAME, CREATE_OPTIONS
FROM INFORMATION_SCHEMA.TABLES
WHERE CREATE_OPTIONS = 'ENCRYPTION="Y"';
```
## Origem: Métodos de contorno de lentidão na plataforma
```sql
select COD_DATASET from serv_dataset where DSL_DATASET like '%FluigDS%'; 
select nr_documento, COD_EVENT from event_ficha where DSL_EVENT like '%FluigDS%'; 
select COD_EVENT from event_geral where DSL_EVENT like '%FluigDS%'; 
select event_proces.COD_DEF_PROCES, COD_EVENT from event_proces where DSL_EVENT like '%FluigDS%';
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig, melhorando desempenho e estabilidade
```sql
/* Anexos de processos nunca acessados */
SELECT nr_versao, nr_documento, nm_arquivo_fisico, num_tam_arq_fisic FROM documento d WHERE nr_acessos = 0 AND tp_documento=7 OR (tp_documento=2 AND EXISTS(SELECT ap.NR_DOCUMENTO FROM anexo_proces ap WHERE ap.NR_DOCUMENTO = d.NR_DOCUMENTO) ) ORDER BY num_tam_arq_fisic DESC, nr_documento DESC
/* Documentos que não são anexos de processos, nunca acessados */
SELECT nr_versao, nr_documento, nm_arquivo_fisico, num_tam_arq_fisic FROM documento d WHERE nr_acessos = 0 AND tp_documento=2 AND not EXISTS(SELECT ap.NR_DOCUMENTO FROM anexo_proces ap WHERE ap.NR_DOCUMENTO = d.NR_DOCUMENTO ) ORDER BY num_tam_arq_fisic DESC, nr_documento DESC
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig, melhorando desempenho e estabilidade
```sql
/* documentos com a data de expiração menor do que a data atual */
SELECT distinct(nr_documento) FROM documento WHERE dt_expiracao < CURDATE()
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig, melhorando desempenho e estabilidade
```sql
/*
log_type:
0 - LOGIN,
1 - LOGINERROR,
2 - LOGOUT
access_date
Data no formato 'YYYY-MM-DD HH:mm:ss'
*/
SELECT * FROM fdn_accesslog
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig melhorando desempenho e estabilidade
```sql
/* Anexos de processos nunca acessados */
SELECT nr_versao, nr_documento, nm_arquivo_fisico, num_tam_arq_fisic FROM documento d WHERE nr_acessos = 0 AND tp_documento=7 OR (tp_documento=2 AND EXISTS(SELECT ap.NR_DOCUMENTO FROM anexo_proces ap WHERE ap.NR_DOCUMENTO = d.NR_DOCUMENTO) ) ORDER BY num_tam_arq_fisic DESC, nr_documento DESC
/* Documentos que não são anexos de processos, nunca acessados */
SELECT nr_versao, nr_documento, nm_arquivo_fisico, num_tam_arq_fisic FROM documento d WHERE nr_acessos = 0 AND tp_documento=2 AND not EXISTS(SELECT ap.NR_DOCUMENTO FROM anexo_proces ap WHERE ap.NR_DOCUMENTO = d.NR_DOCUMENTO ) ORDER BY num_tam_arq_fisic DESC, nr_documento DESC
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig melhorando desempenho e estabilidade
```sql
/* documentos com a data de expiração menor do que a data atual */
SELECT distinct(nr_documento) FROM documento WHERE dt_expiracao < CURDATE()
```
## Origem: 10 dicas para turbinar seu TOTVS Fluig melhorando desempenho e estabilidade
```sql
/*
log_type:
0 - LOGIN,
1 - LOGINERROR,
2 - LOGOUT
access_date
Data no formato 'YYYY-MM-DD HH:mm:ss'
*/
SELECT * FROM fdn_accesslog
```
## Origem: Desenvolvimento de relatórios
```sql
SELECT vol_fisic.CD_VOL_FISIC,
vol_fisic.COD_EMPRESA,
vol_fisic.NOM_DIR_FISIC,
vol_fisic.ID_TIP_VOL_FISIC
FROM vol_fisic
WHERE vol_fisic.COD_EMPRESA = ${WKCompany}
```
## Origem: Desenvolvimento de relatórios
```sql
importPackage(Packages.com.datasul.technology.webdesk.dataset.service);
importPackage(Packages.java.util);
var qd = null;
var rs = null;
var rowMap = null;
var sqlJpa = "";
qd = new QueryDelegate();
sqlJpa = "SELECT count(d.documentPK.documentId), d.publisherId from Document d WHERE
d.documentPK.companyId = ${WKCompany} GROUP BY d.publisherId";
rs = qd.getResultQuery(sqlJpa);
var rowMap = rs.get(0);
//Busca o valor do Count
rowMap.get("d.publisherId");
//Busca o Código do documento
rowMap.get("count(d.documentPK.documentId)");
```
## Origem: Como salvar Imagens e Vídeos locais em formulários usando o componente FLUIGC.richeditor
```sql
async function saveFilesECM(files, type) {
let currentHTML = editor?.getData() || '';
const parser = new DOMParser();
const doc = parser.parseFromString(currentHTML, 'text/html');
const parentId = '37'; // Código da pasta do ECM
for (const file of files) {
try {
const fileUrl = file.url;
const fileName = file.alt;
const data = await uploadFileECM(fileName, parentId);
const newUrl = `/content-management/api/v2/documents/${data.documentId}/stream`;
const filesInDoc = doc.querySelectorAll(type);
filesInDoc.forEach(target => {
const idx = target.src.indexOf('/webdesk');
const normalizedFileUrl = encodeURI(fileUrl);
if (idx >= 0 && target.src.substring(idx) === normalizedFileUrl) {
target.src = newUrl;
}
});
} catch (error) {
console.error('Erro ao processar arquivo:', error);
}
}
return doc.body.innerHTML;
}
```