-2

Well, first sorry for the title if it's not clear enough. I didn't know how to explain it more clearly.

So, the idea is to have a class (I'm using PHP, however, it's more general idea), to represent the SQL functionality but via the class methods. For example, imagine the following SQL statement:

SELECT p_id, p_name FROM tbl_products

It could be something like this:

$API->Data->Action = 'select';
$API->Data->Fields = 'p_id, p_name';    // comma separated
$API->Data->Table = 'tbl_products';

And I can reassemble it like:

$Query = $API->Data->Action . ' ' . $API->Data->Fields . ' FROM ' . 
         $API->Data->Table . ' WHERE available = 1';

The API should provide for some programmers, however it could be short, simple and easy to use. If they want to run their very-own SQL statements, they have that ability also.

Question:

Which keywords you will use as the function names' which could be clear for both novice and professional programmers, but not very similar to SQL standard keywords?

For example, instead of LIMIT 0, 15 I can use something like First and Last and then combine it with the offset. But what about something like ASC and DESC? INSERT, UPDATE and DELETE are enough clear, but for a person that let say he doesn't know anything about SQL, SELECT could be a little bit confusing.

  • Please let me know your general ideas
  • And offer me your desired 'keywords' also

I know that it might not look like a serious question somehow, but I really appreciate your help and ideas! For me they are much more than just a 'keyword', and that's why I'm asking for your ideas! Thank you everyone for any answer!

Solution:

I finally came up with this:

// Pure SQL Query
$API->Database->Query('SQL HERE');
$API->Database->Result;    // It contains the results as an Object

// For 'SELECT' you don't need any command, just Process() it ...
$API->Data->Table = 'Products';
$API->Data->Fields = 'p_id, p_name';
$API->Data->Offset = 'last';
$API->Data->Limit = 15;
$API->Data->Process();

// 'UPDATE', need passing some variables like above plus something like 'WHERE'
$API->Data->Save();

// 'INSERT'
$API->Data->Add();

// Save() without 'WHERE' would insert also

// 'DELETE'
$API->Data->Delete();

I'm still looking to improve it as there are still some unclear points there, so feel free to share your ideas! :)

4

1 回答 1

1

你可以构建这样的东西:

class DBAdapter extends PDO
{
  public function __construct( array $params)
  {
     // construct the connection
  }

  public function Select ($table)
  {
    // do stuff for a table select
    return $this;
  }

  public function Count ($table)
  {
    // do stuff for a table count
    return $this;
  }


  public function Where ($condition)
  {
    // do stuff for a where
    return $this;
  }

  public function Execute ()
  {
    // execute stuff
    return $result;
  }
}

然后像这样使用它:

$db = new DBAdapter(array('host' => 'localhost'));
$results = $db->Select('mytable')->Where('id > 2')->Execute();
于 2012-09-19T10:15:06.870 回答