db/lib.php
/* -----
 * Create a collection based on the given data
 ----- */
Create a collection based on the given data
Create a collection based on the given data
function createCollectionFromData ( $connection, $collection, $dataset ) {
	$dataset = array_values( array_filter( $dataset ) );
	$columnNames = $dataset[ 0 ];
	$columnLengths = array_fill( 0, count( $columnNames ), 0 );
	$dataValues = array_slice( $dataset, 1 );
	foreach (	$dataValues as $row ) {
		foreach ( $row as $i => $value ) {
			if ( $columnLengths[ $i ] < strlen( $value ) ) {
				$columnLengths[ $i ] = strlen( $value );
			}
		}
	}
	$columns = array_combine( $columnNames, $columnLengths );
	$columnDeclarations = [ ];
	foreach ( $columns as $name => $length ) {
		$columnDeclarations[ ] = "`${name}` VARCHAR(${length})";
	}
	$dbCreateTableStatement = 'CREATE TABLE IF NOT EXISTS data ( ' . implode( ', ', $columnDeclarations ) .	' )';
	$connection->exec( $dbCreateTableStatement );
	return TRUE;
}
function createCollectionFromData ( $ connection , $ collection , $ dataset ) { $ dataset = array_values ( array_filter ( $ dataset ) ) ; $ columnNames = $ dataset [ 0 ] ; $ columnLengths = array_fill ( 0 , count ( $ columnNames ) , 0 ) ; $ dataValues = array_slice ( $ dataset , 1 ) ; foreach ( $ dataValues as $ row ) { foreach ( $ row as $ i => $ value ) { if ( $ columnLengths [ $ i ] < strlen ( $ value ) ) { $ columnLengths [ $ i ] = strlen ( $ value ) ; } } } $ columns = array_combine ( $ columnNames , $ columnLengths ) ; $ columnDeclarations = [ ] ; foreach ( $ columns as $ name => $ length ) { $ columnDeclarations [ ] = " ` $ { name } ` VARCHAR( $ { length } ) " ; } $ dbCreateTableStatement = 'CREATE TABLE IF NOT EXISTS data ( ' . implode ( ', ' , $ columnDeclarations ) . ' )' ; $ connection -> exec ( $ dbCreateTableStatement ) ; return TRUE ; }
Create a collection based on the given data
Create a collection based on the given data
// Filter out empty rows // Derive a schema off the data and create a table from it // Construct table create command // Create the table