Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
yotta
/
pictogram
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
60
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
bae67b3b
authored
Mar 16, 2017
by
Arturo Montejo Ráez
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
node_modules_overwrite added
parent
efad6d91
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
249 additions
and
0 deletions
sails/src/node_modules_overwrite/README.md
sails/src/node_modules_overwrite/sails-mysql/lib/sql.js
sails/src/node_modules_overwrite/sails-mysql/lib/utils.js
sails/src/node_modules_overwrite/README.md
0 → 100644
View file @
bae67b3b
Files contained here are intented to overwrite dependencies code, because of a enhacement or a bug not included in official distributions and done by us.
sails/src/node_modules_overwrite/sails-mysql/lib/sql.js
0 → 100644
View file @
bae67b3b
This diff is collapsed.
Click to expand it.
sails/src/node_modules_overwrite/sails-mysql/lib/utils.js
0 → 100644
View file @
bae67b3b
/**
* Utility Functions
*/
// Dependencies
var
mysql
=
require
(
'mysql'
);
var
_
=
require
(
'lodash'
);
var
url
=
require
(
'url'
);
// Module Exports
var
utils
=
module
.
exports
=
{};
/**
* Parse URL string from config
*
* Parse URL string into connection config parameters
*/
utils
.
parseUrl
=
function
(
config
)
{
if
(
!
_
.
isString
(
config
.
url
))
{
return
config
;
}
var
obj
=
url
.
parse
(
config
.
url
);
config
.
host
=
obj
.
hostname
||
config
.
host
;
config
.
port
=
obj
.
port
||
config
.
port
;
if
(
_
.
isString
(
obj
.
pathname
))
{
config
.
database
=
obj
.
pathname
.
split
(
'/'
)[
1
]
||
config
.
database
;
}
if
(
_
.
isString
(
obj
.
auth
))
{
config
.
user
=
obj
.
auth
.
split
(
':'
)[
0
]
||
config
.
user
;
config
.
password
=
obj
.
auth
.
split
(
':'
)[
1
]
||
config
.
password
;
}
return
config
;
};
/**
* Prepare values
*
* Transform a JS date to SQL date and functions
* to strings.
*/
utils
.
prepareValue
=
function
(
value
)
{
if
(
_
.
isUndefined
(
value
)
||
value
===
null
)
{
return
value
;
}
// Cast functions to strings
if
(
_
.
isFunction
(
value
))
{
value
=
value
.
toString
();
}
// Store Arrays and Objects as strings
if
(
_
.
isArray
(
value
)
||
value
.
constructor
&&
value
.
constructor
.
name
===
'Object'
)
{
try
{
value
=
JSON
.
stringify
(
value
);
}
catch
(
e
)
{
// just keep the value and let the db handle an error
value
=
value
;
}
}
// Cast dates to SQL
if
(
_
.
isDate
(
value
))
{
value
=
utils
.
toSqlDate
(
value
);
}
return
mysql
.
escape
(
value
);
};
/**
* Builds a Select statement determining if Aggeregate options are needed.
*/
utils
.
buildSelectStatement
=
function
(
criteria
,
table
,
schemaDefs
)
{
var
query
=
''
;
if
(
criteria
.
groupBy
||
criteria
.
sum
||
criteria
.
average
||
criteria
.
min
||
criteria
.
max
)
{
query
=
'SELECT '
;
// Append groupBy columns to select statement
if
(
criteria
.
groupBy
)
{
if
(
_
.
isArray
(
criteria
.
groupBy
))
{
_
.
each
(
criteria
.
groupBy
,
function
(
opt
){
query
+=
opt
+
', '
;
});
}
else
{
query
+=
criteria
.
groupBy
+
', '
;
}
}
// Handle SUM
if
(
criteria
.
sum
)
{
if
(
_
.
isArray
(
criteria
.
sum
))
{
_
.
each
(
criteria
.
sum
,
function
(
opt
){
query
+=
'SUM('
+
opt
+
') AS '
+
opt
+
', '
;
});
}
else
{
query
+=
'SUM('
+
criteria
.
sum
+
') AS '
+
criteria
.
sum
+
', '
;
}
}
// Handle AVG (casting to float to fix percision with trailing zeros)
if
(
criteria
.
average
)
{
if
(
_
.
isArray
(
criteria
.
average
))
{
_
.
each
(
criteria
.
average
,
function
(
opt
){
query
+=
'AVG('
+
opt
+
') AS '
+
opt
+
', '
;
});
}
else
{
query
+=
'AVG('
+
criteria
.
average
+
') AS '
+
criteria
.
average
+
', '
;
}
}
// Handle MAX
if
(
criteria
.
max
)
{
if
(
_
.
isArray
(
criteria
.
max
))
{
_
.
each
(
criteria
.
max
,
function
(
opt
){
query
+=
'MAX('
+
opt
+
') AS '
+
opt
+
', '
;
});
}
else
{
query
+=
'MAX('
+
criteria
.
max
+
') AS '
+
criteria
.
max
+
', '
;
}
}
// Handle MIN
if
(
criteria
.
min
)
{
if
(
_
.
isArray
(
criteria
.
min
))
{
_
.
each
(
criteria
.
min
,
function
(
opt
){
query
+=
'MIN('
+
opt
+
') AS '
+
opt
+
', '
;
});
}
else
{
query
+=
'MIN('
+
criteria
.
min
+
') AS '
+
criteria
.
min
+
', '
;
}
}
// trim trailing comma
query
=
query
.
slice
(
0
,
-
2
)
+
' '
;
// Add FROM clause
return
query
+=
'FROM `'
+
table
+
'` '
;
}
/**
* If no aggregate options lets just build a normal query
*/
// Add all keys to the select statement for this table
query
+=
'SELECT '
;
var
selectKeys
=
[],
joinSelectKeys
=
[];
if
(
!
schemaDefs
[
table
]
)
{
throw
new
Error
(
'Schema definition missing for table: `'
+
table
+
'`'
);
}
_
.
each
(
schemaDefs
[
table
],
function
(
schemaDef
,
key
)
{
selectKeys
.
push
({
table
:
table
,
key
:
key
});
});
// Check for joins
if
(
criteria
.
joins
||
criteria
.
join
)
{
var
joins
=
criteria
.
joins
||
criteria
.
join
;
_
.
each
(
joins
,
function
(
join
)
{
if
(
!
join
.
select
)
{
return
;
}
_
.
each
(
_
.
keys
(
schemaDefs
[
join
.
child
.
toLowerCase
()]),
function
(
key
)
{
var
_join
=
_
.
cloneDeep
(
join
);
_join
.
key
=
key
;
joinSelectKeys
.
push
(
_join
);
});
// Remove the foreign key for this join from the selectKeys array
selectKeys
=
selectKeys
.
filter
(
function
(
select
)
{
var
keep
=
true
;
if
(
select
.
key
===
join
.
parentKey
&&
join
.
removeParentKey
)
{
keep
=
false
;
}
return
keep
;
});
});
}
// Add all the columns to be selected that are not joins
_
.
each
(
selectKeys
,
function
(
select
)
{
query
+=
'`'
+
select
.
table
+
'`.`'
+
select
.
key
+
'`, '
;
});
// Add all the columns from the joined tables
_
.
each
(
joinSelectKeys
,
function
(
select
)
{
// Create an alias by prepending the child table with the alias of the join
var
alias
=
select
.
alias
.
toLowerCase
()
+
'_'
+
select
.
child
.
toLowerCase
();
// If this is a belongs_to relationship, keep the foreign key name from the AS part
// of the query. This will result in a selected column like: "user"."id" AS "user_id__id"
if
(
select
.
model
)
{
return
query
+=
mysql
.
escapeId
(
alias
)
+
'.'
+
mysql
.
escapeId
(
select
.
key
)
+
' AS '
+
mysql
.
escapeId
(
select
.
parentKey
+
'__'
+
select
.
key
)
+
', '
;
}
// If a junctionTable is used, the child value should be used in the AS part of the
// select query.
if
(
select
.
junctionTable
)
{
return
query
+=
mysql
.
escapeId
(
alias
)
+
'.'
+
mysql
.
escapeId
(
select
.
key
)
+
' AS '
+
mysql
.
escapeId
(
select
.
alias
+
'__'
+
select
.
key
)
+
', '
;
}
// Else if a hasMany attribute is being selected, use the alias plus the child
return
query
+=
mysql
.
escapeId
(
alias
)
+
'.'
+
mysql
.
escapeId
(
select
.
key
)
+
' AS '
+
mysql
.
escapeId
(
select
.
alias
+
'__'
+
select
.
key
)
+
', '
;
});
// Remove the last comma
query
=
query
.
slice
(
0
,
-
2
)
+
' FROM `'
+
table
+
'` '
;
return
query
;
};
utils
.
toSqlDate
=
function
toSqlDate
(
date
)
{
date
=
date
.
getFullYear
()
+
'-'
+
(
'00'
+
(
date
.
getMonth
()
+
1
)).
slice
(
-
2
)
+
'-'
+
(
'00'
+
date
.
getDate
()).
slice
(
-
2
)
+
' '
+
(
'00'
+
date
.
getHours
()).
slice
(
-
2
)
+
':'
+
(
'00'
+
date
.
getMinutes
()).
slice
(
-
2
)
+
':'
+
(
'00'
+
date
.
getSeconds
()).
slice
(
-
2
);
return
date
;
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment