MySQL. Issue with module design.
#1

Hello folks!

I just started to learn SAMP coding and trying to build modular system instead off massive one file but there is the issue.

file: main.pwn

Код:
#include <a_samp>
#include <a_mysql>

#include "../include/mysql.pwn"

new mysql;

main(){
}

OnGameModeInit() {
	
	mysql = Mysql_Initi();
	
	return 1;
}
file: mysql.pwn

Код:
#include <a_samp>
#include <a_mysql>

#define HOST "localhost"
#define USER "root"
#define DB "test"
#define PWD "12345"

Mysql_Initi() {

	new mysql;
	
	mysql = mysql_connect(HOST, USER, DB, PWD);
	mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG); 
	
	if (mysql_errno(mysql) != 0) print("MySQL is broken");
	
	return mysql;
}
The issue is that invalid connection handle (id: 0).

Questions:

1. How i can connect to mysql once and use it across all includes to make queries?

2. How to get it work as include in example above?

Thank you in advance.
Reply
#2

Hello.

Nobody knows?

Let me explain more clear:

In PHP, for example, following code is valid:

File dbsettings.php

Код:
<?php
define('DBHOST', 'localhost'); // имя хоста
define('DBUSER', 'root'); // имя пользователя
define('DBPASSWD', ''); // пароль
define('DBNAME', 'test'); // имя базы данных
?>
File dbconnect.php

Код:
$link = mysql_connect($host, $user, $passwd) or die('Could not connect to database');

}
File main.php

Код:
<?php
require_once 'dbsettings.php';
require_once 'dconnect.php';

//here variables DBHOST, DBUSER is visible since they are global, also connection with mysql also established and i 
can do following

$r=mysql_query («SELECT * FROM gb ORDER BY dt DESC»);
Hope it's more clear now.

Btw, i'm using R39 version of mysql plugin.
Reply
#3

The problem is that PHP is way more dynamic. Pawn is supposed to be one file to edit small stuff.

I have had the same problems with my modulair design, so the big parts, I had in my main file. Things as mysql_connect. I am not competent enough to give you a real answer, but I can tell you that I would've probably done it the same way.
Reply
#4

Quote:
Originally Posted by mamorunl
Посмотреть сообщение
The problem is that PHP is way more dynamic. Pawn is supposed to be one file to edit small stuff.

I have had the same problems with my modulair design, so the big parts, I had in my main file. Things as mysql_connect. I am not competent enough to give you a real answer, but I can tell you that I would've probably done it the same way.
Thank you for ur input Mamorunl.

I think i just don't know something and it's possible.

There one more question: is code in includes executing after including?

It's strange:

main.pwn

Код:
#include <a_samp>
#include <a_mysql>

#include "../include/mysql.inc"

...some query with mysql handler...
mysql.inc

Код:
#include <a_samp>
#include <a_mysql>

#define HOST "localhost"
#define USER "root"
#define DB "test"
#define PWD "12345"

new mysql;

mysql = mysql_connect(HOST, USER, DB, PWD);
mysql_log(LOG_ALL); 
	
if (mysql_errno(mysql) != 0) print("MySQL is broken");
After trying to compile:

Код:
\../include/mysql.inc(11) : error 010: invalid function or declaration
\../include/mysql.inc(14) : error 010: invalid function or declaration
Reply
#5

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 they're 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) { ... } 
Reply
#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
#7

Your code does not work because it is not in a callback/function. Pawn is not PHP. The include directive simply copies the content from one file into another and in the end you will still end up with a single gamemode file. So you include the MySQL class once and then you'll be able to use GetConnectionHandle in every other file.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)