I have a function to insert some values into a table, but before inserting I want to check if e-mail address it's right. If not, break the function and returns an error. Case true, go on.
case when _email ~ '^[^#\s]+#[^#\s]+(\.[^#\s]+)+$' = true
then raise exception 'Incorrect email'
_email is the parameter of funcion.
But it's not working. Should I use "IF" or other conditional?
CASE works, but IF seems more appropriate.
You have some pointless noise in the expression and I think you got the logic backwards: 'Incorrect email' should be triggered if _email does not match the pattern:
IF _email ~ '^[^#\s]+#[^#\s]+(\.[^#\s]+)+$' -- drop the pointless "= true"
THEN -- do nothing - I inverted the logic
ELSE RAISE EXCEPTION 'Incorrect email';
END IF;
The new ASSERT (Postgres 9.5+) would also work, but that's really meant for debugging:
ASSERT _email ~ '^[^#\s]+#[^#\s]+(\.[^#\s]+)+$', 'Incorrect email';
You should be able to use the case inside plpgsql. Obviously what you are trying to do can be done through if statement as well...
case when _email ~ '^[^#\s]+#[^#\s]+(\.[^#\s]+)+$' = true then
raise exception 'Incorrect email';
else
--proceed with insert
end case;
Related
I have a TextBox where the user can write "Like pattern" to see if a string is matching. However if the user doesn't close the "[" the like operator will throw an error, that the argument is invalid for "pattern".
So I wrote something like this:
Private Function ValidateLikeString() As Boolean
Try
Dim valid As Boolean = DummyText Like TextBoxPrefix.Text
Return True
Catch ex As Exception
Return False
End Try
End Function
The problem is the following: if DummyText is 123 and TextBoxPrefix.Text is [1, it will work just fine.
However if TextBoxPrefix.Text is 2[1, it won't throw the exception because the first character won't match so the like operator will return false sooner than the exception is thrown. Of course if the DummyText will be 232 the exception will throw.
What I want to achieve is check TextBoxPrefix.Text against all cases if it's valid for pattern. I haven't found another case where the pattern is invalid only if the [ isn't closed, but if there are other problematic strings I want a test on those too.
I am trying to comment a particulate statement.
My first approach is to return a comment in case statement is an 'Expression Statement' and expression is a particular 'Method Call Expression'.
new ModifierVisitor<Object>() {
public Visitable visit(ExpressionStmt expStmt, Object arg) {
Expression exp = expStmt.getExpression();
if (exp.isMethodCallExpr()) {
// My other logic goes here
return new LineComment(expStmt.toString());
}
}
}
But it failed while dumping the unit back to string.
java.lang.ClassCastException: com.github.javaparser.ast.comments.LineComment cannot be cast to com.github.javaparser.ast.stmt.Statement
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1329)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.stmt.BlockStmt.accept(BlockStmt.java:76)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:1220)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.body.MethodDeclaration.accept(MethodDeclaration.java:104)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.printMembers(DefaultPrettyPrinterVisitor.java:190)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:419)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.body.ClassOrInterfaceDeclaration.accept(ClassOrInterfaceDeclaration.java:98)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:325)
at com.github.javaparser.printer.DefaultPrettyPrinterVisitor.visit(DefaultPrettyPrinterVisitor.java:163)
at com.github.javaparser.ast.CompilationUnit.accept(CompilationUnit.java:133)
at com.github.javaparser.printer.DefaultPrettyPrinter.print(DefaultPrettyPrinter.java:104)
at com.github.javaparser.ast.Node.toString(Node.java:320)
As it suggests that you can replace a 'Statement' with another statement so instead I tried another approach to replace the statement and with an 'Empty Statement'. It kind of worked for me but the output does not look good as it leaves extra ';' after the commented line.
At third I tried to go deeper and instead of replacing the statement I tried to replace the expression with an comment. That too failed as mentioned in SO - Javaparser comment expression.
Any idea how to fix this ?
I tried a workaround which does not feel like a good solution, but gives me the expected result for now:
BlockStmt blockStmt = (BlockStmt) expStmt.getParentNode().get();
blockStmt.getStatement(blockStmt.getStatements().indexOf(expStmt) + 1).setLineComment(expStmt.toString());
return null;
I'm new to PHP so this will sound basic to most people but I need to write the code for when a variable returns nothing (blank).
My variable is $winner, but sometimes there is no winner, in this case it just leaves the page blank, I would like it so if there is no winner then it will display "no winner".
This is my attempt:
if empty($winner) {
echo "no winner";
}
You can make a function to check the variable's valaue with null or empty...
function IsEmptyString($Season){
return (!isset($Season) || trim($Season)==='');
}
This function can be used to check the same.
Just use:
if (!$winner) { // will catch "", null
echo "no winner"
}
I am trying to implement functionality wherein an attribute, once set, cannot be changed on an ActiveRecord model. To this end, I have written the following methods:
def address
self[:address]
end
def address=(val)
if new_record?
self[:address] = val
else
errors.add(:address, "Cannot change address, once it is set")
return false # tried return nil here first, did not work
end
end
Am I doing something wrong here? I want the object to be invalid once I try to change an address, but I do not get any errors when I do obj.valid?
EDIT: The value is not changed once it is set, but I would like to get invalid object when I do the validation via obj.valid?
When you do obj.valid?, it clears all of your errors, and then runs each of the validations in turn. To have this produce an error on validation, you'll have to move that logic into a validation block.
Here's an example of one way to do that with an instance variable:
def address=(val)
if new_record?
self[:address] = val
else
#addr_change = true
return false # tried return nil here first, did not work
end
end
validate do |user|
errors.add(:address, "Cannot change address, once it is set") if #addr_change
end
Hi is there away to detect the length of a byte before I get the error message:
Length cannot be less than zero. Parameter name: length
I get the error on this line:
new_username = new_username.Substring(0, new_username.IndexOf(" Joined "))
I am removing the "joined" from the string I get....how can I ignore it is "joined" isnt the the data?
Thanks
I would test to see what IndexOf returned before using it in this context:
if(new_username.IndexOf(" Joined") > 0)
{
new_username = new_username.Substring(0, new_username.IndexOf(" Joined "))
}
Try this:
new_username = new_Username.Replace(" Joined ", "")
Be warned that this will remove all occurrences of the "Joined" substring rather than just the first.
It looks like new_username.IndexOf(" Joined ") is returning -1 meaning the string " Joined" was not found by Substring. I would break this out into two statements:
The error you are seeing is that you are effectively making this call:
new_username = new_username.Substring(0, -1)