Webman-framework

Lightweight, Component-based, and Database-oriented Web Application Framework

About | Overview | Documentation

 

Documentation > Modules and APIs > Text_DB_Map

Text_DB_Map

 

Description:

The core module used to support database operations (insert and update) through the mapping of content data from the pre-formatted text file. By default, inside the text file, the "\t" and "\n" characters are used to separate the columns and the rows of database items.

 

Dependencies:

Webman-framework's Core Modules:

  • DB_Utilities (Composition)
  • Table_List_Data (Composition)

 

1. Instantiation and Basic Parameter Setting


my $txt2db = new Text_DB_Map;


1.1 Input Text File

Input from text based file is simply set via set_Text_File_Name function.

$txt2db->set_Text_File_Name("???.txt");


1.2 Scalar Type Input

Option to directly pass the input content without using text based pre-formatted input file.

my $text_content = "$data_r1c1\t$data_r1c2\t$data_r1c3\n$data_r2c1\t$data_r2c2\t$data_r2c3";
$txt2db->set_Text_File_Content($text_content);


1.3 Columns and Rows Spliter Characters

Default spliters for rows and columns are the "\t" and "\n" characters. Other characters such as "|", ":", and "," are also possible to be set via $spliter_col and $spliter_row arguments below.

$txt2db->set_Spliter_Column($spliter_col);
$txt2db->set_Spliter_Column(spliter_row);


1.4 Database Connection and Table Name

The argument $db_conn is an instance of database connection created using CPAN's DBI module and the argument $table_name is the table name where the insert or update operations will be applied to.

$txt2db->set_Conn($db_conn);
$txt2db->set_Table_Name($table_name);


1.5 Update Key Field

For update operation the key field name is required. It's used to feed the SQL update key: update ... where $key_field_name='...';

$txt2db->set_Key_Field_Name($key_field_name);


1.6 Auto Key Field Type

For insert operation on tables which implement automatic key-field values creation, the next function can be used. The $key_type argument can be "auto_increment", "36Base", or "62Base"and the default is "auto_increment". Read DB_Utilities module documentation for details about "36Base" and "62Base" key types.

$txt2db->set_Key_Field_Type($key_type);


1.7 Auto Key Field Length

This function is specifically applied for the above "36Base" and "62Base" key types. If ignored the default length is automatically set to 6.

$txt2db->set_Key_Field_Length("length_");



 
2. Table_List_Data Instance

This function is automatically called inside insert_Row and update_Row functions (see section 3) but can be optionally called manually to create and return Table_List_Data module instance

my $tld = $txt2db->generate_TLD;



 
3. Insert and Update Operations

3.1 Insert Operation

The argument for set_Field_List function is an anonymous array reference. The array's length must exaclty the same with the number of columns from the input text file that separated by the special column's spliter character (see section 1.3). The items of the array represent the database table field names.

$txt2db->set_Field_List(["field_name_c1_", "field_name_c2_", "field_name_c3_"]);
$txt2db->insert_Row;


3.2 Update Operation

The argument for set_Field_List function is exactly the same as explained in section 3.1.

$txt2db->set_Field_List(["field_name_c1_", "field_name_c2_", "field_name_c3_"]);
$txt2db->update_Row;


3.3 Insert/Upate Items Debug

Print back item rows being insert/update.

my $tld = $txt2db->get_TLD;
print $tld->get_Table_List;