简体   繁体   English

我应该使用复杂的多维数组还是创建对象?

[英]Should I use a complex multi-dimensional array or create an object?

I'm writing a script to import csv's into MySQL. 我正在编写一个脚本,将csv导入MySQL。 The csv's will have variable numbers of fields, different data types and field sizes. CSV将具有可变数量的字段,不同的数据类型和字段大小。

My current plan is to do a pass through the csv and collect statistical information on it to inform the MySQL CREATE TABLE query. 我当前的计划是通过csv并收集有关它的统计信息,以通知MySQL CREATE TABLE查询。

My conception was to create an array in this format: 我的想法是用这种格式创建一个数组:

$csv_table_data = array(
  ['columns'] => array(
    [0] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [1] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [2] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
  ),
  ['some_other_data'] = array()
);

So, by accessing $csv_table_data['columns'][$i] I can access each column's attributes and use that to create the MySQL table. 因此,通过访问$ csv_table_data ['columns'] [$ i],我可以访问每个列的属性,并使用该属性创建MySQL表。 Besides $csv_table_data['columns'] I also have other data like total_rows, number_of_fields, and may add more. 除了$ csv_table_data ['columns']外,我还有其他数据,例如total_rows,number_of_fields,并且可能会添加更多数据。

I can gather all this data in one pass, create the appropriate table, and then populate it in a second pass. 我可以通过一次收集所有这些数据,创建适当的表,然后在第二次通过填充它。

I haven't used much object-oriented programming, but with all this data should I consider creating an object with these various properties, rather than creating this complex array? 我没有使用太多面向对象的编程,但是我应该考虑所有这些数据来创建具有这些各种属性的对象,而不是创建这个复杂的数组?

What do you think about it in terms of readability, maintainability, speed of execution, and any other considerations that occur to you? 您在可读性,可维护性,执行速度以及发生的任何其他考虑方面如何看待它?

Thanks 谢谢

I think you should use Classes, not only one big Object. 我认为您应该使用类,而不仅仅是一个大对象。 Maybe you split it up to 2 or 3 Classes. 也许您将其拆分为2或3个班级。 It's much more cleaner as only arrays. 它比数组更清洁。

Something like this 像这样

class Table{

private $data = array();
private $otherData = 'what_ever';

public function getData(){
 return $this->data;
}

public function addData(Row $row){
 $this->data[] = $row;
}

//Add getter and setter


}

class Row{

private $min_size;
private $max_size;
private $avg_size;
private $type;

public function setMinSize($minSize){
$this->min_size = $minSize;
}

public function getMinSize(){
 return $this->min_size;
}


//Add more getter and setter

}

if you have a limited number of files you want to insert you can use MySql's internal functions. 如果您要插入的文件数量有限,则可以使用MySql的内部函数。 To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. 要导入数据文件,请先将其上传到您的主目录,以便该文件现在位于我们本地系统上的/importfile.csv中。 Then you type the following SQL at the mysql prompt: LOAD DATA LOCAL INFILE '/importfile.csv' INTO TABLE test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' (field1, filed2, field3); 然后在mysql提示符下键入以下SQL:LOAD DATA LOCAL INFILE'/importfile.csv'INTO TABLE test_table FIELDS TERMINATED BY','LINES TERMINATED BY'\\ n'(field1,filed2,field3);

The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively. 上面的SQL语句告诉MySQL服务器在本地文件系统上找到您的INFILE,将文件的每一行读取为单独的行,将任何逗号字符视为列定界符,并将其作为字段field1放入MySQL test_table中,field2和field3。 Many of the above SQL clauses are optional and you should read the MySQL documentation on the proper use of this statement. 上面的许多SQL子句都是可选的,您应该阅读MySQL文档以正确使用此语句。

On the other hand if you have to many files or you want to allow users to upload csv which you then in turn add them to the DB, I recommend using the 'array' version as it seems simpler to traverse. 另一方面,如果您有很多文件,或者要允许用户上传csv,然后又将其添加到数据库,则我建议使用“数组”版本,因为它看起来更容易遍历。

Cheers, Denis 丹尼斯干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM