Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 04-14-2009, 04:50 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
SQL Worksheet

If anyone has ever worked with Oracle's SQL Worksheet this should bring back some memories.



Recently Stefan added SQLite support to GS2 and it's pretty great but when you're testing your queries it gets tedious constantly updating the scripts on your server.

This tool should save you less scripting frustration and more time developing the queries before altering your script for them.

PHP Code:

/**
     Coded by fowlplay4
     Feel free to alter the script, hell you can even say you wrote it
     but I'd really appreciate it if you left my name at the top.
*/

function onActionServerSide(commandmessage) {
  switch (
command) {
    case 
"execute":
      
// Break apart multiple queries.
      
temp.queries message.tokenize(";");
      
// Perform Queries
      
for (temp.queryqueries) {
        
this.trigger("PerformQuery"query);
      }
      break;
  }
}

function 
onPerformQuery(query) {
  
// Custom Worksheet Commands
  
if (query.lower().starts("echo")) {
    
player.triggerclient(this.name"echo"query.substring("echo ".length()));
    return;
  }
  
// Query the Database
  
temp.results requestsql(querytrue);
  if (
results.error != "") {
    
// Display Error if Exists
    
player.triggerclient(this.name"error"results.error);
  }
  else if (
results.rows.size() > 0) {
    
// Determine Column Names
    
temp.columns results.rows[0].getdynamicvarnames();
    for (
temp.0results.rows.size(); i++) {
      
// Pools Data Accordingly
      
temp.data "";
      for (
temp.columncolumns) {
        
temp.data.add(results.rows[i].(@column));
      }
      
// Add Data to Rows
      
temp.rows.add(temp.data);
    }
    
// Send Data to Client
    
player.triggerclient(this.name"results", {columns,rows});
  }
  else if (
results.affectedrows 0) {
    
player.triggerclient(this.name"affected"results.affectedrows);
  }
  else 
player.triggerclient(this.name"completed"query.tokenize()[0]);  
}

//#CLIENTSIDE

// Opens GUI with chat command 
function onPlayerChats() {
  if (
player.chat == "/sql") {
    
player.chat "";
    
onCreateGUI();
  }
}

// Creates Worksheet GUI
function onCreateGUI() {
  new 
GuiWindowCtrl("SQL") {
    
profile GuiBlueWindowProfile;
    
clientrelative true;
    
clientextent "498,470";
    
canmaximize false;
    
canmove true;
    
canresize false;
    
closequery false;
    
destroyonhide true;
    
text "Graal SQL Worksheet";
    
= (screenwidth width) / 2;
    
= (screenheight height) / 2;

    new 
GuiScrollCtrl("SQL_MultiLineEdit1_Scroll") {
      
profile GuiBlueScrollProfile;
      
height 273;
      
hscrollbar "dynamic";
      
vscrollbar "dynamic";
      
width 405;
      
90;
      
6;

      new 
GuiMLTextEditCtrl("SQLQuery") {
        
profile GuiBlueMLTextEditProfile;
        
height 16;
        
horizsizing "width";
        
text "";
        
width 380;
      }
    }
    new 
GuiButtonCtrl("SQL_Button1") {
      
profile GuiBlueButtonProfile;
      
text "Execute Query";
      
width 80;
      
6;
      
5;
    }
    new 
GuiScrollCtrl("SQL_MultiLine1_Scroll") {
      
profile GuiBlueScrollProfile;
      
height 186;
      
hscrollbar "dynamic";
      
vscrollbar "dynamic";
      
width 405;
      
90;
      
281;

      new 
GuiMLTextCtrl("SQLConsole") {
        
profile GuiBlueMLTextProfile;
        
height 16;
        
horizsizing "width";
        
text "";
        
width 380;
      }
    }
    new 
GuiButtonCtrl("SQL_Button2") {
      
profile GuiBlueButtonProfile;
      
text "Clear Query";
      
width 80;
      
6;
      
39;
    }
    new 
GuiButtonCtrl("SQL_Button3") {
      
profile GuiBlueButtonProfile;
      
text "Clear Console";
      
width 80;
      
6;
      
73;
    }
  }
  
sqlecho("SQL Worksheet - Scripted by fowlplay4");
}

