Sabtu, 26 Desember 2009

Customizing the DBNavigator

Source :http://delphi.about.com/od/usedbvcl/l/aa090203a.htm

Buttons: array[TNavigateBtn] of TNavButton;

TNavigateBtn =
(nbFirst, nbPrior, nbNext, nbLast, nbInsert,
nbDelete, nbEdit, nbPost, nbCancel, nbRefresh);

type THackDBNavigator = class(TDBNavigator);

type
TForm1 = class(TForm)
...

procedure TForm1.FormCreate(Sender: TObject);
SetupHackedNavigator(DBNavigator1, ImageList1);
end;

type
TForm1 = class(TForm)
...
private
procedure SetupHackedNavigator(const Navigator : TDBNavigator;
const Glyphs : TImageList);


uses Buttons; //!!! don't forget

procedure TForm1.SetupHackedNavigator
(const Navigator : TDBNavigator;
const Glyphs : TImageList);
const
Captions : array[TNavigateBtn] of string =
('Initial', 'Previous', 'Later', 'Final', 'Add',
'Erase', 'Correct', 'Send', 'Withdraw', 'Revive');
(*
Captions : array[TNavigateBtn] of string =
('First', 'Prior', 'Next', 'Last', 'Insert',
'Delete', 'Edit', 'Post', 'Cancel', 'Refresh');

in Croatia (localized):
Captions : array[TNavigateBtn] of string =
('Prvi', 'Prethodni', 'Slijedeci', 'Zadnji', 'Dodaj',
'Obrisi', 'Promjeni', 'Spremi', 'Odustani', 'Osvjezi');
*)

var
btn : TNavigateBtn;
begin
for btn := Low(TNavigateBtn) to High(TNavigateBtn) do
with
THackDBNavigator(Navigator).Buttons[btn] do
begin
//from the Captions const array
Caption := Captions[btn];

//the number of images in the Glyph property
NumGlyphs := 1;
// Remove the old glyph.
Glyph := nil;
// Assign the custom one
Glyphs.GetBitmap(Integer(btn),Glyph);
// gylph above text
Layout := blGlyphTop;

// explained later
OnMouseUp := HackNavMouseUp;
end;
end;
(*SetupHackedNavigator*)


procedure TForm1.HackNavMouseUp
(Sender:TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
const MoveBy : integer = 5;
begin
if NOT (Sender is TNavButton) then Exit;

case TNavButton(Sender).Index of
nbPrior:
if (ssCtrl in Shift) then
TDBNavigator(TNavButton(Sender).Parent).
DataSource.DataSet.MoveBy(-MoveBy);
nbNext:
if (ssCtrl in Shift) then
TDBNavigator(TNavButton(Sender).Parent).
DataSource.DataSet.MoveBy(MoveBy);
end;
end;(*HackNavMouseUp*)


type
TForm1 = class(TForm)
...
private
procedure SetupHackedNavigator(const Navigator : TDBNavigator;
const Glyphs : TImageList);
procedure HackNavMouseUp(Sender:TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);


procedure TForm1.DBNavigator1Click
(Sender: TObject; Button: TNavigateBtn);
function CtrlDown : Boolean;
var
State : TKeyboardState;
begin
GetKeyboardState(State);
Result := ((State[vk_Control] And 128) <> 0);
end;
const MoveBy : integer = 5;
begin
case Button of
nbPrior:
if CtrlDown then
DBNavigator1.DataSource.DataSet.MoveBy(-MoveBy);
nbNext:
if CtrlDown then
DBNavigator1.DataSource.DataSet.MoveBy(MoveBy);
end; //case
end;(*DBNavigator2Click*)










Jumat, 18 Desember 2009

MYSQL : CREATE PROCEDURE

Source :
http://www.java2s.com/Tutorial/MySQL/0240__Cursor/Usingwhilelooptoloopthroughacursor.htm

mysql>

mysql>
mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc (in_customer_id INT)
-> BEGIN
->
-> DECLARE l_last_row_fetched int;
-> DECLARE l_id int;
-> DECLARE l_first_name VARCHAR(30);
-> DECLARE l_last_name VARCHAR(30);
->
->
-> DECLARE c1 CURSOR FOR
-> SELECT id,first_name, last_name
-> FROM employee
-> WHERE id=in_customer_id;
->
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row_fetched=1;
->
-> SET l_last_row_fetched=0;
->
->
-> OPEN c1;
-> cursor_loop:LOOP
-> FETCH c1 INTO l_id,l_first_name,l_last_name;
-> IF l_last_row_fetched=1 THEN
-> LEAVE cursor_loop;
-> END IF;
-> /*Do something with the row fetched*/
-> END LOOP cursor_loop;
-> CLOSE c1;
-> SET l_last_row_fetched=0;
->
->
->
-> END$$
Query OK, 0 rows affected (0.02 sec)

mysql> delimiter ;
mysql>
mysql> call myProc(1);
Query OK, 0 rows affected (0.00 sec)

mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql>
mysql>
mysql> drop table Employee;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

http://dev.mysql.com/doc/refman/5.0/en/cursors.html
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN cur1;
OPEN cur2;

REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;

CLOSE cur1;
CLOSE cur2;
END

INGIN KERJA DARI RUMAH?