Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
basic
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor 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
xrm
basic
Commits
d1df9174
Commit
d1df9174
authored
5 years ago
by
Johannes Hörmann
Browse files
Options
Downloads
Patches
Plain Diff
fix some sql generation of sql builder, add "or"
parent
98441ff0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js
+267
-123
267 additions, 123 deletions
.../campaignactiongroup/children/sqltests/onActionProcess.js
process/Sql_lib/process.js
+138
-34
138 additions, 34 deletions
process/Sql_lib/process.js
with
405 additions
and
157 deletions
entity/Person_entity/entityfields/campaignactiongroup/children/sqltests/onActionProcess.js
+
267
−
123
View file @
d1df9174
This diff is collapsed.
Click to expand it.
process/Sql_lib/process.js
+
138
−
34
View file @
d1df9174
...
...
@@ -661,7 +661,8 @@ function SqlBuilder (pAlias)
_sqlStorage
:
""
,
// save, if the last condition was an OR. For better bracket-placement
_lastWasOr
:
false
_lastWasOr
:
false
,
_previouslyOnlyOr
:
false
}
}
...
...
@@ -844,8 +845,7 @@ SqlBuilder.prototype.where = function(pCondition)
return
this
;
}
SqlBuilder
.
prototype
.
andCondition
=
function
(
pCondition
,
pMandatory
)
SqlBuilder
.
prototype
.
_whereCondition
=
function
(
pCondition
,
pMandatory
,
pAddPreparedConditionCallback
,
pBrackets
)
{
if
(
pCondition
===
undefined
)
return
this
;
...
...
@@ -859,10 +859,8 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
// the field is a simple string -> just add the string, no prepared statement
if
(
typeofSql
==
"
string
"
)
{
if
(
this
.
hasCondition
())
this
.
_where
.
_sqlStorage
+=
"
and
"
;
this
.
_where
.
_sqlStorage
+=
sql
;
pAddPreparedConditionCallback
([
sql
,
[]])
return
this
;
}
...
...
@@ -870,8 +868,12 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
if
(
Array
.
isArray
(
sql
))
{
this
.
_where
.
preparedValues
=
this
.
_where
.
preparedValues
.
concat
(
sql
[
1
]);
return
this
.
andCondition
(
"
(
"
+
sql
[
0
]
+
"
)
"
);
// add only brackets if needed
if
(
pBrackets
)
sql
[
0
]
=
"
(
"
+
sql
[
0
]
+
"
)
"
;
pAddPreparedConditionCallback
([
sql
[
0
],
[]],
pBrackets
)
return
this
;
}
...
...
@@ -879,14 +881,15 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
// TODO: maybe call add(null, sql, pMandatory, pCond, ...) if sql.isFullSelect() and pCond is proviede. Else use only select and throw error -> better: only one possible way and a clear solution if someone does it wrong less implict
if
(
sql
instanceof
SqlBuilder
)
{
var
condString
=
"
(
"
+
sql
.
_where
.
_sqlStorage
+
"
)
"
;
// add only brackets if needed
var
sqlString
=
sql
.
_where
.
_sqlStorage
;
if
(
pBrackets
)
sqlString
=
"
(
"
+
sqlString
+
"
)
"
;
var
condString
=
sqlString
;
if
(
condString
.
trim
()
!=
""
)
{
this
.
andCondition
(
condString
);
if
(
sql
.
_where
.
preparedValues
)
{
this
.
_where
.
preparedValues
=
this
.
_where
.
preparedValues
.
concat
(
sql
.
_where
.
preparedValues
);
}
pAddPreparedConditionCallback
([
condString
,
sql
.
_where
.
preparedValues
],
pBrackets
);
}
return
this
;
}
...
...
@@ -894,7 +897,7 @@ SqlBuilder.prototype.andCondition = function(pCondition, pMandatory)
throw
SqlBuilder
.
ERROR_INVALID_CONDITION_VALUE_TYPE
();
}
SqlBuilder
.
prototype
.
and
Subquery
=
function
(
pSubquery
,
pMandatory
,
pCond
)
SqlBuilder
.
prototype
.
_where
Subquery
=
function
(
pSubquery
,
pMandatory
,
pCond
ition
,
pAddPreparedConditionCallback
)
{
if
(
pSubquery
===
undefined
)
return
this
;
...
...
@@ -906,11 +909,11 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
var
typeofSql
=
typeof
sql
;
// the field is a simple string -> just add the string, no prepared statement
// the field is an array -> it is a prepared condition
// the field is an array -> it is a prepared
statement which already SHOULD contain exists or another
condition
// Both can be handled by _prepare
if
(
Array
.
isArray
(
sql
)
||
typeofSql
==
"
string
"
)
{
this
.
an
dCondition
(
this
.
_prepare
(
undefined
,
sql
,
pCond
));
pAddPrepare
dCondition
Callback
(
this
.
_prepare
(
undefined
,
sql
,
pCond
ition
));
return
this
;
}
...
...
@@ -919,8 +922,8 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
{
var
subQuery
=
pSubquery
;
// Without condition this function cannot be used
if
(
!
pCond
)
// Without condition this function cannot be used
with SqlBuilder object as it cannot contain a condition
if
(
!
pCond
ition
)
{
throw
SqlBuilder
.
ERROR_NO_CONDITION
();
}
...
...
@@ -928,7 +931,7 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
if
(
subQuery
.
isFullSelect
())
{
var
preparedObj
=
subQuery
.
build
();
this
.
an
dCondition
(
this
.
_prepare
(
undefined
,
preparedObj
,
pCond
));
pAddPrepare
dCondition
Callback
(
this
.
_prepare
(
undefined
,
preparedObj
,
pCond
ition
));
}
else
if
(
pMandatory
)
{
...
...
@@ -955,7 +958,7 @@ SqlBuilder.prototype.andSubquery = function(pSubquery, pMandatory, pCond)
* }
* @return {SqlBuilder} current SqlBuilder object
*/
SqlBuilder
.
prototype
.
and
=
function
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
)
SqlBuilder
.
prototype
.
_addWhere
=
function
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
,
pAddPreparedConditionCallback
)
{
// For now: treat "" as null just like db.insert, db.table etc. Maybe otion for later: more pMandatory-types
if
(
pValue
===
""
)
...
...
@@ -964,34 +967,48 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
if
(
pMandatory
===
undefined
)
pMandatory
=
true
;
//
// just call the andCondition function if it is only a Condition
//
if (!pFieldOrCond !== undefined && (pValue === undefined) && pCond === undefined && pFieldType === undefined)
//
{
//
return this.
and
Condition(pFieldOrCond, pMandatory);
//
}
// just call the andCondition function if it is only a Condition
if
(
!
pFieldOrCond
!==
undefined
&&
(
pValue
===
undefined
)
&&
pCond
===
undefined
&&
pFieldType
===
undefined
)
{
return
this
.
_where
Condition
(
pFieldOrCond
,
pMandatory
,
pAddPreparedConditionCallback
,
true
);
}
// first check the default-mandatory-cases: null or undefined. everything else such as ch
3
ecking $-variables is done later
// first check the default-mandatory-cases: null or undefined. everything else such as checking $-variables is done later
if
(
pMandatory
&&
!
(
pValue
===
false
||
pValue
))
throw
SqlBuilder
.
ERROR_VALUE_IS_MANDATORY
();
// a field and a value is given OR pFieldOrSQL == null and pValue instanceof SqlBuilder and pCond is given -> preparedSQL
if
((
typeof
pFieldOrCond
==
"
string
"
||
Array
.
isArray
(
pFieldOrCond
))
||
(
!
pFieldOrCond
&&
pCond
))
// a field is string or array -> normal case
// OR !pFieldOrCond and pValue and pCond is given -> preparedSQL/SqlBuilder can be used without field if pCond is set
if
((
typeof
pFieldOrCond
==
"
string
"
||
Array
.
isArray
(
pFieldOrCond
))
||
(
!
pFieldOrCond
&&
(
pCond
&&
pValue
instanceof
SqlBuilder
||
!
(
pValue
instanceof
SqlBuilder
))))
{
var
field
=
pFieldOrCond
;
var
typeofValue
=
typeof
pValue
;
// pValue can be...
// ... a SqlBuilder -> it is a SqlBuilder containing a complete subquery
if
(
pValue
instanceof
SqlBuilder
||
Array
.
isArray
(
pValue
)
||
(
typeofValue
==
"
string
"
&&
(
pFieldOrCond
==
undefined
||
pFieldOrCond
==
null
)))
{
if
(
pFieldOrCond
!==
null
&&
pFieldOrCond
!==
undefined
)
{
if
(
!
pCond
)
pCond
=
"
# = ?
"
pCond
=
SqlUtils
.
replaceConditionTemplate
(
pCond
,
'
#
'
,
SqlUtils
.
parseField
(
pFieldOrCond
)[
0
])
}
return
this
.
andSubquery
(
pValue
,
pMandatory
,
pCond
)
else
{
if
(
!
pCond
)
pCond
=
"
?
"
}
return
this
.
_whereSubquery
(
pValue
,
pMandatory
,
pCond
,
pAddPreparedConditionCallback
);
}
if
(
!
pCond
)
pCond
=
"
# = ?
"
// ... a string starting with $ -> jdito varable which has to be resolved
if
(
typeofValue
==
"
string
"
&&
pValue
[
0
]
==
"
$
"
)
{
...
...
@@ -1004,7 +1021,7 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
if
(
pValue
)
{
this
.
and
Condition
(
this
.
_prepare
(
field
,
pValue
,
pCond
,
pFieldType
));
this
.
_where
Condition
(
this
.
_prepare
(
field
,
pValue
,
pCond
,
pFieldType
)
,
undefined
,
pAddPreparedConditionCallback
);
}
return
this
;
}
...
...
@@ -1017,7 +1034,7 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
// ... everything else -> just pass it
if
(
pValue
===
false
||
pValue
)
this
.
and
Condition
(
this
.
_prepare
(
field
,
pValue
,
pCond
,
pFieldType
));
this
.
_where
Condition
(
this
.
_prepare
(
field
,
pValue
,
pCond
,
pFieldType
)
,
undefined
,
pAddPreparedConditionCallback
);
return
this
;
}
...
...
@@ -1025,6 +1042,93 @@ SqlBuilder.prototype.and = function(pFieldOrCond, pValue, pMandatory, pCond, pFi
throw
SqlBuilder
.
ERROR_UNSUPPORTED_PARAMETER_COMBINATION
();
}
SqlBuilder
.
prototype
.
_and
=
function
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
)
{
var
that
=
this
;
return
this
.
_addWhere
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
,
function
(
pPreparedCondition
)
{
that
.
_where
.
_previouslyOnlyOr
=
false
;
if
(
pPreparedCondition
.
length
==
2
&&
typeof
pPreparedCondition
[
0
]
==
"
string
"
&&
pPreparedCondition
[
0
]
!=
""
&&
Array
.
isArray
(
pPreparedCondition
[
1
]))
{
if
(
that
.
hasCondition
())
that
.
_where
.
_sqlStorage
+=
"
and
"
;
that
.
_where
.
_sqlStorage
+=
pPreparedCondition
[
0
];
that
.
_where
.
preparedValues
=
that
.
_where
.
preparedValues
.
concat
(
pPreparedCondition
[
1
]);
}
})
}
SqlBuilder
.
prototype
.
_or
=
function
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
)
{
var
that
=
this
;
return
this
.
_addWhere
(
pFieldOrCond
,
pValue
,
pMandatory
,
pCond
,
pFieldType
,
function
(
pPreparedCondition
,
pAlreadySurroundedByBrackets
)
{
if
(
pPreparedCondition
.
length
==
2
&&
typeof
pPreparedCondition
[
0
]
==
"
string
"
&&
pPreparedCondition
[
0
]
!=
""
&&
Array
.
isArray
(
pPreparedCondition
[
1
]))
{
if
(
that
.
_where
.
_previouslyOnlyOr
)
{
that
.
_where
.
_sqlStorage
=
that
.
_where
.
_sqlStorage
+
"
or
"
+
pPreparedCondition
[
0
];
that
.
_where
.
_lastWasOr
=
true
;
}
else
if
(
that
.
hasCondition
()
&&
!
that
.
_where
.
_lastWasOr
)
{
var
cond
=
pPreparedCondition
[
0
];
if
(
!
pAlreadySurroundedByBrackets
)
cond
=
"
(
"
+
pPreparedCondition
[
0
]
+
"
)
"
;
that
.
_where
.
_sqlStorage
=
"
(
"
+
that
.
_where
.
_sqlStorage
+
"
) or
"
+
cond
;
that
.
_where
.
_lastWasOr
=
true
;
}
else
if
(
that
.
hasCondition
()
&&
that
.
_where
.
_lastWasOr
)
{
var
cond
=
pPreparedCondition
[
0
];
if
(
!
pAlreadySurroundedByBrackets
)
cond
=
"
(
"
+
pPreparedCondition
[
0
]
+
"
)
"
;
that
.
_where
.
_sqlStorage
=
that
.
_where
.
_sqlStorage
+
"
or
"
+
cond
;
that
.
_where
.
_lastWasOr
=
true
;
}
else
{
if
(
!
that
.
hasCondition
())
that
.
_where
.
_previouslyOnlyOr
=
true
;
that
.
_where
.
_sqlStorage
=
pPreparedCondition
[
0
];
}
that
.
_where
.
preparedValues
=
that
.
_where
.
preparedValues
.
concat
(
pPreparedCondition
[
1
]);
}
})
}
SqlBuilder
.
prototype
.
or
=
function
(
pFieldOrCond
,
pValue
,
pCond
,
pFieldType
)
{
return
this
.
_or
(
pFieldOrCond
,
pValue
,
true
,
pCond
,
pFieldType
);
}
SqlBuilder
.
prototype
.
orIfSet
=
function
(
pFieldOrCond
,
pValue
,
pCond
,
pFieldType
)
{
return
this
.
_or
(
pFieldOrCond
,
pValue
,
false
,
pCond
,
pFieldType
);
}
SqlBuilder
.
prototype
.
and
=
function
(
pFieldOrCond
,
pValue
,
pCond
,
pFieldType
)
{
return
this
.
_and
(
pFieldOrCond
,
pValue
,
true
,
pCond
,
pFieldType
);
}
SqlBuilder
.
prototype
.
andIfSet
=
function
(
pFieldOrCond
,
pValue
,
pCond
,
pFieldType
)
{
return
this
.
_and
(
pFieldOrCond
,
pValue
,
false
,
pCond
,
pFieldType
);
}
/**
* Sets the order by clause of the sql.
* @param {String} pOrderBy
...
...
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