// Prints Results from Server
function onActionClientside(commandmessage) {
  switch (
command) {
    case 
"error":
      
sqlecho("SQL Error:" SPC message "\n");
      break;
    case 
"results":
      
temp.columns cuteArray(message[0]);
      
sqlecho("SQL Query returned" SPC message[1].size() @ " rows\n" columns "\n==============");
      for (
temp.rowmessage[1]) {
        
sqlecho(cuteArray(row));
      }
      
sqlecho("");
      break;
    case 
"affected":
      
sqlecho("SQL Query affected" SPC message SPC "rows!");
      break;
    case 
"completed":
      
sqlecho("SQL" SPC message.upper() SPC "Query completed successfully.\n");
      break;
    case 
"echo":
      
sqlecho("SQL: " message);
      break;
  }
}

// Readable Array Text Format
function cuteArray(array) {
  if (array.
size() == 1) return array[0];
  else for (
temp.element: array) {
    
temp.result @= element ",";
  }
  return 
result.substring(0,result.length()-1);
}

// Outputs to Worksheet Console
function sqlecho(text) {
  
SQLConsole.addtext(text "\n"false);
  
SQLConsole.scrolltobottom();
}

function 
SQL_Button1.onAction() {
  
// Button "Execute Query" has been pressed
  
temp.query "";
  for (
temp.lineSQLQuery.getlines()) {
    
temp.query @= line SPC "";
  }
  
temp.query temp.query.trim();
  if (
query != "")
    
triggerserver("gui"name"execute"query);
}

function 
SQL_Button2.onAction() {
  
// Button "Clear Query" has been pressed
  
SQLQuery.settext("");
}

function 
SQL_Button3.onAction() {
  
// Button "Clear Console" has been pressed
  
SQLConsole.settext("");

Now I could spend the time explaining the small feature set but it's best shown, so feel free to upload the script onto your server and copy and paste the following set of queries into the GUI and click Execute.

PHP Code:
DROP TABLE test;

CREATE TABLE test (
   
account varchar not null default '' primary key
   
age int not null default 1
);

INSERT INTO test VALUES ('fowlplay4'18);
INSERT INTO test VALUES ('Stefan'33);
INSERT INTO test VALUES ('Graal'11);
INSERT INTO test VALUES ('Unixmad'42);

SELECT *
FROM test;

UPDATE test
SET account 
'Graalonline'
WHERE account 'Graal';

SELECT account AS 'Graal Name'age AS 'Years'
FROM test;

ECHO 
Completed
After executing you should see the following in the console. If you get a weird/no response you might not have SQL enabled, just restart your npcserver.

PHP Code:
// You may get a table doesn't exist error here, just ignore it.
SQL DROP Query completed successfully.

SQL CREATE Query completed successfully.

SQL INSERT Query completed successfully.

SQL INSERT Query completed successfully.

SQL INSERT Query completed successfully.

SQL INSERT Query completed successfully.

SQL Query returned 4 rows
account
,age
==============
fowlplay4,18
Stefan
,33
Graal
,11
Unixmad
,42

SQL UPDATE Query completed successfully
.

SQL Query returned 4 rows
Graal Name
,Years
==============
fowlplay4,18
Stefan
,33
Graalonline
,11
Unixmad
,42 
Now if you're already using databases, be careful with this tool. One wrong query could wipe your database clean :P

If you have any issues/concerns I'll answer them as best as I can, as SQL Support is updated I'll update the tool to include support for the new features added.

Enjoy.
__________________
Quote:
Reply With Quote
  #2  
Old 04-14-2009, 05:02 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Very cool and useful, great job!
__________________
Reply With Quote
  #3  
Old 04-14-2009, 06:28 AM
Matt Matt is offline
iZone Administrator
Matt's Avatar
Join Date: Apr 2005
Location: United States
Posts: 2,690
Matt is a jewel in the roughMatt is a jewel in the rough
Very helpful, thanks.
__________________
Need Playerworld or Account support?
GraalOnline/Toonslab Support Center
Reply With Quote
  #4  
Old 04-14-2009, 07:23 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Not as fancy as the Oracle SQL Worksheet, but still great stuff. Nice work, Jer.
Reply With Quote
  #5  
Old 04-14-2009, 07:56 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
I like it. Although, I'm not a big fan of Oracle. Interned there for a quarter, and the work environment slowly sucks the soul out of you.

But, I digress. That's a very useful tool, and I might actually use it sometime .
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 06:08 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.