26 public const REGEX =
"/([a-z]*)\\(([0-9]*)\\)/u";
27 protected static array $field_map = array(
36 protected static array $length_map = array(
48 protected string $table_name =
'';
49 protected string $class_name =
'';
50 protected array $structure = array();
51 protected array $ids = array();
53 public function __construct(
string $table_name,
string $class_name)
55 $this->setClassName($class_name);
56 $this->setTableName($table_name);
57 $this->readStructure();
60 public function readStructure(): void
62 $sql =
'DESCRIBE ' . $this->getTableName();
63 $res = self::getDB()->query($sql);
64 while (
$data = self::getDB()->fetchObject(
$res)) {
65 $this->addStructure(
$data);
69 public function downloadClassFile(): void
72require_once('./Services/ActiveRecord/class.ActiveRecord.php');
80class {CLASS_NAME} extends ActiveRecord {
86 static function returnDbTableName() {
87 return '{TABLE_NAME}';
93 public function getConnectorContainerName() {
94 return '{TABLE_NAME}';
98 [
'{CLASS_NAME}',
'{TABLE_NAME}'],
99 [$this->getClassName(), $this->getTableName()],
103 foreach ($this->getStructure() as $str) {
107 foreach ($this->returnAttributesForField($str) as
$name => $value) {
108 $member .=
' * @con_' .
$name .
' ' . $value .
"\n";
112 protected \${FIELD_NAME};
116 $member = str_replace([
'{FIELD_NAME}',
'{DECLARATION}'], [$str->Field,
' '], $member);
118 $all_members .= $member;
127 header(
'Content-type: application/x-httpd-php');
128 header(
"Content-Disposition: attachment; filename=\"class." . $this->getClassName() .
".php\"");
136 protected function returnAttributesForField(stdClass $field): array
143 if ($field->Null ===
'NO') {
147 if ($field->Key ===
'PRI') {
154 protected static function lookupFieldType(
string $field_name): string
156 preg_match(self::REGEX, $field_name, $matches);
158 return self::$field_map[$matches[1]];
164 protected static function lookupFieldLength(
string $field_name)
166 $field_type = self::lookupFieldType($field_name);
168 preg_match(self::REGEX, $field_name, $matches);
170 if (self::$length_map[$field_type][$matches[2]]) {
171 return self::$length_map[$field_type][$matches[2]];
184 public function setTableName(
string $table_name): void
186 $this->table_name = $table_name;
189 public function getTableName(): string
191 return $this->table_name;
197 public function setStructure(array $structure): void
199 $this->structure = $structure;
205 public function getStructure(): array
207 return $this->structure;
210 public function addStructure(stdClass $structure): void
212 if (!in_array($structure->Field, $this->ids)) {
213 $this->structure[] = $structure;
214 $this->ids[] = $structure->Field;
218 public function setClassName(
string $class_name): void
220 $this->class_name = $class_name;
223 public function getClassName(): string
225 return $this->class_name;
const FIELD_TYPE_TIMESTAMP
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc