dbTalk Databases Forums  

Can/should this be added to MySQL++?

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Can/should this be added to MySQL++? in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jim Langston
 
Posts: n/a

Default Can/should this be added to MySQL++? - 09-26-2006 , 09:15 PM






------=_Part_9848_29612577.1159290886589
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

MySQL++ didn't do some of the things I wanted it to do, so I wound up
writing my own wrapper for MySQL. But I think it would be good if these
could be added to MySQL++, but I'm not sure if they would conflict.

The main thing my class does is it allows a user class/structure to be
mapped like this:


void CCharFieldMap::SetMap( CCharacter* Base )
{
SetBase( Base );
SetOffset( "Version", Base->Version );
SetOffset( "Name", Base->Name );
SetOffset( "Password", Base->Password );
SetOffset( "Avatar", Base->Avatar );
SetOffset( "Race", *reinterpret_cast<unsigned int*>( &Base->Race ) );
SetOffset( "Sex", *reinterpret_cast<unsigned int*>( &Base->Sex ) );
SetOffset( "GM", Base->GM );
SetOffset( "GMLevel", Base->GMLevel );
// Health
SetOffset( "Mana", Base->Health.Mana_ );
SetOffset( "ManaMax", Base->Health.ManaMax_ );
SetOffset( "Overall", Base->Health.Parts[BP_None].Current_ );
SetOffset( "OverallMax", Base->Health.Parts[BP_None].Max_ );
SetOffset( "Head", Base->Health.Parts[BP_Head].Current_ );

// Many of these for fields in the db

}

The reason I set this up is so I can do this:

CCharacter Player;
Player.FieldMap.SetMap( &Player );

CMySQLTable PlayerTable;

if ( ! PlayerTable.init( "192.168.1.100", "serpardum", "likeidtellyou",
"abyssal", "players", 3307 ) )
{
std::cerr << "Initialization failed of Player table" << std::endl;
std::string wait;
std::getline( std::cin, wait );

return 1;
}

if ( ! PlayerTable.LoadTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable.OutputMapByColumn();

PlayerTable >> Player;

At this point the Player instance has been loaded with the fields from the
db, all handled by the CMySQLTable class. I also allow

PlayerTable << Player;

to set the fields in the table so I can .update() or .insert() them.

I figured it this way. Yes, the user has to map each field, but then, if
they are loading the fields they are going to have to go through each field
one way or another anyway. Either with << or some other method. So I
figure do it once and get it over with, then you can load, update, insert,
etc.. without having to do it again.

Of couse one size doesnt' always fit all so I've overloaded other things
too.

PlayerTable["Name"] = "Insert Test";

PlayerTable.Update( "Name", PlayerTable["Name"].Value() );

This only sets the "Name" field. The parms added to the Update are the key
name and value to find for the update used in the select (update blah blah
where Name = Value.

Of course you can also say:

std::string NameField = PlayerTable["Name"];

The main things being overriden exposed are =, >>, << and []

How hard would it be to implement this in MySQL++ if it even should?
Incidently, the .init() call builds an internal map of the fields with the
field names and types so the user doesn't have to, just map the field name
with the offset into the class.

Of course, one size doesn't fit all, especially for player classes that are
some form of linked list or something so they can also do this:

void CSkill::MySQLWrite( CMySQLTable& ms, const std::string& PlayerName,
const std::string& Prefix ) const
{
if ( Name_.length() > 0 && ( Value_ != 0.0 || Active_ ) )
{
ms["PlayerName"] = PlayerName;
ms["Name"] = Prefix + Name_;
ms["Value"] = Value_;
ms["Active"] = Active_;
ms["Activated"] = Activated_;
ms.Insert();
}
for ( std::map< std::string, CSkill >::const_iterator it =
Skills_.begin(); it != Skills_.end(); ++it )
{
(*it).second.MySQLWrite( ms, PlayerName, ( Name_.length() > 0 ?
Prefix + Name_ + "|" : "" ) );
}
}

I guess my question is, is this something people other than me would want?
If enough people would want this and I get the okay from the MySQL++ team I
can look at implementing this into MySQL++

------=_Part_9848_29612577.1159290886589--

Reply With Quote
  #2  
Old   
Jim Langston
 
Posts: n/a

Default Re: Can/should this be added to MySQL++? - 09-26-2006 , 09:16 PM






------=_Part_16518_2910127.1159322985798
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On 9/26/06, Warren Young <mysqlpp (AT) etr-usa (DOT) com> wrote:
Quote:
Jim Langston wrote:
SetOffset( "Race", *reinterpret_cast<unsigned int*>( &Base->Race ) );

*RETCH*

The need for reinterpret_cast alone is a deal-killer for me. This is
one of the reasons I don't like to use raw C.

Yes, I looked and looked but couldnt' find a better solution for this. Race
is an enum.

Quote:
if ( ! PlayerTable.LoadTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable.OutputMapByColumn();

PlayerTable >> Player;

I guess I'm not studying your code closely enough, because I don't see
what you're doing here that can't be done with SSQLS.

I asked before on, I believe, this list for the ability to go directly to my
table and was told it couldn't be done. I asked specifically if MySQL++ had
the ability to load directly into a class/structure and was told no. You
are telling me I can do this with SSQLS?

Quote:
I also allow

PlayerTable << Player;

to set the fields in the table so I can .update() or .insert() them.
The only difference between this and SSQLS is that you are inserting
into a table object, whereas you insert into a query object with SSQLS.
Six of one, half a dozen of the other as far as I can tell.

Player is my own class with it's own variables. An instance that's being
used by the application. I was told I couldnt' do this in MySQL++

Quote:
How hard would it be to implement this in MySQL++ if it even should?
It seems to me that you're trying to reinvent SSQLS. What isn't clear
to me yet is that you've done it better.

------=_Part_16518_2910127.1159322985798--


Reply With Quote
  #3  
Old   
Warren Young
 
Posts: n/a

Default Re: Can/should this be added to MySQL++? - 09-26-2006 , 09:49 PM



Jim Langston wrote:
Quote:
SetOffset( "Race", *reinterpret_cast<unsigned int*>( &Base->Race ) );
*RETCH*

The need for reinterpret_cast alone is a deal-killer for me. This is
one of the reasons I don't like to use raw C.

Quote:
if ( ! PlayerTable.LoadTable( "Name", "Serpardum" ) )
std::cout << "Serpardum not found" << std::endl;
else
PlayerTable.OutputMapByColumn();

PlayerTable >> Player;
I guess I'm not studying your code closely enough, because I don't see
what you're doing here that can't be done with SSQLS.

Quote:
I also allow

PlayerTable << Player;

to set the fields in the table so I can .update() or .insert() them.
The only difference between this and SSQLS is that you are inserting
into a table object, whereas you insert into a query object with SSQLS.
Six of one, half a dozen of the other as far as I can tell.

Quote:
How hard would it be to implement this in MySQL++ if it even should?
It seems to me that you're trying to reinvent SSQLS. What isn't clear
to me yet is that you've done it better.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Warren Young
 
Posts: n/a

Default Re: Can/should this be added to MySQL++? - 09-28-2006 , 06:44 AM



Jim Langston wrote:
Quote:
I asked specifically if MySQL++ had
the ability to load directly into a class/structure and was told no. You
are telling me I can do this with SSQLS?
Sure.

SSQLS stands for Specialized SQL _Structures_. A struct is just a class
that defaults to everything-public. There is absolutely no other
difference. So, you define the SSQLS after your table schema, and then
derive another class from that struct that adds whatever additional data
and methods you need.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.