Пример #1
0
bool MysqlDatabase::exists(void) {
  bool ret = false;

  if ( conn == NULL || mysql_ping(conn) )
  {
    CLog::Log(LOGERROR, "Not connected to database, test of existence is not possible.");
    return ret;
  }

  MYSQL_RES* result = mysql_list_dbs(conn, db.c_str());
  if (result == NULL)
  {
    CLog::Log(LOGERROR,"Database is not present, does the user has CREATE DATABASE permission");
    return false;
  }

  ret = (mysql_num_rows(result) > 0);
  mysql_free_result(result);

  // Check if there is some tables ( to permit user with no create database rights
  if (ret)
  {
    result = mysql_list_tables(conn, NULL);
    if (result != NULL)
      ret = (mysql_num_rows(result) > 0);

    mysql_free_result(result);
  }

  return ret;
}
Пример #2
0
int database::init(OBJECT *parent)
{
	gld_string flags = get_clientflags_property().get_string();
	
	gl_verbose("mysql_connect(hostname='%s',username='******',password='******',schema='%s',port=%u,socketname='%s',clientflags=0x%016llx[%s])",
		(const char*)hostname,(const char*)username,(const char*)password,(const char*)schema,port,(const char*)socketname,get_clientflags(),(const char*)flags);

	mysql = mysql_real_connect(mysql_client,hostname,username,strcpy(password,"")?password:NULL,NULL,port,socketname,(unsigned long)clientflags);
	if ( mysql==NULL )
		exception("mysql connect failed - %s", mysql_error(mysql_client));
	else
		gl_verbose("MySQL server info: %s", mysql_get_server_info(mysql));

	// autoname schema
	if ( strcmp(get_schema(),"")==0 )
	{
		char buffer[1024];
		gld_global model("modelname");
		if ( model.to_string(buffer,sizeof(buffer))>0 )
			set_schema(buffer);
	}

	// drop schema
	if ( get_options()&DBO_DROPSCHEMA && query("DROP DATABASE IF EXISTS `%s`", get_schema()) )
	{
		if ( strcmp(get_schema(),"gridlabd")==0 )
			gl_warning("%s uses NEWDB option on the default schema '%s' - this is extremely risky", get_name(), get_schema());
		gl_verbose("schema '%s' dropped ok", get_schema());
	}

	// check schema
	MYSQL_RES *res = mysql_list_dbs(mysql,get_schema());
	if ( mysql_num_rows(res)==0 )
	{
		if ( !(get_options()&DBO_NOCREATE) )
		{
			if ( query("CREATE DATABASE IF NOT EXISTS `%s`", get_schema()) )
				gl_verbose("created schema '%s' ok", get_schema());
		}
		else
			exception("NOCREATE option prevents automatic creation of schema '%s'", get_schema());
	}
	else
		gl_verbose("schema '%s' found ok", get_schema());

	// use schema
	if ( mysql_select_db(mysql,get_schema())!=0 )
		exception("unable to select schema '%s'", get_schema());

	// execute on_init script
	if ( strcmp(get_on_init(),"")!=0 )
	{
		gl_verbose("%s running on_init script '%s'", get_name(), get_on_init());
		int res = run_script(get_on_init());
		if ( res<=0 )
			exception("on_init script '%s' failed at line %d: %s", get_on_init(), -res, get_last_error());
	}
	return 1;
}
Пример #3
0
Variant f_mysql_list_dbs(CVarRef link_identifier /* = null */) {
  MYSQL *conn = MySQL::GetConn(link_identifier);
  if (!conn) return false;
  MYSQL_RES *res = mysql_list_dbs(conn, NULL);
  if (!res) {
    raise_warning("Unable to save MySQL query result");
    return false;
  }
  return Resource(NEWOBJ(MySQLResult)(res));
}
Пример #4
0
static JSVAL list_dbs(JSARGS args) {
	HandleScope scope;
	MYSQL *handle = (MYSQL *) args[0]->IntegerValue();
	const char *wild = NULL;
	if (args.Length() > 1) {
		String::Utf8Value pat(args[1]->ToString());
		wild = *pat;
	}
	return scope.Close(Integer::New((unsigned long)mysql_list_dbs(handle, wild)));
}
Пример #5
0
static Variant HHVM_FUNCTION(mysql_list_dbs,
                      const Variant& link_identifier /* = null */) {
  MYSQL *conn = MySQL::GetConn(link_identifier);
  if (!conn) return false;
  MYSQL_RES *res = mysql_list_dbs(conn, nullptr);
  if (!res) {
    raise_warning("Unable to save MySQL query result");
    return false;
  }
  return Variant(makeSmartPtr<MySQLResult>(res));
}
Пример #6
0
// MYSQL_RES *mysql_list_dbs(MYSQL *mysql, const char *wild)
static IDL_VPTR IDL_mg_mysql_list_dbs(int argc, IDL_VPTR *argv) {
    char *wildcard = NULL;
    if (argc > 1) {
        if (argv[1]->type == IDL_TYP_STRING) {
            wildcard = IDL_VarGetString(argv[1]);
        }
    }
    MYSQL_RES *result = mysql_list_dbs((MYSQL *)argv[0]->value.ptrint,
                                       wildcard);
    return IDL_GettmpMEMINT((IDL_MEMINT) result);
}
Пример #7
0
void NxMocapMysql::ListDatabases( std::vector<std::string> & DatabaseList )
{
    if( !mSql->mConnection ) return;
    DatabaseList.clear();
    MYSQL_RES * Mysql_Result = mysql_list_dbs( mSql->mConnection, NULL );
    MYSQL_ROW Mysql_Row ;
    while ( (Mysql_Row = mysql_fetch_row(Mysql_Result))) {
        std::ostringstream os;
        os << Mysql_Row[0];
        std::string buffer(os.str());
        DatabaseList.push_back( buffer );
    }
    mysql_free_result( Mysql_Result );
}
Пример #8
0
int Mysql::Obtener_dbs(wxChoice db[]){
	if((resultado = mysql_list_dbs(sock,NULL))){
		db->Clear();//Limpiamos todos los item del choice para agrgar nuevos
		c = (int) mysql_num_fields(resultado);
		while((fila = mysql_fetch_row(resultado))){
			for(int j = 0 ; j < c ; j++){
				(fila[j]==NULL) ? db->Append(wxT("NULL")) : db->Append(wxString::FromAscii(fila[j]));
			}
		}
		mysql_free_result(resultado); // Liberar el resultado de la consulta
		return 1;
	}
	else{
		return 0;
	}
} 
Пример #9
0
int main(int argc, char **argv)
{
  int	count, num;
  MYSQL mysql,*sock;
  MYSQL_RES *res;
  char	qbuf[160];

  if (argc != 2)
  {
    fprintf(stderr,"usage : select_test <dbname>\n\n");
    exit(1);
  }

  if (!(sock = mysql_connect(&mysql,NULL,0,0)))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
    perror("");
    exit(1);
  }
  mysql.reconnect= 1;

  if (mysql_select_db(sock,argv[1]) < 0)
  {
    fprintf(stderr,"Couldn't select database %s!\n%s\n",argv[1],
	    mysql_error(sock));
    exit(1);
  }

  if (!(res=mysql_list_dbs(sock,NULL)))
  {
    fprintf(stderr,"Couldn't list dbs!\n%s\n",mysql_error(sock));
    exit(1);
  }
  mysql_free_result(res);
  if (!(res=mysql_list_tables(sock,NULL)))
  {
    fprintf(stderr,"Couldn't list tables!\n%s\n",mysql_error(sock));
    exit(1);
  }
  mysql_free_result(res);

  mysql_close(sock);
  exit(0);
  return 0;
}
Пример #10
0
//---------------------------------------------------------------------------
bool __fastcall TForm1::get_dbs(void)
{
 MYSQL_RES *res;

 if (!is_server_started)
  return false;

 if (!(res=mysql_list_dbs(MySQL,"%")))
  return false;

 deque<string> rows = fill_rows(res);

 mysql_free_result(res);
 fill_tree(rows,MySQLDbs,db_root,DBView,1);
 info_server->Text = mysql_get_server_info(MySQL);

 return true;
}
Пример #11
0
bool wxMySQL::EnumDatabases(wxArrayString & array)
{
	MYSQL_RES * res;
	MYSQL_ROW row;
	array.Clear();
	do
	{
		res = mysql_list_dbs(m_pDB,NULL);
		if(!res) break;
		while(row = mysql_fetch_row(res))
		{
			array.Add(wxString(row[0]));			
		}
		mysql_free_result(res);
	}
	while(false);
	m_LastErrorMessage = mysql_error(m_pDB);
	return true;
}
Пример #12
0
/*	list_dbs(db=nil)	*/
static VALUE list_dbs(int argc, VALUE* argv, VALUE obj)
{
    unsigned int i, n;
    VALUE db, ret;
    MYSQL* m = GetHandler(obj);
    MYSQL_RES* res;

    rb_scan_args(argc, argv, "01", &db);
    res = mysql_list_dbs(m, NILorSTRING(db));
    if (res == NULL)
	mysql_raise(m);

    n = mysql_num_rows(res);
    ret = rb_ary_new2(n);
    for (i=0; i<n; i++)
	rb_ary_store(ret, i, rb_tainted_str_new2(mysql_fetch_row(res)[0]));
    mysql_free_result(res);
    return ret;
}
Пример #13
0
int main(int argc, char **argv)
{
  int	count, num;
  MYSQL mysql,*sock;
  MYSQL_RES *res;
  char	qbuf[160];

  if (argc != 3)
  {
    fprintf(stderr,"usage : select_test <dbname> <num>\n\n");
    exit(1);
  }

  mysql_init(&mysql);
  if (!(sock = mysql_real_connect(&mysql,NULL,0,0,argv[1],0,NULL,0)))
  {
    fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
    perror("");
    exit(1);
  }
  mysql.reconnect= 1;

  count = 0;
  num = atoi(argv[2]);
  while (count < num)
  {
    sprintf(qbuf,SELECT_QUERY,count);
    if(!(res=mysql_list_dbs(sock,NULL)))
    {
      fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
      exit(1);
    }
    printf("number of fields: %d\n",mysql_num_rows(res));
    mysql_free_result(res);
    count++;
  }
  mysql_close(sock);
  exit(0);
  return 0;					/* Keep some compilers happy */
}
Пример #14
0
int
lmt_db_list (char *user, char *pass, List *lp)
{
    char *host = lmt_conf_get_db_host ();
    int port = lmt_conf_get_db_port ();
    List l = list_create ((ListDelF)free);
    MYSQL *conn = NULL;
    MYSQL_RES *res = NULL;
    MYSQL_ROW row;
    int retval = -1;

    if (!(conn = mysql_init (NULL)))
        msg_exit ("out of memory");
    if (!mysql_real_connect (conn, host, user, pass, NULL, port, NULL, 0)) {
        if (lmt_conf_get_db_debug ())
            msg ("lmt_db_list: %s",  mysql_error (conn));
        goto done;
    }
    if (!(res = mysql_list_dbs (conn, "filesystem_%"))) {
        if (lmt_conf_get_db_debug ())
            msg ("lmt_db_list: unable to list lmt databases");
        goto done;
    }
    while ((row = mysql_fetch_row (res)))
        list_append (l, xstrdup (row[0]));
    *lp = l;
    retval = 0;
done:
    if (res)
        mysql_free_result (res);
    if (conn)
        mysql_close (conn);
    if (retval < 0)
        list_destroy (l);
    return retval;
}