2 * Bacula Catalog Database Create record interface routines
4 * Kern Sibbald, March 2000
9 Copyright (C) 2000-2005 Kern Sibbald
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 version 2 as amended with additional clauses defined in the
14 file LICENSE in the main source directory.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 the file LICENSE for additional details.
23 /* The following is necessary so that we do not include
24 * the dummy external definition of DB.
26 #define __SQL_C /* indicate that this is sql.c */
31 #if HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL
33 /* -----------------------------------------------------------------------
35 * Generic Routines (or almost generic)
37 * -----------------------------------------------------------------------
40 /* Forward referenced subroutines */
41 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
42 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
43 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar);
46 /* Imported subroutines */
47 extern void print_dashes(B_DB *mdb);
48 extern void print_result(B_DB *mdb);
49 extern int QueryDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
50 extern int InsertDB(const char *file, int line, JCR *jcr, B_DB *db, char *select_cmd);
51 extern int UpdateDB(const char *file, int line, JCR *jcr, B_DB *db, char *update_cmd);
52 extern void split_path_and_file(JCR *jcr, B_DB *mdb, const char *fname);
55 /* Create a new record for the Job
56 * Returns: 0 on failure
60 db_create_job_record(JCR *jcr, B_DB *mdb, JOB_DBR *jr)
62 char dt[MAX_TIME_LENGTH];
71 stime = jr->SchedTime;
74 localtime_r(&stime, &tm);
75 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
76 JobTDate = (utime_t)stime;
80 "INSERT INTO Job (Job,Name,Type,Level,JobStatus,SchedTime,JobTDate) VALUES "
81 "('%s','%s','%c','%c','%c','%s',%s)",
82 jr->Job, jr->Name, (char)(jr->JobType), (char)(jr->JobLevel),
83 (char)(jr->JobStatus), dt, edit_uint64(JobTDate, ed1));
85 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
86 Mmsg2(&mdb->errmsg, _("Create DB Job record %s failed. ERR=%s\n"),
87 mdb->cmd, sql_strerror(mdb));
91 jr->JobId = sql_insert_id(mdb, _("Job"));
98 /* Create a JobMedia record for medium used this job
99 * Returns: false on failure
103 db_create_jobmedia_record(JCR *jcr, B_DB *mdb, JOBMEDIA_DBR *jm)
107 char ed1[50], ed2[50];
111 /* Now get count for VolIndex */
112 Mmsg(mdb->cmd, "SELECT count(*) from JobMedia");
113 count = get_sql_record_max(jcr, mdb);
120 "INSERT INTO JobMedia (JobId,MediaId,FirstIndex,LastIndex,"
121 "StartFile,EndFile,StartBlock,EndBlock,VolIndex,Copy,Stripe) "
122 "VALUES (%s,%s,%u,%u,%u,%u,%u,%u,%u,%u,%u)",
123 edit_int64(jm->JobId, ed1),
124 edit_int64(jm->MediaId, ed2),
125 jm->FirstIndex, jm->LastIndex,
126 jm->StartFile, jm->EndFile, jm->StartBlock, jm->EndBlock,count,
127 jm->Copy, jm->Stripe);
129 Dmsg0(300, mdb->cmd);
130 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
131 Mmsg2(&mdb->errmsg, _("Create JobMedia record %s failed: ERR=%s\n"), mdb->cmd,
135 /* Worked, now update the Media record with the EndFile and EndBlock */
137 "UPDATE Media SET EndFile=%u, EndBlock=%u WHERE MediaId=%u",
138 jm->EndFile, jm->EndBlock, jm->MediaId);
139 if (!UPDATE_DB(jcr, mdb, mdb->cmd)) {
140 Mmsg2(&mdb->errmsg, _("Update Media record %s failed: ERR=%s\n"), mdb->cmd,
146 Dmsg0(300, "Return from JobMedia\n");
152 /* Create Unique Pool record
153 * Returns: false on failure
157 db_create_pool_record(JCR *jcr, B_DB *mdb, POOL_DBR *pr)
160 char ed1[30], ed2[30], ed3[50];
162 Dmsg0(200, "In create pool\n");
164 Mmsg(mdb->cmd, "SELECT PoolId,Name FROM Pool WHERE Name='%s'", pr->Name);
165 Dmsg1(200, "selectpool: %s\n", mdb->cmd);
167 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
168 mdb->num_rows = sql_num_rows(mdb);
169 if (mdb->num_rows > 0) {
170 Mmsg1(&mdb->errmsg, _("pool record %s already exists\n"), pr->Name);
171 sql_free_result(mdb);
175 sql_free_result(mdb);
180 "INSERT INTO Pool (Name,NumVols,MaxVols,UseOnce,UseCatalog,"
181 "AcceptAnyVolume,AutoPrune,Recycle,VolRetention,VolUseDuration,"
182 "MaxVolJobs,MaxVolFiles,MaxVolBytes,PoolType,LabelType,LabelFormat) "
183 "VALUES ('%s',%u,%u,%d,%d,%d,%d,%d,%s,%s,%u,%u,%s,'%s',%d,'%s')",
185 pr->NumVols, pr->MaxVols,
186 pr->UseOnce, pr->UseCatalog,
188 pr->AutoPrune, pr->Recycle,
189 edit_uint64(pr->VolRetention, ed1),
190 edit_uint64(pr->VolUseDuration, ed2),
191 pr->MaxVolJobs, pr->MaxVolFiles,
192 edit_uint64(pr->MaxVolBytes, ed3),
193 pr->PoolType, pr->LabelType, pr->LabelFormat);
194 Dmsg1(200, "Create Pool: %s\n", mdb->cmd);
195 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
196 Mmsg2(&mdb->errmsg, _("Create db Pool record %s failed: ERR=%s\n"),
197 mdb->cmd, sql_strerror(mdb));
201 pr->PoolId = sql_insert_id(mdb, _("Pool"));
209 * Create Unique Device record
210 * Returns: false on failure
214 db_create_device_record(JCR *jcr, B_DB *mdb, DEVICE_DBR *dr)
217 char ed1[30], ed2[30];
219 Dmsg0(200, "In create Device\n");
221 Mmsg(mdb->cmd, "SELECT DeviceId,Name FROM Device WHERE Name='%s'", dr->Name);
222 Dmsg1(200, "selectdevice: %s\n", mdb->cmd);
224 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
225 mdb->num_rows = sql_num_rows(mdb);
226 if (mdb->num_rows > 0) {
227 Mmsg1(&mdb->errmsg, _("Device record %s already exists\n"), dr->Name);
228 sql_free_result(mdb);
232 sql_free_result(mdb);
237 "INSERT INTO Device (Name,MediaTypeId,StorageId) VALUES ('%s',%s,%s)",
239 edit_uint64(dr->MediaTypeId, ed1),
240 edit_int64(dr->StorageId, ed2));
241 Dmsg1(200, "Create Device: %s\n", mdb->cmd);
242 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
243 Mmsg2(&mdb->errmsg, _("Create db Device record %s failed: ERR=%s\n"),
244 mdb->cmd, sql_strerror(mdb));
248 dr->DeviceId = sql_insert_id(mdb, _("Device"));
258 * Create a Unique record for Storage -- no duplicates
259 * Returns: false on failure
260 * true on success with id in sr->StorageId
262 bool db_create_storage_record(JCR *jcr, B_DB *mdb, STORAGE_DBR *sr)
268 Mmsg(mdb->cmd, "SELECT StorageId,AutoChanger FROM Storage WHERE Name='%s'", sr->Name);
272 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
273 mdb->num_rows = sql_num_rows(mdb);
274 /* If more than one, report error, but return first row */
275 if (mdb->num_rows > 1) {
276 Mmsg1(&mdb->errmsg, _("More than one Storage record!: %d\n"), (int)(mdb->num_rows));
277 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
279 if (mdb->num_rows >= 1) {
280 if ((row = sql_fetch_row(mdb)) == NULL) {
281 Mmsg1(&mdb->errmsg, _("error fetching Storage row: %s\n"), sql_strerror(mdb));
282 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
283 sql_free_result(mdb);
287 sr->StorageId = str_to_int64(row[0]);
288 sr->AutoChanger = atoi(row[1]); /* bool */
289 sql_free_result(mdb);
293 sql_free_result(mdb);
297 Mmsg(mdb->cmd, "INSERT INTO Storage (Name,AutoChanger)"
298 " VALUES ('%s',%d)", sr->Name, sr->AutoChanger);
300 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
301 Mmsg2(&mdb->errmsg, _("Create DB Storage record %s failed. ERR=%s\n"),
302 mdb->cmd, sql_strerror(mdb));
303 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
306 sr->StorageId = sql_insert_id(mdb, _("Storage"));
316 * Create Unique MediaType record
317 * Returns: false on failure
321 db_create_mediatype_record(JCR *jcr, B_DB *mdb, MEDIATYPE_DBR *mr)
325 Dmsg0(200, "In create mediatype\n");
327 Mmsg(mdb->cmd, "SELECT MediaTypeId,MediaType FROM MediaType WHERE MediaType='%s'", mr->MediaType);
328 Dmsg1(200, "selectmediatype: %s\n", mdb->cmd);
330 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
331 mdb->num_rows = sql_num_rows(mdb);
332 if (mdb->num_rows > 0) {
333 Mmsg1(&mdb->errmsg, _("mediatype record %s already exists\n"), mr->MediaType);
334 sql_free_result(mdb);
338 sql_free_result(mdb);
343 "INSERT INTO MediaType (MediaType,ReadOnly) "
347 Dmsg1(200, "Create mediatype: %s\n", mdb->cmd);
348 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
349 Mmsg2(&mdb->errmsg, _("Create db mediatype record %s failed: ERR=%s\n"),
350 mdb->cmd, sql_strerror(mdb));
354 mr->MediaTypeId = sql_insert_id(mdb, _("MediaType"));
363 * Create Media record. VolumeName and non-zero Slot must be unique
365 * Returns: 0 on failure
369 db_create_media_record(JCR *jcr, B_DB *mdb, MEDIA_DBR *mr)
372 char ed1[50], ed2[50], ed3[50], ed4[50], ed5[50], ed6[50], ed7[50], ed8[50];
376 Mmsg(mdb->cmd, "SELECT MediaId FROM Media WHERE VolumeName='%s'",
378 Dmsg1(500, "selectpool: %s\n", mdb->cmd);
380 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
381 mdb->num_rows = sql_num_rows(mdb);
382 if (mdb->num_rows > 0) {
383 Mmsg1(&mdb->errmsg, _("Volume \"%s\" already exists.\n"), mr->VolumeName);
384 sql_free_result(mdb);
388 sql_free_result(mdb);
393 "INSERT INTO Media (VolumeName,MediaType,PoolId,MaxVolBytes,VolCapacityBytes,"
394 "Recycle,VolRetention,VolUseDuration,MaxVolJobs,MaxVolFiles,"
395 "VolStatus,Slot,VolBytes,InChanger,VolReadTime,VolWriteTime,VolParts,"
396 "EndFile,EndBlock,LabelType,StorageId) "
397 "VALUES ('%s','%s',%u,%s,%s,%d,%s,%s,%u,%u,'%s',%d,%s,%d,%s,%s,%d,0,0,%d,%s)",
399 mr->MediaType, mr->PoolId,
400 edit_uint64(mr->MaxVolBytes,ed1),
401 edit_uint64(mr->VolCapacityBytes, ed2),
403 edit_uint64(mr->VolRetention, ed3),
404 edit_uint64(mr->VolUseDuration, ed4),
409 edit_uint64(mr->VolBytes, ed5),
411 edit_uint64(mr->VolReadTime, ed6),
412 edit_uint64(mr->VolWriteTime, ed7),
415 edit_int64(mr->StorageId, ed8)
419 Dmsg1(500, "Create Volume: %s\n", mdb->cmd);
420 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
421 Mmsg2(&mdb->errmsg, _("Create DB Media record %s failed. ERR=%s\n"),
422 mdb->cmd, sql_strerror(mdb));
425 mr->MediaId = sql_insert_id(mdb, _("Media"));
427 if (mr->set_label_date) {
428 char dt[MAX_TIME_LENGTH];
429 if (mr->LabelDate == 0) {
430 mr->LabelDate = time(NULL);
432 localtime_r(&mr->LabelDate, &tm);
433 strftime(dt, sizeof(dt), "%Y-%m-%d %T", &tm);
434 Mmsg(mdb->cmd, "UPDATE Media SET LabelDate='%s' "
435 "WHERE MediaId=%d", dt, mr->MediaId);
436 stat = UPDATE_DB(jcr, mdb, mdb->cmd);
441 * Make sure that if InChanger is non-zero any other identical slot
442 * has InChanger zero.
444 db_make_inchanger_unique(jcr, mdb, mr);
451 * Create a Unique record for the client -- no duplicates
452 * Returns: 0 on failure
453 * 1 on success with id in cr->ClientId
455 int db_create_client_record(JCR *jcr, B_DB *mdb, CLIENT_DBR *cr)
459 char ed1[50], ed2[50];
462 Mmsg(mdb->cmd, "SELECT ClientId,Uname FROM Client WHERE Name='%s'", cr->Name);
465 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
466 mdb->num_rows = sql_num_rows(mdb);
467 /* If more than one, report error, but return first row */
468 if (mdb->num_rows > 1) {
469 Mmsg1(&mdb->errmsg, _("More than one Client!: %d\n"), (int)(mdb->num_rows));
470 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
472 if (mdb->num_rows >= 1) {
473 if ((row = sql_fetch_row(mdb)) == NULL) {
474 Mmsg1(&mdb->errmsg, _("error fetching Client row: %s\n"), sql_strerror(mdb));
475 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
476 sql_free_result(mdb);
480 cr->ClientId = str_to_int64(row[0]);
482 bstrncpy(cr->Uname, row[1], sizeof(cr->Uname));
484 cr->Uname[0] = 0; /* no name */
486 sql_free_result(mdb);
490 sql_free_result(mdb);
494 Mmsg(mdb->cmd, "INSERT INTO Client (Name,Uname,AutoPrune,"
495 "FileRetention,JobRetention) VALUES "
496 "('%s','%s',%d,%s,%s)", cr->Name, cr->Uname, cr->AutoPrune,
497 edit_uint64(cr->FileRetention, ed1),
498 edit_uint64(cr->JobRetention, ed2));
500 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
501 Mmsg2(&mdb->errmsg, _("Create DB Client record %s failed. ERR=%s\n"),
502 mdb->cmd, sql_strerror(mdb));
503 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
507 cr->ClientId = sql_insert_id(mdb, _("Client"));
519 * Create a Unique record for the counter -- no duplicates
520 * Returns: 0 on failure
521 * 1 on success with counter filled in
523 int db_create_counter_record(JCR *jcr, B_DB *mdb, COUNTER_DBR *cr)
529 memset(&mcr, 0, sizeof(mcr));
530 bstrncpy(mcr.Counter, cr->Counter, sizeof(mcr.Counter));
531 if (db_get_counter_record(jcr, mdb, &mcr)) {
532 memcpy(cr, &mcr, sizeof(COUNTER_DBR));
538 Mmsg(mdb->cmd, "INSERT INTO Counters (Counter,MinValue,MaxValue,CurrentValue,"
539 "WrapCounter) VALUES ('%s','%d','%d','%d','%s')",
540 cr->Counter, cr->MinValue, cr->MaxValue, cr->CurrentValue,
543 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
544 Mmsg2(&mdb->errmsg, _("Create DB Counters record %s failed. ERR=%s\n"),
545 mdb->cmd, sql_strerror(mdb));
546 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
557 * Create a FileSet record. This record is unique in the
558 * name and the MD5 signature of the include/exclude sets.
559 * Returns: 0 on failure
560 * 1 on success with FileSetId in record
562 bool db_create_fileset_record(JCR *jcr, B_DB *mdb, FILESET_DBR *fsr)
569 fsr->created = false;
570 Mmsg(mdb->cmd, "SELECT FileSetId,CreateTime FROM FileSet WHERE "
571 "FileSet='%s' AND MD5='%s'", fsr->FileSet, fsr->MD5);
574 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
575 mdb->num_rows = sql_num_rows(mdb);
576 if (mdb->num_rows > 1) {
577 Mmsg1(&mdb->errmsg, _("More than one FileSet!: %d\n"), (int)(mdb->num_rows));
578 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
580 if (mdb->num_rows >= 1) {
581 if ((row = sql_fetch_row(mdb)) == NULL) {
582 Mmsg1(&mdb->errmsg, _("error fetching FileSet row: ERR=%s\n"), sql_strerror(mdb));
583 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
584 sql_free_result(mdb);
588 fsr->FileSetId = str_to_int64(row[0]);
589 if (row[1] == NULL) {
590 fsr->cCreateTime[0] = 0;
592 bstrncpy(fsr->cCreateTime, row[1], sizeof(fsr->cCreateTime));
594 sql_free_result(mdb);
598 sql_free_result(mdb);
601 if (fsr->CreateTime == 0 && fsr->cCreateTime[0] == 0) {
602 fsr->CreateTime = time(NULL);
604 localtime_r(&fsr->CreateTime, &tm);
605 strftime(fsr->cCreateTime, sizeof(fsr->cCreateTime), "%Y-%m-%d %T", &tm);
608 Mmsg(mdb->cmd, "INSERT INTO FileSet (FileSet,MD5,CreateTime) "
609 "VALUES ('%s','%s','%s')", fsr->FileSet, fsr->MD5, fsr->cCreateTime);
611 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
612 Mmsg2(&mdb->errmsg, _("Create DB FileSet record %s failed. ERR=%s\n"),
613 mdb->cmd, sql_strerror(mdb));
614 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
618 fsr->FileSetId = sql_insert_id(mdb, _("FileSet"));
631 * dev_t st_dev; * device *
632 * ino_t st_ino; * inode *
633 * mode_t st_mode; * protection *
634 * nlink_t st_nlink; * number of hard links *
635 * uid_t st_uid; * user ID of owner *
636 * gid_t st_gid; * group ID of owner *
637 * dev_t st_rdev; * device type (if inode device) *
638 * off_t st_size; * total size, in bytes *
639 * unsigned long st_blksize; * blocksize for filesystem I/O *
640 * unsigned long st_blocks; * number of blocks allocated *
641 * time_t st_atime; * time of last access *
642 * time_t st_mtime; * time of last modification *
643 * time_t st_ctime; * time of last inode change *
650 * Create File record in B_DB
652 * In order to reduce database size, we store the File attributes,
653 * the FileName, and the Path separately. In principle, there
654 * is a single FileName record and a single Path record, no matter
655 * how many times it occurs. This is this subroutine, we separate
656 * the file and the path and create three database records.
658 int db_create_file_attributes_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
662 Dmsg1(300, "Fname=%s\n", ar->fname);
663 Dmsg0(500, "put_file_into_catalog\n");
665 * Make sure we have an acceptable attributes record.
667 if (!(ar->Stream == STREAM_UNIX_ATTRIBUTES ||
668 ar->Stream == STREAM_UNIX_ATTRIBUTES_EX)) {
669 Mmsg1(&mdb->errmsg, _("Attempt to put non-attributes into catalog. Stream=%d\n"),
671 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
676 split_path_and_file(jcr, mdb, ar->fname);
678 if (!db_create_filename_record(jcr, mdb, ar)) {
681 Dmsg1(500, "db_create_filename_record: %s\n", mdb->esc_name);
684 if (!db_create_path_record(jcr, mdb, ar)) {
687 Dmsg1(500, "db_create_path_record: %s\n", mdb->esc_name);
689 /* Now create master File record */
690 if (!db_create_file_record(jcr, mdb, ar)) {
693 Dmsg0(500, "db_create_file_record OK\n");
695 Dmsg3(300, "CreateAttributes Path=%s File=%s FilenameId=%d\n", mdb->path, mdb->fname, ar->FilenameId);
705 * This is the master File entry containing the attributes.
706 * The filename and path records have already been created.
708 static int db_create_file_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
711 static char *no_sig = "0";
716 ASSERT(ar->FilenameId);
718 if (ar->Sig == NULL) {
726 "INSERT INTO File (FileIndex,JobId,PathId,FilenameId,"
727 "LStat,MD5) VALUES (%u,%u,%u,%u,'%s','%s')",
728 ar->FileIndex, ar->JobId, ar->PathId, ar->FilenameId,
731 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
732 Mmsg2(&mdb->errmsg, _("Create db File record %s failed. ERR=%s"),
733 mdb->cmd, sql_strerror(mdb));
734 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
738 ar->FileId = sql_insert_id(mdb, _("File"));
744 /* Create a Unique record for the Path -- no duplicates */
745 static int db_create_path_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
750 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->pnl+2);
751 db_escape_string(mdb->esc_name, mdb->path, mdb->pnl);
753 if (mdb->cached_path_id != 0 && mdb->cached_path_len == mdb->pnl &&
754 strcmp(mdb->cached_path, mdb->path) == 0) {
755 ar->PathId = mdb->cached_path_id;
759 Mmsg(mdb->cmd, "SELECT PathId FROM Path WHERE Path='%s'", mdb->esc_name);
761 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
762 mdb->num_rows = sql_num_rows(mdb);
763 if (mdb->num_rows > 1) {
765 Mmsg2(&mdb->errmsg, _("More than one Path!: %s for path: %s\n"),
766 edit_uint64(mdb->num_rows, ed1), mdb->path);
767 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
769 /* Even if there are multiple paths, take the first one */
770 if (mdb->num_rows >= 1) {
771 if ((row = sql_fetch_row(mdb)) == NULL) {
772 Mmsg1(&mdb->errmsg, _("error fetching row: %s\n"), sql_strerror(mdb));
773 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
774 sql_free_result(mdb);
779 ar->PathId = str_to_int64(row[0]);
780 sql_free_result(mdb);
782 if (ar->PathId != mdb->cached_path_id) {
783 mdb->cached_path_id = ar->PathId;
784 mdb->cached_path_len = mdb->pnl;
785 pm_strcpy(mdb->cached_path, mdb->path);
790 sql_free_result(mdb);
793 Mmsg(mdb->cmd, "INSERT INTO Path (Path) VALUES ('%s')", mdb->esc_name);
795 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
796 Mmsg2(&mdb->errmsg, _("Create db Path record %s failed. ERR=%s\n"),
797 mdb->cmd, sql_strerror(mdb));
798 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
802 ar->PathId = sql_insert_id(mdb, _("Path"));
807 if (stat && ar->PathId != mdb->cached_path_id) {
808 mdb->cached_path_id = ar->PathId;
809 mdb->cached_path_len = mdb->pnl;
810 pm_strcpy(mdb->cached_path, mdb->path);
815 /* Create a Unique record for the filename -- no duplicates */
816 static int db_create_filename_record(JCR *jcr, B_DB *mdb, ATTR_DBR *ar)
820 mdb->esc_name = check_pool_memory_size(mdb->esc_name, 2*mdb->fnl+2);
821 db_escape_string(mdb->esc_name, mdb->fname, mdb->fnl);
823 Mmsg(mdb->cmd, "SELECT FilenameId FROM Filename WHERE Name='%s'", mdb->esc_name);
825 if (QUERY_DB(jcr, mdb, mdb->cmd)) {
826 mdb->num_rows = sql_num_rows(mdb);
827 if (mdb->num_rows > 1) {
829 Mmsg2(&mdb->errmsg, _("More than one Filename! %s for file: %s\n"),
830 edit_uint64(mdb->num_rows, ed1), mdb->fname);
831 Jmsg(jcr, M_WARNING, 0, "%s", mdb->errmsg);
833 if (mdb->num_rows >= 1) {
834 if ((row = sql_fetch_row(mdb)) == NULL) {
835 Mmsg2(&mdb->errmsg, _("Error fetching row for file=%s: ERR=%s\n"),
836 mdb->fname, sql_strerror(mdb));
837 Jmsg(jcr, M_ERROR, 0, "%s", mdb->errmsg);
840 ar->FilenameId = str_to_int64(row[0]);
842 sql_free_result(mdb);
843 return ar->FilenameId > 0;
845 sql_free_result(mdb);
848 Mmsg(mdb->cmd, "INSERT INTO Filename (Name) VALUES ('%s')", mdb->esc_name);
850 if (!INSERT_DB(jcr, mdb, mdb->cmd)) {
851 Mmsg2(&mdb->errmsg, _("Create db Filename record %s failed. ERR=%s\n"),
852 mdb->cmd, sql_strerror(mdb));
853 Jmsg(jcr, M_FATAL, 0, "%s", mdb->errmsg);
856 ar->FilenameId = sql_insert_id(mdb, _("Filename"));
858 return ar->FilenameId > 0;
861 #endif /* HAVE_SQLITE3 || HAVE_MYSQL || HAVE_SQLITE || HAVE_POSTGRESQL */