Prev | Current Page 270 | Next

L. McColl-Sylvester and F. Ponticelli

"Professional haXe and Neko"

Note how the and and or
constructors are recursive, as they use Condition in their parameters. The following two enumerations
represent the operators used when comparing a field with a value.
What is needed now is a utility class to manipulate your enum s.
class QueryTools
{
static public function toSql(q : Condition) : String
{
return switch(q)
{
case TestIsNull(field):
field + ??? IS NULL???;
case TestNumber(field, operator, value):
field + ??? ??? + getNumericOp(operator) + ??? ??? + value;
case TestText(field, operator, value):
field + ??? ??? + getTextOp(operator) + ??? ??????
+ StringTools.replace(value, ?????™???, ???\??™???) + ?????™???;
case And(tests):
join(tests, ???AND???);
case Or(tests):
join(tests, ???OR???);
}
}
137
Chapter 5: Delving Into Object-Oriented Programming
private static function join(tests : Array < Condition > , op : String) : String
{
return ???(??? + Lambda.map(tests, toSql).join(??? ??? + op + ??? ???) + ???)???;
}
private static function getNumericOp(op : NumericOp) : String
{
return switch(op)
{
case Equal: ???=???;
case Different: ???!=???;
case MoreThan: ??? > ???;
case LessThan: ??? < ???;
}
}
private static function getTextOp(op : TextOp) : String
{
return switch(op)
{
case Same: ???=???;
case Like: ???LIKE???;
}
}
}
The syntax to extract the parameters from a constructor in a switch statement is the following:
switch(name)
{
case Constructor(p1, p2): /* do something here */;
}
The param has no type specified because the type is inherited directly from the constructor definition.


Pages:
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282