MySQL. Issue with module design.
#6

Quote:
Originally Posted by Vince
Посмотреть сообщение
With modular designs, pay close attention to the scope of the variables. If you need to use globals in a module, declare them as static rather than new so they are only visible in the module they're used in. Local variables declared within functions usually need not be declared static since there only visible to the function, anyway.

For you MySQL "class", create a global variable to hold the connection handle. Make it static so it is only visible in that file. This connection handle can be used in that file as is. If you need to use the same handle in other files, create a "getter" (like in Java), a simple wrapper that returns the variable.

PHP код:
static gConnectionHandle = -1;
public 
OnGameModeInit()
{
    
gConnectionHandle mysql_connect(...);
}
stock GetConnectionHandle()
    return 
gConnectionHandle;
// hook stuff for OnGameModeInit goes here, see tutorial on ALS hooking 
This means that in another file you can't do this:
PHP код:
printf("%d"gConnectionHandle); 
Because the variable isn't recognized and you will get an error, but you can do this:
PHP код:
printf("%d"GetConnectionHandle()); 
This effectively gives you a read-only variable.

To make things easier for myself, I created an include named "default.inc" and placed it the pawno/include folder. This file will be implicitly included by the compiler without the need to specify it yourself. Then I defined these:

PHP код:
#define private static
#define protected 
private and protected are keywords from C and although they don't exist in Pawn, Pawno and others editors will still highlight them as keywords. So if I wanted to make a function that is only visible in the current file I'd write:

PHP код:
private myFunction(foobar) { ... } 
Thank you a lot Vince! Very helpful.

Getter it's nice but main idea is to connect once in include and then use this handler everywhere. With you code logic i will be need to initiliaze connection in every file and as far as i know mysql implementation for PAWN don't contain check if there is already established connection.

if you don't mind here is some questions:

1. Why my example not working? Except static and getter code is the same.

2. Why in include "invalid function or declaration" is appear?
Reply


Messages In This Thread
MySQL. Issue with module design. - by ZZ - 15.06.2015, 17:25
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 08:50
Re: MySQL. Issue with module design. - by mamorunl - 16.06.2015, 09:17
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 09:45
Re: MySQL. Issue with module design. - by Vince - 16.06.2015, 09:58
Re: MySQL. Issue with module design. - by ZZ - 16.06.2015, 10:15
Re: MySQL. Issue with module design. - by Vince - 16.06.2015, 10:30

Forum Jump:


Users browsing this thread: 1 Guest(s)