views:

58

answers:

2
DROP PROCEDURE IF EXISTS HaveSomeFun;
CREATE PROCEDURE HaveSomeFun(user_id CHAR(50),house_id CHAR(50),room_id CHAR(50),fun_text TEXT,video_url CHAR(100))
BEGIN
 DECLARE query_full TEXT;
 SET @fields_part = 'INSERT INTO fun(FunKey,UserKey,FunBody,LastModified';
 SET @values_part = CONCAT(') VALUES( NewBinKey(), KeyToBin(\"', user_id, '\"), \"', fun_text, '\", NOW() ');
 IF (house_id) THEN
  SET @fields_part = CONCAT(@fields_part, ', HouseKey');
  SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', house_id, '\')');
 END IF;
 IF (room_id) THEN
  SET @fields_part = CONCAT(@fields_part, ', RoomKey');
  SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', room_id, '\')');
 END IF;
 IF (video_url IS NOT NULL) THEN
  SET @fields_part = CONCAT(@fields_part, ', VideoURL');
  SET @values_part = CONCAT(@values_part, ', "', video_url, '"');
 END IF;
 SET query_full  = CONCAT(@fields_part, @values_part, ' );');
 SET @query_full = query_full;
 PREPARE STMT FROM @query_full;
 EXECUTE STMT;
 SELECT query_full;
END;

And

CALL HaveSomeFun('29B455DE-A9BC-102D-9C16-00163EEDFCFC', '', 'F82C47A8-64DE-11DF-9D7E-0026B9481364', 'Jokes apart', '');

will construct the below string in the variable query_full

INSERT INTO fun(FunKey,UserKey,FunBody,LastModified, VideoURL) VALUES( NewBinKey(), KeyToBin("29B455DE-A9BC-102D-9C16-00163EEDFCFC"), "Jokes apart", NOW() , "" );

But I need to get

INSERT INTO fun(FunKey,UserKey,FunBody,LastModified, RoomKey, VideoURL) VALUES( NewBinKey(), KeyToBin("29B455DE-A9BC-102D-9C16-00163EEDFCFC"), "Jokes apart", NOW() , KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364'), "" );

Something is missing in the check IF (room_id) THEN. But I cannot impose IF (room_id IS NOT NULL) THEN since it will create KeyToBin('') and RoomKey is foreign key , KeyToBin('') will produce an invalid RoomKey.

Any Idea?

A: 

You can just write code for both cases:

IF (room_id IS NOT NULL) THEN
  SET @fields_part = CONCAT(@fields_part, ', RoomKey');
  SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', room_id, '\')');
ELSE
  SET @fields_part = CONCAT(@fields_part, ', RoomKey');
  SET @values_part = CONCAT(@values_part, ', NULL');
END IF;

Insted of NULL can be KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364') or something else.

Or you can use nested IF (result will be the same).

IF (room_id) THEN
  SET @fields_part = CONCAT(@fields_part, ', RoomKey');
  IF (room_id IS NULL) THEN
    SET @values_part = CONCAT(@values_part, ', 
      KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364');
  ELSE
    SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', room_id, '\')');
  END IF;
END IF;

Insted of KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364' ypu can place NULL or something else...

ksogor
No, A call like `CALL HaveSomeFun('29B455DE-A9BC-102D-9C16-00163EEDFCFC', '', 'F82C47A8-64DE-11DF-9D7E-0026B9481364', 'Jokes apart', '');`will produce `INSERT INTO fun(FunKey,UserKey,FunBody,LastModified, RoomKey, VideoURL) VALUES( NewBinKey(), KeyToBin("29B455DE-A9BC-102D-9C16-00163EEDFCFC"), "Jokes apart", NOW() , KeyToBin(''), "" );`
Mithun P
Idea was replace `KeyToBin('')` with `KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364')` which is your expected result.
ksogor
No man, it should not produce `KeyToBin('')` or `KeyToBin('F82C47A8-64DE-11DF-9D7E-0026B9481364')`, instead RoomKey part should not come to the concatenated string, and when inserting it's default value `NULL` will be inserted
Mithun P
You wrote: "But I cannot impose IF (room_id IS NOT NULL) THEN since it will create KeyToBin('')". WHAT will produce KeyToBin, if your default value is NULL?Any way, `INSERT INTO (field1, field2) VALUES (value1, NULL)` is the same with `INSERT INTO (field1) VALUES (value1)` (if you have NULL as default value) from the database point.
ksogor
A: 

Got the issue, it should be like

DROP PROCEDURE IF EXISTS HaveSomeFun;
CREATE PROCEDURE HaveSomeFun(user_id CHAR(50),house_id CHAR(50),room_id CHAR(50),fun_text TEXT,video_url CHAR(100))
BEGIN
 DECLARE query_full TEXT;
 SET @fields_part = 'INSERT INTO fun(FunKey,UserKey,FunBody,LastModified';
 SET @values_part = CONCAT(') VALUES( NewBinKey(), KeyToBin(\"', user_id, '\"), \"', fun_text, '\", NOW() ');
 IF (house_id != '') THEN
  SET @fields_part = CONCAT(@fields_part, ', HouseKey');
  SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', house_id, '\')');
 END IF;
 IF (room_id != '') THEN
  SET @fields_part = CONCAT(@fields_part, ', RoomKey');
  SET @values_part = CONCAT(@values_part, ', KeyToBin(\'', room_id, '\')');
 END IF;
 IF (video_url IS NOT NULL) THEN
  SET @fields_part = CONCAT(@fields_part, ', VideoURL');
  SET @values_part = CONCAT(@values_part, ', "', video_url, '"');
 END IF;
 SET query_full  = CONCAT(@fields_part, @values_part, ' );');
 SET @query_full = query_full;
 PREPARE STMT FROM @query_full;
 EXECUTE STMT;
 SELECT query_full;
END;
Mithun P