Find out remaining values in enum comparing to array - Groovy - arraylist

In groovy, I wrote following code:
enum ENVIRONMENT("test1, test2, test3, test4, test5")
I have an array with values ("test1, test2, test3")
I have to compare the values in enum to values in arraylist .
I have to display all values in arraylist and if values are missing in arraylist(like test4, test5), I have to show it some color
Please help!

This can be achieved by using minus() on ArrayList:
enum Environment {
TEST1, TEST2, TEST3, TEST4, TEST5
}
List listToCompare = ['TEST1', 'TEST2', 'TEST3']
assert Environment.values()*.toString() - listToCompare == ['TEST4', 'TEST5']

Related

How to delete items from an array in Vue

I have a function called updateAnswer with multiple dynamic parameters.
updateAnswer(key, answer, array = false) {
if (array) {
if(this.answers.contains.find(element => element === answer)) {
//Delete item from array if already element already exists in this.answers.contains array.
} else {
Vue.set(this.answers, key, [...this.answers.contains, answer]);
}
} else {
Vue.set(this.answers, key, answer);
}
},
I'd like to know how delete an item in the array if the value already exists in the array.
You can use method called splice:
Just reference on your array and set values in the brackets the first is referenced on the position, the second is how many datas you want to splice/delete.
The function looks like this:
this.array.splice(value, value)
Lets see on an example - you have array food= [apple, banana, strawberry] than I'm using this.food.splice(1,1)..
my array looks now like this food = [apple, strawberry] - first value in my brackets are the position, the second one is the amount of "numbers" you want to delete.
Hopefully this helps you out!
I suppose each value in this.answers.contains is unique?
Anyways, if you just want to delete the item if already exists, I suggest filter(). It should look like below:
if(this.answers.contains.find(element => element === answer)) {
this.answers.contains = this.answers.contains.filter(c => c !== answer)
}
Also, the if condition if(this.answers.contains.find(element => element === answer)) could also be replaced by if(this.answers.contains.includes(answer))
Hope that could help you.

How to get insert statement with data as String

Perhaps I use Table.insertStatementFor wrong.
I'm using Slick 3.0.x and Scala-2.11.x
There are the following classes and objects:
case class User(id:Int, name:String)
class Users(tag: Tag) extends Table[User](tag, "users") {
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def * = (id, name) <> (User.tupled, User.unapply)
}
val table = TableQuery[Users]
I want to get insert statement string with data.
The method table.insertStatement returns
INSERT INTO users(id, name) VALUES(?,?)
I want to get the following string for User(1, "John"):
INSERT INTO users(id, name) VALUES(1, 'John');
I tried use method table.insertStatementFor(User(1, "John")), but it is not works and raised exception:
not enough arguments for method insertStatementFor: (implicit shape: scala.slick.lifted.Shape[_ <: scala.slick.lifted.FlatShapeLevel, models.User, ...
Does the solution of this task exist?

If statements with SQL and returning string values

I'm using SQL and I need to return one string if the value of a field is 5 and another string if the value of the field is 4. I have something like this right now:
SELECT * FROM tablename WHERE value1=4 OR value1=5;
In PHP for example it might be like this (but I can't use PHP for my application):
if ($value1 == 4) {
$value1 = 'free';
} elseif ($value1 == 5) {
$value1 = 'not free';
} elseif...etc.
Anyone know how to accomplish what I want with SQL only?
Then you would do something like:
select (case when value1 = 4 then 'free' else 'not free' end) as newval
from tablename
where value1 in (4, 5);

How do the relational fields work internally?

I have used all these 3 relational fields i.e many2one,one2many,many2many but still now I'm unable to know exactly how do they work internally,So please throw some light on their working .
I'll give you a simple example.
class a(osv.osv):
_name = 'a'
_columns = {
'f1': fields.many2one('b', 'Creates new column "f1" in "a" table'),
'f2': fields.one2many('b', 'xxx', 'Creates new column "xxx" in "b" table'),
'f3': fields.many2many('b', 'aaa', 'a_id', 'b_id', 'Creates new table "aaa" with column "a_id" related to "a" object and column "b_id" related to "b" object'),
}
a()
class b(osv.osv):
_name = 'b'
b()
You can always look on your database and compare it with code if my example wasn't clear.

How to add whole array to one field in database

I have tried everything, array_push, multidimensional array and so on and nothing worked for me.
Following situation:
try {
if (isset($_SESSION['list']) > 0) {
foreach($_SESSION['list'] as $id=> $quantity) {
$sQuery = "SELECT * FROM table WHERE id = '".$id."' ";
$oStmt = $db->prepare($sQuery);
$oStmt->execute();
while($aRow = $oStmt->fetch(PDO::FETCH_ASSOC)) {
$id = $aRow['id'];
$name = $aRow['name'];
$volume = $aRow['volume'];
}
$testar = array(array('name' => $name, 'volume' => $volume, 'quantity' => $quantity));
$sQuery = "INSERT INTO table (array_data, date) VALUES ('$testar', NOW())";
$oStmt = $db->prepare($sQuery);
$oStmt->execute();
print_r($testar);
}
}
else {
echo 'Nothing to add';
}
}
catch(PDOException $e) {
$sMsg = '<p>
Regelnummer: '.$e->getLine().'<br />
Bestand: '.$e->getFile().'<br />
Foutmelding: '.$e->getMessage().'
</p>';
trigger_error($sMsg);
}
when I print_r($testar); I get this:
Array ( [0] => Array ( [name] => test 1 [volume] => 1.50 [quantity] => 4 ) ) Array ( [0] => Array ( [name] => test 2 [volume] => 2.50 [quantity] => 5 ) ) Array ( [0] => Array ( [name] => test 3 [volume] => 2.50 [quantity] => 2 ) )
but when I add it to database I only see: ARRAY.
How is that possible?
What I want is to add the whole Array to one field in database. Is that possible and how can I arrange that?
Usually, a column in an SQL database should have only one value, not an array. If you have multiple values, you should values individually, either as separate columns if they are a set of totally different types of data, or else as a single column on multiple rows of a dependent table if the array is multiple values of the same type of data.
This rule comes from First Normal Form.
But if you really need to store a PHP array in one row, you can convert the PHP array into a string with PHP's serialize() function. This is better than implode() because serialize() preserves hash keys, arrays of arrays, etc.
$testar = array(array('name' => $name, 'volume' => $volume, 'quantity' => $quantity));
$testar_serialized = serialize($testar);
$sQuery = "INSERT INTO table (array_data, date) VALUES (?, NOW())";
$oStmt = $db->prepare($sQuery);
$oStmt->execute( array($testar_serialized) );
I would start looking at the implode function.
Than you can add all the elements in the array to the field in the database, represented as a string.
A few things:
You don't insert an array into an SQL table; you insert the values of it.
Your SQL table should have a "column" for each field (e.g. name, volume, quantity), and will get a "row" for each entry in the outer array.
You should not have a variable reference in your SQL. This is called an “SQL injection vector,” and (due to accident or malice) will eventually wreak havoc upon you.
What you should do where your INSERT is, is:
prepare your SQL statement once:
INSERT INTO table (name, volume, quantity) VALUES (?,?,?);
The ? mark where the SQL drivers will place your values, which you specify later.
This tells the database to get ready to accept (1 or more) inserts in this form, and
shows the database driver (PDO, mysqli, …) where to put the values when you later
call execute
For each row in the array:
call execute with the list of values, e.g. ->execute ($testar->[$i]->['name'], …