$tableSchema = array('raffles' => array('raffleid' => 'STRING', 'raffledescription' => 'STRING', 'creatorid' => 'STRING', 'created' => 'DATETIME', 'privacy' => 'STRING', 'status' => 'STRING'), 'participants' => array('raffleid' => 'STRING', 'participantid' => 'STRING', 'comment' => 'STRING', 'joined' => 'DATETIME'), 'winners' => array('raffleid' => 'STRING', 'winnerid' => 'STRING', 'raffled' => 'DATETIME')); $plusService = new Google_Service_Plus($client); $person = $plusService->people->get('me'); $uuid = new uuid(); $raffleids = array(uuid::v5(uuid::v4(), 'synapp\\info\\tools\\gplusraffle'), uuid::v5(uuid::v4(), 'synapp\\info\\tools\\gplusraffle'), uuid::v5(uuid::v4(), 'synapp\\info\\tools\\gplusraffle'), uuid::v5(uuid::v4(), 'synapp\\info\\tools\\gplusraffle')); $insertQueries = array(array('tableName' => 'raffles', 'sql' => " ('raffleid','raffledescription','creatorid','created','privacy','status') " . "VALUES ('{$raffleids['0']}', 'test raffle 1','{$person->id}','" . date("Y-m-d H:i:s") . "','public','closed')"), array('tableName' => 'raffles', 'sql' => " ('raffleid','raffledescription','creatorid','created','privacy','status') " . "VALUES ('{$raffleids['1']}', 'test raffle 2','{$person->id}','" . date("Y-m-d H:i:s") . "','public','closed')"), array('tableName' => 'raffles', 'sql' => " ('raffleid','raffledescription','creatorid','created','privacy','status') " . "VALUES ('{$raffleids['2']}', 'test raffle 3','{$person->id}','" . date("Y-m-d H:i:s") . "','public','closed')"), array('tableName' => 'raffles', 'sql' => " ('raffleid','raffledescription','creatorid','created','privacy','status') " . "VALUES ('{$raffleids['3']}', 'test raffle 4','{$person->id}','" . date("Y-m-d H:i:s") . "','public','closed')"), array('tableName' => 'participants', 'sql' => " ('raffleid','participantid','comment','joined') " . "VALUES ('{$raffleids['0']}', '{$person->id}', 'Comment 1','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'participants', 'sql' => " ('raffleid','participantid','joined') " . "VALUES ('{$raffleids['1']}', '{$person->id}', 'Comment 2','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'participants', 'sql' => " ('raffleid','participantid','joined') " . "VALUES ('{$raffleids['2']}', '{$person->id}', 'Comment 3','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'participants', 'sql' => " ('raffleid','participantid','joined') " . "VALUES ('{$raffleids['3']}', '{$person->id}', 'Comment 4','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'winners', 'sql' => " ('raffleid','winnerid','raffled') " . "VALUES ('{$raffleids['0']}', '{$person->id}','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'winners', 'sql' => " ('raffleid','winnerid','raffled') " . "VALUES ('{$raffleids['1']}', '{$person->id}','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'winners', 'sql' => " ('raffleid','winnerid','raffled') " . "VALUES ('{$raffleids['2']}', '{$person->id}','" . date("Y-m-d H:i:s") . "')"), array('tableName' => 'winners', 'sql' => " ('raffleid','winnerid','raffled') " . "VALUES ('{$raffleids['3']}', '{$person->id}','" . date("Y-m-d H:i:s") . "')")); $selectQueries = array(array('tableName' => 'raffles', 'sql' => ""), array('tableName' => 'participants', 'sql' => ""), array('tableName' => 'winners', 'sql' => ""), array('tableName' => 'raffles', 'sql' => " WHERE raffleid='{$raffleids['0']}'"), array('tableName' => 'raffles', 'sql' => " WHERE raffleid!='{$raffleids['0']}'")); $fusionTablesService = new Google_Service_Fusiontables($client); $errors = array(); $tableids = array(); foreach ($tableSchema as $tableName => $columns) { $tableColumns = array(); foreach ($columns as $columnName => $columnType) { $column = new Google_Service_Fusiontables_Column(); $column->setName($columnName); $column->setType($columnType); $tableColumns[] = $column; } $table = new Google_Service_Fusiontables_Table(); $table->setName($tableName); $table->setColumns($tableColumns); $table->setIsExportable('true'); // create fusion tables, saving their ids try { $response = $fusionTablesService->table->insert($table); $tableids[$tableName] = $response->tableId; } catch (Google_Service_Exception $e) { $errors[] = $e->getErrors(); } //error_log($e->getMessage()); }
/** * Creates the given Schema of fusion tables * * @param null $client * @param null|array $tableSchema tables and structures * @param null $debug * @throws Exception */ public function createTables($client = null, $tableSchema = null, $debug = null) { if ($tableSchema === null) { $tableSchema = $this->tableSchema; } if ($debug === null) { $debug = $this->debug; } if ($client === null) { $client = $this->client; } if (!$client instanceof Google_Client) { throw new Exception('$client is not an instance of Google_Client.', 500); } $service = new Google_Service_Fusiontables($client); $tableIds = array(); foreach ($tableSchema as $tableName => $columns) { $tableColumns = array(); foreach ($columns as $columnName => $columnType) { $column = new Google_Service_Fusiontables_Column(); $column->setName($columnName); $column->setType($columnType); $tableColumns[] = $column; } $table = new Google_Service_Fusiontables_Table(); $table->setName($tableName); $table->setColumns($tableColumns); $table->setIsExportable('true'); try { $response = $service->table->insert($table); $tableIds[$tableName] = $response->tableId; if ($debug) { error_log(var_export($tableIds, true)); } } catch (Exception $e) { error_log($e->getMessage()); } $this->tableIds = $tableIds; } }