Skip to content

Commit

Permalink
refs #3545 added missing file, updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnich committed Sep 27, 2019
1 parent 8c83c20 commit 93444d0
Show file tree
Hide file tree
Showing 20 changed files with 774 additions and 117 deletions.
4 changes: 2 additions & 2 deletions examples/test/qlib/SqlUtil/SqlUtilTestBase.qm
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public class SqlTestBase inherits QUnit::Test {
assertEq(Type::Int, row.id.type());
}

assertThrows("RUNTIME-TYPE-ERROR", \dp.searchSingleRecord(), {"id": "abc"});
assertThrows("RUNTIME-TYPE-ERROR", \dp.searchSingleRecord(), {"id": <00>});

# get first string column
string fname;
Expand All @@ -182,7 +182,7 @@ public class SqlTestBase inherits QUnit::Test {

AbstractSavepointHelper sph = table.getSavepointHelper();
on_exit sph.rollback();
assertThrows("RUNTIME-TYPE-ERROR", \dp.updateSingleRecord(), ({"id": "abc"}, {"id": 1}));
assertThrows("RUNTIME-TYPE-ERROR", \dp.updateSingleRecord(), ({"id": <00>}, {"id": 1}));
assertThrows("RUNTIME-TYPE-ERROR", \dp.updateSingleRecord(), ({"id": NULL}, {"id": 1}));
}
}
Expand Down
116 changes: 116 additions & 0 deletions qlib/DataProvider/QoreDateDataTypeBase.qc
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# -*- mode: qore; indent-tabs-mode: nil -*-
#! Qore QoreDateDataTypeBase class definition

/** QoreDateDataTypeBase.qc Copyright 2019 Qore Technologies, s.r.o.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

# assume local scope for variables, do not use "$" signs
%new-style
# require type definitions everywhere
%require-types
#! strict argument handling
%strict-args
# enable all warnings
%enable-all-warnings

%requires reflection

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on a date type with validation for string parsing and time zone support
/** @note %Qore "soft" types that do not accept @ref nothing also accept @ref null; to ensure that soft types
inheriting this class do not accept @ref null, use the \c qore.no_null option in the constructor
*/
public class QoreDateDataTypeBase inherits QoreDataType {
private {
#! supported options
const SupportedOptions = {
"date.output_timezone": <DataProviderTypeOptionInfo>{
"type": "object<TimeZone>",
"desc": "the time zone to use when writing to the type",
},
};

#! supported soft options
const SupportedSoftOptions = {
"date.format": <DataProviderTypeOptionInfo>{
"type": Type::String,
"desc": "the format string for converting strings to dates",
},
"date.input_timezone": <DataProviderTypeOptionInfo>{
"type": "object<TimeZone>",
"desc": "the time zone for external data when writing to the type and converting strings to dates",
},
};
}

#! creates the object
/** @throw TYPE-ERROR invalid type; must be a date type
*/
private constructor(Type type, *hash<auto> options) : QoreDataType(type, options) {
if (type.getBaseTypeCode() != NT_DATE) {
throw "TYPE-ERROR", sprintf("cannot create a %s object from type %y", self.className(),
type.getName());
}
# translations are performed on date types if there is an output_timezone
if (options.output_timezone) {
remove base_type_hash{NT_DATE};
}
# set the input timezone option unconditionally
if (!options."date.input_timezone") {
self.options."date.input_timezone" = TimeZone::get();
}
}

#! returns the value if the value can be assigned to the type
/** @param value the value to assign to the type

@return the value to be assigned; can be converted by the type

@throw RUNTIME-TYPE-ERROR value cannot be assigned to type
*/
auto acceptsValue(auto value) {
if (exists value && value !== NULL) {
# try to convert from the source type
if (value.typeCode() !== NT_DATE) {
# only convert if the type can be converted
if (!soft_type || !accept_type_hash{value.type()}) {
throw "RUNTIME-TYPE-ERROR", sprintf("type %y cannot be assigned from a value of type %y",
getName(), value.type());
}
value = options."date.input_timezone".date(value, options."date.format");
}

# convert to the output TimeZone if necessary
if (options."date.output_timezone") {
value = options."date.output_timezone".date(value);
}
return value;
}
return QoreDataType::acceptsValue(value);
}

#! returns supported options
*hash<string, hash<DataProviderTypeOptionInfo>> getSupportedOptions() {
return QoreDataType::getSupportedOptions() + SupportedOptions + (soft_type ? SupportedSoftOptions : NOTHING);
}
}
}
44 changes: 44 additions & 0 deletions qlib/DataProvider/QoreFloatDataType.qc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- mode: qore; indent-tabs-mode: nil -*-
#! Qore QoreFloatDataType class definition

