Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
simcaq-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
simcaq
simcaq-node
Commits
b4fdd6e8
Commit
b4fdd6e8
authored
7 years ago
by
Vytor Calixto
Browse files
Options
Downloads
Plain Diff
Merge branch 'multibuild' into siope_route
parents
f2efe894
fabb86b7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!116
Release v1.0.0
,
!91
Siope route
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/libs/middlewares/reqQueryFields.js
+116
-102
116 additions, 102 deletions
src/libs/middlewares/reqQueryFields.js
with
116 additions
and
102 deletions
src/libs/middlewares/reqQueryFields.js
+
116
−
102
View file @
b4fdd6e8
...
...
@@ -192,126 +192,140 @@ class ReqQueryFields {
};
}
build
(
)
{
build
Query
(
req
,
sql
)
{
// "Constrói" o SQL
return
(
req
,
res
,
next
)
=>
{
let
hasJoined
=
{};
let
thisTable
=
req
.
sql
.
tableFrom
;
// Foreach no campos
Object
.
keys
(
this
.
fields
).
forEach
((
key
)
=>
{
// Campo
let
field
=
this
.
fields
[
key
];
log
.
debug
(
field
);
// `param` aqui é o atributo no objeto `req` (dims, filter, search, ...)
let
param
=
req
[
field
.
name
];
// Fazemos um foreach nos parametros dentro do atributo
Object
.
keys
(
param
).
forEach
((
k
)
=>
{
let
values
=
_
.
merge
(
this
.
fieldValues
,
field
.
values
);
if
(
typeof
values
[
k
]
!==
'
undefined
'
)
{
// Clonamos para não alterar o original
let
value
=
_
.
clone
(
values
[
k
]);
if
(
value
.
parseOnly
)
return
;
// Checa se não fizemos o join para este valor e se é necessário fazer
if
(
!
hasJoined
[
value
.
table
]
&&
typeof
value
.
join
!==
'
undefined
'
)
{
let
foreignTable
=
''
;
if
(
value
.
join
.
foreignTable
)
foreignTable
=
value
.
join
.
foreignTable
+
'
.
'
;
if
(
value
.
join
.
foreignTable
===
'
@
'
)
foreignTable
=
thisTable
+
'
.
'
;
// Fazemos o join
let
onClause
=
''
;
if
(
Array
.
isArray
(
value
.
join
.
primary
))
{
// Se é um array, montamos a cláusula ON com mais de uma coluna
value
.
join
.
primary
.
forEach
((
column
,
index
,
arr
)
=>
{
onClause
+=
foreignTable
+
value
.
join
.
foreign
[
index
]
+
'
=
'
+
value
.
table
+
'
.
'
+
column
;
if
(
index
<
arr
.
length
-
1
)
{
onClause
+=
'
AND
'
;
}
});
}
else
{
onClause
=
foreignTable
+
value
.
join
.
foreign
+
'
=
'
+
value
.
table
+
'
.
'
+
value
.
join
.
primary
;
}
req
.
sql
.
join
(
value
.
table
,
null
,
onClause
);
// Marcamos o join como feito para não ter problemas
hasJoined
[
value
.
table
]
=
true
;
let
hasJoined
=
{};
let
thisTable
=
sql
.
tableFrom
;
// Foreach no campos
Object
.
keys
(
this
.
fields
).
forEach
((
key
)
=>
{
// Campo
let
field
=
this
.
fields
[
key
];
log
.
debug
(
field
);
// `param` aqui é o atributo no objeto `req` (dims, filter, search, ...)
let
param
=
req
[
field
.
name
];
// Fazemos um foreach nos parametros dentro do atributo
Object
.
keys
(
param
).
forEach
((
k
)
=>
{
let
values
=
_
.
merge
(
this
.
fieldValues
,
field
.
values
);
if
(
typeof
values
[
k
]
!==
'
undefined
'
)
{
// Clonamos para não alterar o original
let
value
=
_
.
clone
(
values
[
k
]);
if
(
value
.
parseOnly
)
return
;
// Checa se não fizemos o join para este valor e se é necessário fazer
if
(
!
hasJoined
[
value
.
table
]
&&
typeof
value
.
join
!==
'
undefined
'
)
{
let
foreignTable
=
''
;
if
(
value
.
join
.
foreignTable
)
foreignTable
=
value
.
join
.
foreignTable
+
'
.
'
;
if
(
value
.
join
.
foreignTable
===
'
@
'
)
foreignTable
=
thisTable
+
'
.
'
;
// Fazemos o join
let
onClause
=
''
;
if
(
Array
.
isArray
(
value
.
join
.
primary
))
{
// Se é um array, montamos a cláusula ON com mais de uma coluna
value
.
join
.
primary
.
forEach
((
column
,
index
,
arr
)
=>
{
onClause
+=
foreignTable
+
value
.
join
.
foreign
[
index
]
+
'
=
'
+
value
.
table
+
'
.
'
+
column
;
if
(
index
<
arr
.
length
-
1
)
{
onClause
+=
'
AND
'
;
}
});
}
else
{
onClause
=
foreignTable
+
value
.
join
.
foreign
+
'
=
'
+
value
.
table
+
'
.
'
+
value
.
join
.
primary
;
}
// Se o valor é um campo a ser incluído no SELECT
if
(
typeof
field
.
field
!==
'
undefined
'
&&
field
.
field
)
{
log
.
debug
(
'
SELECT
'
);
let
table
=
value
.
table
;
if
(
table
===
'
@
'
)
table
=
thisTable
;
if
(
Array
.
isArray
(
value
.
resultField
))
{
value
.
tableField
.
forEach
((
f
,
i
)
=>
{
req
.
sql
.
field
(
table
+
'
.
'
+
f
,
value
.
resultField
[
i
]
||
f
)
.
group
(
table
+
'
.
'
+
f
)
.
order
(
table
+
'
.
'
+
f
);
})
}
else
{
req
.
sql
.
field
(
table
+
'
.
'
+
value
.
tableField
,
value
.
resultField
||
value
.
tableField
)
.
order
(
table
+
'
.
'
+
value
.
tableField
)
.
group
(
table
+
'
.
'
+
value
.
tableField
);
}
sql
.
join
(
value
.
table
,
null
,
onClause
);
// Marcamos o join como feito para não ter problemas
hasJoined
[
value
.
table
]
=
true
;
}
// Se o valor é um campo a ser incluído no SELECT
if
(
typeof
field
.
field
!==
'
undefined
'
&&
field
.
field
)
{
log
.
debug
(
'
SELECT
'
);
let
table
=
value
.
table
;
if
(
table
===
'
@
'
)
table
=
thisTable
;
if
(
Array
.
isArray
(
value
.
resultField
))
{
value
.
tableField
.
forEach
((
f
,
i
)
=>
{
sql
.
field
(
table
+
'
.
'
+
f
,
value
.
resultField
[
i
]
||
f
)
.
group
(
table
+
'
.
'
+
f
)
.
order
(
table
+
'
.
'
+
f
);
})
}
else
{
sql
.
field
(
table
+
'
.
'
+
value
.
tableField
,
value
.
resultField
||
value
.
tableField
)
.
order
(
table
+
'
.
'
+
value
.
tableField
)
.
group
(
table
+
'
.
'
+
value
.
tableField
);
}
// Se o valor é um campo para ser usado no WHERE
if
(
typeof
field
.
where
!==
'
undefined
'
&&
field
.
where
)
{
log
.
debug
(
'
WHERE
'
);
// Valor do where
let
whereValue
=
param
[
k
];
log
.
debug
(
'
whereValue
'
);
log
.
debug
(
whereValue
);
log
.
debug
(
`Where value é array?
${
Array
.
isArray
(
whereValue
)}
`
);
}
// Se o valor é um campo para ser usado no WHERE
if
(
typeof
field
.
where
!==
'
undefined
'
&&
field
.
where
)
{
log
.
debug
(
'
WHERE
'
);
// Valor do where
let
tbl
=
value
.
where
.
table
||
value
.
table
;
if
(
tbl
===
'
@
'
)
tbl
=
thisTable
;
// multiple where
if
(
Array
.
isArray
(
value
.
where
.
field
))
{
let
lower
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(?)
'
:
'
?
'
;
let
whereField
=
''
;
let
whereValues
=
[];
value
.
where
.
field
.
forEach
((
f
,
i
,
arr
)
=>
{
whereField
+=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(
'
+
tbl
+
'
.
'
+
value
.
where
.
field
[
i
]
+
'
)
'
:
tbl
+
'
.
'
+
value
.
where
.
field
[
i
];
whereField
+=
'
'
+
value
.
where
.
relation
+
'
?
'
;
if
(
i
<
arr
.
length
-
1
)
{
whereField
+=
'
'
+
value
.
where
.
condition
+
'
'
;
}
let
whereValue
=
param
[
k
];
log
.
debug
(
'
whereValue
'
);
log
.
debug
(
whereValue
);
log
.
debug
(
`Where value é array?
${
Array
.
isArray
(
whereValue
)}
`
);
if
(
Array
.
isArray
(
whereValue
))
{
let
whereString
=
'
(
'
;
for
(
let
i
=
0
;
i
<
whereValue
.
length
;
++
i
)
{
whereString
+=
whereField
;
whereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
[
i
]));
if
(
i
<
whereValue
.
length
-
1
)
{
whereString
+=
'
OR
'
;
}
}
whereString
+=
'
)
'
;
}
else
{
whereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
));
}
});
let
tbl
=
value
.
where
.
table
||
value
.
table
;
if
(
tbl
===
'
@
'
)
tbl
=
thisTable
;
// multiple where
if
(
Array
.
isArray
(
value
.
where
.
field
))
{
let
lower
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(?)
'
:
'
?
'
;
let
whereField
=
''
;
let
whereValues
=
[];
value
.
where
.
field
.
forEach
((
f
,
i
,
arr
)
=>
{
whereField
+=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(
'
+
tbl
+
'
.
'
+
value
.
where
.
field
[
i
]
+
'
)
'
:
tbl
+
'
.
'
+
value
.
where
.
field
[
i
];
whereField
+=
'
'
+
value
.
where
.
relation
+
'
?
'
;
if
(
i
<
arr
.
length
-
1
)
{
whereField
+=
'
'
+
value
.
where
.
condition
+
'
'
;
}
req
.
sql
.
where
(
whereField
,
...
whereValues
);
}
else
{
let
whereField
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(
'
+
tbl
+
'
.
'
+
value
.
where
.
field
+
'
)
'
:
tbl
+
'
.
'
+
value
.
where
.
field
;
let
lower
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(?)
'
:
'
?
'
;
if
(
Array
.
isArray
(
whereValue
))
{
if
(
Array
.
isArray
(
whereValue
))
{
let
whereString
=
'
(
'
;
let
arrayWhereValues
=
[];
for
(
let
i
=
0
;
i
<
whereValue
.
length
;
++
i
)
{
whereString
+=
whereField
+
'
'
+
value
.
where
.
relation
+
lower
;
arrayW
hereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
[
i
]));
whereString
+=
whereField
;
w
hereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
[
i
]));
if
(
i
<
whereValue
.
length
-
1
)
{
whereString
+=
'
OR
'
;
}
}
whereString
+=
'
)
'
;
req
.
sql
.
where
(
whereString
,
...
arrayWhereValues
);
}
else
{
req
.
sql
.
where
(
whereField
+
'
'
+
value
.
where
.
relation
+
lower
,
parseWhereValue
(
value
.
where
.
type
,
whereValue
));
whereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
));
}
});
sql
.
where
(
whereField
,
...
whereValues
);
}
else
{
let
whereField
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(
'
+
tbl
+
'
.
'
+
value
.
where
.
field
+
'
)
'
:
tbl
+
'
.
'
+
value
.
where
.
field
;
let
lower
=
(
value
.
where
.
type
===
'
string
'
)
?
'
LOWER(?)
'
:
'
?
'
;
if
(
Array
.
isArray
(
whereValue
))
{
let
whereString
=
'
(
'
;
let
arrayWhereValues
=
[];
for
(
let
i
=
0
;
i
<
whereValue
.
length
;
++
i
)
{
whereString
+=
whereField
+
'
'
+
value
.
where
.
relation
+
lower
;
arrayWhereValues
.
push
(
parseWhereValue
(
value
.
where
.
type
,
whereValue
[
i
]));
if
(
i
<
whereValue
.
length
-
1
)
{
whereString
+=
'
OR
'
;
}
}
whereString
+=
'
)
'
;
sql
.
where
(
whereString
,
...
arrayWhereValues
);
}
else
{
sql
.
where
(
whereField
+
'
'
+
value
.
where
.
relation
+
lower
,
parseWhereValue
(
value
.
where
.
type
,
whereValue
));
}
}
}
});
}
});
});
return
sql
;
}
build
()
{
return
(
req
,
res
,
next
)
=>
{
req
.
sql
=
this
.
buildQuery
(
req
,
req
.
sql
);
next
();
};
}
multibuild
()
{
return
(
req
,
res
,
next
)
=>
{
req
.
querySet
.
forEach
((
query
)
=>
{
query
=
this
.
buildQuery
(
req
,
query
);
});
next
();
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment