Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 03-12-2018, 02:54 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is just really niceMysticalDragon is just really nice
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
SQL File System (Use SQL with ease)

Before using this system, I would highly recommend you use Chris Vimes SQL Explorer, to create your tables.

How it works?
SQL File System runs your SQL Queries from the file directory sql/*. Novo originally wrote this system when I started to get into heavy development since I had such a learning curve with SQL. And in all honesty working with SQL has never been easier.

-NPC Server Needs these rights
PHP Code:
-Depending on how many sub-folders you use.
rw sql/*
rw sql/*/
*
rw sql/*/*/
It also supports multiple databases.
~Functions~
/**
* This method updates the database
*
* @param query_name the named query
* @param args the array of query arguments
* @param debug echos in RC what SQL is doing
* @param db what SQL Database you want to use /uses main.db if null
*/
update(temp.query_name, temp.args, temp.debug, temp.db)


/**
* This method returns a result set from the database
*
* @param query_name the named query
* @param args the array of query arguments
* @param debug echos in RC what SQL is doing
* @param db what SQL Database you want to use /uses main.db if null
* @return the results (TSQLRequest)
*/
read(temp.query_name, temp.args, temp.debug, temp.db)


/**
* This method returns the first column values as an array
*
* @param query_name the named query
* @param args the array of query arguments
* @param debug echos in RC what SQL is doing
* @param db what SQL Database you want to use /uses main.db if null
* @return the first column results
*/
readFirst(temp.query_name, temp.args, temp.debug, temp.db)

-Examples on how to use the System.
We are going to create a table called test using SQL Explorer with the rows test1, test2, test3 with test1 being unique.
PHP Code:
NPC DatabasetestSQL
function onCreated() {
  
temp.row1 "test1"//usually your unique ID
  
temp.rowData = {"test2""test3"};
  
addTesttemp.row1temp.rowData); //Adds The Data
}

public function 
addTest(temp.row1temp.rowData) {
  
temp.query_args = {};
  
temp.query_args.add(temp.row1);
  
temp.query_args.addarray(temp.rowData);
  
SQL.update("test/add"temp.query_args);
  
  
// Cache SQL here (optional ?)
  
this.(@"test"temp.row1) = temp.rowData;

PHP Code:
Filesql/test/add.txt (same as SQL.update)
INSERT INTO items (
  
test1,
  
test2,
  
test3
)

VALUES (
  
'%s',
  
'%s',
  
'%s'

But what if you want to add to an existing row? First you need to check if the data actually exists. You could use the cache for this or check SQL Directly. We are going to check SQL directly in this example but I would suggest you cache the data.

PHP Code:
NPC DatabasesqlTest2 (same as SQL.update)
function 
onCreated() {
  
SQL.update("test/createIfDoesntExist", {"test1"});
  
SQL.update("test/add"temp.data);

PHP Code:
Filesql/test/createIfDoesntExist.txt (same as SQL.update)
INSERT OR IGNORE INTO test(
  
test1,
  
test2,
  
test3
)

VALUES(
  
'%s',
  
1,
  
2

How to easily get the SQL Data.
PHP Code:
NPC DatabasesqlTest3
function onCreated() {
  
temp.data getAll();
  echo(
temp.data);
}

public function 
getAll() {
  
temp.ids = new[0];
  for (
temp.rowSQL.read("test/getAll").rows) {
    
temp.ids.add(temp.row[0]);
  }
   return 
temp.ids;

PHP Code:
Filesql/test/getAll.txt
SELECT
  test1
FROM
  test
ORDER BY
  test1 ASC 
Attached Both Scripts SQL (Database) and functions_sql (Class)
Attached Files
File Type: txt functions_sql.txt (3.2 KB, 217 views)
File Type: txt SQL.txt (4.3 KB, 278 views)
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]




Last edited by MysticalDragon; 03-12-2018 at 08:55 PM..
Reply With Quote
 


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 12:44 PM.


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