/** QoreFloatDataType.qc Copyright 2019 Qore Technologies, s.r.o.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

# assume local scope for variables, do not use "$" signs
%new-style
# require type definitions everywhere
%require-types
#! strict argument handling
%strict-args
# enable all warnings
%enable-all-warnings

%requires reflection

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on float
public class QoreFloatDataType inherits QoreFloatDataTypeBase {
#! creates the object
constructor(*hash<auto> options) : QoreFloatDataTypeBase(FloatType, options) {
}
}
}
86 changes: 86 additions & 0 deletions qlib/DataProvider/QoreFloatDataTypeBase.qc
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- mode: qore; indent-tabs-mode: nil -*-
#! Qore QoreFloatDataTypeBase class definition

/** QoreFloatDataTypeBase.qc Copyright 2019 Qore Technologies, s.r.o.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

# assume local scope for variables, do not use "$" signs
%new-style
# require type definitions everywhere
%require-types
#! strict argument handling
%strict-args
# enable all warnings
%enable-all-warnings

%requires reflection

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on softnumber with validation for parsing strings
public class QoreFloatDataTypeBase inherits QoreDataType {
private {
#! supported soft options
const SupportedSoftOptions = {
"number.format": <DataProviderTypeOptionInfo>{
"type": Type::String,
"desc": "the format string for converting strings to numbers",
},
};
}

#! creates the object
/** @throw TYPE-ERROR invalid type; must be a number type
*/
private constructor(Type type, *hash<auto> options) : QoreDataType(type, options) {
if (type.getBaseTypeCode() != NT_FLOAT) {
throw "TYPE-ERROR", sprintf("cannot create a %s object from type %y", self.className(),
type.getName());
}
}

#! returns the value if the value can be assigned to the type
/** @param value the value to assign to the type

@return the value to be assigned; can be converted by the type

@throw RUNTIME-TYPE-ERROR value cannot be assigned to type
*/
auto acceptsValue(auto value) {
# try to convert from a string
if (value.typeCode() == NT_STRING && soft_type) {
if (options."number.format") {
return parse_float(value, options."number.format");
}
if (value !~ /^([-+])?[0-9]+(\.[0-9]+)?+$/)
throw "RUNTIME-TYPE-ERROR", sprintf("value %y cannot be parsed by type %y; the input data "
"contains non-numeric text", value, getName());
return number(value);
}
return QoreDataType::acceptsValue(value);
}

#! returns supported options
*hash<string, hash<DataProviderTypeOptionInfo>> getSupportedOptions() {
return QoreDataType::getSupportedOptions() + (soft_type ? SupportedSoftOptions : NOTHING);
}
}
}
44 changes: 44 additions & 0 deletions qlib/DataProvider/QoreFloatOrNothingDataType.qc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- mode: qore; indent-tabs-mode: nil -*-
#! Qore QoreFloatOrNothingDataType class definition

/** QoreFloatOrNothingDataType.qc Copyright 2019 Qore Technologies, s.r.o.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

# assume local scope for variables, do not use "$" signs
%new-style
# require type definitions everywhere
%require-types
#! strict argument handling
%strict-args
# enable all warnings
%enable-all-warnings

%requires reflection

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on *float
public class QoreFloatOrNothingDataType inherits QoreFloatDataTypeBase {
#! creates the object
constructor(*hash<auto> options) : QoreFloatDataTypeBase(FloatOrNothingType, options) {
}
}
}
20 changes: 19 additions & 1 deletion qlib/DataProvider/QoreIntDataTypeBase.qc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on softint with validation for parsing strings
#! describes a data type based on int types with validation for parsing strings when used with soft types
public class QoreIntDataTypeBase inherits QoreDataType {
private {
#! supported soft options
const SupportedSoftOptions = {
"number.format": <DataProviderTypeOptionInfo>{
"type": Type::String,
"desc": "the format string for converting strings to numbers",
},
};
}

#! creates the object
/** @throw TYPE-ERROR invalid type; must be an integer type
*/
Expand All @@ -57,6 +67,9 @@ public class QoreIntDataTypeBase inherits QoreDataType {
auto acceptsValue(auto value) {
# try to convert from a string
if (value.typeCode() == NT_STRING && soft_type) {
if (options."number.format") {
return parse_int(value, options."number.format");
}
# check for valid integer values
if (value !~ /^([-+])?[0-9]+$/) {
throw "RUNTIME-TYPE-ERROR", sprintf("value %y cannot by parsed by type %y; the input data contains "
Expand All @@ -67,5 +80,10 @@ public class QoreIntDataTypeBase inherits QoreDataType {

return QoreDataType::acceptsValue(value);
}

#! returns supported options
*hash<string, hash<DataProviderTypeOptionInfo>> getSupportedOptions() {
return QoreDataType::getSupportedOptions() + (soft_type ? SupportedSoftOptions : NOTHING);
}
}
}
2 changes: 1 addition & 1 deletion qlib/DataProvider/QoreNumberDataTypeBase.qc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#! contains all public definitions in the DataProvider module
public namespace DataProvider {
#! describes a data type based on softnumber with validation for parsing strings
#! describes a data type based on number types with validation for parsing strings when used with soft types
public class QoreNumberDataTypeBase inherits QoreDataType {
private {
#! supported soft options
Expand Down
Loading

0 comments on commit 93444d0

Please sign in to comment.